Open Source Repository

Home /hibernate/hibernate-3.2.4.ga | Repository Home



org/hibernate/criterion/ProjectionList.java
//$Id: ProjectionList.java 6490 2005-04-23 07:09:03Z oneovthafew $
package org.hibernate.criterion;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.type.Type;
import org.hibernate.util.ArrayHelper;

/**
 @author Gavin King
 */
public class ProjectionList implements Projection {
  
  private List elements = new ArrayList();
  
  protected ProjectionList() {}
  
  public ProjectionList create() {
    return new ProjectionList();
  }
  
  public ProjectionList add(Projection proj) {
    elements.add(proj);
    return this;
  }

  public ProjectionList add(Projection projection, String alias) {
    return addProjections.alias(projection, alias) );
  }

  public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)
  throws HibernateException {
    List types = new ArrayListgetLength() );
    for int i=0; i<getLength(); i++ ) {
      Type[] elemTypes = getProjection(i).getTypes(criteria, criteriaQuery);
      ArrayHelper.addAll(types, elemTypes);
    }
    return ArrayHelper.toTypeArray(types);
  }
  
  public String toSqlString(Criteria criteria, int loc, CriteriaQuery criteriaQuery
  throws HibernateException {
    StringBuffer buf = new StringBuffer();
    for int i=0; i<getLength(); i++ ) {
      Projection proj = getProjection(i);
      buf.appendproj.toSqlString(criteria, loc, criteriaQuery) );
      loc += proj.getColumnAliases(loc).length;
      if i<elements.size()-buf.append(", ");
    }
    return buf.toString();
  }
  
  public String toGroupSqlString(Criteria criteria, CriteriaQuery criteriaQuery
  throws HibernateException {
    StringBuffer buf = new StringBuffer();
    for int i=0; i<getLength(); i++ ) {
      Projection proj = getProjection(i);
      if proj.isGrouped() ) {
        buf.appendproj.toGroupSqlString(criteria, criteriaQuery) )
          .append(", ");
      }
    }
    if buf.length()>buf.setLengthbuf.length()-)//pull off the last ", "
    return buf.toString();
  }
  
  public String[] getColumnAliases(int loc) {
    List result = new ArrayListgetLength() );
    for int i=0; i<getLength(); i++ ) {
      String[] colAliases = getProjection(i).getColumnAliases(loc);
      ArrayHelper.addAll(result, colAliases);
      loc+=colAliases.length;
    }
    return ArrayHelper.toStringArray(result);
  }

  public String[] getColumnAliases(String alias, int loc) {
    for int i=0; i<getLength(); i++ ) {
      String[] result = getProjection(i).getColumnAliases(alias, loc);
      if (result!=nullreturn result;
      loc += getProjection(i).getColumnAliases(loc).length;
    }
    return null;
  }

  public Type[] getTypes(String alias, Criteria criteria, CriteriaQuery criteriaQuery) {
    for int i=0; i<getLength(); i++ ) {
      Type[] result = getProjection(i).getTypes(alias, criteria, criteriaQuery);
      if (result!=nullreturn result;
    }
    return null;
  }

  public String[] getAliases() {
    List result = new ArrayListgetLength() );
    for int i=0; i<getLength(); i++ ) {
      String[] aliases = getProjection(i).getAliases();
      ArrayHelper.addAll(result, aliases);
    }
    return ArrayHelper.toStringArray(result);

  }
  
  public Projection getProjection(int i) {
    return (Projectionelements.get(i);
  }
  
  public int getLength() {
    return elements.size();
  }
  
  public String toString() {
    return elements.toString();
  }

  public boolean isGrouped() {
    for int i=0; i<getLength(); i++ ) {
      if getProjection(i).isGrouped() ) return true;
    }
    return false;
  }
}