Added sourceReference
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / persistence / query / Grouping.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.persistence.query;
11
12
13 import org.hibernate.Criteria;
14 import org.hibernate.criterion.Order;
15 import org.hibernate.criterion.Projection;
16 import org.hibernate.criterion.ProjectionList;
17 import org.hibernate.criterion.Property;
18
19 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
20
21 public class Grouping {
22 private String associatedObject;
23 private String associatedObjectAlias;
24 private String propertyName;
25 protected String name;
26 private SortOrder order;
27
28 public Grouping(String propertyPath, String name, String associatedObjectAlias, SortOrder order) {
29 int pos;
30 if((pos = propertyPath.indexOf('.', 0)) >= 0){
31 this.associatedObject = propertyPath.substring(0, pos);
32 this.propertyName = propertyPath.substring(pos + 1);
33 } else {
34 this.propertyName = propertyPath;
35 }
36 this.name = name;
37 this.order = order;
38 this.associatedObjectAlias = associatedObjectAlias;
39 }
40
41 protected void setPropertyName(String propertyName) {
42 this.propertyName = propertyName;
43 }
44
45 public String getPropertyName() {
46 return propertyName;
47 }
48
49 public String getAssociatedObj() {
50 return associatedObject;
51 }
52
53 public String getAssociatedObjectAlias() {
54 return associatedObjectAlias;
55 }
56
57 public String getName() {
58 return name;
59 }
60
61 protected SortOrder getOrder() {
62 return order;
63 }
64
65 public void addOrder(Criteria criteria) {
66 if(order != null) {
67 if(order.equals(SortOrder.ASCENDING)) {
68 criteria.addOrder(Order.asc(this.name));
69 } else {
70 criteria.addOrder(Order.desc(this.name));
71 }
72 }
73 }
74
75 public void addProjection(ProjectionList projectionList) {
76 if(associatedObjectAlias != null) {
77 projectionList.add(Property.forName(associatedObjectAlias + "." + propertyName).group(),name);
78 } else {
79 projectionList.add(Property.forName(propertyName).group(),name);
80 }
81 }
82
83 }