Skip to content

Method: CriteriaParameterFiller()

1: /**
2: * Copyright (C) 2022 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.TypedQueryImpl;
18: import cz.cvut.kbss.jopa.model.query.criteria.ParameterExpression;
19: import cz.cvut.kbss.jopa.query.criteria.expressions.ExpressionLiteralImpl;
20: import cz.cvut.kbss.jopa.query.criteria.expressions.ParameterExpressionImpl;
21:
22: import java.util.HashMap;
23: import java.util.Map;
24:
25: public class CriteriaParameterFiller {
26: private final HashMap<String, ExpressionLiteralImpl> literalParameters;
27: private int counter;
28:
29: public CriteriaParameterFiller() {
30: this.literalParameters = new HashMap<>();
31: this.counter = 0;
32: }
33:
34: /**
35: * Register literal expression as query parameter and return generated name for query.
36: *
37: * @param parameter - literal expression
38: * @return String - generated name for query
39: */
40: public String registerParameter(ExpressionLiteralImpl parameter) {
41: String name = generateParameterName();
42: literalParameters.put(name, parameter);
43: return ":" + name;
44: }
45:
46: /**
47: * Register parameter expression as query parameter. Return real name if exists, generated name otherwise.
48: *
49: * @param parameter - parameter expression
50: * @return String - real name for query if exists, generated name for query otherwise
51: */
52: public String registerParameter(ParameterExpression parameter) {
53: if (parameter.getName() == null) {
54: String name = generateParameterName();
55: ((ParameterExpressionImpl) parameter).setNameIfUnnamed(name);
56: }
57: return ":" + parameter.getName();
58: }
59:
60: /**
61: * Sets value from literal expressions registered as parameters to query parameters.
62: *
63: * @param query - TypedQuery fom setting parameters value
64: */
65: public <T> void setValuesToRegisteredParameters(TypedQueryImpl<T> query) {
66: for (Map.Entry<String, ExpressionLiteralImpl> e : literalParameters.entrySet()) {
67: if (e.getValue().getLanguageTag() != null) {
68: query.setParameter(e.getKey(), (String) e.getValue().getValue(), e.getValue().getLanguageTag());
69: } else {
70: query.setParameter(e.getKey(), literalParameters.get(e.getKey()).getValue());
71: }
72: }
73: }
74:
75: private String generateParameterName() {
76: return "generatedName" + this.counter++;
77: }
78: }