KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > criterion > InExpression


1 //$Id: InExpression.java,v 1.15 2005/07/19 23:25:35 oneovthafew Exp $
2
package org.hibernate.criterion;
3
4 import java.util.ArrayList JavaDoc;
5
6
7
8 import org.hibernate.Criteria;
9 import org.hibernate.EntityMode;
10 import org.hibernate.HibernateException;
11
12
13 import org.hibernate.engine.TypedValue;
14
15 import org.hibernate.type.AbstractComponentType;
16 import org.hibernate.type.Type;
17 import org.hibernate.util.StringHelper;
18
19 /**
20  * Constrains the property to a specified list of values
21  * @author Gavin King
22  */

23 public class InExpression implements Criterion {
24
25     private final String JavaDoc propertyName;
26     private final Object JavaDoc[] values;
27
28     protected InExpression(String JavaDoc propertyName, Object JavaDoc[] values) {
29         this.propertyName = propertyName;
30         this.values = values;
31     }
32
33     public String JavaDoc toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
34     throws HibernateException {
35         String JavaDoc[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
36         String JavaDoc singleValueParam = StringHelper.repeat( "?, ", columns.length-1 ) + "?";
37         if ( columns.length>1 ) singleValueParam = '(' + singleValueParam + ')';
38         String JavaDoc params = values.length>0 ?
39             StringHelper.repeat( singleValueParam + ", ", values.length-1 ) + singleValueParam :
40             "";
41         String JavaDoc cols = StringHelper.join(", ", columns);
42         if ( columns.length>1 ) cols = '(' + cols + ')';
43         return cols + " in (" + params + ')';
44     }
45
46     public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery)
47     throws HibernateException {
48         ArrayList JavaDoc list = new ArrayList JavaDoc();
49         Type type = criteriaQuery.getTypeUsingProjection(criteria, propertyName);
50         if ( type.isComponentType() ) {
51             AbstractComponentType actype = (AbstractComponentType) type;
52             Type[] types = actype.getSubtypes();
53             for ( int i=0; i<types.length; i++ ) {
54                 for ( int j=0; j<values.length; j++ ) {
55                     Object JavaDoc subval = values[j]==null ?
56                         null :
57                         actype.getPropertyValues( values[j], EntityMode.POJO )[i];
58                     list.add( new TypedValue( types[i], subval, EntityMode.POJO ) );
59                 }
60             }
61         }
62         else {
63             for ( int j=0; j<values.length; j++ ) {
64                 list.add( new TypedValue( type, values[j], EntityMode.POJO ) );
65             }
66         }
67         return (TypedValue[]) list.toArray( new TypedValue[ list.size() ] );
68     }
69
70     public String JavaDoc toString() {
71         return propertyName + " in (" + StringHelper.toString(values) + ')';
72     }
73
74 }
75
Popular Tags