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.api.service.IReferenceService;
|
27
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
28
|
import eu.etaxonomy.cdm.model.description.Feature;
|
29
|
import eu.etaxonomy.cdm.model.description.FeatureTree;
|
30
|
import eu.etaxonomy.cdm.model.name.Rank;
|
31
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
32
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
33
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
34
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
35
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
36
|
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao;
|
37
|
import eu.etaxonomy.cdm.persistence.dao.description.IFeatureDao;
|
38
|
import eu.etaxonomy.cdm.persistence.dao.description.IFeatureTreeDao;
|
39
|
import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
|
40
|
import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
|
41
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
|
42
|
import eu.etaxonomy.cdm.persistence.fetch.CdmFetch;
|
43
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
44
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
45
|
import eu.etaxonomy.cdm.remote.dto.FeatureTO;
|
46
|
import eu.etaxonomy.cdm.remote.dto.FeatureTreeTO;
|
47
|
import eu.etaxonomy.cdm.remote.dto.NameSTO;
|
48
|
import eu.etaxonomy.cdm.remote.dto.NameTO;
|
49
|
import eu.etaxonomy.cdm.remote.dto.ReferenceSTO;
|
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
|
|
89
|
|
90
|
|
91
|
//FIXME commented out below, since refactoring is urgently needed see ticket#593 http://dev.e-taxonomy.eu/trac/ticket/593
|
92
|
// @Autowired
|
93
|
// private AnnotatableDaoImpl<AnnotatableEntity> annotatableDao;
|
94
|
|
95
|
//--------------------------
|
96
|
@Autowired
|
97
|
private IReferenceService referenceService;
|
98
|
|
99
|
|
100
|
private final int MAXRESULTS = 500;
|
101
|
|
102
|
private static List<OrderHint> defaultReferenceOrder = new ArrayList();
|
103
|
|
104
|
|
105
|
|
106
|
// private CdmEntityDaoBase entityDAO = new CdmEntityDaoBase<CdmBase>();
|
107
|
|
108
|
|
109
|
public CdmServiceImpl() {
|
110
|
|
111
|
super();
|
112
|
defaultReferenceOrder.add(new OrderHint("authorTeam.persistentTitleCache", OrderHint.SortOrder.ASCENDING));
|
113
|
defaultReferenceOrder.add(new OrderHint("year", OrderHint.SortOrder.ASCENDING));
|
114
|
defaultReferenceOrder.add(new OrderHint("persistentTitleCache", OrderHint.SortOrder.ASCENDING));
|
115
|
}
|
116
|
|
117
|
|
118
|
/**
|
119
|
* find matching taxonbase instance or throw CdmObjectNonExisting exception.
|
120
|
* never returns null!
|
121
|
* @param uuid
|
122
|
* @return
|
123
|
* @throws CdmObjectNonExisting
|
124
|
*/
|
125
|
private TaxonBase getCdmTaxonBase(UUID uuid) throws CdmObjectNonExisting{
|
126
|
TaxonBase tb = taxonDAO.findByUuid(uuid);
|
127
|
if (tb==null){
|
128
|
throw new CdmObjectNonExisting(uuid.toString(), TaxonBase.class);
|
129
|
}
|
130
|
return tb;
|
131
|
}
|
132
|
private Taxon getCdmTaxon(UUID uuid) throws CdmObjectNonExisting{
|
133
|
Taxon t = null;
|
134
|
try {
|
135
|
t = (Taxon) getCdmTaxonBase(uuid);
|
136
|
} catch (ClassCastException e) {
|
137
|
throw new CdmObjectNonExisting(uuid.toString(), Taxon.class);
|
138
|
}
|
139
|
return t;
|
140
|
}
|
141
|
private TaxonNameBase getCdmTaxonNameBase(UUID uuid) throws CdmObjectNonExisting{
|
142
|
TaxonNameBase tnb = nameDAO.findByUuid(uuid);
|
143
|
if (tnb==null){
|
144
|
throw new CdmObjectNonExisting(uuid.toString(), TaxonNameBase.class);
|
145
|
}
|
146
|
return tnb;
|
147
|
}
|
148
|
private ReferenceBase getCdmReferenceBase(UUID uuid) throws CdmObjectNonExisting{
|
149
|
ReferenceBase ref = refDAO.findByUuid(uuid);
|
150
|
if (ref==null){
|
151
|
throw new CdmObjectNonExisting(uuid.toString(), ReferenceBase.class);
|
152
|
}
|
153
|
return ref;
|
154
|
}
|
155
|
|
156
|
public NameTO getName(UUID uuid, Enumeration<Locale> locales) throws CdmObjectNonExisting{
|
157
|
TaxonNameBase tnb = getCdmTaxonNameBase(uuid);
|
158
|
NameTO n = nameAssembler.getTO(tnb, locales);
|
159
|
return n;
|
160
|
}
|
161
|
|
162
|
public NameSTO getSimpleName(UUID uuid, Enumeration<Locale> locales) throws CdmObjectNonExisting{
|
163
|
TaxonNameBase tnb = getCdmTaxonNameBase(uuid);
|
164
|
NameSTO n = nameAssembler.getSTO(tnb, locales);
|
165
|
return n;
|
166
|
}
|
167
|
|
168
|
public TaxonTO getTaxon(UUID taxonUuid, UUID featureTreeUuid, Enumeration<Locale> locales) throws CdmObjectNonExisting{
|
169
|
TaxonBase tb = taxonDAO.findByUuid(taxonUuid);
|
170
|
if (tb==null){
|
171
|
throw new CdmObjectNonExisting(taxonUuid.toString(), TaxonBase.class);
|
172
|
}
|
173
|
FeatureTree featureTree = null;
|
174
|
if (featureTreeUuid != null){
|
175
|
featureTree = featureTreeDAO.findByUuid(featureTreeUuid);
|
176
|
if (featureTree == null){
|
177
|
throw new CdmObjectNonExisting(featureTreeUuid.toString(), FeatureTree.class);
|
178
|
}
|
179
|
}else{
|
180
|
logger.info("No featureTree at this point. Building default tree with all available features.");
|
181
|
featureTree = FeatureTree.NewInstance(featureDAO.list());
|
182
|
}
|
183
|
TaxonTO t = taxonAssembler.getTO(tb, featureTree, locales);
|
184
|
return t;
|
185
|
}
|
186
|
|
187
|
public TaxonSTO getSimpleTaxon(UUID uuid, Enumeration<Locale> locales) throws CdmObjectNonExisting{
|
188
|
TaxonBase tb = getCdmTaxonBase(uuid);
|
189
|
TaxonSTO t = taxonAssembler.getSTO(tb, locales);
|
190
|
return t;
|
191
|
}
|
192
|
|
193
|
public Class whatis(UUID uuid) throws CdmObjectNonExisting{
|
194
|
//CdmBase cb = entityDAO.findByUuid(uuid);
|
195
|
return this.getClass();
|
196
|
}
|
197
|
|
198
|
public ResultSetPageSTO<TaxonSTO> findTaxa(String q, Set<UUID> sec, Set<UUID> higherTaxa, MatchMode matchMode, boolean onlyAccepted, int page, int pagesize, Enumeration<Locale> locales) {
|
199
|
ResultSetPageSTO<TaxonSTO> resultSetPage = new ResultSetPageSTO<TaxonSTO>();
|
200
|
|
201
|
resultSetPage.setPageNumber(page);
|
202
|
resultSetPage.setPageSize(pagesize);
|
203
|
|
204
|
// TODO: add other criteria. Has to be done in DAO...
|
205
|
|
206
|
List<Criterion> criteria = new ArrayList<Criterion>();
|
207
|
if(sec.size()>0){
|
208
|
List<ReferenceBase> secundum = new ArrayList<ReferenceBase>();
|
209
|
for (UUID uuid : sec){
|
210
|
ReferenceBase referenceBase = refDAO.findByUuid(uuid);
|
211
|
secundum.add(referenceBase);
|
212
|
}
|
213
|
criteria.add(Restrictions.in("sec", secundum));
|
214
|
}
|
215
|
|
216
|
resultSetPage.setTotalResultsCount((int)taxonDAO.countMatchesByName(q, matchMode, onlyAccepted, criteria));
|
217
|
// if(MAXRESULTS > 0 && rs.getTotalResultsCount() > MAXRESULTS){
|
218
|
// rs.setTotalResultsCount(-1);
|
219
|
// return rs;
|
220
|
// }
|
221
|
|
222
|
List<TaxonBase> results = taxonDAO.findByTitle(q, matchMode, page, pagesize, criteria);
|
223
|
//descriptionDAO
|
224
|
resultSetPage.setResultsOnPage(results.size());
|
225
|
for (TaxonBase taxonBase : results){
|
226
|
resultSetPage.getResults().add(taxonAssembler.getSTO(taxonBase, locales));
|
227
|
}
|
228
|
return resultSetPage;
|
229
|
}
|
230
|
|
231
|
/* (non-Javadoc)
|
232
|
* @see eu.etaxonomy.cdm.remote.service.ICdmService#getAcceptedTaxon(java.util.Set, java.util.Enumeration)
|
233
|
*/
|
234
|
public Hashtable<String, List<TaxonSTO>> getAcceptedTaxa(Set<UUID> uuids, Enumeration<Locale> locales) throws CdmObjectNonExisting {
|
235
|
|
236
|
Hashtable<String, List<TaxonSTO>> stoTable = new Hashtable<String, List<TaxonSTO>>();
|
237
|
for (UUID uuid : uuids) {
|
238
|
List<TaxonSTO> stoList = new ArrayList<TaxonSTO>();
|
239
|
TaxonBase tb = getCdmTaxonBase(uuid);
|
240
|
|
241
|
if (tb.getClass().equals(Taxon.class)){
|
242
|
TaxonSTO t = taxonAssembler.getSTO(tb, locales);
|
243
|
stoList.add(t);
|
244
|
}else{
|
245
|
Synonym s = (Synonym)tb;
|
246
|
Set<Taxon> taxa = s.getAcceptedTaxa();
|
247
|
for (Taxon tx: taxa){
|
248
|
TaxonSTO t = taxonAssembler.getSTO(tx, locales);
|
249
|
stoList.add(t);
|
250
|
}
|
251
|
}
|
252
|
stoTable.put(uuid.toString(), stoList);
|
253
|
}
|
254
|
return stoTable;
|
255
|
}
|
256
|
|
257
|
public List<TreeNode> getChildTaxa(UUID uuid) throws CdmObjectNonExisting {
|
258
|
Taxon tx = getCdmTaxon(uuid);
|
259
|
return taxonAssembler.getTreeNodeListSortedByName(tx.getTaxonomicChildren());
|
260
|
}
|
261
|
|
262
|
public List<TreeNode> getParentTaxa(UUID uuid) throws CdmObjectNonExisting {
|
263
|
ArrayList<TreeNode> result = new ArrayList<TreeNode>();
|
264
|
Taxon tx = getCdmTaxon(uuid);
|
265
|
result.add(taxonAssembler.getTreeNode(tx));
|
266
|
while(tx.getTaxonomicParent() != null){
|
267
|
Taxon parent = tx.getTaxonomicParent();
|
268
|
result.add(taxonAssembler.getTreeNode(parent));
|
269
|
tx=parent;
|
270
|
}
|
271
|
return result;
|
272
|
}
|
273
|
|
274
|
public ReferenceBase getReference(UUID uuid, Enumeration<Locale> locales) throws CdmObjectNonExisting{
|
275
|
/*ReferenceBase ref = getCdmReferenceBase(uuid);
|
276
|
ReferenceTO r = refAssembler.getTO(ref, locales); */
|
277
|
ReferenceBase r = referenceService.getReferenceByUuid(uuid);
|
278
|
/* Initialise required collections .. */
|
279
|
return r;
|
280
|
}
|
281
|
|
282
|
public Pager<ReferenceBase> listReferences(Integer pageSize, Integer pageNumber) throws CdmObjectNonExisting {
|
283
|
|
284
|
return referenceService.getAllReferences(pageSize, pageNumber, defaultReferenceOrder);
|
285
|
}
|
286
|
|
287
|
|
288
|
public ReferenceSTO getSimpleReference(UUID uuid, Enumeration<Locale> locales) throws CdmObjectNonExisting{
|
289
|
ReferenceBase ref = getCdmReferenceBase(uuid);
|
290
|
ReferenceSTO r = refAssembler.getSTO(ref, locales);
|
291
|
return r;
|
292
|
}
|
293
|
public List<TreeNode> getRootTaxa(UUID uuid) throws CdmObjectNonExisting {
|
294
|
ReferenceBase sec = null;
|
295
|
if(uuid != null){
|
296
|
sec = getCdmReferenceBase(uuid);
|
297
|
}
|
298
|
List<Taxon> rt = taxonDAO.getRootTaxa(Rank.GENUS(), sec, null, true, false);
|
299
|
return taxonAssembler.getTreeNodeListSortedByName(rt);
|
300
|
}
|
301
|
|
302
|
public Set<ReferencedEntityBaseSTO> getTypes(UUID uuid) {
|
303
|
// TODO Auto-generated method stub
|
304
|
return null;
|
305
|
}
|
306
|
|
307
|
@Transactional(readOnly = false)
|
308
|
public void saveTaxon(Taxon t){
|
309
|
taxonDAO.save(t);
|
310
|
}
|
311
|
|
312
|
public ResultSetPageSTO<TaxonSTO> getAternativeTaxa(UUID uuid, Enumeration<Locale> locales)
|
313
|
throws CdmObjectNonExisting {
|
314
|
// TODO Auto-generated method stub
|
315
|
return null;
|
316
|
}
|
317
|
public List<NameSTO> getSimpleNames(Set<UUID> uuids, Enumeration<Locale> locales){
|
318
|
List<NameSTO> nameList = new ArrayList<NameSTO>();
|
319
|
for (UUID u: uuids){
|
320
|
try {
|
321
|
nameList.add(getSimpleName(u, locales));
|
322
|
} catch (CdmObjectNonExisting e) {
|
323
|
logger.warn("Name UUID "+u+" does not exist!");
|
324
|
}
|
325
|
}
|
326
|
return nameList;
|
327
|
}
|
328
|
public List<ReferenceSTO> getSimpleReferences(Set<UUID> uuids, Enumeration<Locale> locales) {
|
329
|
List<ReferenceSTO> refList = new ArrayList<ReferenceSTO>();
|
330
|
for (UUID u: uuids){
|
331
|
try {
|
332
|
refList.add(getSimpleReference(u,locales));
|
333
|
} catch (CdmObjectNonExisting e) {
|
334
|
logger.warn("Reference UUID "+u+" does not exist!");
|
335
|
}
|
336
|
}
|
337
|
return refList;
|
338
|
}
|
339
|
public List<TaxonSTO> getSimpleTaxa(Set<UUID> uuids, Enumeration<Locale> locales){
|
340
|
List<TaxonSTO> taxList = new ArrayList<TaxonSTO>();
|
341
|
for (UUID u: uuids){
|
342
|
try {
|
343
|
taxList.add(getSimpleTaxon(u,locales));
|
344
|
} catch (CdmObjectNonExisting e) {
|
345
|
logger.warn("Taxon UUID "+u+" does not exist!");
|
346
|
}
|
347
|
}
|
348
|
return taxList;
|
349
|
}
|
350
|
public List<FeatureTO> getFeatures(Enumeration<Locale> locales) throws CdmObjectNonExisting {
|
351
|
List<FeatureTO> featureList = new ArrayList<FeatureTO>();
|
352
|
|
353
|
for (Feature feature : featureDAO.list()){
|
354
|
featureList.add(descriptionAssembler.getTO(feature, locales));
|
355
|
}
|
356
|
|
357
|
return featureList;
|
358
|
}
|
359
|
public List<FeatureTreeTO> getFeatureTrees(Enumeration<Locale> locales)
|
360
|
throws CdmObjectNonExisting {
|
361
|
List<FeatureTreeTO> featureTreeList = new ArrayList<FeatureTreeTO>();
|
362
|
|
363
|
for (FeatureTree featureTree : featureTreeDAO.list()){
|
364
|
featureTreeList.add(descriptionAssembler.getTO(featureTree, locales));
|
365
|
}
|
366
|
|
367
|
return featureTreeList;
|
368
|
}
|
369
|
|
370
|
//FIXME commented out below, since refactoring is urgently needed see ticket#593 http://dev.e-taxonomy.eu/trac/ticket/593
|
371
|
// /* (non-Javadoc)
|
372
|
// * @see eu.etaxonomy.cdm.remote.service.ICdmService#getAnnotations(java.util.UUID)
|
373
|
// */
|
374
|
// public AnnotationTO getAnnotation(UUID uuid, Enumeration<Locale> locales)
|
375
|
// throws CdmObjectNonExisting {
|
376
|
// List<AnnotationTO> annotationList = new ArrayList<AnnotationTO>();
|
377
|
//
|
378
|
// // TODO the cast should be removed, but i (niels) don't know how at the moment
|
379
|
// AnnotatableEntity annotatableEntity = (AnnotatableEntity) annotatableDao.findByUuid(uuid);
|
380
|
//
|
381
|
//
|
382
|
// return annotationAssembler.getTO(annotatableEntity, locales);
|
383
|
// }
|
384
|
// /* (non-Javadoc)
|
385
|
// * @see eu.etaxonomy.cdm.remote.service.ICdmService#saveAnnotation(java.util.UUID, eu.etaxonomy.cdm.model.common.Annotation)
|
386
|
// */
|
387
|
// @Transactional(readOnly=false)
|
388
|
// public UUID saveAnnotation(UUID uuid, Annotation annotation)
|
389
|
// throws CdmObjectNonExisting {
|
390
|
//
|
391
|
// AnnotatableEntity annotatableEntity = (AnnotatableEntity) annotatableDao.findByUuid(uuid);
|
392
|
//
|
393
|
// annotatableEntity.addAnnotation(annotation);
|
394
|
//
|
395
|
// UUID updatedUuid = annotatableDao.saveOrUpdate(annotatableEntity);
|
396
|
// annotatableDao.flush();
|
397
|
//
|
398
|
// return updatedUuid;
|
399
|
// }
|
400
|
}
|