1 package org.hibernate.criterion; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 7 import org.hibernate.Criteria; 8 import org.hibernate.HibernateException; 9 import org.hibernate.engine.TypedValue; 10 import org.hibernate.util.StringHelper; 11 12 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, CriteriaQuery criteriaQuery) 37 throws HibernateException { 38 ArrayList typedValues = new ArrayList (); 39 Iterator iter = criteria.iterator(); 40 while ( iter.hasNext() ) { 41 TypedValue[] subvalues = ( (Criterion) iter.next() ).getTypedValues(crit, criteriaQuery); 42 for ( int i=0; i<subvalues.length; i++ ) { 43 typedValues.add( subvalues[i] ); 44 } 45 } 46 return (TypedValue[]) typedValues.toArray( new TypedValue[ typedValues.size() ] ); 47 } 48 49 public String toSqlString(Criteria crit, CriteriaQuery criteriaQuery) 50 throws HibernateException { 51 52 if ( criteria.size()==0 ) return "1=1"; 53 54 StringBuffer buffer = new StringBuffer () 55 .append('('); 56 Iterator iter = criteria.iterator(); 57 while ( iter.hasNext() ) { 58 buffer.append( ( (Criterion) iter.next() ).toSqlString(crit, criteriaQuery) ); 59 if ( iter.hasNext() ) buffer.append(' ').append(op).append(' '); 60 } 61 return buffer.append(')').toString(); 62 } 63 64 67 public String toString() { 68 return '(' + StringHelper.join( ' ' + op + ' ', criteria.iterator() ) + ')'; 69 } 70 71 } 72 | Popular Tags |