1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
37
38
39 @ParametersAreNonnullByDefault
40 public final class DeleteQueryContribution {
41
42 final Stream<EntityPath<?>> from;
43
44
45
46
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
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
74
75 public static DeleteQueryContribution mk() {
76 return new DeleteQueryContribution(Stream.<EntityPath<?>>empty(), NO_WHERE, Opt.<BooleanExpression>none(), "");
77 }
78
79
80
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 }