Skip to content

Method: lower(Expression)

1: /**
2: * Copyright (C) 2023 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.query.criteria;
16:
17: import cz.cvut.kbss.jopa.model.CriteriaQueryImpl;
18: import cz.cvut.kbss.jopa.model.query.criteria.*;
19: import cz.cvut.kbss.jopa.query.criteria.expressions.*;
20: import cz.cvut.kbss.jopa.sessions.CriteriaBuilder;
21: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
22:
23: import java.util.Arrays;
24:
25: public class CriteriaBuilderImpl implements CriteriaBuilder {
26:
27: private final UnitOfWorkImpl uow;
28:
29: public CriteriaBuilderImpl(UnitOfWorkImpl uow) {
30: this.uow = uow;
31: }
32:
33: @Override
34: public <T> CriteriaQuery<T> createQuery(Class<T> resultClass) {
35: return new CriteriaQueryImpl<>(new CriteriaQueryHolder<>(resultClass), uow.getMetamodel(), this);
36: }
37:
38: @Override
39: public <N extends Number> Expression<N> abs(Expression<N> x) {
40: validateFunctionArgument(x);
41: return new AbsFunction<>((Class<N>) x.getJavaType(), (AbstractPathExpression) x, this);
42: }
43:
44: @Override
45: public Expression<Integer> count(Expression<?> x) {
46: validateFunctionArgument(x);
47: return new CountFunction((AbstractPathExpression) x, this);
48: }
49:
50: @Override
51: public <N extends Number> Expression<N> ceil(Expression<N> x) {
52: validateFunctionArgument(x);
53: return new CeilFunction<>((Class<N>) x.getJavaType(), (AbstractPathExpression) x, this);
54: }
55:
56: @Override
57: public <N extends Number> Expression<N> floor(Expression<N> x) {
58: validateFunctionArgument(x);
59: return new FloorFunction<>((Class<N>) x.getJavaType(), (AbstractPathExpression) x, this);
60: }
61:
62: @Override
63: public Expression<Integer> length(Expression<String> x) {
64: validateFunctionArgument(x);
65: return new LengthFunction((AbstractPathExpression) x, this);
66: }
67:
68: @Override
69: public <T> ParameterExpression<T> parameter(Class<T> paramClass) {
70: if (paramClass == null) throw new IllegalArgumentException("Class must be defined.");
71: return new ParameterExpressionImpl<>(paramClass, null, this);
72: }
73:
74: @Override
75: public <T> ParameterExpression<T> parameter(Class<T> paramClass, String name) {
76: if (paramClass == null) throw new IllegalArgumentException("Class must be defined.");
77: return new ParameterExpressionImpl<>(paramClass, name, this);
78: }
79:
80: @Override
81: public <T> Expression<T> literal(T value) {
82: if (value == null) throw new IllegalArgumentException("Literal cannot be null.");
83: return new ExpressionLiteralImpl<>(value, this);
84: }
85:
86: @Override
87: public Expression<String> literal(String value, String languageTag) {
88: if (value == null) throw new IllegalArgumentException("Literal cannot be null.");
89: return new ExpressionLiteralImpl<>(value, languageTag, this);
90: }
91:
92: @Override
93: public Expression<String> lower(Expression<String> x) {
94: validateFunctionArgument(x);
95: return new LowerFunction((AbstractPathExpression) x, this);
96: }
97:
98: @Override
99: public Expression<String> upper(Expression<String> x) {
100: validateFunctionArgument(x);
101: return new UpperFunction((AbstractPathExpression) x, this);
102: }
103:
104: private void validateFunctionArgument(Expression<?> x) {
105: if (!(x instanceof AbstractPathExpression)) {
106: throw new IllegalArgumentException("Function can be applied only to path expressions.");
107: }
108: }
109:
110: @Override
111: public Order asc(Expression<?> x) {
112: return new OrderImpl(x);
113: }
114:
115: @Override
116: public Order desc(Expression<?> x) {
117: return new OrderImpl(x, false);
118: }
119:
120:
121: @Override
122: public Predicate and(Expression<Boolean> x, Expression<Boolean> y) {
123: return new CompoundedPredicateImpl(Predicate.BooleanOperator.AND, Arrays.asList(x, y), this);
124: }
125:
126: @Override
127: public Predicate and(Predicate... restrictions) {
128: if (restrictions.length == 1) return new SimplePredicateImpl(restrictions[0], this);
129: else return new CompoundedPredicateImpl(Predicate.BooleanOperator.AND, Arrays.asList(restrictions), this);
130: }
131:
132: @Override
133: public Predicate or(Expression<Boolean> x, Expression<Boolean> y) {
134: return new CompoundedPredicateImpl(Predicate.BooleanOperator.OR, Arrays.asList(x, y), this);
135: }
136:
137: @Override
138: public Predicate or(Predicate... restrictions) {
139: if (restrictions.length == 1)
140: return new SimplePredicateImpl(Predicate.BooleanOperator.OR, restrictions[0], this);
141: else return new CompoundedPredicateImpl(Predicate.BooleanOperator.OR, Arrays.asList(restrictions), this);
142: }
143:
144: @Override
145: public Predicate equal(Expression<?> x, Expression<?> y) {
146: return new SimplePredicateImpl(
147: new ExpressionEqualImpl((AbstractExpression<?>) x, (AbstractExpression<?>) y, this), this);
148: }
149:
150: @Override
151: public Predicate equal(Expression<?> x, Object y) {
152: return new SimplePredicateImpl(
153: new ExpressionEqualImpl((AbstractExpression<?>) x, new ExpressionLiteralImpl<>(y, this), this), this);
154: }
155:
156: @Override
157: public Predicate equal(Expression<?> x, String y, String languageTag) {
158: return new SimplePredicateImpl(
159: new ExpressionEqualImpl((AbstractExpression<?>) x, new ExpressionLiteralImpl<>(y, languageTag, this),
160: this), this);
161: }
162:
163: @Override
164: public Predicate notEqual(Expression<?> x, Expression<?> y) {
165: return new SimplePredicateImpl(
166: new ExpressionNotEqualImpl((AbstractExpression<?>) x, (AbstractExpression<?>) y, this), this);
167: }
168:
169: @Override
170: public Predicate notEqual(Expression<?> x, Object y) {
171: return new SimplePredicateImpl(
172: new ExpressionNotEqualImpl((AbstractExpression<?>) x, new ExpressionLiteralImpl<>(y, this), this),
173: this);
174:
175: }
176:
177: @Override
178: public Predicate like(Expression<String> x, Expression<String> pattern) {
179: return new SimplePredicateImpl(
180: new ExpressionLikeImpl((AbstractExpression<String>) x, (AbstractExpression<String>) pattern, this),
181: this);
182: }
183:
184: @Override
185: public Predicate like(Expression<String> x, String pattern) {
186: return new SimplePredicateImpl(
187: new ExpressionLikeImpl((AbstractExpression<String>) x, new ExpressionLiteralImpl<>(pattern, this),
188: this), this);
189: }
190:
191: @Override
192: public Predicate notLike(Expression<String> x, Expression<String> pattern) {
193: return new SimplePredicateImpl(
194: new ExpressionNotLikeImpl((AbstractExpression<String>) x, (AbstractExpression<String>) pattern, this),
195: this);
196: }
197:
198: @Override
199: public Predicate notLike(Expression<String> x, String pattern) {
200: return new SimplePredicateImpl(
201: new ExpressionNotLikeImpl((AbstractExpression<String>) x, new ExpressionLiteralImpl<>(pattern, this),
202: this), this);
203: }
204:
205: @Override
206: public Predicate not(Expression<Boolean> restriction) {
207: return wrapExpressionToPredicateWithRepair(restriction).not();
208: }
209:
210: @Override
211: public <T> In<T> in(Expression<? extends T> expression) {
212: return new ExpressionInImpl<>(expression, this);
213: }
214:
215: @Override
216: public <T> In<T> notIn(Expression<? extends T> expression) {
217: In<T> inExpression = new ExpressionInImpl<>(expression, this);
218: inExpression.not();
219: return inExpression;
220: }
221:
222: @Override
223: public <Y extends Comparable<? super Y>> Predicate greaterThan(Expression<? extends Y> x,
224: Expression<? extends Y> y) {
225: return new SimplePredicateImpl(
226: new ExpressionGreaterThanImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
227: }
228:
229: @Override
230: public <Y extends Comparable<? super Y>> Predicate greaterThan(Expression<? extends Y> x, Y y) {
231: return new SimplePredicateImpl(
232: new ExpressionGreaterThanImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this), this),
233: this);
234: }
235:
236: @Override
237: public <Y extends Comparable<? super Y>> Predicate greaterThanOrEqual(Expression<? extends Y> x,
238: Expression<? extends Y> y) {
239: return new SimplePredicateImpl(
240: new ExpressionGreaterThanOrEqualImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
241: }
242:
243: @Override
244: public <Y extends Comparable<? super Y>> Predicate greaterThanOrEqual(Expression<? extends Y> x, Y y) {
245: return new SimplePredicateImpl(
246: new ExpressionGreaterThanOrEqualImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this),
247: this), this);
248: }
249:
250: @Override
251: public <Y extends Comparable<? super Y>> Predicate lessThan(Expression<? extends Y> x, Expression<? extends Y> y) {
252: return new SimplePredicateImpl(
253: new ExpressionLessThanImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
254: }
255:
256: @Override
257: public <Y extends Comparable<? super Y>> Predicate lessThan(Expression<? extends Y> x, Y y) {
258: return new SimplePredicateImpl(
259: new ExpressionLessThanImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this), this),
260: this);
261: }
262:
263: @Override
264: public <Y extends Comparable<? super Y>> Predicate lessThanOrEqual(Expression<? extends Y> x,
265: Expression<? extends Y> y) {
266: return new SimplePredicateImpl(
267: new ExpressionLessThanOrEqualImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
268: }
269:
270: @Override
271: public <Y extends Comparable<? super Y>> Predicate lessThanOrEqual(Expression<? extends Y> x, Y y) {
272: return new SimplePredicateImpl(
273: new ExpressionLessThanOrEqualImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this),
274: this), this);
275: }
276:
277:
278: /**
279: * Method wraps given boolean expression to Predicate and if path expression occur, it wrap it to
280: * ExpressionEqualsImpl before. For example: {@literal Expression<Boolean> expression =
281: * factory.get("attributeName");} Looks like boolean expression but in fact it is not boolean expression, so we need
282: * to fix this.
283: *
284: * @param expression - boolean or path expression
285: * @return Expression wrapped in Predicate
286: */
287: public Predicate wrapExpressionToPredicateWithRepair(Expression<Boolean> expression) {
288: if (expression instanceof Predicate) {
289: return (Predicate) expression;
290: } else if (expression instanceof AbstractPathExpression) {
291: return new SimplePredicateImpl(
292: new ExpressionEqualImpl((AbstractExpression) expression, (AbstractExpression) this.literal(true),
293: this), this);
294: } else {
295: return new SimplePredicateImpl(expression, this);
296: }
297: }
298:
299: }