Project

General

Profile

Download (4.07 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.cdm.api.service;
12

    
13
import java.util.ArrayList;
14
import java.util.List;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import org.apache.log4j.Logger;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.stereotype.Service;
21
import org.springframework.transaction.annotation.Transactional;
22

    
23
import eu.etaxonomy.cdm.api.service.config.FeatureNodeDeletionConfigurator;
24
import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
25
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
26
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
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;
32

    
33
/**
34
 * @author n.hoffmann
35
 * @created Aug 5, 2010
36
 * @version 1.0
37
 */
38
@Service
39
@Transactional(readOnly = false)
40
public class FeatureNodeServiceImpl extends VersionableServiceBase<FeatureNode, IFeatureNodeDao> implements IFeatureNodeService {
41
	private static final Logger logger = Logger.getLogger(FeatureNodeServiceImpl.class);
42

    
43
	@Override
44
    @Autowired
45
	protected void setDao(IFeatureNodeDao dao) {
46
		this.dao = dao;
47
	}
48

    
49
	@Autowired
50
    private ITermService termService;
51

    
52
	 @Override
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);
58
	     Feature feature;
59
	     if (result.isOk()){
60
	         FeatureNode parent = node.getParent();
61
             parent = HibernateProxyHelper.deproxy(parent, FeatureNode.class);
62
	         List<FeatureNode> children = new ArrayList(node.getChildNodes());
63

    
64
	         if (config.getChildHandling().equals(ChildHandling.DELETE)){
65

    
66
	             for (FeatureNode child: children){
67
	                 deleteFeatureNode(child.getUuid(), config);
68
	                // node.removeChild(child);
69
	             }
70
	             if (parent != null){
71
	                 parent.removeChild(node);
72
	             }
73

    
74
	         } else{
75

    
76
	             if (parent != null){
77
	                 parent.removeChild(node);
78
	                 for (FeatureNode child: children){
79
	                     node.removeChild(child);
80
	                     parent.addChild(child);
81
	                 }
82
	             }else{
83
	                 result.setAbort();
84
	                 result.addException(new ReferencedObjectUndeletableException("The root node can not be deleted without its child nodes"));
85
	                 return result;
86
	             }
87
	         }
88

    
89
	         dao.delete(node);
90
	         if (config.isDeleteElement()){
91
                 feature = node.getFeature();
92
                 termService.delete(feature.getUuid());
93
             }
94
	     }
95

    
96

    
97
	     return result;
98
	 }
99

    
100
	 @Override
101
	 public DeleteResult isDeletable(FeatureNode node, FeatureNodeDeletionConfigurator config){
102
	     DeleteResult result = new DeleteResult();
103
	     Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(node);
104
	     for (CdmBase ref:references){
105
	         if (ref instanceof FeatureNode){
106
	             break;
107
	         }
108
	         if (ref instanceof FeatureTree){
109
	             FeatureTree refTree = HibernateProxyHelper.deproxy(ref, FeatureTree.class);
110
	             if (node.getFeatureTree().equals((refTree))){
111
	                 break;
112
	             }
113
	         }
114
	         result.setAbort();
115
	         result.addException(new ReferencedObjectUndeletableException("The featureNode is referenced by " + ref.getUserFriendlyDescription() +" with id " +ref.getId()));
116

    
117
	     }
118
	     return result;
119
	 }
120

    
121
}
(22-22/97)