View Javadoc

1   /*
2    * Copyright 2004-2005 Emmanouil Batsis
3    * 
4    * Licensed under the GNU General Public 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.gnu.org/licenses/gpl.txt
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   * 
16   */
17  package com.geekologue.md4j.dao.hibernate;
18  
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.LinkedHashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  import org.apache.log4j.Logger;
27  import org.hibernate.Criteria;
28  import org.hibernate.criterion.ProjectionList;
29  import org.hibernate.criterion.Projections;
30  
31  /***
32   * @author manos
33   * 
34   */
35  public class Helper {
36      private static Logger log = Logger.getLogger(Helper.class);
37  
38      /***
39       * Produce a List containint Maps for each row for the given scalar query
40       * This method will only change the given criteria to add the appropriate
41       * projections, aliases must already be properly set, for example, a
42       * projection "foo.bar" assumes "foo" is already an alias.
43       * 
44       * @param query
45       * @param projectionProps
46       */
47      public static final List getQueryResultAsMapList(Criteria query,
48              Set projectionProps) {
49          ProjectionList projectionList = Projections.projectionList();
50          for (Iterator iter = projectionProps.iterator(); iter.hasNext();) {
51              String projectionName = (String) iter.next();
52              projectionList.add(Projections.property(projectionName));
53          }
54          query.setProjection(projectionList);
55          List qResults = query.list();
56          Object[] propNames = projectionProps.toArray();
57          List mapList = new ArrayList(qResults.size());
58          for (int i = 0; i < qResults.size(); i++) {
59              Object oRow = qResults.get(i);
60              HashMap map = new HashMap();
61              if (oRow.getClass().isArray()) {
62                  Object[] row = (Object[]) oRow;
63                  for (int j = 0; j < row.length; j++) {
64                      map.put(propNames[j], row[j]);
65                  }
66              } else {
67                  map.put(propNames[0], oRow);
68              }
69              mapList.add(map);
70          }
71          return mapList;
72      }
73  
74      /***
75       * 
76       * @param query
77       * @param projectionProps
78       */
79      public static final Map getQueryResultAsMap(Criteria query,
80              String keyProjectionProp, String valueProjectionProp) {
81          if (valueProjectionProp == null) {
82              valueProjectionProp = keyProjectionProp;
83          }
84          // we need linked hash map for proper insert-based ordering
85          Map map = new LinkedHashMap();
86          ProjectionList projectionList = Projections.projectionList().add(
87                  Projections.property(keyProjectionProp).as("mapKey0")).add(
88                  Projections.property(valueProjectionProp).as("mapValue0"));
89          query.setProjection(projectionList);
90          List qResults = query.list();
91          for (int i = 0; i < qResults.size(); i++) {
92              Object[] row = (Object[]) qResults.get(i);
93              log.info("Adding: " + row[0] + ", " + row[1]);
94              map.put(row[0], row[1]);
95          }
96          return map;
97      }
98  }