1 package org.hibernate.criterion; 3 4 import java.util.ArrayList ; 5 import java.util.List ; 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 15 public class ProjectionList implements Projection { 16 17 private List elements = new ArrayList (); 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 alias) { 31 return add( Projections.alias(projection, alias) ); 32 } 33 34 public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery) 35 throws HibernateException { 36 List types = new ArrayList ( 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 toSqlString(Criteria criteria, int loc, CriteriaQuery criteriaQuery) 45 throws HibernateException { 46 StringBuffer buf = new StringBuffer (); 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 toGroupSqlString(Criteria criteria, CriteriaQuery criteriaQuery) 57 throws HibernateException { 58 StringBuffer buf = new StringBuffer (); 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 ); return buf.toString(); 68 } 69 70 public String [] getColumnAliases(int loc) { 71 List result = new ArrayList ( getLength() ); 72 for ( int i=0; i<getLength(); i++ ) { 73 String [] 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 [] getColumnAliases(String alias, int loc) { 81 for ( int i=0; i<getLength(); i++ ) { 82 String [] 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 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 [] getAliases() { 98 List result = new ArrayList ( getLength() ); 99 for ( int i=0; i<getLength(); i++ ) { 100 String [] 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 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 |