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.PropertyName;
24  import org.opencastproject.assetmanager.impl.persistence.EntityPaths;
25  import org.opencastproject.assetmanager.impl.persistence.QPropertyDto;
26  import org.opencastproject.assetmanager.impl.persistence.QSnapshotDto;
27  import org.opencastproject.assetmanager.impl.query.DeleteQueryContribution.Where;
28  
29  import com.entwinemedia.fn.data.Opt;
30  import com.mysema.query.jpa.JPASubQuery;
31  import com.mysema.query.types.expr.BooleanExpression;
32  
33  import java.util.function.Function;
34  
35  /**
36   * A place to share common predicate constructor methods for properties.
37   */
38  public final class PropertyPredicates implements EntityPaths {
39    private static final QSnapshotDto Q_SNAPSHOT_ALIAS = new QSnapshotDto("s");
40    private static final QPropertyDto Q_PROPERTY_ALIAS = new QPropertyDto("p");
41  
42    public static final Opt NONE = Opt.none();
43    public static final Function<QPropertyDto, Opt<BooleanExpression>> NO_VALUE =
44        new Function<QPropertyDto, Opt<BooleanExpression>>() {
45          @Override public Opt<BooleanExpression> apply(QPropertyDto qPropertyDto) {
46            return NONE;
47          }
48        };
49  
50    private PropertyPredicates() {
51    }
52  
53    /**
54     * Create a 'where' expression for queries that filter by some property based criteria.
55     *
56     * @param propertyName the full qualified name of the property
57     * @param mkValueExpression a function to create a property value expression;
58     *        use the passed property DTO to create the expression
59     */
60    public static BooleanExpression mkWhereSelect(
61            PropertyName propertyName,
62            Function<QPropertyDto, Opt<BooleanExpression>> mkValueExpression) {
63      return mkWhereSelect(Opt.some(propertyName.getNamespace()), Opt.some(propertyName.getName()), mkValueExpression);
64    }
65  
66    /**
67     * Create a 'where' expression for queries that filter by some property based criteria.
68     *
69     * @param namespace
70     *         the property namespace to use
71     * @param propertyName
72     *         the name of the property
73     * @param mkValueExpression
74     *         a function to create a property value expression; use the passed property DTO to create the expression
75     */
76    public static BooleanExpression mkWhereSelect(
77        Opt<String> namespace,
78        Opt<String> propertyName,
79        Function<QPropertyDto, Opt<BooleanExpression>> mkValueExpression) {
80      return new JPASubQuery().from(Q_PROPERTY_ALIAS)
81          .where(Q_SNAPSHOT.mediaPackageId.eq(Q_PROPERTY_ALIAS.mediaPackageId)
82              .and(namespace.isSome()
83                  ? Q_PROPERTY_ALIAS.namespace.eq(namespace.get())
84                  // Just passing null like in the other predicates yields an NPE for some reason I do not know.
85                  // The isNotNull predicate prevents this.
86                  : Q_PROPERTY_ALIAS.namespace.isNotNull())
87              .and(propertyName.isSome()
88                  ? Q_PROPERTY_ALIAS.propertyName.eq(propertyName.get())
89                  : null)
90              .and(mkValueExpression.apply(Q_PROPERTY_ALIAS).orNull()))
91          .exists();
92    }
93  
94    public static Where mkWhereDelete(
95            final PropertyName propertyName,
96            final Function<QPropertyDto, Opt<BooleanExpression>> mkValueExpression) {
97      return mkWhereDelete(Opt.some(propertyName.getNamespace()), Opt.some(propertyName.getName()), mkValueExpression);
98    }
99  
100   public static Where mkWhereDelete(
101           final Opt<String> namespace,
102           final Opt<String> propertyName,
103           final Function<QPropertyDto, Opt<BooleanExpression>> mkValueExpression) {
104     final Opt<BooleanExpression> valueExpression = mkValueExpression.apply(Q_PROPERTY);
105     final BooleanExpression propertyPredicate = (namespace.isSome()
106             ? Q_PROPERTY.namespace.eq(namespace.get())
107             // Just passing null like in the other predicates yields an NPE for some reason I do not know.
108             // The isNotNull predicate prevents this.
109             : Q_PROPERTY.namespace.isNotNull())
110             .and(propertyName.isSome() ? Q_PROPERTY.propertyName.eq(propertyName.get()) : null)
111             .and(valueExpression.isSome() ? valueExpression.get() : null);
112     //
113     return new Where() {
114       @Override public BooleanExpression fromSnapshot(QSnapshotDto e) {
115         return new JPASubQuery()
116                 .from(Q_PROPERTY)
117                 .where(e.mediaPackageId.eq(Q_PROPERTY.mediaPackageId).and(propertyPredicate))
118                 .exists();
119       }
120 
121       @Override public BooleanExpression fromProperty(QPropertyDto p) {
122         return p.mediaPackageId.in(
123                 new JPASubQuery()
124                         .from(p)
125                         .where(propertyPredicate)
126                         .distinct()
127                         .list(p.mediaPackageId));
128       }
129     };
130   }
131 }