1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.api.service;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Collection;
|
15
|
import java.util.Collections;
|
16
|
import java.util.Comparator;
|
17
|
import java.util.List;
|
18
|
import java.util.Map;
|
19
|
import java.util.TreeMap;
|
20
|
import java.util.UUID;
|
21
|
|
22
|
import org.apache.log4j.Logger;
|
23
|
import org.springframework.beans.factory.annotation.Autowired;
|
24
|
import org.springframework.stereotype.Service;
|
25
|
import org.springframework.transaction.annotation.Propagation;
|
26
|
import org.springframework.transaction.annotation.Transactional;
|
27
|
|
28
|
import eu.etaxonomy.cdm.common.IProgressMonitor;
|
29
|
import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
|
30
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
31
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
32
|
import eu.etaxonomy.cdm.model.media.Media;
|
33
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
34
|
import eu.etaxonomy.cdm.model.media.MediaUtils;
|
35
|
import eu.etaxonomy.cdm.model.name.Rank;
|
36
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
37
|
import eu.etaxonomy.cdm.model.taxon.ITaxonNodeComparator;
|
38
|
import eu.etaxonomy.cdm.model.taxon.ITreeNode;
|
39
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
40
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
41
|
import eu.etaxonomy.cdm.persistence.dao.BeanInitializer;
|
42
|
import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
|
43
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
|
44
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
|
45
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
46
|
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
|
47
|
|
48
|
/**
|
49
|
* @author n.hoffmann
|
50
|
* @created Sep 21, 2009
|
51
|
* @version 1.0
|
52
|
*/
|
53
|
@Service
|
54
|
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
|
55
|
public class ClassificationServiceImpl extends IdentifiableServiceBase<Classification, IClassificationDao> implements IClassificationService {
|
56
|
private static final Logger logger = Logger.getLogger(ClassificationServiceImpl.class);
|
57
|
|
58
|
@Autowired
|
59
|
private ITaxonNodeDao taxonNodeDao;
|
60
|
@Autowired
|
61
|
private ITaxonDao taxonDao;
|
62
|
@Autowired
|
63
|
private BeanInitializer defaultBeanInitializer;
|
64
|
private Comparator<? super TaxonNode> taxonNodeComparator;
|
65
|
@Autowired
|
66
|
public void setTaxonNodeComparator(ITaxonNodeComparator<? super TaxonNode> taxonNodeComparator){
|
67
|
this.taxonNodeComparator = (Comparator<? super TaxonNode>) taxonNodeComparator;
|
68
|
}
|
69
|
|
70
|
|
71
|
/*
|
72
|
* (non-Javadoc)
|
73
|
* @see eu.etaxonomy.cdm.api.service.IClassificationService#loadTaxonNodeByTaxon(eu.etaxonomy.cdm.model.taxon.Taxon, java.util.UUID, java.util.List)
|
74
|
*/
|
75
|
public TaxonNode loadTaxonNodeByTaxon(Taxon taxon, UUID classificationUuid, List<String> propertyPaths){
|
76
|
Classification tree = dao.load(classificationUuid);
|
77
|
TaxonNode node = tree.getNode(taxon);
|
78
|
|
79
|
return loadTaxonNode(node.getUuid(), propertyPaths);
|
80
|
}
|
81
|
|
82
|
@Deprecated // use loadTaxonNode(UUID, List<String>) instead
|
83
|
public TaxonNode loadTaxonNode(TaxonNode taxonNode, List<String> propertyPaths){
|
84
|
return taxonNodeDao.load(taxonNode.getUuid(), propertyPaths);
|
85
|
}
|
86
|
|
87
|
public TaxonNode loadTaxonNode(UUID taxonNodeUuid, List<String> propertyPaths){
|
88
|
return taxonNodeDao.load(taxonNodeUuid, propertyPaths);
|
89
|
}
|
90
|
|
91
|
/*
|
92
|
* (non-Javadoc)
|
93
|
* @see eu.etaxonomy.cdm.api.service.IClassificationService#loadRankSpecificRootNodes(eu.etaxonomy.cdm.model.taxon.Classification, eu.etaxonomy.cdm.model.name.Rank, java.util.List)
|
94
|
*/
|
95
|
public List<TaxonNode> loadRankSpecificRootNodes(Classification classification, Rank rank, List<String> propertyPaths){
|
96
|
|
97
|
List<TaxonNode> rootNodes = dao.loadRankSpecificRootNodes(classification, rank, propertyPaths);
|
98
|
|
99
|
//sort nodes by TaxonName
|
100
|
Collections.sort(rootNodes, taxonNodeComparator);
|
101
|
|
102
|
// initialize all nodes
|
103
|
defaultBeanInitializer.initializeAll(rootNodes, propertyPaths);
|
104
|
|
105
|
return rootNodes;
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* (non-Javadoc)
|
110
|
* @see eu.etaxonomy.cdm.api.service.ITaxonService#loadTreeBranchTo(eu.etaxonomy.cdm.model.taxon.TaxonNode, eu.etaxonomy.cdm.model.name.Rank, java.util.List)
|
111
|
* FIXME Candidate for harmonization
|
112
|
* move to classification service
|
113
|
*/
|
114
|
public List<TaxonNode> loadTreeBranch(TaxonNode taxonNode, Rank baseRank, List<String> propertyPaths){
|
115
|
|
116
|
TaxonNode thisNode = taxonNodeDao.load(taxonNode.getUuid(), propertyPaths);
|
117
|
List<TaxonNode> pathToRoot = new ArrayList<TaxonNode>();
|
118
|
pathToRoot.add(thisNode);
|
119
|
|
120
|
TaxonNode parentNode = thisNode.getParent();
|
121
|
while(parentNode != null){
|
122
|
TaxonNode parent = parentNode;
|
123
|
Rank parentNodeRank = parent.getTaxon().getName().getRank();
|
124
|
// stop if the next parent is higher than the baseRank
|
125
|
if(baseRank != null && baseRank.isLower(parentNodeRank)){
|
126
|
break;
|
127
|
}
|
128
|
pathToRoot.add(parentNode);
|
129
|
parentNode = parentNode.getParent();
|
130
|
}
|
131
|
|
132
|
// initialize and invert order of nodes in list
|
133
|
defaultBeanInitializer.initializeAll(pathToRoot, propertyPaths);
|
134
|
Collections.reverse(pathToRoot);
|
135
|
|
136
|
return pathToRoot;
|
137
|
}
|
138
|
|
139
|
/*
|
140
|
* (non-Javadoc)
|
141
|
* @see eu.etaxonomy.cdm.api.service.IClassificationService#loadTreeBranchToTaxon(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.Classification, eu.etaxonomy.cdm.model.name.Rank, java.util.List)
|
142
|
*/
|
143
|
public List<TaxonNode> loadTreeBranchToTaxon(Taxon taxon, Classification classification, Rank baseRank, List<String> propertyPaths){
|
144
|
Classification tree = dao.load(classification.getUuid());
|
145
|
taxon = (Taxon) taxonDao.load(taxon.getUuid());
|
146
|
TaxonNode node = tree.getNode(taxon);
|
147
|
if(node == null){
|
148
|
logger.warn("The specified taxon is not found in the given tree.");
|
149
|
return null;
|
150
|
}
|
151
|
return loadTreeBranch(node, baseRank, propertyPaths);
|
152
|
}
|
153
|
|
154
|
|
155
|
/* (non-Javadoc)
|
156
|
* @see eu.etaxonomy.cdm.api.service.IClassificationService#loadChildNodesOfTaxon(eu.etaxonomy.cdm.model.taxon.TaxonNode, java.util.List)
|
157
|
*/
|
158
|
public List<TaxonNode> loadChildNodesOfTaxonNode(TaxonNode taxonNode,
|
159
|
List<String> propertyPaths) {
|
160
|
taxonNode = taxonNodeDao.load(taxonNode.getUuid());
|
161
|
List<TaxonNode> childNodes = new ArrayList<TaxonNode>(taxonNode.getChildNodes());
|
162
|
defaultBeanInitializer.initializeAll(childNodes, propertyPaths);
|
163
|
Collections.sort(childNodes, taxonNodeComparator);
|
164
|
return childNodes;
|
165
|
}
|
166
|
|
167
|
/*
|
168
|
* (non-Javadoc)
|
169
|
* @see eu.etaxonomy.cdm.api.service.IClassificationService#loadChildNodesOfTaxon(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.Classification, java.util.List)
|
170
|
*/
|
171
|
public List<TaxonNode> loadChildNodesOfTaxon(Taxon taxon, Classification classification, List<String> propertyPaths){
|
172
|
Classification tree = dao.load(classification.getUuid());
|
173
|
taxon = (Taxon) taxonDao.load(taxon.getUuid());
|
174
|
|
175
|
TaxonNode node = tree.getNode(taxon);
|
176
|
if(node != null){
|
177
|
return loadChildNodesOfTaxonNode(node, propertyPaths);
|
178
|
} else {
|
179
|
return null;
|
180
|
}
|
181
|
}
|
182
|
|
183
|
|
184
|
|
185
|
/*
|
186
|
* (non-Javadoc)
|
187
|
* @see eu.etaxonomy.cdm.api.service.IClassificationService#getTaxonNodeByUuid(java.util.UUID)
|
188
|
*/
|
189
|
public TaxonNode getTaxonNodeByUuid(UUID uuid) {
|
190
|
return taxonNodeDao.findByUuid(uuid);
|
191
|
}
|
192
|
|
193
|
/*
|
194
|
* (non-Javadoc)
|
195
|
* @see eu.etaxonomy.cdm.api.service.IClassificationService#getTreeNodeByUuid(java.util.UUID)
|
196
|
*/
|
197
|
public ITreeNode getTreeNodeByUuid(UUID uuid){
|
198
|
ITreeNode treeNode = taxonNodeDao.findByUuid(uuid);
|
199
|
if(treeNode == null){
|
200
|
treeNode = dao.findByUuid(uuid);
|
201
|
}
|
202
|
|
203
|
return treeNode;
|
204
|
}
|
205
|
|
206
|
/* (non-Javadoc)
|
207
|
* @see eu.etaxonomy.cdm.api.service.IClassificationService#listClassifications(java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)
|
208
|
*/
|
209
|
public List<Classification> listClassifications(Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
210
|
return dao.list(limit, start, orderHints, propertyPaths);
|
211
|
}
|
212
|
|
213
|
/* (non-Javadoc)
|
214
|
* @see eu.etaxonomy.cdm.api.service.IClassificationService#removeTaxonNode(eu.etaxonomy.cdm.model.taxon.Classification)
|
215
|
*/
|
216
|
public UUID removeTaxonNode(TaxonNode taxonNode) {
|
217
|
return taxonNodeDao.delete(taxonNode);
|
218
|
}
|
219
|
/* (non-Javadoc)
|
220
|
* @see eu.etaxonomy.cdm.api.service.IClassificationService#removeTreeNode(eu.etaxonomy.cdm.model.taxon.ITreeNode)
|
221
|
*/
|
222
|
public UUID removeTreeNode(ITreeNode treeNode) {
|
223
|
if(treeNode instanceof Classification){
|
224
|
return dao.delete((Classification) treeNode);
|
225
|
}else if(treeNode instanceof TaxonNode){
|
226
|
return taxonNodeDao.delete((TaxonNode)treeNode);
|
227
|
}
|
228
|
return null;
|
229
|
}
|
230
|
/* (non-Javadoc)
|
231
|
* @see eu.etaxonomy.cdm.api.service.IClassificationService#saveTaxonNode(eu.etaxonomy.cdm.model.taxon.Classification)
|
232
|
*/
|
233
|
public UUID saveTaxonNode(TaxonNode taxonNode) {
|
234
|
return taxonNodeDao.save(taxonNode);
|
235
|
}
|
236
|
|
237
|
/*
|
238
|
* (non-Javadoc)
|
239
|
* @see eu.etaxonomy.cdm.api.service.IClassificationService#saveTaxonNodeAll(java.util.Collection)
|
240
|
*/
|
241
|
public Map<UUID, TaxonNode> saveTaxonNodeAll(
|
242
|
Collection<TaxonNode> taxonNodeCollection) {
|
243
|
return taxonNodeDao.saveAll(taxonNodeCollection);
|
244
|
}
|
245
|
|
246
|
/* (non-Javadoc)
|
247
|
* @see eu.etaxonomy.cdm.api.service.IClassificationService#saveTreeNode(eu.etaxonomy.cdm.model.taxon.ITreeNode)
|
248
|
*/
|
249
|
public UUID saveTreeNode(ITreeNode treeNode) {
|
250
|
if(treeNode instanceof Classification){
|
251
|
return dao.save((Classification) treeNode);
|
252
|
}else if(treeNode instanceof TaxonNode){
|
253
|
return taxonNodeDao.save((TaxonNode)treeNode);
|
254
|
}
|
255
|
return null;
|
256
|
}
|
257
|
|
258
|
public List<TaxonNode> getAllNodes(){
|
259
|
return taxonNodeDao.list(null,null);
|
260
|
}
|
261
|
|
262
|
/*
|
263
|
* (non-Javadoc)
|
264
|
* @see eu.etaxonomy.cdm.api.service.ITaxonService#getUuidAndTitleCacheOfAcceptedTaxa(eu.etaxonomy.cdm.model.taxon.Classification)
|
265
|
*/
|
266
|
public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(Classification classification) {
|
267
|
return taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification);
|
268
|
}
|
269
|
|
270
|
/* (non-Javadoc)
|
271
|
* @see eu.etaxonomy.cdm.api.service.IdentifiableServiceBase#getUuidAndTitleCache()
|
272
|
*/
|
273
|
@Override
|
274
|
public List<UuidAndTitleCache<Classification>> getUuidAndTitleCache() {
|
275
|
return dao.getUuidAndTitleCache();
|
276
|
}
|
277
|
|
278
|
/* (non-Javadoc)
|
279
|
* @see eu.etaxonomy.cdm.api.service.IClassificationService#getAllMediaForChildNodes(eu.etaxonomy.cdm.model.taxon.TaxonNode, java.util.List, int, int, int, java.lang.String[])
|
280
|
*/
|
281
|
public Map<UUID, List<MediaRepresentation>> getAllMediaForChildNodes(
|
282
|
TaxonNode taxonNode, List<String> propertyPaths, int size,
|
283
|
int height, int widthOrDuration, String[] mimeTypes) {
|
284
|
|
285
|
TreeMap<UUID, List<MediaRepresentation>> result = new TreeMap<UUID, List<MediaRepresentation>>();
|
286
|
List<Media> taxonMedia = new ArrayList<Media>();
|
287
|
List<MediaRepresentation> mediaRepresentations = new ArrayList<MediaRepresentation>();
|
288
|
|
289
|
//add all media of the children to the result map
|
290
|
if (taxonNode != null){
|
291
|
|
292
|
List<TaxonNode> nodes = new ArrayList<TaxonNode>();
|
293
|
|
294
|
nodes.add(loadTaxonNode(taxonNode, propertyPaths));
|
295
|
nodes.addAll(loadChildNodesOfTaxonNode(taxonNode, propertyPaths));
|
296
|
|
297
|
if (nodes != null){
|
298
|
for(TaxonNode node : nodes){
|
299
|
Taxon taxon = node.getTaxon();
|
300
|
for (TaxonDescription taxonDescription: taxon.getDescriptions()){
|
301
|
for (DescriptionElementBase descriptionElement: taxonDescription.getElements()){
|
302
|
for(Media media : descriptionElement.getMedia()){
|
303
|
taxonMedia.add(media);
|
304
|
|
305
|
//find the best matching representation
|
306
|
mediaRepresentations.add(MediaUtils.findBestMatchingRepresentation(media,null, size, height, widthOrDuration, mimeTypes));
|
307
|
|
308
|
}
|
309
|
}
|
310
|
}
|
311
|
result.put(taxon.getUuid(), mediaRepresentations);
|
312
|
|
313
|
}
|
314
|
}
|
315
|
|
316
|
}
|
317
|
|
318
|
|
319
|
return result;
|
320
|
|
321
|
}
|
322
|
|
323
|
public Map<UUID, List<MediaRepresentation>> getAllMediaForChildNodes(Taxon taxon, Classification taxTree, List<String> propertyPaths, int size, int height, int widthOrDuration, String[] mimeTypes){
|
324
|
TaxonNode node = taxTree.getNode(taxon);
|
325
|
|
326
|
return getAllMediaForChildNodes(node, propertyPaths, size, height, widthOrDuration, mimeTypes);
|
327
|
}
|
328
|
|
329
|
|
330
|
|
331
|
|
332
|
|
333
|
/* (non-Javadoc)
|
334
|
* @see eu.etaxonomy.cdm.api.service.ServiceBase#setDao(eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao)
|
335
|
*/
|
336
|
@Autowired
|
337
|
protected void setDao(IClassificationDao dao) {
|
338
|
this.dao = dao;
|
339
|
}
|
340
|
|
341
|
|
342
|
/* (non-Javadoc)
|
343
|
* @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache(java.lang.Integer, eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy)
|
344
|
*/
|
345
|
@Override
|
346
|
public void updateTitleCache(Class<? extends Classification> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<Classification> cacheStrategy, IProgressMonitor monitor) {
|
347
|
if (clazz == null){
|
348
|
clazz = Classification.class;
|
349
|
}
|
350
|
super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
|
351
|
}
|
352
|
|
353
|
|
354
|
}
|