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.api.query.Predicate;
24  import org.opencastproject.assetmanager.impl.RuntimeTypes;
25  
26  import com.entwinemedia.fn.Fn2;
27  import com.mysema.query.jpa.impl.JPAQueryFactory;
28  import com.mysema.query.types.expr.BooleanExpression;
29  
30  public abstract class AbstractPredicate implements Predicate, QueryContributor {
31    private final AbstractPredicate self = this;
32  
33    /**
34     * Join two predicates by <code>op</code>.
35     */
36    private Predicate binaryOp(
37        final Predicate right,
38        final Fn2<BooleanExpression, BooleanExpression, BooleanExpression> op
39    ) {
40      return new AbstractPredicate() {
41        @Override public SelectQueryContribution contributeSelect(JPAQueryFactory f) {
42          final SelectQueryContribution cLeft = self.contributeSelect(f);
43          final SelectQueryContribution cRight = RuntimeTypes.convert(right).contributeSelect(f);
44          return SelectQueryContribution.mk()
45                  .from(cLeft.from.append(cRight.from))
46                  .join(cLeft.join.append(cRight.join))
47                  .where(JpaFns.op(op, cLeft.where, cRight.where));
48        }
49  
50        @Override public DeleteQueryContribution contributeDelete(String owner) {
51          final DeleteQueryContribution cLeft = self.contributeDelete(owner);
52          final DeleteQueryContribution cRight = RuntimeTypes.convert(right).contributeDelete(owner);
53          return DeleteQueryContribution.mk()
54                  .from(cLeft.from.append(cRight.from))
55                  .where(JpaFns.op(op, cLeft.where, cRight.where));
56        }
57      };
58    }
59  
60    @Override public Predicate and(final Predicate right) {
61      return binaryOp(right, JpaFns.and);
62    }
63  
64    @Override public Predicate or(final Predicate right) {
65      return binaryOp(right, JpaFns.or);
66    }
67  
68    @Override public Predicate not() {
69      return new AbstractPredicate() {
70        @Override public SelectQueryContribution contributeSelect(JPAQueryFactory f) {
71          final SelectQueryContribution c = self.contributeSelect(f);
72          return SelectQueryContribution.mk().from(c.from).join(c.join).where(c.where.map(JpaFns.not));
73        }
74  
75        @Override public DeleteQueryContribution contributeDelete(String owner) {
76          final DeleteQueryContribution c = self.contributeDelete(owner);
77          return DeleteQueryContribution.mk().from(c.from).where(c.where.then(JpaFns.not));
78        }
79      };
80    }
81  }