Revision 8f6bbcc2
Added by Katja Luther over 8 years ago
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/TaxonNodeServiceImpl.java | ||
---|---|---|
35 | 35 |
import eu.etaxonomy.cdm.model.name.TaxonNameBase; |
36 | 36 |
import eu.etaxonomy.cdm.model.reference.Reference; |
37 | 37 |
import eu.etaxonomy.cdm.model.taxon.Classification; |
38 |
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode; |
|
39 | 38 |
import eu.etaxonomy.cdm.model.taxon.Synonym; |
40 | 39 |
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship; |
41 | 40 |
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType; |
... | ... | |
118 | 117 |
} |
119 | 118 |
|
120 | 119 |
|
121 |
|
|
120 |
Classification classification = oldTaxonNode.getClassification(); |
|
122 | 121 |
Taxon oldTaxon = (Taxon) HibernateProxyHelper.deproxy(oldTaxonNode.getTaxon()); |
123 | 122 |
Taxon newAcceptedTaxon = (Taxon)this.taxonService.load(newAcceptedTaxonNode.getTaxon().getUuid()); |
124 | 123 |
// Move oldTaxon to newTaxon |
... | ... | |
240 | 239 |
conf.setDeleteNameIfPossible(false); |
241 | 240 |
|
242 | 241 |
if (result.isOk()){ |
243 |
result = taxonService.deleteTaxon(oldTaxon.getUuid(), conf, null);
|
|
242 |
result = taxonService.deleteTaxon(oldTaxon.getUuid(), conf, classification.getUuid());
|
|
244 | 243 |
}else{ |
245 | 244 |
result.setStatus(Status.OK); |
246 | 245 |
TaxonNodeDeletionConfigurator config = new TaxonNodeDeletionConfigurator(); |
... | ... | |
288 | 287 |
*/ |
289 | 288 |
@Override |
290 | 289 |
@Transactional(readOnly = false) |
291 |
public DeleteResult deleteTaxonNodes(Set<ITaxonTreeNode> nodes, TaxonDeletionConfigurator config) {
|
|
290 |
public DeleteResult deleteTaxonNodes(List<TaxonNode> list, TaxonDeletionConfigurator config) {
|
|
292 | 291 |
|
293 | 292 |
if (config == null){ |
294 | 293 |
config = new TaxonDeletionConfigurator(); |
... | ... | |
296 | 295 |
DeleteResult result = new DeleteResult(); |
297 | 296 |
List<UUID> deletedUUIDs = new ArrayList<UUID>(); |
298 | 297 |
Classification classification = null; |
299 |
for (ITaxonTreeNode treeNode:nodes){ |
|
298 |
List<TaxonNode> taxonNodes = new ArrayList<TaxonNode>(list); |
|
299 |
for (TaxonNode treeNode:taxonNodes){ |
|
300 | 300 |
if (treeNode != null){ |
301 |
if (treeNode instanceof TaxonNode){ |
|
302 |
TaxonNode taxonNode; |
|
303 |
taxonNode = HibernateProxyHelper.deproxy(treeNode, TaxonNode.class); |
|
304 |
TaxonNode parent = taxonNode.getParent(); |
|
305 |
//check whether the node has children or the children are already deleted |
|
306 |
if(taxonNode.hasChildNodes()) { |
|
307 |
Set<ITaxonTreeNode> children = new HashSet<ITaxonTreeNode> (); |
|
308 |
List<TaxonNode> childNodesList = taxonNode.getChildNodes(); |
|
309 |
children.addAll(childNodesList); |
|
310 |
int compare = config.getTaxonNodeConfig().getChildHandling().compareTo(ChildHandling.DELETE); |
|
311 |
boolean childHandling = (compare == 0)? true: false; |
|
312 |
if (childHandling){ |
|
313 |
boolean changeDeleteTaxon = false; |
|
314 |
if (!config.getTaxonNodeConfig().isDeleteTaxon()){ |
|
315 |
config.getTaxonNodeConfig().setDeleteTaxon(true); |
|
316 |
changeDeleteTaxon = true; |
|
317 |
} |
|
318 |
DeleteResult resultNodes = deleteTaxonNodes(children, config); |
|
319 |
if (!resultNodes.isOk()){ |
|
320 |
result.addExceptions(resultNodes.getExceptions()); |
|
321 |
result.setStatus(resultNodes.getStatus()); |
|
322 |
} |
|
323 |
if (changeDeleteTaxon){ |
|
324 |
config.getTaxonNodeConfig().setDeleteTaxon(false); |
|
325 |
} |
|
326 |
|
|
327 |
} else { |
|
328 |
//move the children to the parent |
|
329 | 301 |
|
330 |
for (TaxonNode child: childNodesList){ |
|
331 |
parent.addChildNode(child, child.getReference(), child.getMicroReference()); |
|
332 |
} |
|
333 |
|
|
334 |
} |
|
335 |
} |
|
302 |
TaxonNode taxonNode; |
|
303 |
taxonNode = HibernateProxyHelper.deproxy(treeNode, TaxonNode.class); |
|
304 |
TaxonNode parent = taxonNode.getParent(); |
|
305 |
//check whether the node has children or the children are already deleted |
|
306 |
if(taxonNode.hasChildNodes()) { |
|
307 |
List<TaxonNode> children = new ArrayList<TaxonNode> (); |
|
308 |
List<TaxonNode> childNodesList = taxonNode.getChildNodes(); |
|
309 |
children.addAll(childNodesList); |
|
310 |
int compare = config.getTaxonNodeConfig().getChildHandling().compareTo(ChildHandling.DELETE); |
|
311 |
boolean childHandling = (compare == 0)? true: false; |
|
312 |
if (childHandling){ |
|
313 |
boolean changeDeleteTaxon = false; |
|
314 |
if (!config.getTaxonNodeConfig().isDeleteTaxon()){ |
|
315 |
config.getTaxonNodeConfig().setDeleteTaxon(true); |
|
316 |
changeDeleteTaxon = true; |
|
317 |
} |
|
318 |
DeleteResult resultNodes = deleteTaxonNodes(children, config); |
|
319 |
if (!resultNodes.isOk()){ |
|
320 |
result.addExceptions(resultNodes.getExceptions()); |
|
321 |
result.setStatus(resultNodes.getStatus()); |
|
322 |
} |
|
323 |
if (changeDeleteTaxon){ |
|
324 |
config.getTaxonNodeConfig().setDeleteTaxon(false); |
|
325 |
} |
|
326 |
|
|
327 |
} else { |
|
328 |
//move the children to the parent |
|
329 |
|
|
330 |
for (TaxonNode child: childNodesList){ |
|
331 |
parent.addChildNode(child, child.getReference(), child.getMicroReference()); |
|
332 |
} |
|
333 |
|
|
334 |
} |
|
335 |
} |
|
336 | 336 |
|
337 |
classification = taxonNode.getClassification();
|
|
337 |
classification = taxonNode.getClassification(); |
|
338 | 338 |
|
339 |
if (classification.getRootNode().equals(taxonNode)){
|
|
340 |
classification.removeRootNode();
|
|
341 |
classification = null;
|
|
342 |
}else if (classification.getChildNodes().contains(taxonNode)){
|
|
343 |
Taxon taxon = taxonNode.getTaxon();
|
|
344 |
classification.deleteChildNode(taxonNode);
|
|
339 |
if (classification.getRootNode().equals(taxonNode)){ |
|
340 |
classification.removeRootNode(); |
|
341 |
classification = null; |
|
342 |
}else if (classification.getChildNodes().contains(taxonNode)){ |
|
343 |
Taxon taxon = taxonNode.getTaxon(); |
|
344 |
classification.deleteChildNode(taxonNode); |
|
345 | 345 |
|
346 |
//node is rootNode
|
|
347 |
if (taxon != null){
|
|
346 |
//node is rootNode |
|
347 |
if (taxon != null){ |
|
348 | 348 |
|
349 |
if (config.getTaxonNodeConfig().isDeleteTaxon()){
|
|
350 |
taxonService.saveOrUpdate(taxon);
|
|
351 |
saveOrUpdate(taxonNode);
|
|
349 |
if (config.getTaxonNodeConfig().isDeleteTaxon()){ |
|
350 |
taxonService.saveOrUpdate(taxon); |
|
351 |
saveOrUpdate(taxonNode); |
|
352 | 352 |
|
353 |
TaxonDeletionConfigurator configNew = new TaxonDeletionConfigurator();
|
|
354 |
DeleteResult resultTaxon = taxonService.deleteTaxon(taxon.getUuid(), configNew, classification.getUuid());
|
|
355 |
if (!resultTaxon.isOk()){
|
|
356 |
result.addExceptions(resultTaxon.getExceptions());
|
|
357 |
result.setStatus(resultTaxon.getStatus());
|
|
358 |
}
|
|
353 |
TaxonDeletionConfigurator configNew = new TaxonDeletionConfigurator(); |
|
354 |
DeleteResult resultTaxon = taxonService.deleteTaxon(taxon.getUuid(), configNew, classification.getUuid()); |
|
355 |
if (!resultTaxon.isOk()){ |
|
356 |
result.addExceptions(resultTaxon.getExceptions()); |
|
357 |
result.setStatus(resultTaxon.getStatus()); |
|
358 |
} |
|
359 | 359 |
|
360 |
} |
|
361 | 360 |
} |
362 |
classification = null; |
|
363 |
|
|
364 |
} else { |
|
365 |
classification = null; |
|
366 |
Taxon taxon = taxonNode.getTaxon(); |
|
367 |
//node is rootNode |
|
368 |
if (taxon != null){ |
|
369 |
taxon.removeTaxonNode(taxonNode); |
|
370 |
if (config.getTaxonNodeConfig().isDeleteTaxon()){ |
|
371 |
TaxonDeletionConfigurator configNew = new TaxonDeletionConfigurator(); |
|
372 |
saveOrUpdate(taxonNode); |
|
373 |
|
|
374 |
taxonService.saveOrUpdate(taxon); |
|
375 |
DeleteResult resultTaxon = taxonService.deleteTaxon(taxon.getUuid(), configNew, null); |
|
376 |
|
|
377 |
if (!resultTaxon.isOk()){ |
|
378 |
result.addExceptions(resultTaxon.getExceptions()); |
|
379 |
result.setStatus(resultTaxon.getStatus()); |
|
380 |
} |
|
381 |
} |
|
361 |
} |
|
362 |
classification = null; |
|
363 |
|
|
364 |
} else { |
|
365 |
classification = null; |
|
366 |
Taxon taxon = taxonNode.getTaxon(); |
|
367 |
taxon = HibernateProxyHelper.deproxy(taxon, Taxon.class); |
|
368 |
if (taxon != null){ |
|
369 |
taxon.removeTaxonNode(taxonNode); |
|
370 |
if (config.getTaxonNodeConfig().isDeleteTaxon()){ |
|
371 |
TaxonDeletionConfigurator configNew = new TaxonDeletionConfigurator(); |
|
372 |
saveOrUpdate(taxonNode); |
|
373 |
taxonService.saveOrUpdate(taxon); |
|
374 |
DeleteResult resultTaxon = taxonService.deleteTaxon(taxon.getUuid(), configNew, null); |
|
375 |
|
|
376 |
if (!resultTaxon.isOk()){ |
|
377 |
result.addExceptions(resultTaxon.getExceptions()); |
|
378 |
result.setStatus(resultTaxon.getStatus()); |
|
379 |
} |
|
382 | 380 |
} |
381 |
} |
|
383 | 382 |
|
384 |
} |
|
385 |
|
|
386 |
result.addUpdatedObject(parent); |
|
387 |
if(result.getCdmEntity() == null){ |
|
388 |
result.setCdmEntity(taxonNode); |
|
389 |
} |
|
390 |
UUID uuid = dao.delete(taxonNode); |
|
391 |
logger.debug("Deleted node " +uuid.toString()); |
|
392 |
}else { |
|
393 |
classification = (Classification) treeNode; |
|
394 |
|
|
395 |
} |
|
383 |
} |
|
396 | 384 |
|
397 |
//deletedUUIDs.add(treeNode.getUuid()); |
|
385 |
result.addUpdatedObject(parent); |
|
386 |
if(result.getCdmEntity() == null){ |
|
387 |
result.setCdmEntity(taxonNode); |
|
388 |
} |
|
389 |
UUID uuid = dao.delete(taxonNode); |
|
390 |
logger.debug("Deleted node " +uuid.toString()); |
|
398 | 391 |
|
399 | 392 |
} |
400 | 393 |
} |
401 |
if (classification != null){ |
|
394 |
/*if (classification != null){
|
|
402 | 395 |
result.addUpdatedObject(classification); |
403 | 396 |
DeleteResult resultClassification = classService.delete(classification); |
404 | 397 |
if (!resultClassification.isOk()){ |
405 | 398 |
result.addExceptions(resultClassification.getExceptions()); |
406 | 399 |
result.setStatus(resultClassification.getStatus()); |
407 | 400 |
} |
408 |
} |
|
401 |
}*/
|
|
409 | 402 |
return result; |
410 | 403 |
|
411 | 404 |
} |
... | ... | |
414 | 407 |
@Override |
415 | 408 |
@Transactional(readOnly = false) |
416 | 409 |
public DeleteResult deleteTaxonNodes(Collection<UUID> nodeUuids, TaxonDeletionConfigurator config) { |
417 |
Set<ITaxonTreeNode> nodes = new HashSet<ITaxonTreeNode>();
|
|
410 |
List<TaxonNode> nodes = new ArrayList<TaxonNode>();
|
|
418 | 411 |
for(UUID nodeUuid : nodeUuids) { |
419 | 412 |
nodes.add(dao.load(nodeUuid)); |
420 | 413 |
} |
... | ... | |
441 | 434 |
} |
442 | 435 |
DeleteResult result = new DeleteResult(); |
443 | 436 |
|
444 |
if (config.getTaxonNodeConfig().isDeleteTaxon()){ |
|
437 |
if (config.getTaxonNodeConfig().getChildHandling().equals(TaxonNodeDeletionConfigurator.ChildHandling.MOVE_TO_PARENT)){ |
|
438 |
Object[] children = node.getChildNodes().toArray(); |
|
439 |
TaxonNode childNode; |
|
440 |
for (Object child: children){ |
|
441 |
childNode = (TaxonNode) child; |
|
442 |
parent.addChildNode(childNode, childNode.getReference(), childNode.getMicroReference()); |
|
443 |
} |
|
444 |
}else{ |
|
445 |
deleteTaxonNodes(node.getChildNodes(), config); |
|
446 |
} |
|
447 |
|
|
448 |
if (config.getTaxonNodeConfig().isDeleteTaxon() && (config.isDeleteInAllClassifications() || taxon.getTaxonNodes().size() == 1)){ |
|
445 | 449 |
result = taxonService.deleteTaxon(taxon.getUuid(), config, node.getClassification().getUuid()); |
446 | 450 |
result.addUpdatedObject(parent); |
447 | 451 |
if (result.isOk()){ |
... | ... | |
453 | 457 |
|
454 | 458 |
result.setCdmEntity(node); |
455 | 459 |
boolean success = taxon.removeTaxonNode(node); |
460 |
dao.save(parent); |
|
456 | 461 |
taxonService.saveOrUpdate(taxon); |
457 | 462 |
result.addUpdatedObject(parent); |
458 | 463 |
|
459 | 464 |
if (success){ |
460 | 465 |
result.setStatus(Status.OK); |
461 |
if (!dao.delete(node).equals(null)){ |
|
466 |
parent = HibernateProxyHelper.deproxy(parent, TaxonNode.class); |
|
467 |
int index = parent.getChildNodes().indexOf(node); |
|
468 |
if (index > -1){ |
|
469 |
parent.removeChild(index); |
|
470 |
} |
|
471 |
if (!dao.delete(node, config.getTaxonNodeConfig().getChildHandling().equals(TaxonNodeDeletionConfigurator.ChildHandling.DELETE)).equals(null)){ |
|
462 | 472 |
return result; |
463 | 473 |
} else { |
464 | 474 |
result.setError(); |
465 | 475 |
return result; |
466 | 476 |
} |
467 | 477 |
}else{ |
468 |
result.setError(); |
|
469 |
result.addException(new Exception("The node can not be removed from the taxon.")); |
|
478 |
if (dao.findByUuid(node.getUuid()) != null){ |
|
479 |
result.setError(); |
|
480 |
result.addException(new Exception("The node can not be removed from the taxon.")); |
|
481 |
} |
|
470 | 482 |
return result; |
471 | 483 |
} |
472 | 484 |
|
Also available in: Unified diff
fix #5179 and adapting the tests because the classification argument is needed now