removing parameters for WorkingSetService method
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / persistence / dao / hibernate / description / WorkingSetDao.java
1 package eu.etaxonomy.cdm.persistence.dao.hibernate.description;
2
3 import java.util.HashMap;
4 import java.util.HashSet;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Set;
8 import java.util.UUID;
9
10 import org.apache.log4j.Logger;
11 import org.hibernate.Criteria;
12 import org.hibernate.Query;
13 import org.hibernate.criterion.Restrictions;
14 import org.springframework.beans.factory.annotation.Qualifier;
15 import org.springframework.stereotype.Repository;
16
17 import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
18 import eu.etaxonomy.cdm.model.description.CategoricalData;
19 import eu.etaxonomy.cdm.model.description.DescriptionBase;
20 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
21 import eu.etaxonomy.cdm.model.description.DescriptiveSystemRole;
22 import eu.etaxonomy.cdm.model.description.Feature;
23 import eu.etaxonomy.cdm.model.description.QuantitativeData;
24 import eu.etaxonomy.cdm.model.description.WorkingSet;
25 import eu.etaxonomy.cdm.persistence.dao.description.IWorkingSetDao;
26 import eu.etaxonomy.cdm.persistence.dao.hibernate.common.AnnotatableDaoImpl;
27
28 @Repository
29 @Qualifier("workingSetDaoImpl")
30 public class WorkingSetDao extends AnnotatableDaoImpl<WorkingSet> implements IWorkingSetDao {
31 private static final Logger logger = Logger.getLogger(WorkingSetDao.class);
32
33 public WorkingSetDao() {
34 super(WorkingSet.class);
35 }
36
37 public Map<DescriptionBase, Set<DescriptionElementBase>> getDescriptionElements(WorkingSet workingSet, Set<Feature> features, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
38 checkNotInPriorView("WorkingSetDao.getDescriptionElements(WorkingSet workingSet, Set<Feature> features, Integer pageSize,Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
39 Query query = getSession().createQuery("select description from WorkingSet workingSet join workingSet.descriptions description order by description.titleCache asc");
40
41 if(pageSize != null) {
42 query.setMaxResults(pageSize);
43 if(pageNumber != null) {
44 query.setFirstResult(pageNumber * pageSize);
45 } else {
46 query.setFirstResult(0);
47 }
48 }
49 List<DescriptionBase> descriptions = (List<DescriptionBase>)query.list();
50 Map<DescriptionBase, Set<DescriptionElementBase>> result = new HashMap<DescriptionBase, Set<DescriptionElementBase>>();
51 for(DescriptionBase description : descriptions) {
52 Criteria criteria = getSession().createCriteria(DescriptionElementBase.class);
53 criteria.add(Restrictions.eq("inDescription", description));
54 if(features != null && !features.isEmpty()) {
55 criteria.add(Restrictions.in("feature", features));
56 }
57
58 List<DescriptionElementBase> r = (List<DescriptionElementBase>)criteria.list();
59 defaultBeanInitializer.initializeAll(r, propertyPaths);
60 result.put(description, new HashSet<DescriptionElementBase>(r));
61 }
62 return result;
63 }
64
65 @Override
66 public <T extends DescriptionElementBase> Map<UuidAndTitleCache, Map<UUID, Set<T>>> getTaxonFeatureDescriptionElementMap(Class<T> clazz, UUID workingSetUuid, DescriptiveSystemRole role) {
67 checkNotInPriorView("WorkingSetDao.getTaxonFeatureDescriptionElementMap(WorkingSet workingSet, Set<Feature> features, Integer pageSize,Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
68 Map<UuidAndTitleCache, Map<UUID, Set<T>>> result = new HashMap<UuidAndTitleCache, Map<UUID, Set<T>>>();
69 try {
70
71 //for maps see
72 // http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html
73 // Elements of indexed collections (arrays, lists, and maps) can be referred to by index in a where clause only:
74 //
75 // Example: from Order order where order.items[0].id = 1234
76
77
78 //feature
79 String strQueryTreeId = "SELECT ws.descriptiveSystem.id FROM WorkingSet ws join ws.descriptiveSystem tree WHERE ws.uuid = :workingSetUuid ";
80 Query queryTree = getSession().createQuery(strQueryTreeId);
81 queryTree.setParameter("workingSetUuid", workingSetUuid);
82 List<?> trees = queryTree.list();
83
84
85 String ftSelect = "SELECT feature.id FROM FeatureNode node join node.feature as feature " +
86 " WHERE node.featureTree.id in (:trees) ";
87 Query ftQuery = getSession().createQuery(ftSelect);
88 ftQuery.setParameterList("trees", trees);
89 List<?> features = ftQuery.list();
90
91 String strClass = (clazz == null )? "DescriptionElementBase" : clazz.getSimpleName();
92
93 String fetch = "";
94 if (clazz.equals(CategoricalData.class)){
95 fetch = " left join fetch el.states stateList join fetch stateList.state ";
96 }else if (clazz.equals(QuantitativeData.class)){
97 fetch = " left join fetch el.statisticalValues valueList join fetch valueList.type ";
98 }
99
100 String strQuery = " select taxon.uuid, taxon.titleCache, feature.uuid, el "
101 + " from " + strClass + " el "
102 + " join el.feature feature "
103 + " join el.inDescription d "
104 + " join d.taxon taxon "
105 + " join d.workingSets ws "
106 + " join ws.descriptiveSystem ftree "
107 + fetch
108 + " where ws.uuid = :workingSetUuid "
109 + " and el.class = :clazz "
110 + " and feature.id in (:features) "
111 + " order by taxon.uuid asc, feature.uuid asc"
112 ;
113 Query query = getSession().createQuery(strQuery);
114
115 query.setParameter("workingSetUuid", workingSetUuid);
116 query.setParameter("clazz", clazz.getSimpleName());
117 query.setParameterList("features", features);
118
119 //NOTE: Paging does not work with fetch
120
121 // fill result
122 List<Object[]> list = query.list();
123 for (Object[] listEntry : list){
124 UUID taxonUuid = (UUID)listEntry[0];
125 String titleCache = (String)listEntry[1];
126 UuidAndTitleCache taxon = new UuidAndTitleCache(taxonUuid, titleCache);
127 UUID featureUuid = (UUID)listEntry[2];
128 T data = (T)listEntry[3];
129 Map<UUID, Set<T>> taxonMap = result.get(taxon);
130 if (taxonMap == null){
131 taxonMap = new HashMap<UUID, Set<T>>();
132 result.put(taxon, taxonMap);
133 }
134 Set<T> featureSet = taxonMap.get(featureUuid);
135 if (featureSet == null){
136 featureSet = new HashSet<T>();
137 taxonMap.put(featureUuid, featureSet);
138 }else{
139 if (logger.isDebugEnabled()){logger.debug("feature set already exists");}
140 }
141 featureSet.add(data);
142
143 }
144
145 // defaultBeanInitializer.initialize(
146
147 return result;
148 } catch (Exception e) {
149 e.printStackTrace();
150 throw new RuntimeException(e);
151 }
152 }
153 }