1 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.description
;
3 import java
.awt
.datatransfer
.StringSelection
;
4 import java
.util
.Formatter
;
5 import java
.util
.HashMap
;
6 import java
.util
.HashSet
;
10 import java
.util
.UUID
;
12 import org
.apache
.log4j
.Logger
;
13 import org
.hibernate
.Criteria
;
14 import org
.hibernate
.HibernateException
;
15 import org
.hibernate
.Query
;
16 import org
.hibernate
.criterion
.Restrictions
;
17 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
18 import org
.springframework
.dao
.DataAccessException
;
19 import org
.springframework
.stereotype
.Repository
;
21 import eu
.etaxonomy
.cdm
.model
.common
.UuidAndTitleCache
;
22 import eu
.etaxonomy
.cdm
.model
.description
.CategoricalData
;
23 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
24 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
25 import eu
.etaxonomy
.cdm
.model
.description
.DescriptiveSystemRole
;
26 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
27 import eu
.etaxonomy
.cdm
.model
.description
.QuantitativeData
;
28 import eu
.etaxonomy
.cdm
.model
.description
.WorkingSet
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
30 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IWorkingSetDao
;
31 import eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
.AnnotatableDaoImpl
;
34 @Qualifier("workingSetDaoImpl")
35 public class WorkingSetDao
extends AnnotatableDaoImpl
<WorkingSet
> implements IWorkingSetDao
{
36 private static final Logger logger
= Logger
.getLogger(WorkingSetDao
.class);
38 public WorkingSetDao() {
39 super(WorkingSet
.class);
42 public Map
<DescriptionBase
, Set
<DescriptionElementBase
>> getDescriptionElements(WorkingSet workingSet
, Set
<Feature
> features
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
43 checkNotInPriorView("WorkingSetDao.getDescriptionElements(WorkingSet workingSet, Set<Feature> features, Integer pageSize,Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
44 Query query
= getSession().createQuery("select description from WorkingSet workingSet join workingSet.descriptions description order by description.titleCache asc");
46 if(pageSize
!= null) {
47 query
.setMaxResults(pageSize
);
48 if(pageNumber
!= null) {
49 query
.setFirstResult(pageNumber
* pageSize
);
51 query
.setFirstResult(0);
54 List
<DescriptionBase
> descriptions
= (List
<DescriptionBase
>)query
.list();
55 Map
<DescriptionBase
, Set
<DescriptionElementBase
>> result
= new HashMap
<DescriptionBase
, Set
<DescriptionElementBase
>>();
56 for(DescriptionBase description
: descriptions
) {
57 Criteria criteria
= getSession().createCriteria(DescriptionElementBase
.class);
58 criteria
.add(Restrictions
.eq("inDescription", description
));
59 if(features
!= null && !features
.isEmpty()) {
60 criteria
.add(Restrictions
.in("feature", features
));
63 List
<DescriptionElementBase
> r
= (List
<DescriptionElementBase
>)criteria
.list();
64 defaultBeanInitializer
.initializeAll(r
, propertyPaths
);
65 result
.put(description
, new HashSet
<DescriptionElementBase
>(r
));
71 public <T
extends DescriptionElementBase
> Map
<UuidAndTitleCache
, Map
<UUID
, Set
<T
>>> getTaxonFeatureDescriptionElementMap(Class
<T
> clazz
, UUID workingSetUuid
, DescriptiveSystemRole role
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
73 checkNotInPriorView("WorkingSetDao.getTaxonFeatureDescriptionElementMap(WorkingSet workingSet, Set<Feature> features, Integer pageSize,Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
74 Map
<UuidAndTitleCache
, Map
<UUID
, Set
<T
>>> result
= new HashMap
<UuidAndTitleCache
, Map
<UUID
, Set
<T
>>>();
78 // http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html
79 // Elements of indexed collections (arrays, lists, and maps) can be referred to by index in a where clause only:
81 // Example: from Order order where order.items[0].id = 1234
85 String strQueryTreeId
= "SELECT ws.descriptiveSystem.id FROM WorkingSet ws join ws.descriptiveSystem tree WHERE ws.uuid = :workingSetUuid ";
86 Query queryTree
= getSession().createQuery(strQueryTreeId
);
87 queryTree
.setParameter("workingSetUuid", workingSetUuid
);
88 List
<?
> trees
= queryTree
.list();
91 String ftSelect
= "SELECT feature.id FROM FeatureNode node join node.feature as feature " +
92 " WHERE node.featureTree.id in (:trees) ";
93 Query ftQuery
= getSession().createQuery(ftSelect
);
94 ftQuery
.setParameterList("trees", trees
);
95 List
<?
> features
= ftQuery
.list();
97 String strClass
= (clazz
== null )?
"DescriptionElementBase" : clazz
.getSimpleName();
100 if (clazz
.equals(CategoricalData
.class)){
101 fetch
= " left join fetch el.states stateList join fetch stateList.state ";
102 }else if (clazz
.equals(QuantitativeData
.class)){
103 fetch
= " left join fetch el.statisticalValues valueList join fetch valueList.type ";
106 String strQuery
= " select taxon.uuid, taxon.titleCache, feature.uuid, el "
107 + " from " + strClass
+ " el "
108 + " join el.feature feature "
109 + " join el.inDescription d "
110 + " join d.taxon taxon "
111 + " join d.workingSets ws "
112 + " join ws.descriptiveSystem ftree "
114 + " where ws.uuid = :workingSetUuid "
115 + " and el.class = :clazz "
116 + " and feature.id in (:features) "
117 + " order by taxon.uuid asc, feature.uuid asc"
119 // System.out.println(strQuery);
120 Query query
= getSession().createQuery(strQuery
);
124 query
.setParameter("workingSetUuid", workingSetUuid
);
125 query
.setParameter("clazz", clazz
.getSimpleName());
126 query
.setParameterList("features", features
);
128 List
<Object
[]> list
= query
.list();
129 logger
.warn("Retrieved data for " + clazz
== null?
"Structured Data":clazz
.getSimpleName());
130 for (Object
[] listEntry
: list
){
131 UUID taxonUuid
= (UUID
)listEntry
[0];
132 String titleCache
= (String
)listEntry
[1];
133 UuidAndTitleCache taxon
= new UuidAndTitleCache(taxonUuid
, titleCache
);
134 UUID featureUuid
= (UUID
)listEntry
[2];
135 T data
= (T
)listEntry
[3];
136 Map
<UUID
, Set
<T
>> taxonMap
= result
.get(taxon
);
137 if (taxonMap
== null){
138 taxonMap
= new HashMap
<UUID
, Set
<T
>>();
139 result
.put(taxon
, taxonMap
);
141 Set
<T
> featureSet
= taxonMap
.get(featureUuid
);
142 if (featureSet
== null){
143 featureSet
= new HashSet
<T
>();
144 taxonMap
.put(featureUuid
, featureSet
);
146 if (logger
.isDebugEnabled()){logger
.debug("feature set already exists");}
148 featureSet
.add(data
);
151 logger
.warn("Filled resultset");
153 // defaultBeanInitializer.initialize(
156 } catch (Exception e
) {
158 throw new RuntimeException(e
);