Revision bf692139
Added by Katja Luther over 7 years ago
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/PolytomousKeyServiceImpl.java | ||
---|---|---|
10 | 10 |
package eu.etaxonomy.cdm.api.service; |
11 | 11 |
|
12 | 12 |
import java.util.ArrayList; |
13 |
import java.util.Iterator; |
|
13 | 14 |
import java.util.List; |
15 |
import java.util.Set; |
|
14 | 16 |
import java.util.UUID; |
15 | 17 |
|
16 | 18 |
import org.springframework.beans.factory.annotation.Autowired; |
17 | 19 |
import org.springframework.stereotype.Service; |
18 | 20 |
import org.springframework.transaction.annotation.Transactional; |
19 | 21 |
|
22 |
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase; |
|
23 |
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException; |
|
20 | 24 |
import eu.etaxonomy.cdm.api.service.pager.Pager; |
21 | 25 |
import eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl; |
22 | 26 |
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl; |
23 | 27 |
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor; |
28 |
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper; |
|
29 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
|
24 | 30 |
import eu.etaxonomy.cdm.model.description.PolytomousKey; |
25 | 31 |
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode; |
26 | 32 |
import eu.etaxonomy.cdm.model.taxon.TaxonBase; |
27 | 33 |
import eu.etaxonomy.cdm.persistence.dao.description.IIdentificationKeyDao; |
28 | 34 |
import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyDao; |
29 |
import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyNodeDao; |
|
30 | 35 |
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao; |
31 | 36 |
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy; |
32 | 37 |
|
... | ... | |
36 | 41 |
|
37 | 42 |
private IIdentificationKeyDao identificationKeyDao; |
38 | 43 |
private ITaxonDao taxonDao; |
39 |
private IPolytomousKeyNodeDao nodeDao; |
|
44 |
@Autowired |
|
45 |
private IPolytomousKeyNodeService nodeService; |
|
40 | 46 |
|
41 | 47 |
|
42 | 48 |
@Override |
... | ... | |
55 | 61 |
this.taxonDao = taxonDao; |
56 | 62 |
} |
57 | 63 |
|
58 |
@Autowired |
|
59 |
protected void setDao(IPolytomousKeyNodeDao nodeDao) { |
|
60 |
this.nodeDao = nodeDao; |
|
61 |
} |
|
64 |
|
|
62 | 65 |
|
63 | 66 |
|
64 | 67 |
/* (non-Javadoc) |
... | ... | |
153 | 156 |
|
154 | 157 |
@Override |
155 | 158 |
public DeleteResult delete(PolytomousKey key){ |
156 |
DeleteResult result = new DeleteResult(); |
|
159 |
//DeleteResult result = new DeleteResult();
|
|
157 | 160 |
PolytomousKeyNode root = key.getRoot(); |
158 |
key.setRoot(null); |
|
159 |
try{ |
|
160 |
nodeDao.delete(root); |
|
161 |
}catch(Exception e){ |
|
162 |
result.addException(e); |
|
163 |
result.setAbort(); |
|
164 |
return result; |
|
161 |
|
|
162 |
|
|
163 |
DeleteResult result = isDeletable(key, null); |
|
164 |
|
|
165 |
if (result.isOk()){ |
|
166 |
try{ |
|
167 |
nodeService.delete(root.getUuid(), true); |
|
168 |
}catch(Exception e){ |
|
169 |
result.addException(e); |
|
170 |
result.setAbort(); |
|
171 |
return result; |
|
172 |
} |
|
173 |
try{ |
|
174 |
dao.delete(key); |
|
175 |
}catch(Exception e){ |
|
176 |
result.addException(e); |
|
177 |
result.setAbort(); |
|
178 |
return result; |
|
179 |
} |
|
165 | 180 |
} |
166 |
try{ |
|
167 |
dao.delete(key); |
|
168 |
}catch(Exception e){ |
|
169 |
result.addException(e); |
|
170 |
result.setAbort(); |
|
171 |
return result; |
|
172 |
} |
|
173 | 181 |
return result; |
174 | 182 |
} |
175 | 183 |
|
184 |
@Override |
|
185 |
public DeleteResult isDeletable(PolytomousKey key, DeleteConfiguratorBase config){ |
|
186 |
DeleteResult result = new DeleteResult(); |
|
187 |
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(key); |
|
188 |
if (references != null){ |
|
189 |
Iterator<CdmBase> iterator = references.iterator(); |
|
190 |
CdmBase ref; |
|
191 |
while (iterator.hasNext()){ |
|
192 |
ref = iterator.next(); |
|
193 |
if ((ref instanceof PolytomousKeyNode) ){ |
|
194 |
PolytomousKeyNode node = HibernateProxyHelper.deproxy(ref, PolytomousKeyNode.class); |
|
195 |
if (!node.getKey().equals(key)){ |
|
196 |
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. " ; |
|
197 |
result.addException(new ReferencedObjectUndeletableException(message)); |
|
198 |
result.setAbort(); |
|
199 |
result.addRelatedObject(ref); |
|
200 |
} |
|
201 |
} |
|
202 |
} |
|
203 |
} |
|
204 |
return result; |
|
205 |
} |
|
206 |
|
|
176 | 207 |
} |
Also available in: Unified diff
fix delete of polytomous keys