KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: ProjectionList.java,v 1.15 2005/04/23 07:09:03 oneovthafew Exp $
2
package org.hibernate.criterion;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.hibernate.Criteria;
8 import org.hibernate.HibernateException;
9 import org.hibernate.type.Type;
10 import org.hibernate.util.ArrayHelper;
11
12 /**
13  * @author Gavin King
14  */

15 public class ProjectionList implements Projection {
16     
17     private List JavaDoc elements = new ArrayList JavaDoc();
18     
19     protected ProjectionList() {}
20     
21     public ProjectionList create() {
22         return new ProjectionList();
23     }
24     
25     public ProjectionList add(Projection proj) {
26         elements.add(proj);
27         return this;
28     }
29
30     public ProjectionList add(Projection projection, String JavaDoc alias) {
31         return add( Projections.alias(projection, alias) );
32     }
33
34     public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)
35     throws HibernateException {
36         List JavaDoc types = new ArrayList JavaDoc( getLength() );
37         for ( int i=0; i<getLength(); i++ ) {
38             Type[] elemTypes = getProjection(i).getTypes(criteria, criteriaQuery);
39             ArrayHelper.addAll(types, elemTypes);
40         }
41         return ArrayHelper.toTypeArray(types);
42     }
43     
44     public String JavaDoc toSqlString(Criteria criteria, int loc, CriteriaQuery criteriaQuery)
45     throws HibernateException {
46         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
47         for ( int i=0; i<getLength(); i++ ) {
48             Projection proj = getProjection(i);
49             buf.append( proj.toSqlString(criteria, loc, criteriaQuery) );
50             loc += proj.getColumnAliases(loc).length;
51             if ( i<elements.size()-1 ) buf.append(", ");
52         }
53         return buf.toString();
54     }
55     
56     public String JavaDoc toGroupSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
57     throws HibernateException {
58         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
59         for ( int i=0; i<getLength(); i++ ) {
60             Projection proj = getProjection(i);
61             if ( proj.isGrouped() ) {
62                 buf.append( proj.toGroupSqlString(criteria, criteriaQuery) )
63                     .append(", ");
64             }
65         }
66         if ( buf.length()>2 ) buf.setLength( buf.length()-2 ); //pull off the last ", "
67
return buf.toString();
68     }
69     
70     public String JavaDoc[] getColumnAliases(int loc) {
71         List JavaDoc result = new ArrayList JavaDoc( getLength() );
72         for ( int i=0; i<getLength(); i++ ) {
73             String JavaDoc[] colAliases = getProjection(i).getColumnAliases(loc);
74             ArrayHelper.addAll(result, colAliases);
75             loc+=colAliases.length;
76         }
77         return ArrayHelper.toStringArray(result);
78     }
79
80     public String JavaDoc[] getColumnAliases(String JavaDoc alias, int loc) {
81         for ( int i=0; i<getLength(); i++ ) {
82             String JavaDoc[] result = getProjection(i).getColumnAliases(alias, loc);
83             if (result!=null) return result;
84             loc += getProjection(i).getColumnAliases(loc).length;
85         }
86         return null;
87     }
88
89     public Type[] getTypes(String JavaDoc alias, Criteria criteria, CriteriaQuery criteriaQuery) {
90         for ( int i=0; i<getLength(); i++ ) {
91             Type[] result = getProjection(i).getTypes(alias, criteria, criteriaQuery);
92             if (result!=null) return result;
93         }
94         return null;
95     }
96
97     public String JavaDoc[] getAliases() {
98         List JavaDoc result = new ArrayList JavaDoc( getLength() );
99         for ( int i=0; i<getLength(); i++ ) {
100             String JavaDoc[] aliases = getProjection(i).getAliases();
101             ArrayHelper.addAll(result, aliases);
102         }
103         return ArrayHelper.toStringArray(result);
104
105     }
106     
107     public Projection getProjection(int i) {
108         return (Projection) elements.get(i);
109     }
110     
111     public int getLength() {
112         return elements.size();
113     }
114     
115     public String JavaDoc toString() {
116         return elements.toString();
117     }
118
119     public boolean isGrouped() {
120         for ( int i=0; i<getLength(); i++ ) {
121             if ( getProjection(i).isGrouped() ) return true;
122         }
123         return false;
124     }
125 }
126
Popular Tags