Project

General

Profile

Download (7.25 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.persistence.dao.taxon.ITaxonDao;
36
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
37

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

    
42
	private IIdentificationKeyDao identificationKeyDao;
43
	private ITaxonDao taxonDao;
44
	@Autowired
45
	private IPolytomousKeyNodeService nodeService;
46

    
47

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

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

    
59
	@Autowired
60
	protected void setDao(ITaxonDao taxonDao) {
61
		this.taxonDao = taxonDao;
62
	}
63

    
64

    
65

    
66

    
67
	/* (non-Javadoc)
68
	 * @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache(java.lang.Integer, eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy)
69
	 */
70
	@Override
71
	public void updateTitleCache(Class<? extends PolytomousKey> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<PolytomousKey> cacheStrategy, IProgressMonitor monitor) {
72
		if (clazz == null){
73
			clazz = PolytomousKey.class;
74
		}
75
		super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
76
	}
77

    
78

    
79
	/* (non-Javadoc)
80
	 * @see eu.etaxonomy.cdm.api.service.IFeatureTreeService#loadWithNodes(java.util.UUID, java.util.List, java.util.List)
81
	 */
82
	@Override
83
    public PolytomousKey loadWithNodes(UUID uuid, List<String> propertyPaths, List<String> nodePaths) {
84

    
85
		if(nodePaths == null){
86
			nodePaths = new ArrayList<String>();
87
		}
88
		nodePaths.add("children");
89

    
90
		List<String> rootPaths = new ArrayList<String>();
91
		rootPaths.add("root");
92
		for(String path : nodePaths) {
93
			rootPaths.add("root." + path);
94
		}
95

    
96
		if(propertyPaths != null) {
97
		    rootPaths.addAll(propertyPaths);
98
		}
99

    
100
		PolytomousKey polytomousKey = load(uuid, rootPaths);
101
		dao.loadNodes(polytomousKey.getRoot(),nodePaths);
102
		return polytomousKey;
103
	}
104

    
105
	/**
106
	 * Returns the polytomous key specified by the given <code>uuid</code>.
107
	 *
108
	 * @see eu.etaxonomy.cdm.api.service.ServiceBase#load(java.util.UUID, java.util.List)
109
	 */
110
	@Override
111
	public PolytomousKey load(UUID uuid, List<String> propertyPaths) {
112
		return super.load(uuid, propertyPaths);
113
	}
114

    
115

    
116
	/* (non-Javadoc)
117
	 * @see eu.etaxonomy.cdm.api.service.IPolytomousKeyService#findByTaxonomicScope(eu.etaxonomy.cdm.model.taxon.TaxonBase, java.lang.Class, java.lang.Integer, java.lang.Integer, java.util.List)
118
	 */
119
	@Override
120
	public Pager<PolytomousKey> findByTaxonomicScope(
121
			TaxonBase taxon, Integer pageSize,
122
			Integer pageNumber, List<String> propertyPaths, List<String> nodePaths) {
123

    
124
		List<PolytomousKey> list = new ArrayList<PolytomousKey>();
125
		Long numberOfResults = identificationKeyDao.countByTaxonomicScope(taxon.getUuid(), PolytomousKey.class);
126
		if(AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)){
127
			list = identificationKeyDao.findByTaxonomicScope(taxon.getUuid(), PolytomousKey.class, pageSize, pageNumber, propertyPaths);
128
		}
129
		if (nodePaths != null) {
130
			for (PolytomousKey polytomousKey : list) {
131
				dao.loadNodes(polytomousKey.getRoot(), nodePaths);
132
			}
133
		}
134
		Pager<PolytomousKey> pager = new DefaultPagerImpl<PolytomousKey>(pageNumber, numberOfResults.intValue(), pageSize, list);
135

    
136
		return pager;
137
	}
138

    
139
	@Override
140
	public UpdateResult updateAllNodeNumberings(UUID polytomousKeyUuid) {
141
	    UpdateResult result = new UpdateResult();
142
	    PolytomousKey polytomousKey = dao.load(polytomousKeyUuid);
143
	    polytomousKey.getRoot().refreshNodeNumbering();
144
	    return result;
145
	}
146

    
147
	@Override
148
	public UpdateResult updateAllNodeNumberings() {
149
	    UpdateResult result = new UpdateResult();
150
	    List<PolytomousKey> polytomousKeys = dao.list();
151
	    for(PolytomousKey polytomousKey : polytomousKeys) {
152
	        polytomousKey.getRoot().refreshNodeNumbering();
153
	    }
154
	    return result;
155
	}
156

    
157
	@Override
158
	public DeleteResult delete(PolytomousKey key){
159
	    //DeleteResult result = new DeleteResult();
160
	    PolytomousKeyNode root = key.getRoot();
161

    
162

    
163
	    DeleteResult result = isDeletable(key, null);
164

    
165
	    if (result.isOk()){
166
    	    try{
167
    	        if (root != null){
168
    	            nodeService.delete(root.getUuid(), true);
169
    	        }
170
    	    }catch(Exception e){
171
    	        result.addException(e);
172
    	        result.setAbort();
173
    	        return result;
174
    	    }
175
    	    try{
176
    	        dao.delete(key);
177
    	    }catch(Exception e){
178
                result.addException(e);
179
                result.setAbort();
180
                return result;
181
            }
182
	    }
183
        return result;
184
	}
185

    
186
	@Override
187
    public DeleteResult isDeletable(PolytomousKey key, DeleteConfiguratorBase config){
188
        DeleteResult result = new DeleteResult();
189
        Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(key);
190
        if (references != null){
191
           Iterator<CdmBase> iterator = references.iterator();
192
           CdmBase ref;
193
           while (iterator.hasNext()){
194
               ref = iterator.next();
195
               if ((ref instanceof PolytomousKeyNode) ){
196
                   PolytomousKeyNode node = HibernateProxyHelper.deproxy(ref, PolytomousKeyNode.class);
197
                   if (!node.getKey().equals(key)){
198
                       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. " ;
199
                       result.addException(new ReferencedObjectUndeletableException(message));
200
                       result.setAbort();
201
                       result.addRelatedObject(ref);
202
                   }
203
               }
204
           }
205
        }
206
        return result;
207
    }
208

    
209
}
(80-80/98)