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.FeatureNode;
|
28
|
import eu.etaxonomy.cdm.model.description.FeatureTree;
|
29
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
30
|
import eu.etaxonomy.cdm.persistence.dao.description.IFeatureNodeDao;
|
31
|
|
32
|
/**
|
33
|
* @author n.hoffmann
|
34
|
* @since 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
|
if (result.isOk()){
|
58
|
FeatureNode parent = node.getParent();
|
59
|
parent = HibernateProxyHelper.deproxy(parent, FeatureNode.class);
|
60
|
List<FeatureNode> children = new ArrayList(node.getChildNodes());
|
61
|
|
62
|
if (config.getChildHandling().equals(ChildHandling.DELETE)){
|
63
|
|
64
|
for (FeatureNode child: children){
|
65
|
deleteFeatureNode(child.getUuid(), config);
|
66
|
// node.removeChild(child);
|
67
|
}
|
68
|
if (parent != null){
|
69
|
parent.removeChild(node);
|
70
|
}
|
71
|
|
72
|
} else{
|
73
|
|
74
|
if (parent != null){
|
75
|
parent.removeChild(node);
|
76
|
for (FeatureNode child: children){
|
77
|
node.removeChild(child);
|
78
|
parent.addChild(child);
|
79
|
}
|
80
|
}else{
|
81
|
result.setAbort();
|
82
|
result.addException(new ReferencedObjectUndeletableException("The root node can not be deleted without its child nodes"));
|
83
|
return result;
|
84
|
}
|
85
|
}
|
86
|
|
87
|
dao.delete(node);
|
88
|
result.addDeletedObject(node);
|
89
|
if(parent!=null){
|
90
|
result.addUpdatedObject(parent);
|
91
|
}
|
92
|
if (config.isDeleteElement()){
|
93
|
DefinedTermBase term = node.getTerm();
|
94
|
termService.delete(term.getUuid());
|
95
|
result.addDeletedObject(term);
|
96
|
}
|
97
|
}
|
98
|
return result;
|
99
|
}
|
100
|
|
101
|
@Override
|
102
|
public UpdateResult createChildFeatureNode(FeatureNode node, DefinedTermBase term){
|
103
|
term = termService.save(term);
|
104
|
return addChildFeatureNode(node, term);
|
105
|
}
|
106
|
|
107
|
@Override
|
108
|
public UpdateResult addChildFeatureNode(FeatureNode node, DefinedTermBase featureChild){
|
109
|
return addChildFeatureNode(node, featureChild, 0);
|
110
|
}
|
111
|
|
112
|
@Override
|
113
|
public UpdateResult addChildFeatureNode(UUID nodeUUID, UUID termChildUuid){
|
114
|
return addChildFeatureNode(nodeUUID, termChildUuid, 0);
|
115
|
}
|
116
|
|
117
|
@Override
|
118
|
public UpdateResult addChildFeatureNode(UUID nodeUUID, UUID termChildUuid, int position){
|
119
|
FeatureNode node = load(nodeUUID);
|
120
|
DefinedTermBase child = HibernateProxyHelper.deproxy(termService.load(termChildUuid), DefinedTermBase.class);
|
121
|
return addChildFeatureNode(node, child, position);
|
122
|
}
|
123
|
|
124
|
@Override
|
125
|
public UpdateResult addChildFeatureNode(FeatureNode node, DefinedTermBase term, int position){
|
126
|
FeatureNode childNode = FeatureNode.NewInstance(term);
|
127
|
UpdateResult result = new UpdateResult();
|
128
|
if(position<0) {
|
129
|
node.addChild(childNode);
|
130
|
}
|
131
|
else{
|
132
|
node.addChild(childNode, position);
|
133
|
}
|
134
|
save(childNode);
|
135
|
result.addUpdatedObject(node);
|
136
|
result.setCdmEntity(childNode);
|
137
|
return result;
|
138
|
}
|
139
|
|
140
|
@Override
|
141
|
public DeleteResult isDeletable(FeatureNode node, FeatureNodeDeletionConfigurator config){
|
142
|
DeleteResult result = new DeleteResult();
|
143
|
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(node);
|
144
|
for (CdmBase ref:references){
|
145
|
if (ref instanceof FeatureNode){
|
146
|
break;
|
147
|
}
|
148
|
if (ref instanceof FeatureTree){
|
149
|
FeatureTree refTree = HibernateProxyHelper.deproxy(ref, FeatureTree.class);
|
150
|
if (node.getFeatureTree().equals((refTree))){
|
151
|
break;
|
152
|
}
|
153
|
}
|
154
|
result.setAbort();
|
155
|
result.addException(new ReferencedObjectUndeletableException("The featureNode is referenced by " + ref.getUserFriendlyDescription() +" with id " +ref.getId()));
|
156
|
|
157
|
}
|
158
|
return result;
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* {@inheritDoc}
|
163
|
*/
|
164
|
@Override
|
165
|
public UpdateResult moveFeatureNode(UUID movedNodeUuid, UUID targetNodeUuid, int position) {
|
166
|
UpdateResult result = new UpdateResult();
|
167
|
FeatureNode movedNode = HibernateProxyHelper.deproxy(load(movedNodeUuid), FeatureNode.class);
|
168
|
FeatureNode targetNode = HibernateProxyHelper.deproxy(load(targetNodeUuid), FeatureNode.class);
|
169
|
FeatureNode parent = HibernateProxyHelper.deproxy(movedNode.getParent(), FeatureNode.class);
|
170
|
if(position<0){
|
171
|
targetNode.addChild(movedNode);
|
172
|
}
|
173
|
else{
|
174
|
targetNode.addChild(movedNode, position);
|
175
|
}
|
176
|
result.addUpdatedObject(targetNode);
|
177
|
if(parent!=null){
|
178
|
result.addUpdatedObject(parent);
|
179
|
}
|
180
|
result.setCdmEntity(movedNode);
|
181
|
return result;
|
182
|
}
|
183
|
|
184
|
/**
|
185
|
* {@inheritDoc}
|
186
|
*/
|
187
|
@Override
|
188
|
public UpdateResult moveFeatureNode(UUID movedNodeUuid, UUID targetNodeUuid) {
|
189
|
return moveFeatureNode(movedNodeUuid, targetNodeUuid, -1);
|
190
|
}
|
191
|
|
192
|
}
|