01: package org.hibernate.criterion;
02:
03: import java.util.ArrayList;
04: import java.util.Iterator;
05: import java.util.List;
06:
07: import org.hibernate.Criteria;
08: import org.hibernate.HibernateException;
09: import org.hibernate.engine.TypedValue;
10: import org.hibernate.util.StringHelper;
11:
12: /**
13: * A sequence of a logical expressions combined by some
14: * associative logical operator
15: *
16: * @author Gavin King
17: */
18: public class Junction implements Criterion {
19:
20: private final List criteria = new ArrayList();
21: private final String op;
22:
23: protected Junction(String op) {
24: this .op = op;
25: }
26:
27: public Junction add(Criterion criterion) {
28: criteria.add(criterion);
29: return this ;
30: }
31:
32: public String getOp() {
33: return op;
34: }
35:
36: public TypedValue[] getTypedValues(Criteria crit,
37: CriteriaQuery criteriaQuery) throws HibernateException {
38: ArrayList typedValues = new ArrayList();
39: Iterator iter = criteria.iterator();
40: while (iter.hasNext()) {
41: TypedValue[] subvalues = ((Criterion) iter.next())
42: .getTypedValues(crit, criteriaQuery);
43: for (int i = 0; i < subvalues.length; i++) {
44: typedValues.add(subvalues[i]);
45: }
46: }
47: return (TypedValue[]) typedValues
48: .toArray(new TypedValue[typedValues.size()]);
49: }
50:
51: public String toSqlString(Criteria crit, CriteriaQuery criteriaQuery)
52: throws HibernateException {
53:
54: if (criteria.size() == 0)
55: return "1=1";
56:
57: StringBuffer buffer = new StringBuffer().append('(');
58: Iterator iter = criteria.iterator();
59: while (iter.hasNext()) {
60: buffer.append(((Criterion) iter.next()).toSqlString(crit,
61: criteriaQuery));
62: if (iter.hasNext())
63: buffer.append(' ').append(op).append(' ');
64: }
65: return buffer.append(')').toString();
66: }
67:
68: /**
69: * @see java.lang.Object#toString()
70: */
71: public String toString() {
72: return '(' + StringHelper.join(' ' + op + ' ', criteria
73: .iterator()) + ')';
74: }
75:
76: }
|