Project

General

Profile

Download (6.82 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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.Iterator;
14
import java.util.List;
15
import java.util.Set;
16
import java.util.UUID;
17

    
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.DeleteConfiguratorBase;
23
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
24
import eu.etaxonomy.cdm.api.service.pager.Pager;
25
import eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl;
26
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
27
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
28
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
29
import eu.etaxonomy.cdm.model.common.CdmBase;
30
import eu.etaxonomy.cdm.model.description.PolytomousKey;
31
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
32
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
33
import eu.etaxonomy.cdm.persistence.dao.description.IIdentificationKeyDao;
34
import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyDao;
35
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
36

    
37
@Service
38
@Transactional(readOnly = false)
39
public class PolytomousKeyServiceImpl extends IdentifiableServiceBase<PolytomousKey, IPolytomousKeyDao> implements IPolytomousKeyService {
40

    
41
	private IIdentificationKeyDao identificationKeyDao;
42

    
43
	@Autowired
44
	private IPolytomousKeyNodeService nodeService;
45

    
46

    
47
	@Override
48
    @Autowired
49
	protected void setDao(IPolytomousKeyDao dao) {
50
		this.dao = dao;
51
	}
52

    
53
	@Autowired
54
	protected void setDao(IIdentificationKeyDao identificationKeyDao) {
55
		this.identificationKeyDao = identificationKeyDao;
56
	}
57

    
58
	@Override
59
	public UpdateResult updateCaches(Class<? extends PolytomousKey> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<PolytomousKey> cacheStrategy, IProgressMonitor monitor) {
60
		if (clazz == null){
61
			clazz = PolytomousKey.class;
62
		}
63
		return super.updateCachesImpl(clazz, stepSize, cacheStrategy, monitor);
64
	}
65

    
66
	@Override
67
    public PolytomousKey loadWithNodes(UUID uuid, List<String> propertyPaths, List<String> nodePaths) {
68

    
69
		if(nodePaths == null){
70
			nodePaths = new ArrayList<String>();
71
		}
72
		nodePaths.add("children");
73

    
74
		List<String> rootPaths = new ArrayList<String>();
75
		rootPaths.add("root");
76
		for(String path : nodePaths) {
77
			rootPaths.add("root." + path);
78
		}
79

    
80
		if(propertyPaths != null) {
81
		    rootPaths.addAll(propertyPaths);
82
		}
83

    
84
		PolytomousKey polytomousKey = load(uuid, rootPaths);
85
		dao.loadNodes(polytomousKey.getRoot(),nodePaths);
86
		return polytomousKey;
87
	}
88

    
89
	/**
90
	 * Returns the polytomous key specified by the given <code>uuid</code>.
91
	 *
92
	 * @see eu.etaxonomy.cdm.api.service.ServiceBase#load(java.util.UUID, java.util.List)
93
	 */
94
	@Override
95
	public PolytomousKey load(UUID uuid, List<String> propertyPaths) {
96
		return super.load(uuid, propertyPaths);
97
	}
98

    
99
	@Override
100
	public Pager<PolytomousKey> findByTaxonomicScope(
101
			TaxonBase taxon, Integer pageSize,
102
			Integer pageNumber, List<String> propertyPaths, List<String> nodePaths) {
103

    
104
		List<PolytomousKey> list = new ArrayList<PolytomousKey>();
105
		Long numberOfResults = identificationKeyDao.countByTaxonomicScope(taxon.getUuid(), PolytomousKey.class);
106
		if(AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)){
107
			list = identificationKeyDao.findByTaxonomicScope(taxon.getUuid(), PolytomousKey.class, pageSize, pageNumber, propertyPaths);
108
		}
109
		if (nodePaths != null) {
110
			for (PolytomousKey polytomousKey : list) {
111
				dao.loadNodes(polytomousKey.getRoot(), nodePaths);
112
			}
113
		}
114
		Pager<PolytomousKey> pager = new DefaultPagerImpl<PolytomousKey>(pageNumber, numberOfResults, pageSize, list);
115

    
116
		return pager;
117
	}
118

    
119
	@Override
120
	public UpdateResult updateAllNodeNumberings(UUID polytomousKeyUuid) {
121
	    UpdateResult result = new UpdateResult();
122
	    PolytomousKey polytomousKey = dao.load(polytomousKeyUuid);
123
	    polytomousKey.getRoot().refreshNodeNumbering();
124
	    return result;
125
	}
126

    
127
	@Override
128
	public UpdateResult updateAllNodeNumberings() {
129
	    UpdateResult result = new UpdateResult();
130
	    List<PolytomousKey> polytomousKeys = dao.list();
131
	    for(PolytomousKey polytomousKey : polytomousKeys) {
132
	        polytomousKey.getRoot().refreshNodeNumbering();
133
	    }
134
	    return result;
135
	}
136

    
137
	@Override
138
	public DeleteResult delete(PolytomousKey key){
139
	    //DeleteResult result = new DeleteResult();
140
	    PolytomousKeyNode root = key.getRoot();
141

    
142

    
143
	    DeleteResult result = isDeletable(key.getUuid(), null);
144
	    DeleteResult resultRoot = new DeleteResult();
145
	    if (result.isOk()){
146
    	    try{
147
    	        if (root != null){
148
    	           // root.setKey(null);
149
    	            resultRoot = nodeService.delete(root.getUuid(), true);
150

    
151
    	        }
152
    	    }catch(Exception e){
153
    	        result.addException(e);
154
    	        result.setAbort();
155
    	        return result;
156
    	    }
157
    	    try{
158
    	        if (resultRoot.isOk()){
159
    	            dao.delete(key);
160
    	            result.addDeletedObject(key);
161
    	            result.includeResult(resultRoot);
162
    	        }
163
    	    }catch(Exception e){
164
                result.addException(e);
165
                result.setAbort();
166
                return result;
167
            }
168
	    }
169
        return result;
170
	}
171

    
172
	@Override
173
    public DeleteResult isDeletable(UUID keyUuid, DeleteConfiguratorBase config){
174
        DeleteResult result = new DeleteResult();
175
        PolytomousKey key = this.load(keyUuid);
176
        Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(key);
177
        if (references != null){
178
           Iterator<CdmBase> iterator = references.iterator();
179
           CdmBase ref;
180
           while (iterator.hasNext()){
181
               ref = iterator.next();
182
               if ((ref instanceof PolytomousKeyNode) ){
183
                   PolytomousKeyNode node = HibernateProxyHelper.deproxy(ref, PolytomousKeyNode.class);
184
                   if (!node.getKey().equals(key)){
185
                       String message = "The key is a subkey of " + node.getKey() + ", referenced in node with id: " + node.getId() + ". Please remove the subkey reference first and then delete the key. " ;
186
                       result.addException(new ReferencedObjectUndeletableException(message));
187
                       result.setAbort();
188
                       result.addRelatedObject(ref);
189
                   }
190
               }
191
           }
192
        }
193
        return result;
194
    }
195

    
196
}
(76-76/97)