2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.api
.service
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Arrays
;
14 import java
.util
.Collection
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import javax
.persistence
.EntityNotFoundException
;
22 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
23 import org
.springframework
.stereotype
.Service
;
24 import org
.springframework
.transaction
.annotation
.Transactional
;
26 import eu
.etaxonomy
.cdm
.api
.service
.config
.NodeDeletionConfigurator
.ChildHandling
;
27 import eu
.etaxonomy
.cdm
.api
.service
.config
.TermNodeDeletionConfigurator
;
28 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
29 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.model
.term
.TermNode
;
32 import eu
.etaxonomy
.cdm
.model
.term
.TermTree
;
33 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
34 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.Restriction
;
35 import eu
.etaxonomy
.cdm
.persistence
.dao
.term
.ITermNodeDao
;
36 import eu
.etaxonomy
.cdm
.persistence
.dao
.term
.ITermTreeDao
;
37 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
38 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
39 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
42 @Transactional(readOnly
= false)
43 public class TermTreeServiceImpl
44 extends IdentifiableServiceBase
<TermTree
, ITermTreeDao
>
45 implements ITermTreeService
{
47 private ITermNodeDao termNodeDao
;
50 private ITermNodeService termNodeService
;
54 protected void setDao(ITermTreeDao dao
) {
59 protected void setTermNodeDao(ITermNodeDao termNodeDao
) {
60 this.termNodeDao
= termNodeDao
;
64 @Transactional(readOnly
= false)
65 public UpdateResult
updateCaches(Class
<?
extends TermTree
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<TermTree
> cacheStrategy
, IProgressMonitor monitor
) {
67 clazz
= TermTree
.class;
69 return super.updateCachesImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
73 public Map
<UUID
, TermNode
> saveNodesAll(Collection
<TermNode
> nodeCollection
) {
74 return termNodeDao
.saveAll(nodeCollection
);
78 public Map
<UUID
, TermNode
> saveOrUpdateNodesAll(Collection
<TermNode
> nodeCollection
) {
79 return termNodeDao
.saveOrUpdateAll(nodeCollection
);
83 public TermTree
loadWithNodes(UUID uuid
, List
<String
> propertyPaths
, List
<String
> nodePaths
) {
86 nodePaths
= new ArrayList
<>();
89 if(!nodePaths
.contains("children")) {
90 nodePaths
.add("children");
93 List
<String
> rootPaths
= new ArrayList
<>();
94 rootPaths
.add("root");
95 for(String path
: nodePaths
) {
96 rootPaths
.add("root." + path
);
99 if(propertyPaths
!= null) {
100 rootPaths
.addAll(propertyPaths
);
103 TermTree featureTree
= load(uuid
, rootPaths
);
104 if(featureTree
== null){
105 throw new EntityNotFoundException("No FeatureTree entity found for " + uuid
);
107 dao
.deepLoadNodes(featureTree
.getRoot().getChildNodes(), nodePaths
);
112 * Returns the featureTree specified by the given <code>uuid</code>.
113 * The specified featureTree either can be one of those stored in the CDM database or can be the
114 * DefaultFeatureTree (contains all Features in use).
115 * The uuid of the DefaultFeatureTree is defined in {@link ITermTreeDao#DefaultFeatureTreeUuid}.
117 * @see eu.etaxonomy.cdm.api.service.ServiceBase#load(java.util.UUID, java.util.List)
120 public TermTree
load(UUID uuid
, List
<String
> propertyPaths
) {
121 return super.load(uuid
, propertyPaths
);
125 public DeleteResult
delete(UUID featureTreeUuid
){
126 DeleteResult result
= new DeleteResult();
127 TermTree tree
= dao
.load(featureTreeUuid
);
129 TermNode rootNode
= CdmBase
.deproxy(tree
.getRoot());
130 TermNodeDeletionConfigurator config
= new TermNodeDeletionConfigurator();
131 config
.setChildHandling(ChildHandling
.DELETE
);
132 result
= termNodeService
.deleteNode(rootNode
.getUuid(), config
);
133 //FIXME test if this is necessary
134 tree
.removeRootNode();
137 result
.addDeletedObject(tree
);
143 public <S
extends TermTree
> List
<UuidAndTitleCache
<S
>> getUuidAndTitleCacheByTermType(Class
<S
> clazz
, TermType termType
, Integer limit
,
145 return dao
.getUuidAndTitleCacheByTermType(clazz
, termType
, limit
, pattern
);
149 public List
<TermTree
> list(TermType termType
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
,
150 List
<String
> propertyPaths
) {
151 return dao
.list(null, buildTermTypeFilterRestrictions(termType
), limit
, start
, orderHints
, propertyPaths
);
155 public Pager
<TermTree
> page(TermType termType
, Integer pageSize
, Integer pageIndex
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
157 return page(null, buildTermTypeFilterRestrictions(termType
), pageSize
, pageIndex
, orderHints
, propertyPaths
);
165 public List
<Restriction
<?
>> buildTermTypeFilterRestrictions(TermType termType
) {
166 List
<Restriction
<?
>> filterRestrictions
= null;
167 if(termType
!= null){
168 Set
<TermType
> termTypes
= termType
.getGeneralizationOf(true);
169 termTypes
.add(termType
);
170 filterRestrictions
= Arrays
.asList(new Restriction
<>("termType", null, termTypes
.toArray()));
172 return filterRestrictions
;