View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.database.function.aggregation;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.hibernate.criterion.Projection;
24  import org.hibernate.criterion.ProjectionList;
25  import org.hibernate.criterion.Projections;
26  
27  public class HibernateProjectionList implements IProjectionList {
28  
29  	private ProjectionList projectionList = Projections.projectionList();
30  
31  	private Map columnNums = null;
32  
33  	private List aliases = new ArrayList();
34  	
35  	public HibernateProjectionList() {
36  	}
37  
38  	public ProjectionList getProjectionList() {
39  		return projectionList;
40  	}
41  
42  	public void extendColumn(int columnNum) {
43  		extendColumn(projectionList.getLength(), columnNum);
44  	}
45  	
46  	public void extendColumn(int position, int columnNum) {
47  		if (columnNums == null) {
48  			columnNums = new HashMap();
49  		}
50  		columnNums.put(new Integer(position), new Integer(columnNum));
51  	}
52  
53  	public boolean hasExtendedColumn() {
54  		return columnNums != null;
55  	}
56  
57  	public int getColumnNum(int pos) {
58  		Object num = columnNums.get(new Integer(pos));
59  		if (num == null) {
60  			return 1;
61  		} else {
62  			return ((Integer) num).intValue();
63  		}
64  	}
65  
66  	public IProjectionList addMax(String field, String alias) {
67  		addProjection(Projections.max(field), alias);
68  		return this;
69  	}
70  
71  	public IProjectionList addMin(String field, String alias) {
72  		addProjection(Projections.min(field), alias);
73  		return this;
74  	}
75  
76  	public IProjectionList addAvg(String field, String alias) {
77  		addProjection(Projections.avg(field), alias);
78  		return this;
79  	}
80  
81  	public IProjectionList addSum(String field, String alias) {
82  		addProjection(Projections.sum(field), alias);
83  		return this;
84  	}
85  
86  	public IProjectionList addGroupProperty(String field, String alias) {
87  		addProjection(Projections.groupProperty(field), alias);
88  		return this;
89  	}
90  
91  	public IProjectionList addRowCount(String alias) {
92  		addProjection(Projections.rowCount(), alias);
93  		return this;
94  	}
95  
96  	public IProjectionList addCountProjection(String field, String alias) {
97  		addProjection(Projections.countDistinct(field), alias);
98  		return this;
99  	}
100 
101 	public boolean hasAlias(String alias){
102 		return aliases.contains(alias);
103 	}
104 	
105 	private void addProjection(Projection projection, String alias) {
106 		aliases.add(alias);
107 		projectionList.add(projection, alias);
108 	}
109 }