1
|
/**
|
2
|
* Copyright (C) 2007 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
|
package eu.etaxonomy.cdm.remote.service;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Enumeration;
|
13
|
import java.util.Hashtable;
|
14
|
import java.util.List;
|
15
|
import java.util.Locale;
|
16
|
import java.util.Set;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.hibernate.criterion.Criterion;
|
21
|
import org.hibernate.criterion.Restrictions;
|
22
|
import org.springframework.beans.factory.annotation.Autowired;
|
23
|
import org.springframework.stereotype.Service;
|
24
|
import org.springframework.transaction.annotation.Transactional;
|
25
|
|
26
|
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
|
27
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
28
|
import eu.etaxonomy.cdm.model.description.Feature;
|
29
|
import eu.etaxonomy.cdm.model.description.FeatureTree;
|
30
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
31
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
32
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
33
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
34
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
35
|
import eu.etaxonomy.cdm.persistence.dao.common.IAnnotatableDao;
|
36
|
import eu.etaxonomy.cdm.persistence.dao.common.ITitledDao;
|
37
|
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao;
|
38
|
import eu.etaxonomy.cdm.persistence.dao.description.IFeatureDao;
|
39
|
import eu.etaxonomy.cdm.persistence.dao.description.IFeatureTreeDao;
|
40
|
import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
|
41
|
import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
|
42
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
|
43
|
import eu.etaxonomy.cdm.remote.dto.AnnotationTO;
|
44
|
import eu.etaxonomy.cdm.remote.dto.FeatureTO;
|
45
|
import eu.etaxonomy.cdm.remote.dto.FeatureTreeTO;
|
46
|
import eu.etaxonomy.cdm.remote.dto.NameSTO;
|
47
|
import eu.etaxonomy.cdm.remote.dto.NameTO;
|
48
|
import eu.etaxonomy.cdm.remote.dto.ReferenceSTO;
|
49
|
import eu.etaxonomy.cdm.remote.dto.ReferenceTO;
|
50
|
import eu.etaxonomy.cdm.remote.dto.ReferencedEntityBaseSTO;
|
51
|
import eu.etaxonomy.cdm.remote.dto.ResultSetPageSTO;
|
52
|
import eu.etaxonomy.cdm.remote.dto.TaxonSTO;
|
53
|
import eu.etaxonomy.cdm.remote.dto.TaxonTO;
|
54
|
import eu.etaxonomy.cdm.remote.dto.TreeNode;
|
55
|
import eu.etaxonomy.cdm.remote.dto.assembler.AnnotationAssembler;
|
56
|
import eu.etaxonomy.cdm.remote.dto.assembler.DescriptionAssembler;
|
57
|
import eu.etaxonomy.cdm.remote.dto.assembler.NameAssembler;
|
58
|
import eu.etaxonomy.cdm.remote.dto.assembler.ReferenceAssembler;
|
59
|
import eu.etaxonomy.cdm.remote.dto.assembler.TaxonAssembler;
|
60
|
|
61
|
@Service
|
62
|
@Transactional(readOnly = true)
|
63
|
public class CdmServiceImpl implements ICdmService {
|
64
|
static Logger logger = Logger.getLogger(CdmServiceImpl.class);
|
65
|
|
66
|
@Autowired
|
67
|
private ReferenceAssembler refAssembler;
|
68
|
@Autowired
|
69
|
private NameAssembler nameAssembler;
|
70
|
@Autowired
|
71
|
private TaxonAssembler taxonAssembler;
|
72
|
@Autowired
|
73
|
private DescriptionAssembler descriptionAssembler;
|
74
|
@Autowired
|
75
|
private AnnotationAssembler annotationAssembler;
|
76
|
@Autowired
|
77
|
private ITaxonDao taxonDAO;
|
78
|
@Autowired
|
79
|
private ITaxonNameDao nameDAO;
|
80
|
@Autowired
|
81
|
private IReferenceDao refDAO;
|
82
|
@Autowired
|
83
|
private IDescriptionDao descriptionDAO;
|
84
|
@Autowired
|
85
|
private IFeatureTreeDao featureTreeDAO;
|
86
|
@Autowired
|
87
|
private IFeatureDao featureDAO;
|
88
|
@Autowired
|
89
|
private IAnnotatableDao annotatableDao;
|
90
|
|
91
|
|
92
|
private final int MAXRESULTS = 500;
|
93
|
|
94
|
|
95
|
|
96
|
// private CdmEntityDaoBase entityDAO = new CdmEntityDaoBase<CdmBase>();
|
97
|
|
98
|
|
99
|
/**
|
100
|
* find matching taxonbase instance or throw CdmObjectNonExisting exception.
|
101
|
* never returns null!
|
102
|
* @param uuid
|
103
|
* @return
|
104
|
* @throws CdmObjectNonExisting
|
105
|
*/
|
106
|
private TaxonBase getCdmTaxonBase(UUID uuid) throws CdmObjectNonExisting{
|
107
|
TaxonBase tb = taxonDAO.findByUuid(uuid);
|
108
|
if (tb==null){
|
109
|
throw new CdmObjectNonExisting(uuid.toString(), TaxonBase.class);
|
110
|
}
|
111
|
return tb;
|
112
|
}
|
113
|
private Taxon getCdmTaxon(UUID uuid) throws CdmObjectNonExisting{
|
114
|
Taxon t = null;
|
115
|
try {
|
116
|
t = (Taxon) getCdmTaxonBase(uuid);
|
117
|
} catch (ClassCastException e) {
|
118
|
throw new CdmObjectNonExisting(uuid.toString(), Taxon.class);
|
119
|
}
|
120
|
return t;
|
121
|
}
|
122
|
private TaxonNameBase getCdmTaxonNameBase(UUID uuid) throws CdmObjectNonExisting{
|
123
|
TaxonNameBase tnb = nameDAO.findByUuid(uuid);
|
124
|
if (tnb==null){
|
125
|
throw new CdmObjectNonExisting(uuid.toString(), TaxonNameBase.class);
|
126
|
}
|
127
|
return tnb;
|
128
|
}
|
129
|
private ReferenceBase getCdmReferenceBase(UUID uuid) throws CdmObjectNonExisting{
|
130
|
ReferenceBase ref = refDAO.findByUuid(uuid);
|
131
|
if (ref==null){
|
132
|
throw new CdmObjectNonExisting(uuid.toString(), ReferenceBase.class);
|
133
|
}
|
134
|
return ref;
|
135
|
}
|
136
|
|
137
|
public NameTO getName(UUID uuid, Enumeration<Locale> locales) throws CdmObjectNonExisting{
|
138
|
TaxonNameBase tnb = getCdmTaxonNameBase(uuid);
|
139
|
NameTO n = nameAssembler.getTO(tnb, locales);
|
140
|
return n;
|
141
|
}
|
142
|
|
143
|
public NameSTO getSimpleName(UUID uuid, Enumeration<Locale> locales) throws CdmObjectNonExisting{
|
144
|
TaxonNameBase tnb = getCdmTaxonNameBase(uuid);
|
145
|
NameSTO n = nameAssembler.getSTO(tnb, locales);
|
146
|
return n;
|
147
|
}
|
148
|
|
149
|
public TaxonTO getTaxon(UUID taxonUuid, UUID featureTreeUuid, Enumeration<Locale> locales) throws CdmObjectNonExisting{
|
150
|
TaxonBase tb = taxonDAO.findByUuid(taxonUuid);
|
151
|
if (tb==null){
|
152
|
throw new CdmObjectNonExisting(taxonUuid.toString(), TaxonBase.class);
|
153
|
}
|
154
|
FeatureTree featureTree = null;
|
155
|
if (featureTreeUuid != null){
|
156
|
featureTree = featureTreeDAO.findByUuid(featureTreeUuid);
|
157
|
if (featureTree == null){
|
158
|
throw new CdmObjectNonExisting(featureTreeUuid.toString(), FeatureTree.class);
|
159
|
}
|
160
|
}else{
|
161
|
logger.info("No featureTree at this point. Building default tree with all available features.");
|
162
|
featureTree = FeatureTree.NewInstance(featureDAO.list());
|
163
|
}
|
164
|
TaxonTO t = taxonAssembler.getTO(tb, featureTree, locales);
|
165
|
return t;
|
166
|
}
|
167
|
|
168
|
public TaxonSTO getSimpleTaxon(UUID uuid, Enumeration<Locale> locales) throws CdmObjectNonExisting{
|
169
|
TaxonBase tb = getCdmTaxonBase(uuid);
|
170
|
TaxonSTO t = taxonAssembler.getSTO(tb, locales);
|
171
|
return t;
|
172
|
}
|
173
|
|
174
|
public Class whatis(UUID uuid) throws CdmObjectNonExisting{
|
175
|
//CdmBase cb = entityDAO.findByUuid(uuid);
|
176
|
return this.getClass();
|
177
|
}
|
178
|
|
179
|
public ResultSetPageSTO<TaxonSTO> findTaxa(String q, Set<UUID> sec, Set<UUID> higherTaxa, ITitledDao.MATCH_MODE matchMode, boolean onlyAccepted, int page, int pagesize, Enumeration<Locale> locales) {
|
180
|
ResultSetPageSTO<TaxonSTO> resultSetPage = new ResultSetPageSTO<TaxonSTO>();
|
181
|
|
182
|
resultSetPage.setPageNumber(page);
|
183
|
resultSetPage.setPageSize(pagesize);
|
184
|
|
185
|
// TODO: add other criteria. Has to be done in DAO...
|
186
|
|
187
|
List<Criterion> criteria = new ArrayList<Criterion>();
|
188
|
if(sec.size()>0){
|
189
|
List<ReferenceBase> secundum = new ArrayList<ReferenceBase>();
|
190
|
for (UUID uuid : sec){
|
191
|
ReferenceBase referenceBase = refDAO.findByUuid(uuid);
|
192
|
secundum.add(referenceBase);
|
193
|
}
|
194
|
criteria.add(Restrictions.in("sec", secundum));
|
195
|
}
|
196
|
|
197
|
resultSetPage.setTotalResultsCount((int)taxonDAO.countMatchesByName(q, matchMode, onlyAccepted, criteria));
|
198
|
// if(MAXRESULTS > 0 && rs.getTotalResultsCount() > MAXRESULTS){
|
199
|
// rs.setTotalResultsCount(-1);
|
200
|
// return rs;
|
201
|
// }
|
202
|
|
203
|
List<TaxonBase> results = taxonDAO.findByTitle(q, matchMode, page, pagesize, criteria);
|
204
|
resultSetPage.setResultsOnPage(results.size());
|
205
|
for (TaxonBase taxonBase : results){
|
206
|
resultSetPage.getResults().add(taxonAssembler.getSTO(taxonBase, locales));
|
207
|
}
|
208
|
return resultSetPage;
|
209
|
}
|
210
|
|
211
|
/* (non-Javadoc)
|
212
|
* @see eu.etaxonomy.cdm.remote.service.ICdmService#getAcceptedTaxon(java.util.Set, java.util.Enumeration)
|
213
|
*/
|
214
|
public Hashtable<String, List<TaxonSTO>> getAcceptedTaxa(Set<UUID> uuids, Enumeration<Locale> locales) throws CdmObjectNonExisting {
|
215
|
|
216
|
Hashtable<String, List<TaxonSTO>> stoTable = new Hashtable<String, List<TaxonSTO>>();
|
217
|
for (UUID uuid : uuids) {
|
218
|
List<TaxonSTO> stoList = new ArrayList<TaxonSTO>();
|
219
|
TaxonBase tb = getCdmTaxonBase(uuid);
|
220
|
|
221
|
if (tb.getClass().equals(Taxon.class)){
|
222
|
TaxonSTO t = taxonAssembler.getSTO(tb, locales);
|
223
|
stoList.add(t);
|
224
|
}else{
|
225
|
Synonym s = (Synonym)tb;
|
226
|
Set<Taxon> taxa = s.getAcceptedTaxa();
|
227
|
for (Taxon tx: taxa){
|
228
|
TaxonSTO t = taxonAssembler.getSTO(tx, locales);
|
229
|
stoList.add(t);
|
230
|
}
|
231
|
}
|
232
|
stoTable.put(uuid.toString(), stoList);
|
233
|
}
|
234
|
return stoTable;
|
235
|
}
|
236
|
|
237
|
public List<TreeNode> getChildrenTaxa(UUID uuid) throws CdmObjectNonExisting {
|
238
|
Taxon tx = getCdmTaxon(uuid);
|
239
|
return taxonAssembler.getTreeNodeListSortedByName(tx.getTaxonomicChildren());
|
240
|
}
|
241
|
|
242
|
public List<TreeNode> getParentTaxa(UUID uuid) throws CdmObjectNonExisting {
|
243
|
ArrayList<TreeNode> result = new ArrayList<TreeNode>();
|
244
|
Taxon tx = getCdmTaxon(uuid);
|
245
|
result.add(taxonAssembler.getTreeNode(tx));
|
246
|
while(tx.getTaxonomicParent() != null){
|
247
|
Taxon parent = tx.getTaxonomicParent();
|
248
|
result.add(taxonAssembler.getTreeNode(parent));
|
249
|
tx=parent;
|
250
|
}
|
251
|
return result;
|
252
|
}
|
253
|
|
254
|
public ReferenceTO getReference(UUID uuid, Enumeration<Locale> locales) throws CdmObjectNonExisting{
|
255
|
ReferenceBase ref = getCdmReferenceBase(uuid);
|
256
|
ReferenceTO r = refAssembler.getTO(ref, locales);
|
257
|
return r;
|
258
|
}
|
259
|
public ReferenceSTO getSimpleReference(UUID uuid, Enumeration<Locale> locales) throws CdmObjectNonExisting{
|
260
|
ReferenceBase ref = getCdmReferenceBase(uuid);
|
261
|
ReferenceSTO r = refAssembler.getSTO(ref, locales);
|
262
|
return r;
|
263
|
}
|
264
|
public List<TreeNode> getRootTaxa(UUID uuid) throws CdmObjectNonExisting {
|
265
|
ReferenceBase sec = null;
|
266
|
if(uuid != null){
|
267
|
sec = getCdmReferenceBase(uuid);
|
268
|
}
|
269
|
return taxonAssembler.getTreeNodeListSortedByName(taxonDAO.getRootTaxa(sec));
|
270
|
}
|
271
|
|
272
|
public Set<ReferencedEntityBaseSTO> getTypes(UUID uuid) {
|
273
|
// TODO Auto-generated method stub
|
274
|
return null;
|
275
|
}
|
276
|
|
277
|
@Transactional(readOnly = false)
|
278
|
public void saveTaxon(Taxon t){
|
279
|
taxonDAO.save(t);
|
280
|
}
|
281
|
|
282
|
public ResultSetPageSTO<TaxonSTO> getAternativeTaxa(UUID uuid, Enumeration<Locale> locales)
|
283
|
throws CdmObjectNonExisting {
|
284
|
// TODO Auto-generated method stub
|
285
|
return null;
|
286
|
}
|
287
|
public List<NameSTO> getSimpleNames(Set<UUID> uuids, Enumeration<Locale> locales){
|
288
|
List<NameSTO> nameList = new ArrayList<NameSTO>();
|
289
|
for (UUID u: uuids){
|
290
|
try {
|
291
|
nameList.add(getSimpleName(u, locales));
|
292
|
} catch (CdmObjectNonExisting e) {
|
293
|
logger.warn("Name UUID "+u+" does not exist!");
|
294
|
}
|
295
|
}
|
296
|
return nameList;
|
297
|
}
|
298
|
public List<ReferenceSTO> getSimpleReferences(Set<UUID> uuids, Enumeration<Locale> locales) {
|
299
|
List<ReferenceSTO> refList = new ArrayList<ReferenceSTO>();
|
300
|
for (UUID u: uuids){
|
301
|
try {
|
302
|
refList.add(getSimpleReference(u,locales));
|
303
|
} catch (CdmObjectNonExisting e) {
|
304
|
logger.warn("Reference UUID "+u+" does not exist!");
|
305
|
}
|
306
|
}
|
307
|
return refList;
|
308
|
}
|
309
|
public List<TaxonSTO> getSimpleTaxa(Set<UUID> uuids, Enumeration<Locale> locales){
|
310
|
List<TaxonSTO> taxList = new ArrayList<TaxonSTO>();
|
311
|
for (UUID u: uuids){
|
312
|
try {
|
313
|
taxList.add(getSimpleTaxon(u,locales));
|
314
|
} catch (CdmObjectNonExisting e) {
|
315
|
logger.warn("Taxon UUID "+u+" does not exist!");
|
316
|
}
|
317
|
}
|
318
|
return taxList;
|
319
|
}
|
320
|
public List<FeatureTO> getFeatures(Enumeration<Locale> locales) throws CdmObjectNonExisting {
|
321
|
List<FeatureTO> featureList = new ArrayList<FeatureTO>();
|
322
|
|
323
|
for (Feature feature : featureDAO.list()){
|
324
|
featureList.add(descriptionAssembler.getTO(feature, locales));
|
325
|
}
|
326
|
|
327
|
return featureList;
|
328
|
}
|
329
|
public List<FeatureTreeTO> getFeatureTrees(Enumeration<Locale> locales)
|
330
|
throws CdmObjectNonExisting {
|
331
|
List<FeatureTreeTO> featureTreeList = new ArrayList<FeatureTreeTO>();
|
332
|
|
333
|
for (FeatureTree featureTree : featureTreeDAO.list()){
|
334
|
featureTreeList.add(descriptionAssembler.getTO(featureTree, locales));
|
335
|
}
|
336
|
|
337
|
return featureTreeList;
|
338
|
}
|
339
|
/* (non-Javadoc)
|
340
|
* @see eu.etaxonomy.cdm.remote.service.ICdmService#getAnnotations(java.util.UUID)
|
341
|
*/
|
342
|
public AnnotationTO getAnnotation(UUID uuid, Enumeration<Locale> locales)
|
343
|
throws CdmObjectNonExisting {
|
344
|
List<AnnotationTO> annotationList = new ArrayList<AnnotationTO>();
|
345
|
|
346
|
// TODO the cast should be removed, but i don't knwo how at the moment
|
347
|
AnnotatableEntity annotatableEntity = (AnnotatableEntity) annotatableDao.findByUuid(uuid);
|
348
|
|
349
|
|
350
|
return annotationAssembler.getTO(annotatableEntity, locales);
|
351
|
}
|
352
|
/* (non-Javadoc)
|
353
|
* @see eu.etaxonomy.cdm.remote.service.ICdmService#saveAnnotation(java.util.UUID, eu.etaxonomy.cdm.model.common.Annotation)
|
354
|
*/
|
355
|
@Transactional(readOnly=false)
|
356
|
public UUID saveAnnotation(UUID uuid, Annotation annotation)
|
357
|
throws CdmObjectNonExisting {
|
358
|
|
359
|
AnnotatableEntity annotatableEntity = (AnnotatableEntity) annotatableDao.findByUuid(uuid);
|
360
|
|
361
|
annotatableEntity.addAnnotation(annotation);
|
362
|
|
363
|
UUID updatedUuid = annotatableDao.saveOrUpdate(annotatableEntity);
|
364
|
annotatableDao.flush();
|
365
|
|
366
|
return updatedUuid;
|
367
|
}
|
368
|
}
|