1 package org.synchronoss.cpo.core;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.util.Collection;
26 import org.synchronoss.cpo.core.enums.Comparison;
27 import org.synchronoss.cpo.core.enums.Logical;
28 import org.synchronoss.cpo.core.meta.domain.CpoAttribute;
29 import org.synchronoss.cpo.core.meta.domain.CpoClass;
30
31
32
33
34
35
36
37
38 public class BindableCpoWhere extends Node implements CpoWhere {
39
40
41 private static final long serialVersionUID = 1L;
42
43 static final String[] comparisons = {
44 "=",
45 "<",
46 ">",
47 "<>",
48 "IN",
49 "LIKE",
50 "<=",
51 ">=",
52 "EXISTS",
53 "IS NULL"
54 };
55 static final String[] logicals = {
56 "AND",
57 "OR"
58 };
59
60
61 private Comparison comparison = Comparison.NONE;
62
63
64 private Logical logical = Logical.NONE;
65
66
67 private String attribute = null;
68
69
70 private String rightAttribute = null;
71
72
73 private Object value = null;
74
75
76 private String attributeFunction = null;
77
78
79 private String rightAttributeFunction = null;
80
81
82 private String valueFunction = null;
83
84
85 private boolean not = false;
86
87
88 private String staticValue_ = null;
89
90
91 private String name = "__CPO_WHERE__";
92
93
94
95
96
97
98
99
100
101
102
103 public <T> BindableCpoWhere(Logical logical, String attr, Comparison comp, T value) {
104 setLogical(logical);
105 setAttribute(attr);
106 setComparison(comp);
107 setValue(value);
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121 public <T> BindableCpoWhere(Logical logical, String attr, Comparison comp, T value, boolean not) {
122 setLogical(logical);
123 setAttribute(attr);
124 setComparison(comp);
125 setValue(value);
126 setNot(not);
127 }
128
129
130 public BindableCpoWhere() {}
131
132
133 @Override
134 public void setComparison(Comparison comparison) {
135 this.comparison = comparison;
136 }
137
138
139 @Override
140 public Comparison getComparison() {
141 return this.comparison;
142 }
143
144
145 @Override
146 public void setLogical(Logical logical) {
147 this.logical = logical;
148 }
149
150
151 @Override
152 public Logical getLogical() {
153 return this.logical;
154 }
155
156
157 @Override
158 public void setAttribute(String s) {
159 this.attribute = s;
160 }
161
162
163 @Override
164 public String getAttribute() {
165 return this.attribute;
166 }
167
168
169 @Override
170 public void setRightAttribute(String s) {
171 this.rightAttribute = s;
172 }
173
174
175 @Override
176 public String getRightAttribute() {
177 return this.rightAttribute;
178 }
179
180
181 @Override
182 public void setValue(Object s) {
183 this.value = s;
184 }
185
186
187 @Override
188 public Object getValue() {
189 return this.value;
190 }
191
192
193 @Override
194 public void setStaticValue(String staticValue) {
195 this.staticValue_ = staticValue;
196 }
197
198
199 @Override
200 public String getStaticValue() {
201 return this.staticValue_;
202 }
203
204
205 @Override
206 public boolean getNot() {
207 return this.not;
208 }
209
210
211 @Override
212 public void setNot(boolean b) {
213 this.not = b;
214 }
215
216
217
218
219
220
221
222
223
224
225 public String toString(CpoClass cpoClass) {
226 StringBuilder sb = new StringBuilder();
227 CpoAttribute cpoAttribute = null;
228
229 if (getLogical() != Logical.NONE) {
230 sb.append(" ");
231 sb.append(getLogical().operator);
232 } else if (!hasParent()) {
233
234 sb.append("WHERE");
235 }
236
237 if (getNot()) {
238 sb.append(" NOT");
239 }
240
241 if (getAttribute() != null) {
242 if (!sb.isEmpty()) {
243 sb.append(" ");
244 }
245 String fullyQualifiedColumn;
246
247 cpoAttribute = cpoClass.getAttributeJava(getAttribute());
248 if (cpoAttribute == null) {
249
250
251 fullyQualifiedColumn = getAttribute();
252 } else {
253 fullyQualifiedColumn = buildColumnName(cpoAttribute);
254 }
255
256 if (getAttributeFunction() != null) {
257 if (cpoAttribute != null) {
258 sb.append(
259 buildFunction(
260 getAttributeFunction(), cpoAttribute.getJavaName(), fullyQualifiedColumn));
261 } else {
262 sb.append(getAttributeFunction());
263 }
264 } else {
265 sb.append(fullyQualifiedColumn);
266 }
267 }
268
269 if (getComparison() != Comparison.NONE) {
270 sb.append(" ");
271 sb.append(getComparison().operator);
272 }
273
274 if (getComparison() != Comparison.ISNULL
275 && (getValue() != null || getRightAttribute() != null || getStaticValue() != null)) {
276 sb.append(" ");
277
278 if (getValue() != null) {
279 if (getValueFunction() != null) {
280 if (cpoAttribute == null) {
281 cpoAttribute = cpoClass.getAttributeJava(getRightAttribute());
282 }
283 sb.append(
284 buildFunction(
285 getValueFunction(),
286 getAttributeName(cpoAttribute, getAttribute(), getRightAttribute()),
287 "?"));
288 } else if (getComparison() == Comparison.IN && getValue() instanceof Collection) {
289 Collection coll = (Collection) getValue();
290 sb.append("(");
291 if (coll.size() > 0) {
292 sb.append("?");
293 for (int i = 1; i < coll.size(); i++) {
294 sb.append(", ?");
295 }
296 }
297 sb.append(")");
298 } else {
299 sb.append("?");
300 }
301 } else if (getRightAttribute() != null) {
302 cpoAttribute = cpoClass.getAttributeJava(getRightAttribute());
303 String fullyQualifiedColumn;
304 if (cpoAttribute == null) {
305 fullyQualifiedColumn = getRightAttribute();
306 } else {
307 fullyQualifiedColumn = buildColumnName(cpoAttribute);
308 }
309
310 if (getRightAttributeFunction() != null) {
311 sb.append(
312 buildFunction(
313 getRightAttributeFunction(),
314 getAttributeName(cpoAttribute, getAttribute(), getRightAttribute()),
315 fullyQualifiedColumn));
316 } else {
317 sb.append(fullyQualifiedColumn);
318 }
319 } else if (getStaticValue() != null) {
320 sb.append(getStaticValue());
321 }
322 }
323 return sb.toString();
324 }
325
326 private String getAttributeName(
327 CpoAttribute attribute, String leftAttribute, String rightAttribute) {
328 String attrName = null;
329
330 if (attribute != null) {
331 attrName = attribute.getJavaName();
332 }
333
334 if (attrName == null && leftAttribute != null) {
335 attrName = leftAttribute;
336 }
337
338 if (attrName == null && rightAttribute != null) {
339 attrName = rightAttribute;
340 }
341
342 return attrName;
343 }
344
345
346
347
348
349
350
351 @Override
352 public void addWhere(CpoWhere cw) throws CpoException {
353 try {
354 this.addChild((Node) cw);
355 } catch (ChildNodeException cne) {
356 throw new CpoException("Error Adding Where Statement");
357 }
358 }
359
360
361 @Override
362 public void setAttributeFunction(String s) {
363 this.attributeFunction = s;
364 }
365
366
367 @Override
368 public String getAttributeFunction() {
369 return this.attributeFunction;
370 }
371
372
373 @Override
374 public void setValueFunction(String s) {
375 this.valueFunction = s;
376 }
377
378
379 @Override
380 public String getValueFunction() {
381 return this.valueFunction;
382 }
383
384
385 @Override
386 public void setRightAttributeFunction(String s) {
387 this.rightAttributeFunction = s;
388 }
389
390
391 @Override
392 public String getRightAttributeFunction() {
393 return this.rightAttributeFunction;
394 }
395
396 private String buildFunction(String function, String match, String value) {
397 StringBuilder sb = new StringBuilder();
398 int attrOffset;
399 int fromIndex = 0;
400
401 if (function != null && !function.isEmpty()) {
402 while ((attrOffset = function.indexOf(match, fromIndex)) != -1) {
403 sb.append(function, fromIndex, attrOffset);
404 sb.append(value);
405 fromIndex = attrOffset + match.length();
406 }
407 sb.append(function.substring(fromIndex));
408 }
409
410 return sb.toString();
411 }
412
413
414
415
416
417
418
419
420 protected String buildColumnName(CpoAttribute attribute) {
421 return attribute.getDataName();
422 }
423
424
425 @Override
426 public String getName() {
427 return name;
428 }
429
430
431 @Override
432 public void setName(String name) {
433 this.name = name;
434 }
435 }