1 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.description
;
3 import java
.util
.HashMap
;
4 import java
.util
.HashSet
;
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
;
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
;
29 @Qualifier("workingSetDaoImpl")
30 public class WorkingSetDao
extends AnnotatableDaoImpl
<WorkingSet
> implements IWorkingSetDao
{
31 private static final Logger logger
= Logger
.getLogger(WorkingSetDao
.class);
33 public WorkingSetDao() {
34 super(WorkingSet
.class);
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");
41 if(pageSize
!= null) {
42 query
.setMaxResults(pageSize
);
43 if(pageNumber
!= null) {
44 query
.setFirstResult(pageNumber
* pageSize
);
46 query
.setFirstResult(0);
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
));
58 List
<DescriptionElementBase
> r
= (List
<DescriptionElementBase
>)criteria
.list();
59 defaultBeanInitializer
.initializeAll(r
, propertyPaths
);
60 result
.put(description
, new HashSet
<DescriptionElementBase
>(r
));
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
>>>();
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:
75 // Example: from Order order where order.items[0].id = 1234
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();
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();
91 String strClass
= (clazz
== null )?
"DescriptionElementBase" : clazz
.getSimpleName();
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 ";
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 "
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"
113 Query query
= getSession().createQuery(strQuery
);
115 query
.setParameter("workingSetUuid", workingSetUuid
);
116 query
.setParameter("clazz", clazz
.getSimpleName());
117 query
.setParameterList("features", features
);
119 //NOTE: Paging does not work with fetch
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
);
134 Set
<T
> featureSet
= taxonMap
.get(featureUuid
);
135 if (featureSet
== null){
136 featureSet
= new HashSet
<T
>();
137 taxonMap
.put(featureUuid
, featureSet
);
139 if (logger
.isDebugEnabled()){logger
.debug("feature set already exists");}
141 featureSet
.add(data
);
145 // defaultBeanInitializer.initialize(
148 } catch (Exception e
) {
150 throw new RuntimeException(e
);