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
.description
.Feature
;
28 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
29 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
30 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IFeatureNodeDao
;
34 * @created Aug 5, 2010
38 @Transactional(readOnly
= false)
39 public class FeatureNodeServiceImpl
extends VersionableServiceBase
<FeatureNode
, IFeatureNodeDao
> implements IFeatureNodeService
{
40 private static final Logger logger
= Logger
.getLogger(FeatureNodeServiceImpl
.class);
44 protected void setDao(IFeatureNodeDao dao
) {
49 private ITermService termService
;
52 @Transactional(readOnly
= false)
53 public DeleteResult
deleteFeatureNode(UUID nodeUuid
, FeatureNodeDeletionConfigurator config
) {
54 DeleteResult result
= new DeleteResult();
55 FeatureNode node
= HibernateProxyHelper
.deproxy(dao
.load(nodeUuid
), FeatureNode
.class);
56 result
= isDeletable(node
, config
);
59 FeatureNode parent
= node
.getParent();
60 parent
= HibernateProxyHelper
.deproxy(parent
, FeatureNode
.class);
61 List
<FeatureNode
> children
= new ArrayList(node
.getChildNodes());
63 if (config
.getChildHandling().equals(ChildHandling
.DELETE
)){
65 for (FeatureNode child
: children
){
66 deleteFeatureNode(child
.getUuid(), config
);
67 // node.removeChild(child);
70 parent
.removeChild(node
);
76 parent
.removeChild(node
);
77 for (FeatureNode child
: children
){
78 node
.removeChild(child
);
79 parent
.addChild(child
);
83 result
.addException(new ReferencedObjectUndeletableException("The root node can not be deleted without its child nodes"));
89 if (config
.isDeleteElement()){
90 feature
= node
.getFeature();
91 termService
.delete(feature
.getUuid());
100 public DeleteResult
isDeletable(FeatureNode node
, FeatureNodeDeletionConfigurator config
){
101 DeleteResult result
= new DeleteResult();
102 Set
<CdmBase
> references
= commonService
.getReferencingObjectsForDeletion(node
);
103 for (CdmBase ref
:references
){
104 if (ref
instanceof FeatureNode
){
107 if (ref
instanceof FeatureTree
){
108 FeatureTree refTree
= HibernateProxyHelper
.deproxy(ref
, FeatureTree
.class);
109 if (node
.getFeatureTree().equals((refTree
))){
114 result
.addException(new ReferencedObjectUndeletableException("The featureNode is referenced by " + ref
.getUserFriendlyDescription() +" with id " +ref
.getId()));
124 public UpdateResult
moveFeatureNode(UUID movedNodeUuid
, UUID targetNodeUuid
, int position
) {
125 UpdateResult result
= new UpdateResult();
126 FeatureNode movedNode
= HibernateProxyHelper
.deproxy(dao
.load(movedNodeUuid
), FeatureNode
.class);
127 FeatureNode targetNode
= HibernateProxyHelper
.deproxy(dao
.load(targetNodeUuid
), FeatureNode
.class);
129 targetNode
.addChild(movedNode
);
132 targetNode
.addChild(movedNode
, position
);
134 result
.addUpdatedObject(targetNode
);
135 if(movedNode
.getParent()!=null){
136 result
.addUpdatedObject(movedNode
.getParent());
138 result
.setCdmEntity(targetNode
.getFeatureTree());
146 public UpdateResult
moveFeatureNode(UUID movedNodeUuid
, UUID targetNodeUuid
) {
147 return moveFeatureNode(movedNodeUuid
, targetNodeUuid
, -1);