2 * Copyright (C) 2007 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
.List
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
19 import org
.springframework
.stereotype
.Service
;
20 import org
.springframework
.transaction
.annotation
.Transactional
;
22 import eu
.etaxonomy
.cdm
.api
.service
.config
.FeatureNodeDeletionConfigurator
;
23 import eu
.etaxonomy
.cdm
.api
.service
.config
.NodeDeletionConfigurator
.ChildHandling
;
24 import eu
.etaxonomy
.cdm
.api
.service
.exception
.ReferencedObjectUndeletableException
;
25 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
28 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
29 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
30 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
31 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IFeatureNodeDao
;
39 @Transactional(readOnly
= false)
40 public class FeatureNodeServiceImpl
extends VersionableServiceBase
<FeatureNode
, IFeatureNodeDao
> implements IFeatureNodeService
{
41 private static final Logger logger
= Logger
.getLogger(FeatureNodeServiceImpl
.class);
45 protected void setDao(IFeatureNodeDao dao
) {
50 private ITermService termService
;
53 @Transactional(readOnly
= false)
54 public DeleteResult
deleteFeatureNode(UUID nodeUuid
, FeatureNodeDeletionConfigurator config
) {
55 DeleteResult result
= new DeleteResult();
56 FeatureNode node
= HibernateProxyHelper
.deproxy(dao
.load(nodeUuid
), FeatureNode
.class);
57 result
= isDeletable(node
, config
);
60 FeatureNode parent
= node
.getParent();
61 parent
= HibernateProxyHelper
.deproxy(parent
, FeatureNode
.class);
62 List
<FeatureNode
> children
= new ArrayList(node
.getChildNodes());
64 if (config
.getChildHandling().equals(ChildHandling
.DELETE
)){
66 for (FeatureNode child
: children
){
67 deleteFeatureNode(child
.getUuid(), config
);
68 // node.removeChild(child);
71 parent
.removeChild(node
);
77 parent
.removeChild(node
);
78 for (FeatureNode child
: children
){
79 node
.removeChild(child
);
80 parent
.addChild(child
);
84 result
.addException(new ReferencedObjectUndeletableException("The root node can not be deleted without its child nodes"));
90 result
.addDeletedObject(node
);
92 result
.addUpdatedObject(parent
);
94 if (config
.isDeleteElement()){
95 feature
= node
.getFeature();
96 termService
.delete(feature
.getUuid());
97 result
.addDeletedObject(feature
);
104 public UpdateResult
createChildFeatureNode(FeatureNode node
, Feature featureChild
){
105 DefinedTermBase feature
= termService
.save(featureChild
);
106 FeatureNode childNode
= FeatureNode
.NewInstance(featureChild
);
108 UpdateResult result
= new UpdateResult();
109 node
.addChild(childNode
);
110 result
.addUpdatedObject(node
);
111 result
.setCdmEntity(node
.getFeatureTree());
116 public UpdateResult
addChildFeatureNode(FeatureNode node
, Feature featureChild
){
117 UpdateResult result
= new UpdateResult();
118 FeatureNode childNode
= FeatureNode
.NewInstance(featureChild
);
119 node
.addChild(childNode
);
120 result
.addUpdatedObject(node
);
121 result
.setCdmEntity(node
.getFeatureTree());
126 public UpdateResult
addChildFeatureNode(UUID nodeUUID
, UUID featureChildUuid
){
127 FeatureNode node
= load(nodeUUID
);
128 Feature child
= HibernateProxyHelper
.deproxy(termService
.load(featureChildUuid
), Feature
.class);
129 return addChildFeatureNode(node
, child
);
133 public DeleteResult
isDeletable(FeatureNode node
, FeatureNodeDeletionConfigurator config
){
134 DeleteResult result
= new DeleteResult();
135 Set
<CdmBase
> references
= commonService
.getReferencingObjectsForDeletion(node
);
136 for (CdmBase ref
:references
){
137 if (ref
instanceof FeatureNode
){
140 if (ref
instanceof FeatureTree
){
141 FeatureTree refTree
= HibernateProxyHelper
.deproxy(ref
, FeatureTree
.class);
142 if (node
.getFeatureTree().equals((refTree
))){
147 result
.addException(new ReferencedObjectUndeletableException("The featureNode is referenced by " + ref
.getUserFriendlyDescription() +" with id " +ref
.getId()));
157 public UpdateResult
moveFeatureNode(UUID movedNodeUuid
, UUID targetNodeUuid
, int position
) {
158 UpdateResult result
= new UpdateResult();
159 FeatureNode movedNode
= HibernateProxyHelper
.deproxy(load(movedNodeUuid
), FeatureNode
.class);
160 FeatureNode targetNode
= HibernateProxyHelper
.deproxy(load(targetNodeUuid
), FeatureNode
.class);
161 FeatureNode parent
= HibernateProxyHelper
.deproxy(movedNode
.getParent(), FeatureNode
.class);
163 targetNode
.addChild(movedNode
);
166 targetNode
.addChild(movedNode
, position
);
168 result
.addUpdatedObject(targetNode
);
170 result
.addUpdatedObject(parent
);
172 result
.setCdmEntity(targetNode
.getFeatureTree());
180 public UpdateResult
moveFeatureNode(UUID movedNodeUuid
, UUID targetNodeUuid
) {
181 return moveFeatureNode(movedNodeUuid
, targetNodeUuid
, -1);