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
|
result.addDeletedObject(node);
|
90
|
if(parent!=null){
|
91
|
result.addUpdatedObject(parent);
|
92
|
}
|
93
|
if (config.isDeleteElement()){
|
94
|
feature = node.getFeature();
|
95
|
termService.delete(feature.getUuid());
|
96
|
result.addDeletedObject(feature);
|
97
|
}
|
98
|
}
|
99
|
return result;
|
100
|
}
|
101
|
|
102
|
@Override
|
103
|
public UpdateResult addChildFeatureNode(FeatureNode node, Feature featureChild){
|
104
|
UpdateResult result = new UpdateResult();
|
105
|
FeatureNode childNode = FeatureNode.NewInstance(featureChild);
|
106
|
node.addChild(childNode);
|
107
|
result.addUpdatedObject(node);
|
108
|
return result;
|
109
|
}
|
110
|
|
111
|
@Override
|
112
|
public UpdateResult addChildFeatureNode(UUID nodeUUID, UUID featureChildUuid){
|
113
|
FeatureNode node = load(nodeUUID);
|
114
|
Feature child = HibernateProxyHelper.deproxy(termService.load(featureChildUuid), Feature.class);
|
115
|
return addChildFeatureNode(node, child);
|
116
|
}
|
117
|
|
118
|
@Override
|
119
|
public DeleteResult isDeletable(FeatureNode node, FeatureNodeDeletionConfigurator config){
|
120
|
DeleteResult result = new DeleteResult();
|
121
|
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(node);
|
122
|
for (CdmBase ref:references){
|
123
|
if (ref instanceof FeatureNode){
|
124
|
break;
|
125
|
}
|
126
|
if (ref instanceof FeatureTree){
|
127
|
FeatureTree refTree = HibernateProxyHelper.deproxy(ref, FeatureTree.class);
|
128
|
if (node.getFeatureTree().equals((refTree))){
|
129
|
break;
|
130
|
}
|
131
|
}
|
132
|
result.setAbort();
|
133
|
result.addException(new ReferencedObjectUndeletableException("The featureNode is referenced by " + ref.getUserFriendlyDescription() +" with id " +ref.getId()));
|
134
|
|
135
|
}
|
136
|
return result;
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* {@inheritDoc}
|
141
|
*/
|
142
|
@Override
|
143
|
public UpdateResult moveFeatureNode(UUID movedNodeUuid, UUID targetNodeUuid, int position) {
|
144
|
UpdateResult result = new UpdateResult();
|
145
|
FeatureNode movedNode = HibernateProxyHelper.deproxy(load(movedNodeUuid), FeatureNode.class);
|
146
|
FeatureNode targetNode = HibernateProxyHelper.deproxy(load(targetNodeUuid), FeatureNode.class);
|
147
|
FeatureNode parent = HibernateProxyHelper.deproxy(movedNode.getParent(), FeatureNode.class);
|
148
|
if(position<0){
|
149
|
targetNode.addChild(movedNode);
|
150
|
}
|
151
|
else{
|
152
|
targetNode.addChild(movedNode, position);
|
153
|
}
|
154
|
result.addUpdatedObject(targetNode);
|
155
|
if(parent!=null){
|
156
|
result.addUpdatedObject(parent);
|
157
|
}
|
158
|
result.setCdmEntity(targetNode.getFeatureTree());
|
159
|
return result;
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* {@inheritDoc}
|
164
|
*/
|
165
|
@Override
|
166
|
public UpdateResult moveFeatureNode(UUID movedNodeUuid, UUID targetNodeUuid) {
|
167
|
return moveFeatureNode(movedNodeUuid, targetNodeUuid, -1);
|
168
|
}
|
169
|
|
170
|
}
|