View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  package org.opencastproject.assetmanager.impl.query;
22  
23  import org.opencastproject.assetmanager.impl.persistence.QPropertyDto;
24  import org.opencastproject.assetmanager.impl.persistence.QSnapshotDto;
25  
26  import com.entwinemedia.fn.Fn;
27  import com.entwinemedia.fn.Stream;
28  import com.entwinemedia.fn.data.Opt;
29  import com.mysema.query.types.EntityPath;
30  import com.mysema.query.types.expr.BooleanExpression;
31  
32  import javax.annotation.Nullable;
33  import javax.annotation.ParametersAreNonnullByDefault;
34  
35  /**
36   * Collect contributions to a JPA query.
37   * Each of the builder methods creates a new instance.
38   */
39  @ParametersAreNonnullByDefault
40  public final class DeleteQueryContribution {
41    // CHECKSTYLE:OFF
42    final Stream<EntityPath<?>> from;
43  
44    /**
45     * Where clause constructor function.
46     * <code>(EntityPath from) -> (BooleanExpression | null)</code>
47     */
48    final Fn<EntityPath<?>, BooleanExpression> where;
49  
50    final Opt<BooleanExpression> targetPredicate;
51  
52    final String name;
53  
54    private static final Fn<EntityPath<?>, BooleanExpression> NO_WHERE = new Fn<EntityPath<?>, BooleanExpression>() {
55      @Override public BooleanExpression apply(EntityPath<?> entityPathBase) {
56        return null;
57      }
58    };
59    // CHECKSTYLE:ON
60  
61    public DeleteQueryContribution(
62            Stream<EntityPath<?>> from,
63            Fn<EntityPath<?>, BooleanExpression> where,
64            Opt<BooleanExpression> targetPredicate,
65            String name) {
66      this.from = from;
67      this.where = where;
68      this.targetPredicate = targetPredicate;
69      this.name = name;
70    }
71  
72    /**
73     * Create an empty contribution.
74     */
75    public static DeleteQueryContribution mk() {
76      return new DeleteQueryContribution(Stream.<EntityPath<?>>empty(), NO_WHERE, Opt.<BooleanExpression>none(), "");
77    }
78  
79    /**
80     * Create a copy of contribution <code>c</code>.
81     */
82    public static DeleteQueryContribution mk(DeleteQueryContribution c) {
83      return new DeleteQueryContribution(c.from, c.where, c.targetPredicate, c.name);
84    }
85  
86    DeleteQueryContribution from(Stream<? extends EntityPath<?>> from) {
87      return new DeleteQueryContribution((Stream<EntityPath<?>>) from, where, targetPredicate, name);
88    }
89  
90    DeleteQueryContribution targetPredicate(@Nullable BooleanExpression targetPredicate) {
91      return new DeleteQueryContribution(from, where, Opt.nul(targetPredicate), name);
92    }
93  
94    DeleteQueryContribution targetPredicate(Opt<BooleanExpression> targetPredicate) {
95      return new DeleteQueryContribution(from, where, targetPredicate, name);
96    }
97  
98    DeleteQueryContribution where(Fn<EntityPath<?>, BooleanExpression> where) {
99      return new DeleteQueryContribution(from, where, targetPredicate, name);
100   }
101 
102   DeleteQueryContribution where(final Where where) {
103     return new DeleteQueryContribution(from, toFn(where), targetPredicate, name);
104   }
105 
106   DeleteQueryContribution where(@Nullable final BooleanExpression where) {
107     final Fn<EntityPath<?>, BooleanExpression> w = new Fn<EntityPath<?>, BooleanExpression>() {
108       @Override public BooleanExpression apply(EntityPath<?> entityPath) {
109         return where;
110       }
111     };
112     return new DeleteQueryContribution(from, w, targetPredicate, name);
113   }
114 
115   DeleteQueryContribution name(String name) {
116     return new DeleteQueryContribution(from, where, targetPredicate, name);
117   }
118 
119   /* -- */
120 
121   static Fn<EntityPath<?>, BooleanExpression> toFn(final Where where) {
122     return new Fn<EntityPath<?>, BooleanExpression>() {
123       @Override public BooleanExpression apply(EntityPath<?> from) {
124         if (from instanceof QSnapshotDto) {
125           return where.fromSnapshot((QSnapshotDto) from);
126         } else if (from instanceof QPropertyDto) {
127           return where.fromProperty((QPropertyDto) from);
128         } else {
129           throw new RuntimeException("BUG");
130         }
131       }
132     };
133   }
134 
135   /* -- */
136 
137   @ParametersAreNonnullByDefault
138   interface Where {
139     BooleanExpression fromSnapshot(QSnapshotDto e);
140     BooleanExpression fromProperty(QPropertyDto p);
141   }
142 }