3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.api
.service
;
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
;
19 import java
.util
.TreeMap
;
20 import java
.util
.UUID
;
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
;
28 import eu
.etaxonomy
.cdm
.model
.common
.UuidAndTitleCache
;
29 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
30 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
31 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
32 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentation
;
33 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.ITreeNode
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonomicTree
;
38 import eu
.etaxonomy
.cdm
.persistence
.dao
.BeanInitializer
;
39 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.ITaxonDao
;
40 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.ITaxonNodeDao
;
41 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.ITaxonomicTreeDao
;
42 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
46 * @created Sep 21, 2009
50 @Transactional(propagation
= Propagation
.SUPPORTS
, readOnly
= true)
51 public class TaxonTreeServiceImpl
extends IdentifiableServiceBase
<TaxonomicTree
, ITaxonomicTreeDao
>
52 implements ITaxonTreeService
{
53 private static final Logger logger
= Logger
54 .getLogger(TaxonTreeServiceImpl
.class);
57 private ITaxonNodeDao taxonNodeDao
;
59 private ITaxonDao taxonDao
;
61 private BeanInitializer defaultBeanInitializer
;
62 private Comparator
<?
super TaxonNode
> taxonNodeComparator
;
64 public void setTaxonNodeComparator(ITaxonNodeComparator
<?
super TaxonNode
> taxonNodeComparator
){
65 this.taxonNodeComparator
= (Comparator
<?
super TaxonNode
>) taxonNodeComparator
;
71 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#loadTaxonNodeByTaxon(eu.etaxonomy.cdm.model.taxon.Taxon, java.util.UUID, java.util.List)
73 public TaxonNode
loadTaxonNodeByTaxon(Taxon taxon
, UUID taxonomicTreeUuid
, List
<String
> propertyPaths
){
74 TaxonomicTree tree
= dao
.load(taxonomicTreeUuid
);
75 TaxonNode node
= tree
.getNode(taxon
);
77 return loadTaxonNode(node
, propertyPaths
);
80 public TaxonNode
loadTaxonNode(TaxonNode taxonNode
, List
<String
> propertyPaths
){
81 defaultBeanInitializer
.initialize(taxonNode
, propertyPaths
);
87 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#loadRankSpecificRootNodes(eu.etaxonomy.cdm.model.taxon.TaxonomicTree, eu.etaxonomy.cdm.model.name.Rank, java.util.List)
89 public List
<TaxonNode
> loadRankSpecificRootNodes(TaxonomicTree taxonomicTree
, Rank rank
, List
<String
> propertyPaths
){
90 TaxonomicTree tree
= dao
.load(taxonomicTree
.getUuid());
92 List
<TaxonNode
> rootNodes
= tree
.getRankSpecificRootNodes(rank
);
93 //sort nodes by TaxonName
95 Collections
.sort(rootNodes
, taxonNodeComparator
);
97 // initialize all nodes
98 defaultBeanInitializer
.initializeAll(rootNodes
, propertyPaths
);
105 * @see eu.etaxonomy.cdm.api.service.ITaxonService#loadTreeBranchTo(eu.etaxonomy.cdm.model.taxon.TaxonNode, eu.etaxonomy.cdm.model.name.Rank, java.util.List)
106 * FIXME Candidate for harmonization
107 * move to taxonTreeService
109 public List
<TaxonNode
> loadTreeBranch(TaxonNode taxonNode
, Rank baseRank
, List
<String
> propertyPaths
){
111 TaxonNode thisNode
= taxonNodeDao
.load(taxonNode
.getUuid(), propertyPaths
);
112 List
<TaxonNode
> pathToRoot
= new ArrayList
<TaxonNode
>();
113 pathToRoot
.add(thisNode
);
115 ITreeNode parentNode
= thisNode
.getParent();
116 while(parentNode
instanceof TaxonNode
){
117 TaxonNode parent
= (TaxonNode
) parentNode
;
118 Rank parentNodeRank
= parent
.getTaxon().getName().getRank();
119 // stop if the next parent is higher than the baseRank
120 if(baseRank
!= null && baseRank
.isLower(parentNodeRank
)){
123 pathToRoot
.add(parent
);
124 parentNode
= parent
.getParent();
127 // initialize and invert order of nodes in list
128 defaultBeanInitializer
.initializeAll(pathToRoot
, propertyPaths
);
129 Collections
.reverse(pathToRoot
);
136 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#loadTreeBranchToTaxon(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.TaxonomicTree, eu.etaxonomy.cdm.model.name.Rank, java.util.List)
138 public List
<TaxonNode
> loadTreeBranchToTaxon(Taxon taxon
, TaxonomicTree taxonomicTree
, Rank baseRank
, List
<String
> propertyPaths
){
139 TaxonomicTree tree
= dao
.load(taxonomicTree
.getUuid());
140 taxon
= (Taxon
) taxonDao
.load(taxon
.getUuid());
141 TaxonNode node
= tree
.getNode(taxon
);
143 logger
.warn("The specified tacon is not found in the given tree.");
146 return loadTreeBranch(node
, baseRank
, propertyPaths
);
151 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#loadChildNodesOfTaxon(eu.etaxonomy.cdm.model.taxon.TaxonNode, java.util.List)
153 public List
<TaxonNode
> loadChildNodesOfTaxonNode(TaxonNode taxonNode
,
154 List
<String
> propertyPaths
) {
155 List
<TaxonNode
> childNodes
= new ArrayList
<TaxonNode
>(taxonNode
.getChildNodes());
156 Collections
.sort(childNodes
, taxonNodeComparator
);
157 defaultBeanInitializer
.initializeAll(childNodes
, propertyPaths
);
163 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#loadChildNodesOfTaxon(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.TaxonomicTree, java.util.List)
165 public List
<TaxonNode
> loadChildNodesOfTaxon(Taxon taxon
, TaxonomicTree taxonomicTree
, List
<String
> propertyPaths
){
166 TaxonomicTree tree
= dao
.load(taxonomicTree
.getUuid());
167 taxon
= (Taxon
) taxonDao
.load(taxon
.getUuid());
169 return loadChildNodesOfTaxonNode(tree
.getNode(taxon
), propertyPaths
);
176 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#getTaxonNodeByUuid(java.util.UUID)
178 public TaxonNode
getTaxonNodeByUuid(UUID uuid
) {
179 return taxonNodeDao
.findByUuid(uuid
);
184 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#getTreeNodeByUuid(java.util.UUID)
186 public ITreeNode
getTreeNodeByUuid(UUID uuid
){
187 ITreeNode treeNode
= taxonNodeDao
.findByUuid(uuid
);
188 if(treeNode
== null){
189 treeNode
= dao
.findByUuid(uuid
);
197 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#listTaxonomicTrees(java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)
199 public List
<TaxonomicTree
> listTaxonomicTrees(Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
200 return dao
.list(limit
, start
, orderHints
, propertyPaths
);
205 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#getTaxonomicTreeByUuid(java.util.UUID)
207 public TaxonomicTree
getTaxonomicTreeByUuid(UUID uuid
){
208 return dao
.findByUuid(uuid
);
212 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#removeTaxonNode(eu.etaxonomy.cdm.model.taxon.TaxonomicTree)
214 public UUID
removeTaxonNode(TaxonNode taxonNode
) {
215 return taxonNodeDao
.delete(taxonNode
);
218 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#removeTreeNode(eu.etaxonomy.cdm.model.taxon.ITreeNode)
220 public UUID
removeTreeNode(ITreeNode treeNode
) {
221 if(treeNode
instanceof TaxonomicTree
){
222 return dao
.delete((TaxonomicTree
) treeNode
);
223 }else if(treeNode
instanceof TaxonNode
){
224 return taxonNodeDao
.delete((TaxonNode
)treeNode
);
229 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#saveTaxonNode(eu.etaxonomy.cdm.model.taxon.TaxonomicTree)
231 public UUID
saveTaxonNode(TaxonNode taxonNode
) {
232 return taxonNodeDao
.save(taxonNode
);
237 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#saveTaxonNodeAll(java.util.Collection)
239 public Map
<UUID
, TaxonNode
> saveTaxonNodeAll(
240 Collection
<TaxonNode
> taxonNodeCollection
) {
241 return taxonNodeDao
.saveAll(taxonNodeCollection
);
245 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#saveTreeNode(eu.etaxonomy.cdm.model.taxon.ITreeNode)
247 public UUID
saveTreeNode(ITreeNode treeNode
) {
248 if(treeNode
instanceof TaxonomicTree
){
249 return dao
.save((TaxonomicTree
) treeNode
);
250 }else if(treeNode
instanceof TaxonNode
){
251 return taxonNodeDao
.save((TaxonNode
)treeNode
);
256 public List
<TaxonNode
> getAllNodes(){
257 return taxonNodeDao
.list(null,null);
262 * @see eu.etaxonomy.cdm.api.service.ITaxonService#getUuidAndTitleCacheOfAcceptedTaxa(eu.etaxonomy.cdm.model.taxon.TaxonomicTree)
264 public List
<UuidAndTitleCache
<TaxonNode
>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByTaxonomicTree(TaxonomicTree taxonomicTree
) {
265 return taxonDao
.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByTaxonomicTree(taxonomicTree
);
269 * @see eu.etaxonomy.cdm.api.service.IdentifiableServiceBase#getUuidAndTitleCache()
272 public List
<UuidAndTitleCache
<TaxonomicTree
>> getUuidAndTitleCache() {
273 return dao
.getUuidAndTitleCache();
277 * @see eu.etaxonomy.cdm.api.service.ITaxonTreeService#getAllMediaForChildNodes(eu.etaxonomy.cdm.model.taxon.TaxonNode, java.util.List, int, int, int, java.lang.String[])
279 public Map
<UUID
, List
<MediaRepresentation
>> getAllMediaForChildNodes(
280 TaxonNode taxonNode
, List
<String
> propertyPaths
, int size
,
281 int height
, int widthOrDuration
, String
[] mimeTypes
) {
283 TreeMap
<UUID
, List
<MediaRepresentation
>> result
= new TreeMap
<UUID
, List
<MediaRepresentation
>>();
284 List
<Media
> taxonMedia
= new ArrayList
<Media
>();
285 List
<MediaRepresentation
> mediaRepresentations
= new ArrayList
<MediaRepresentation
>();
287 //add all media of the children to the result map
288 if (taxonNode
!= null){
290 List
<TaxonNode
> nodes
= new ArrayList
<TaxonNode
>();
292 nodes
.add(loadTaxonNode(taxonNode
, propertyPaths
));
293 nodes
.addAll(loadChildNodesOfTaxonNode(taxonNode
, propertyPaths
));
296 for(TaxonNode node
: nodes
){
297 Taxon taxon
= node
.getTaxon();
298 for (TaxonDescription taxonDescription
: taxon
.getDescriptions()){
299 for (DescriptionElementBase descriptionElement
: taxonDescription
.getElements()){
300 for(Media media
: descriptionElement
.getMedia()){
301 taxonMedia
.add(media
);
303 //find the best matching representation
304 mediaRepresentations
.add(media
.findBestMatchingRepresentation(size
, height
, widthOrDuration
, mimeTypes
));
309 result
.put(taxon
.getUuid(), mediaRepresentations
);
321 public Map
<UUID
, List
<MediaRepresentation
>> getAllMediaForChildNodes(Taxon taxon
, TaxonomicTree taxTree
, List
<String
> propertyPaths
, int size
, int height
, int widthOrDuration
, String
[] mimeTypes
){
322 TaxonNode node
= taxTree
.getNode(taxon
);
324 return getAllMediaForChildNodes(node
, propertyPaths
, size
, height
, widthOrDuration
, mimeTypes
);
332 * @see eu.etaxonomy.cdm.api.service.ServiceBase#setDao(eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao)
335 protected void setDao(ITaxonomicTreeDao dao
) {
340 * @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#generateTitleCache()
342 public void generateTitleCache() {
343 logger
.warn("generateTitleCache not yet fully implemented!");