ref #6694 Add service method to move FeatureNodes
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / FeatureNodeServiceImpl.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.api.service;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Set;
15 import java.util.UUID;
16
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;
21
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;
31
32 /**
33 * @author n.hoffmann
34 * @created Aug 5, 2010
35 * @version 1.0
36 */
37 @Service
38 @Transactional(readOnly = false)
39 public class FeatureNodeServiceImpl extends VersionableServiceBase<FeatureNode, IFeatureNodeDao> implements IFeatureNodeService {
40 private static final Logger logger = Logger.getLogger(FeatureNodeServiceImpl.class);
41
42 @Override
43 @Autowired
44 protected void setDao(IFeatureNodeDao dao) {
45 this.dao = dao;
46 }
47
48 @Autowired
49 private ITermService termService;
50
51 @Override
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);
57 Feature feature;
58 if (result.isOk()){
59 FeatureNode parent = node.getParent();
60 parent = HibernateProxyHelper.deproxy(parent, FeatureNode.class);
61 List<FeatureNode> children = new ArrayList(node.getChildNodes());
62
63 if (config.getChildHandling().equals(ChildHandling.DELETE)){
64
65 for (FeatureNode child: children){
66 deleteFeatureNode(child.getUuid(), config);
67 // node.removeChild(child);
68 }
69 if (parent != null){
70 parent.removeChild(node);
71 }
72
73 } else{
74
75 if (parent != null){
76 parent.removeChild(node);
77 for (FeatureNode child: children){
78 node.removeChild(child);
79 parent.addChild(child);
80 }
81 }else{
82 result.setAbort();
83 result.addException(new ReferencedObjectUndeletableException("The root node can not be deleted without its child nodes"));
84 return result;
85 }
86 }
87
88 dao.delete(node);
89 if (config.isDeleteElement()){
90 feature = node.getFeature();
91 termService.delete(feature.getUuid());
92 }
93 }
94
95
96 return result;
97 }
98
99 @Override
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){
105 break;
106 }
107 if (ref instanceof FeatureTree){
108 FeatureTree refTree = HibernateProxyHelper.deproxy(ref, FeatureTree.class);
109 if (node.getFeatureTree().equals((refTree))){
110 break;
111 }
112 }
113 result.setAbort();
114 result.addException(new ReferencedObjectUndeletableException("The featureNode is referenced by " + ref.getUserFriendlyDescription() +" with id " +ref.getId()));
115
116 }
117 return result;
118 }
119
120 /**
121 * {@inheritDoc}
122 */
123 @Override
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);
128 if(position<0){
129 targetNode.addChild(movedNode);
130 }
131 else{
132 targetNode.addChild(movedNode, position);
133 }
134 result.addUpdatedObject(targetNode);
135 if(movedNode.getParent()!=null){
136 result.addUpdatedObject(movedNode.getParent());
137 }
138 result.setCdmEntity(targetNode.getFeatureTree());
139 return result;
140 }
141
142 /**
143 * {@inheritDoc}
144 */
145 @Override
146 public UpdateResult moveFeatureNode(UUID movedNodeUuid, UUID targetNodeUuid) {
147 return moveFeatureNode(movedNodeUuid, targetNodeUuid, -1);
148 }
149
150 }