1 |
1d36aa54
|
Andreas Müller
|
/**
|
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 |
f8d69f2a
|
Patric Plitzner
|
*/
|
9 |
1d36aa54
|
Andreas Müller
|
|
10 |
|
|
package eu.etaxonomy.cdm.api.service;
|
11 |
|
|
|
12 |
|
|
import java.util.ArrayList;
|
13 |
bf692139
|
Katja Luther
|
import java.util.Iterator;
|
14 |
1d36aa54
|
Andreas Müller
|
import java.util.List;
|
15 |
bf692139
|
Katja Luther
|
import java.util.Set;
|
16 |
1d36aa54
|
Andreas Müller
|
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 |
bf692139
|
Katja Luther
|
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
|
23 |
|
|
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
|
24 |
39f75c90
|
Andreas Kohlbecker
|
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 |
9138eea0
|
Andreas Müller
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
28 |
bf692139
|
Katja Luther
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
29 |
|
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
30 |
1d36aa54
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.description.PolytomousKey;
|
31 |
6ae5ca99
|
Katja Luther
|
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
|
32 |
39f75c90
|
Andreas Kohlbecker
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
33 |
|
|
import eu.etaxonomy.cdm.persistence.dao.description.IIdentificationKeyDao;
|
34 |
1d36aa54
|
Andreas Müller
|
import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyDao;
|
35 |
39f75c90
|
Andreas Kohlbecker
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
|
36 |
f197a589
|
Andreas Müller
|
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
|
37 |
1d36aa54
|
Andreas Müller
|
|
38 |
|
|
@Service
|
39 |
77b4a247
|
Andreas Kohlbecker
|
@Transactional(readOnly = false)
|
40 |
1d36aa54
|
Andreas Müller
|
public class PolytomousKeyServiceImpl extends IdentifiableServiceBase<PolytomousKey, IPolytomousKeyDao> implements IPolytomousKeyService {
|
41 |
|
|
|
42 |
39f75c90
|
Andreas Kohlbecker
|
private IIdentificationKeyDao identificationKeyDao;
|
43 |
|
|
private ITaxonDao taxonDao;
|
44 |
bf692139
|
Katja Luther
|
@Autowired
|
45 |
|
|
private IPolytomousKeyNodeService nodeService;
|
46 |
f8d69f2a
|
Patric Plitzner
|
|
47 |
39f75c90
|
Andreas Kohlbecker
|
|
48 |
f8d69f2a
|
Patric Plitzner
|
@Override
|
49 |
|
|
@Autowired
|
50 |
1d36aa54
|
Andreas Müller
|
protected void setDao(IPolytomousKeyDao dao) {
|
51 |
|
|
this.dao = dao;
|
52 |
|
|
}
|
53 |
f8d69f2a
|
Patric Plitzner
|
|
54 |
39f75c90
|
Andreas Kohlbecker
|
@Autowired
|
55 |
|
|
protected void setDao(IIdentificationKeyDao identificationKeyDao) {
|
56 |
|
|
this.identificationKeyDao = identificationKeyDao;
|
57 |
|
|
}
|
58 |
f8d69f2a
|
Patric Plitzner
|
|
59 |
39f75c90
|
Andreas Kohlbecker
|
@Autowired
|
60 |
|
|
protected void setDao(ITaxonDao taxonDao) {
|
61 |
|
|
this.taxonDao = taxonDao;
|
62 |
|
|
}
|
63 |
1d36aa54
|
Andreas Müller
|
|
64 |
bf692139
|
Katja Luther
|
|
65 |
6ae5ca99
|
Katja Luther
|
|
66 |
f197a589
|
Andreas Müller
|
|
67 |
1d36aa54
|
Andreas Müller
|
/* (non-Javadoc)
|
68 |
f197a589
|
Andreas Müller
|
* @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache(java.lang.Integer, eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy)
|
69 |
1d36aa54
|
Andreas Müller
|
*/
|
70 |
|
|
@Override
|
71 |
f197a589
|
Andreas Müller
|
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 |
1d36aa54
|
Andreas Müller
|
}
|
77 |
f8d69f2a
|
Patric Plitzner
|
|
78 |
1d36aa54
|
Andreas Müller
|
|
79 |
|
|
/* (non-Javadoc)
|
80 |
|
|
* @see eu.etaxonomy.cdm.api.service.IFeatureTreeService#loadWithNodes(java.util.UUID, java.util.List, java.util.List)
|
81 |
|
|
*/
|
82 |
f8d69f2a
|
Patric Plitzner
|
@Override
|
83 |
|
|
public PolytomousKey loadWithNodes(UUID uuid, List<String> propertyPaths, List<String> nodePaths) {
|
84 |
|
|
|
85 |
6acf564a
|
Andreas Kohlbecker
|
if(nodePaths == null){
|
86 |
|
|
nodePaths = new ArrayList<String>();
|
87 |
|
|
}
|
88 |
1d36aa54
|
Andreas Müller
|
nodePaths.add("children");
|
89 |
f8d69f2a
|
Patric Plitzner
|
|
90 |
1d36aa54
|
Andreas Müller
|
List<String> rootPaths = new ArrayList<String>();
|
91 |
|
|
rootPaths.add("root");
|
92 |
|
|
for(String path : nodePaths) {
|
93 |
|
|
rootPaths.add("root." + path);
|
94 |
|
|
}
|
95 |
f8d69f2a
|
Patric Plitzner
|
|
96 |
|
|
if(propertyPaths != null) {
|
97 |
1d36aa54
|
Andreas Müller
|
rootPaths.addAll(propertyPaths);
|
98 |
|
|
}
|
99 |
f8d69f2a
|
Patric Plitzner
|
|
100 |
1d36aa54
|
Andreas Müller
|
PolytomousKey polytomousKey = load(uuid, rootPaths);
|
101 |
|
|
dao.loadNodes(polytomousKey.getRoot(),nodePaths);
|
102 |
|
|
return polytomousKey;
|
103 |
|
|
}
|
104 |
f8d69f2a
|
Patric Plitzner
|
|
105 |
1d36aa54
|
Andreas Müller
|
/**
|
106 |
|
|
* Returns the polytomous key specified by the given <code>uuid</code>.
|
107 |
f8d69f2a
|
Patric Plitzner
|
*
|
108 |
1d36aa54
|
Andreas Müller
|
* @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 |
39f75c90
|
Andreas Kohlbecker
|
|
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 |
772393b4
|
l.morris
|
Integer pageNumber, List<String> propertyPaths, List<String> nodePaths) {
|
123 |
f8d69f2a
|
Patric Plitzner
|
|
124 |
39f75c90
|
Andreas Kohlbecker
|
List<PolytomousKey> list = new ArrayList<PolytomousKey>();
|
125 |
353fbf79
|
Andreas Kohlbecker
|
Long numberOfResults = identificationKeyDao.countByTaxonomicScope(taxon.getUuid(), PolytomousKey.class);
|
126 |
39f75c90
|
Andreas Kohlbecker
|
if(AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)){
|
127 |
353fbf79
|
Andreas Kohlbecker
|
list = identificationKeyDao.findByTaxonomicScope(taxon.getUuid(), PolytomousKey.class, pageSize, pageNumber, propertyPaths);
|
128 |
39f75c90
|
Andreas Kohlbecker
|
}
|
129 |
772393b4
|
l.morris
|
if (nodePaths != null) {
|
130 |
|
|
for (PolytomousKey polytomousKey : list) {
|
131 |
f8d69f2a
|
Patric Plitzner
|
dao.loadNodes(polytomousKey.getRoot(), nodePaths);
|
132 |
772393b4
|
l.morris
|
}
|
133 |
|
|
}
|
134 |
396a7563
|
Andreas Kohlbecker
|
Pager<PolytomousKey> pager = new DefaultPagerImpl<PolytomousKey>(pageNumber, numberOfResults, pageSize, list);
|
135 |
772393b4
|
l.morris
|
|
136 |
39f75c90
|
Andreas Kohlbecker
|
return pager;
|
137 |
|
|
}
|
138 |
9138eea0
|
Andreas Müller
|
|
139 |
5901681d
|
Cherian Mathew
|
@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 |
9a23effa
|
Cherian Mathew
|
|
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 |
6ae5ca99
|
Katja Luther
|
@Override
|
158 |
|
|
public DeleteResult delete(PolytomousKey key){
|
159 |
bf692139
|
Katja Luther
|
//DeleteResult result = new DeleteResult();
|
160 |
6ae5ca99
|
Katja Luther
|
PolytomousKeyNode root = key.getRoot();
|
161 |
bf692139
|
Katja Luther
|
|
162 |
|
|
|
163 |
645658b9
|
Katja Luther
|
DeleteResult result = isDeletable(key.getUuid(), null);
|
164 |
060676fe
|
Katja Luther
|
DeleteResult resultRoot = new DeleteResult();
|
165 |
bf692139
|
Katja Luther
|
if (result.isOk()){
|
166 |
|
|
try{
|
167 |
af4cc806
|
Katja Luther
|
if (root != null){
|
168 |
060676fe
|
Katja Luther
|
// root.setKey(null);
|
169 |
|
|
resultRoot = nodeService.delete(root.getUuid(), true);
|
170 |
|
|
|
171 |
af4cc806
|
Katja Luther
|
}
|
172 |
bf692139
|
Katja Luther
|
}catch(Exception e){
|
173 |
|
|
result.addException(e);
|
174 |
|
|
result.setAbort();
|
175 |
|
|
return result;
|
176 |
|
|
}
|
177 |
|
|
try{
|
178 |
060676fe
|
Katja Luther
|
if (resultRoot.isOk()){
|
179 |
|
|
dao.delete(key);
|
180 |
815a9015
|
Katja Luther
|
result.addDeletedObject(key);
|
181 |
|
|
result.includeResult(resultRoot);
|
182 |
060676fe
|
Katja Luther
|
}
|
183 |
bf692139
|
Katja Luther
|
}catch(Exception e){
|
184 |
|
|
result.addException(e);
|
185 |
|
|
result.setAbort();
|
186 |
|
|
return result;
|
187 |
|
|
}
|
188 |
6ae5ca99
|
Katja Luther
|
}
|
189 |
1128808d
|
Cherian Mathew
|
return result;
|
190 |
6ae5ca99
|
Katja Luther
|
}
|
191 |
|
|
|
192 |
bf692139
|
Katja Luther
|
@Override
|
193 |
645658b9
|
Katja Luther
|
public DeleteResult isDeletable(UUID keyUuid, DeleteConfiguratorBase config){
|
194 |
bf692139
|
Katja Luther
|
DeleteResult result = new DeleteResult();
|
195 |
645658b9
|
Katja Luther
|
PolytomousKey key = this.load(keyUuid);
|
196 |
bf692139
|
Katja Luther
|
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(key);
|
197 |
|
|
if (references != null){
|
198 |
|
|
Iterator<CdmBase> iterator = references.iterator();
|
199 |
|
|
CdmBase ref;
|
200 |
|
|
while (iterator.hasNext()){
|
201 |
|
|
ref = iterator.next();
|
202 |
|
|
if ((ref instanceof PolytomousKeyNode) ){
|
203 |
|
|
PolytomousKeyNode node = HibernateProxyHelper.deproxy(ref, PolytomousKeyNode.class);
|
204 |
|
|
if (!node.getKey().equals(key)){
|
205 |
|
|
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. " ;
|
206 |
|
|
result.addException(new ReferencedObjectUndeletableException(message));
|
207 |
|
|
result.setAbort();
|
208 |
|
|
result.addRelatedObject(ref);
|
209 |
|
|
}
|
210 |
|
|
}
|
211 |
|
|
}
|
212 |
|
|
}
|
213 |
|
|
return result;
|
214 |
|
|
}
|
215 |
|
|
|
216 |
1d36aa54
|
Andreas Müller
|
}
|