fix NPE in cloneClassification
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / ClassificationServiceImpl.java
1 /**
2 * Copyright (C) 2007 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.Arrays;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.Comparator;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.TreeMap;
23 import java.util.UUID;
24
25 import javax.persistence.EntityNotFoundException;
26
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.log4j.Logger;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Service;
32 import org.springframework.transaction.annotation.Transactional;
33
34 import eu.etaxonomy.cdm.api.service.config.CreateHierarchyForClassificationConfigurator;
35 import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
36 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
37 import eu.etaxonomy.cdm.api.service.dto.EntityDTO;
38 import eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO;
39 import eu.etaxonomy.cdm.api.service.dto.MarkedEntityDTO;
40 import eu.etaxonomy.cdm.api.service.dto.TaxonInContextDTO;
41 import eu.etaxonomy.cdm.api.service.pager.Pager;
42 import eu.etaxonomy.cdm.api.service.pager.PagerUtils;
43 import eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl;
44 import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
45 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
46 import eu.etaxonomy.cdm.hibernate.HHH_9751_Util;
47 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
48 import eu.etaxonomy.cdm.model.common.CdmBase;
49 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
50 import eu.etaxonomy.cdm.model.common.ITreeNode;
51 import eu.etaxonomy.cdm.model.common.MarkerType;
52 import eu.etaxonomy.cdm.model.common.TreeIndex;
53 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
54 import eu.etaxonomy.cdm.model.description.TaxonDescription;
55 import eu.etaxonomy.cdm.model.media.Media;
56 import eu.etaxonomy.cdm.model.media.MediaRepresentation;
57 import eu.etaxonomy.cdm.model.media.MediaUtils;
58 import eu.etaxonomy.cdm.model.name.INonViralName;
59 import eu.etaxonomy.cdm.model.name.Rank;
60 import eu.etaxonomy.cdm.model.name.TaxonName;
61 import eu.etaxonomy.cdm.model.reference.Reference;
62 import eu.etaxonomy.cdm.model.taxon.Classification;
63 import eu.etaxonomy.cdm.model.taxon.ITaxonNodeComparator;
64 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
65 import eu.etaxonomy.cdm.model.taxon.Synonym;
66 import eu.etaxonomy.cdm.model.taxon.Taxon;
67 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
68 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
69 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
70 import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
71 import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
72 import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
73 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
74 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
75 import eu.etaxonomy.cdm.persistence.dto.ClassificationLookupDTO;
76 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
77 import eu.etaxonomy.cdm.persistence.dto.TaxonStatus;
78 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
79 import eu.etaxonomy.cdm.persistence.query.OrderHint;
80 import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
81 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
82
83 /**
84 * @author n.hoffmann
85 * @created Sep 21, 2009
86 */
87 @Service
88 @Transactional(readOnly = true)
89 public class ClassificationServiceImpl extends IdentifiableServiceBase<Classification, IClassificationDao>
90 implements IClassificationService {
91 private static final Logger logger = Logger.getLogger(ClassificationServiceImpl.class);
92
93 @Autowired
94 private ITaxonNodeDao taxonNodeDao;
95
96 @Autowired
97 private ITaxonDao taxonDao;
98
99 @Autowired
100 private ITaxonNodeService taxonNodeService;
101
102 @Autowired
103 private IDefinedTermDao termDao;
104
105 @Autowired
106 private IBeanInitializer defaultBeanInitializer;
107
108 @Override
109 @Autowired
110 protected void setDao(IClassificationDao dao) {
111 this.dao = dao;
112 }
113
114 private Comparator<? super TaxonNode> taxonNodeComparator;
115
116 @Autowired
117 public void setTaxonNodeComparator(ITaxonNodeComparator<? super TaxonNode> taxonNodeComparator){
118 this.taxonNodeComparator = (Comparator<? super TaxonNode>) taxonNodeComparator;
119 }
120
121 @Override
122 public TaxonNode loadTaxonNodeByTaxon(Taxon taxon, UUID classificationUuid, List<String> propertyPaths){
123 Classification tree = dao.load(classificationUuid);
124 TaxonNode node = tree.getNode(taxon);
125
126 return loadTaxonNode(node.getUuid(), propertyPaths);
127 }
128
129 @Override
130 @Deprecated // use loadTaxonNode(UUID, List<String>) instead
131 public TaxonNode loadTaxonNode(TaxonNode taxonNode, List<String> propertyPaths){
132 return taxonNodeDao.load(taxonNode.getUuid(), propertyPaths);
133 }
134
135 public TaxonNode loadTaxonNode(UUID taxonNodeUuid, List<String> propertyPaths){
136 return taxonNodeDao.load(taxonNodeUuid, propertyPaths);
137 }
138
139 @Override
140 @Transactional(readOnly = false)
141 public UpdateResult cloneClassification(UUID classificationUuid,
142 String name, Reference sec, TaxonRelationshipType relationshipType) {
143 UpdateResult result = new UpdateResult();
144 Classification classification = load(classificationUuid);
145 Classification clone = Classification.NewInstance(name);
146 clone.setReference(sec);
147
148 //clone taxa and taxon nodes
149 List<TaxonNode> childNodes = classification.getRootNode().getChildNodes();
150 for (TaxonNode taxonNode : childNodes) {
151 addChildTaxa(taxonNode, null, clone, relationshipType);
152 }
153 dao.saveOrUpdate(clone);
154 result.setCdmEntity(clone);
155 return result;
156 }
157
158 private void addChildTaxa(TaxonNode originalParentNode, TaxonNode cloneParentNode, Classification classification, TaxonRelationshipType relationshipType){
159 Reference reference = classification.getReference();
160 Taxon cloneTaxon = (Taxon) HibernateProxyHelper.deproxy(originalParentNode.getTaxon(), Taxon.class).clone();
161 cloneTaxon.setSec(reference);
162 String microReference = null;
163 List<TaxonNode> originalChildNodes = originalParentNode.getChildNodes();
164 HHH_9751_Util.removeAllNull(originalChildNodes);
165
166 //add relation between taxa
167 if (relationshipType != null){
168 cloneTaxon.addTaxonRelation(originalParentNode.getTaxon(), relationshipType, reference, microReference);
169 }
170
171 TaxonNode cloneChildNode = null;
172 //add taxon node to either parent node or classification (no parent node)
173 if(cloneParentNode==null){
174 cloneChildNode = classification.addChildTaxon(cloneTaxon, reference, microReference);
175 }
176 else{
177 cloneChildNode = cloneParentNode.addChildTaxon(cloneTaxon, reference, microReference);
178 }
179 taxonNodeDao.saveOrUpdate(cloneChildNode);
180 //add children
181 for (TaxonNode originalChildNode : originalChildNodes) {
182 addChildTaxa(originalChildNode, cloneChildNode, classification, relationshipType);
183 }
184 }
185
186 @Override
187 public List<TaxonNode> listRankSpecificRootNodes(Classification classification, Rank rank, Integer pageSize,
188 Integer pageIndex, List<String> propertyPaths) {
189 return pageRankSpecificRootNodes(classification, rank, pageSize, pageIndex, propertyPaths).getRecords();
190 }
191
192 @Override
193 public Pager<TaxonNode> pageRankSpecificRootNodes(Classification classification, Rank rank, Integer pageSize,
194 Integer pageIndex, List<String> propertyPaths) {
195 long[] numberOfResults = dao.countRankSpecificRootNodes(classification, rank);
196 long totalNumberOfResults = numberOfResults[0] + (numberOfResults.length > 1 ? numberOfResults[1] : 0);
197
198 List<TaxonNode> results = new ArrayList<TaxonNode>();
199
200 if (AbstractPagerImpl.hasResultsInRange(totalNumberOfResults, pageIndex, pageSize)) { // no point checking again
201 Integer limit = PagerUtils.limitFor(pageSize);
202 Integer start = PagerUtils.startFor(pageSize, pageIndex);
203
204 Integer remainingLimit = limit;
205 int[] queryIndexes = rank == null ? new int[]{0} : new int[]{0,1};
206
207 for(int queryIndex: queryIndexes) {
208 if(start != null && start > numberOfResults[queryIndex]) {
209 // start in next query with new start value
210 start = start - (int)numberOfResults[queryIndex];
211 continue;
212 }
213
214 List<TaxonNode> perQueryResults = dao.listRankSpecificRootNodes(classification, rank, remainingLimit, start, propertyPaths, queryIndex);
215 results.addAll(perQueryResults);
216 if(remainingLimit != null ){
217 remainingLimit = remainingLimit - results.size();
218 if(remainingLimit <= 0) {
219 // no need to run further queries if first query returned enough items!
220 break;
221 }
222 // start at with fist item of next query to fetch the remaining items
223 start = 0;
224 }
225 }
226 }
227 // long start_t = System.currentTimeMillis();
228 Collections.sort(results, taxonNodeComparator); // TODO is ordering during the hibernate query in the dao possible?
229 // System.err.println("service.pageRankSpecificRootNodes() - Collections.sort(results, taxonNodeComparator) " + (System.currentTimeMillis() - start_t));
230 return new DefaultPagerImpl<TaxonNode>(pageIndex, (int) totalNumberOfResults, pageSize, results);
231
232 }
233
234 /**
235 * @implements {@link IClassificationService#loadTreeBranch(TaxonNode, Rank, List)
236 * @see eu.etaxonomy.cdm.api.service.ITaxonService#loadTreeBranchTo(eu.etaxonomy.cdm.model.taxon.TaxonNode, eu.etaxonomy.cdm.model.name.Rank, java.util.List)
237 * FIXME Candidate for harmonization
238 * move to classification service
239 */
240 @Override
241 public List<TaxonNode> loadTreeBranch(TaxonNode taxonNode, Rank baseRank, List<String> propertyPaths){
242
243 TaxonNode thisNode = taxonNodeDao.load(taxonNode.getUuid(), propertyPaths);
244 if(baseRank != null){
245 baseRank = (Rank) termDao.load(baseRank.getUuid());
246 }
247 List<TaxonNode> pathToRoot = new ArrayList<TaxonNode>();
248 pathToRoot.add(thisNode);
249
250 while(!thisNode.isTopmostNode()){
251 //TODO why do we need to deproxy here?
252 // without this thisNode.getParent() will return NULL in
253 // some cases (environment dependend?) even if the parent exits
254 TaxonNode parentNode = CdmBase.deproxy(thisNode, TaxonNode.class).getParent();
255
256 if(parentNode == null){
257 throw new NullPointerException("taxonNode " + thisNode + " must have a parent since it is not top most");
258 }
259 if(parentNode.getTaxon() == null){
260 throw new NullPointerException("The taxon associated with taxonNode " + parentNode + " is NULL");
261 }
262 if(parentNode.getTaxon().getName() == null){
263 throw new NullPointerException("The name of the taxon associated with taxonNode " + parentNode + " is NULL");
264 }
265
266 Rank parentNodeRank = parentNode.getTaxon().getName() == null ? null : parentNode.getTaxon().getName().getRank();
267 // stop if the next parent is higher than the baseRank
268 if(baseRank != null && parentNodeRank != null && baseRank.isLower(parentNodeRank)){
269 break;
270 }
271
272 pathToRoot.add(parentNode);
273 thisNode = parentNode;
274 }
275
276 // initialize and invert order of nodes in list
277 defaultBeanInitializer.initializeAll(pathToRoot, propertyPaths);
278 Collections.reverse(pathToRoot);
279
280 return pathToRoot;
281 }
282
283 @Override
284 public List<TaxonNode> loadTreeBranchToTaxon(Taxon taxon, Classification classification, Rank baseRank, List<String> propertyPaths){
285 Classification tree = dao.load(classification.getUuid());
286 taxon = (Taxon) taxonDao.load(taxon.getUuid());
287 TaxonNode node = tree.getNode(taxon);
288 if(node == null){
289 logger.warn("The specified taxon is not found in the given tree.");
290 return null;
291 }
292 return loadTreeBranch(node, baseRank, propertyPaths);
293 }
294
295
296 @Override
297 public List<TaxonNode> loadChildNodesOfTaxonNode(TaxonNode taxonNode,
298 List<String> propertyPaths) {
299 taxonNode = taxonNodeDao.load(taxonNode.getUuid());
300 List<TaxonNode> childNodes = new ArrayList<TaxonNode>(taxonNode.getChildNodes());
301 defaultBeanInitializer.initializeAll(childNodes, propertyPaths);
302 Collections.sort(childNodes, taxonNodeComparator);
303 return childNodes;
304 }
305
306 @Override
307 public List<TaxonNode> listChildNodesOfTaxon(UUID taxonUuid, UUID classificationUuid, Integer pageSize,
308 Integer pageIndex, List<String> propertyPaths){
309
310 Classification classification = dao.load(classificationUuid);
311 Taxon taxon = (Taxon) taxonDao.load(taxonUuid);
312
313 List<TaxonNode> results = dao.listChildrenOf(taxon, classification, pageSize, pageIndex, propertyPaths);
314 Collections.sort(results, taxonNodeComparator); // FIXME this is only a HACK, order during the hibernate query in the dao
315 return results;
316 }
317
318 @Override
319 public Pager<TaxonNode> pageSiblingsOfTaxon(UUID taxonUuid, UUID classificationUuid, Integer pageSize,
320 Integer pageIndex, List<String> propertyPaths){
321
322 Classification classification = dao.load(classificationUuid);
323 Taxon taxon = (Taxon) taxonDao.load(taxonUuid);
324
325 long numberOfResults = dao.countSiblingsOf(taxon, classification);
326
327 List<TaxonNode> results;
328 if(PagerUtils.hasResultsInRange(numberOfResults, pageIndex, pageSize)) {
329 results = dao.listSiblingsOf(taxon, classification, pageSize, pageIndex, propertyPaths);
330 Collections.sort(results, taxonNodeComparator); // FIXME this is only a HACK, order during the hibernate query in the dao
331 } else {
332 results = new ArrayList<>();
333 }
334
335 return new DefaultPagerImpl<TaxonNode>(pageIndex, numberOfResults, pageSize, results);
336 }
337
338 @Override
339 public List<TaxonNode> listSiblingsOfTaxon(UUID taxonUuid, UUID classificationUuid, Integer pageSize,
340 Integer pageIndex, List<String> propertyPaths){
341
342 Pager<TaxonNode> pager = pageSiblingsOfTaxon(taxonUuid, classificationUuid, pageSize, pageIndex, propertyPaths);
343 return pager.getRecords();
344 }
345
346 @Override
347 public TaxonNode getTaxonNodeByUuid(UUID uuid) {
348 return taxonNodeDao.findByUuid(uuid);
349 }
350
351 @Override
352 public ITaxonTreeNode getTreeNodeByUuid(UUID uuid){
353 ITaxonTreeNode treeNode = taxonNodeDao.findByUuid(uuid);
354 if(treeNode == null){
355 treeNode = dao.findByUuid(uuid);
356 }
357
358 return treeNode;
359 }
360
361 @Override
362 public TaxonNode getRootNode(UUID classificationUuid){
363 return dao.getRootNode(classificationUuid);
364 }
365
366 @Override
367 public List<Classification> listClassifications(Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
368 return dao.list(limit, start, orderHints, propertyPaths);
369 }
370
371 @Override
372 public UUID removeTaxonNode(TaxonNode taxonNode) {
373 return taxonNodeDao.delete(taxonNode);
374 }
375 @Override
376 public UUID removeTreeNode(ITaxonTreeNode treeNode) {
377 if(treeNode instanceof Classification){
378 return dao.delete((Classification) treeNode);
379 }else if(treeNode instanceof TaxonNode){
380 return taxonNodeDao.delete((TaxonNode)treeNode);
381 }
382 return null;
383 }
384 @Override
385 public UUID saveTaxonNode(TaxonNode taxonNode) {
386 return taxonNodeDao.save(taxonNode).getUuid();
387 }
388
389 @Override
390 public Map<UUID, TaxonNode> saveTaxonNodeAll(
391 Collection<TaxonNode> taxonNodeCollection) {
392 return taxonNodeDao.saveAll(taxonNodeCollection);
393 }
394
395 @Override
396 public UUID saveClassification(Classification classification) {
397
398 taxonNodeDao.saveOrUpdateAll(classification.getAllNodes());
399 UUID result =dao.saveOrUpdate(classification);
400 return result;
401 }
402
403 @Override
404 public UUID saveTreeNode(ITaxonTreeNode treeNode) {
405 if(treeNode instanceof Classification){
406 return dao.save((Classification) treeNode).getUuid();
407 }else if(treeNode instanceof TaxonNode){
408 return taxonNodeDao.save((TaxonNode)treeNode).getUuid();
409 }
410 return null;
411 }
412
413 @Override
414 public List<TaxonNode> getAllNodes(){
415 return taxonNodeDao.list(null,null);
416 }
417
418 @Override
419 public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(UUID classificationUuid, Integer limit, String pattern) {
420 return taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(dao.load(classificationUuid), limit, pattern);
421 }
422
423 @Override
424 public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(Classification classification, Integer limit, String pattern) {
425 return taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, limit, pattern);
426 }
427
428 @Override
429 public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(UUID classificationUuid ) {
430 return taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(dao.load(classificationUuid), null, null);
431 }
432
433 @Override
434 public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(Classification classification ) {
435 return taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, null, null);
436 }
437
438 @Override
439 public List<UuidAndTitleCache<Classification>> getUuidAndTitleCache(Integer limit, String pattern) {
440 return dao.getUuidAndTitleCache(limit, pattern);
441 }
442
443 @Override
444 public Map<UUID, List<MediaRepresentation>> getAllMediaForChildNodes(
445 TaxonNode taxonNode, List<String> propertyPaths, int size,
446 int height, int widthOrDuration, String[] mimeTypes) {
447
448 TreeMap<UUID, List<MediaRepresentation>> result = new TreeMap<UUID, List<MediaRepresentation>>();
449 List<Media> taxonMedia = new ArrayList<Media>();
450 List<MediaRepresentation> mediaRepresentations = new ArrayList<MediaRepresentation>();
451
452 //add all media of the children to the result map
453 if (taxonNode != null){
454
455 List<TaxonNode> nodes = new ArrayList<TaxonNode>();
456
457 nodes.add(loadTaxonNode(taxonNode, propertyPaths));
458 nodes.addAll(loadChildNodesOfTaxonNode(taxonNode, propertyPaths));
459
460 if (nodes != null){
461 for(TaxonNode node : nodes){
462 Taxon taxon = node.getTaxon();
463 for (TaxonDescription taxonDescription: taxon.getDescriptions()){
464 for (DescriptionElementBase descriptionElement: taxonDescription.getElements()){
465 for(Media media : descriptionElement.getMedia()){
466 taxonMedia.add(media);
467
468 //find the best matching representation
469 mediaRepresentations.add(MediaUtils.findBestMatchingRepresentation(media,null, size, height, widthOrDuration, mimeTypes));
470
471 }
472 }
473 }
474 result.put(taxon.getUuid(), mediaRepresentations);
475
476 }
477 }
478
479 }
480
481 return result;
482
483 }
484
485
486 @Override
487 @Transactional(readOnly = false)
488 public void updateTitleCache(Class<? extends Classification> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<Classification> cacheStrategy, IProgressMonitor monitor) {
489 if (clazz == null){
490 clazz = Classification.class;
491 }
492 super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
493 }
494
495 /**
496 *
497 * @param allNodesOfClassification
498 * @return null - if allNodesOfClassification is empty <br>
499 */
500
501 private Map<String, List<TaxonNode>> getSortedGenusList(Collection<TaxonNode> allNodesOfClassification){
502
503 if(allNodesOfClassification == null || allNodesOfClassification.isEmpty()){
504 return null;
505 }
506 Map<String, List<TaxonNode>> sortedGenusMap = new HashMap<>();
507 for(TaxonNode node:allNodesOfClassification){
508 final TaxonNode tn = node;
509 Taxon taxon = node.getTaxon();
510 INonViralName name = taxon.getName();
511 String genusOrUninomial = name.getGenusOrUninomial();
512 //if rank unknown split string and take first word
513 if(genusOrUninomial == null){
514 String titleCache = taxon.getTitleCache();
515 String[] split = titleCache.split("\\s+");
516 for(String s:split){
517 genusOrUninomial = s;
518 break;
519 }
520 }
521 //if node has children
522
523 //retrieve list from map if not create List
524 if(sortedGenusMap.containsKey(genusOrUninomial)){
525 List<TaxonNode> list = sortedGenusMap.get(genusOrUninomial);
526 list.add(node);
527 sortedGenusMap.put(genusOrUninomial, list);
528 }else{
529 //create List for genus
530 List<TaxonNode> list = new ArrayList<TaxonNode>();
531 list.add(node);
532 sortedGenusMap.put(genusOrUninomial, list);
533 }
534 }
535 return sortedGenusMap;
536 }
537
538 /**
539 *
540 * creates new Classification and parent TaxonNodes at genus level
541 *
542 *
543 * @param map GenusMap which holds a name (Genus) and all the same Taxa as a list
544 * @param classification you want to improve the hierarchy (will not be modified)
545 * @param configurator to change certain settings, if null then standard settings will be taken
546 * @return new classification with parentNodes for each entry in the map
547 */
548 @SuppressWarnings({ "unchecked" })
549 @Transactional(readOnly = false)
550 @Override
551 public UpdateResult createHierarchyInClassification(Classification classification, CreateHierarchyForClassificationConfigurator configurator){
552 UpdateResult result = new UpdateResult();
553 classification = dao.findByUuid(classification.getUuid());
554 Map<String, List<TaxonNode>> map = getSortedGenusList(classification.getAllNodes());
555
556 final String APPENDIX = "repaired";
557 String titleCache = StringUtils.isBlank(classification.getTitleCache()) ? " " : classification.getTitleCache() ;
558 //TODO classification clone???
559 Classification newClassification = Classification.NewInstance(titleCache +" "+ APPENDIX);
560 newClassification.setReference(classification.getReference());
561
562 for(Map.Entry<String, List<TaxonNode>> entry:map.entrySet()){
563 String genus = entry.getKey();
564 List<TaxonNode> listOfTaxonNodes = entry.getValue();
565 TaxonNode parentNode = null;
566 //Search for genus in list
567 for(TaxonNode tNode:listOfTaxonNodes){
568 //take that taxonNode as parent and remove from list with all it possible children
569 //FIXME NPE for name
570 TaxonName name = tNode.getTaxon().getName();
571 if(name.getNameCache().equalsIgnoreCase(genus)){
572 TaxonNode clone = (TaxonNode) tNode.clone();
573 if(!tNode.hasChildNodes()){
574 //FIXME remove classification
575 // parentNode = newClassification.addChildNode(clone, 0, classification.getCitation(), classification.getMicroReference());
576 parentNode = newClassification.addChildNode(clone, 0, clone.getReference(), clone.getMicroReference());
577 //remove taxonNode from list because just added to classification
578 result.addUpdatedObject(tNode);
579 listOfTaxonNodes.remove(tNode);
580 }else{
581 //get all childNodes
582 //save prior Hierarchy and remove them from the list
583 List<TaxonNode> copyAllChildrenToTaxonNode = copyAllChildrenToTaxonNode(tNode, clone, result);
584 // parentNode = newClassification.addChildNode(clone, 0, classification.getCitation(), classification.getMicroReference());
585 //FIXME remove classification
586 parentNode = newClassification.addChildNode(clone, 0, clone.getReference(), clone.getMicroReference());
587 //remove taxonNode from list because just added to classification
588 result.addUpdatedObject(tNode);
589 listOfTaxonNodes.remove(tNode);
590 if(copyAllChildrenToTaxonNode != null){
591 listOfTaxonNodes = (List<TaxonNode>) CollectionUtils.removeAll(listOfTaxonNodes, copyAllChildrenToTaxonNode);
592 }
593 }
594 break;
595 }
596 }
597 if(parentNode == null){
598 //if no match found in list, create parentNode
599 NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
600 TaxonName TaxonName = (TaxonName)parser.parseFullName(genus);
601 //TODO Sec via configurator
602 Taxon taxon = Taxon.NewInstance(TaxonName, null);
603 parentNode = newClassification.addChildTaxon(taxon, 0, null, null);
604 result.addUpdatedObject(parentNode);
605 }
606 //iterate over the rest of the list
607 for(TaxonNode tn : listOfTaxonNodes){
608 //if TaxonNode has a parent and this is not the classification then skip it
609 //and add to new classification via the parentNode as children of it
610 //this should assures to keep the already existing hierarchy
611 //FIXME: Assert is not rootnode --> entrypoint is not classification in future but rather rootNode
612
613 if(!tn.isTopmostNode()){
614 continue; //skip to next taxonNode
615 }
616
617 TaxonNode clone = (TaxonNode) tn.clone();
618 //FIXME: citation from node
619 //TODO: addchildNode without citation and references
620 // TaxonNode taxonNode = parentNode.addChildNode(clone, classification.getCitation(), classification.getMicroReference());
621 TaxonNode taxonNode = parentNode.addChildNode(clone, clone.getReference(), clone.getMicroReference());
622 result.addUnChangedObject(clone);
623 if(tn.hasChildNodes()){
624 //save hierarchy in new classification
625 List<TaxonNode> copyAllChildrenToTaxonNode = copyAllChildrenToTaxonNode(tn, taxonNode, result);
626 if(copyAllChildrenToTaxonNode != null){
627 listOfTaxonNodes = (List<TaxonNode>) CollectionUtils.removeAll(listOfTaxonNodes, copyAllChildrenToTaxonNode);
628 }
629 }
630 }
631 }
632 dao.saveOrUpdate(newClassification);
633 result.setCdmEntity(newClassification);
634 return result;
635 }
636
637 /**
638 *
639 * recursive method to get all childnodes of taxonNode in classification.
640 *
641 * @param classification just for References and Citation, can be null
642 * @param copyFromNode TaxonNode with Children
643 * @param copyToNode TaxonNode which will receive the children
644 * @return List of ChildNode which has been added. If node has no children returns null
645 */
646 private List<TaxonNode> copyAllChildrenToTaxonNode(TaxonNode copyFromNode, TaxonNode copyToNode, UpdateResult result) {
647 List<TaxonNode> childNodes;
648 if(!copyFromNode.hasChildNodes()){
649 return null;
650 }else{
651 childNodes = copyFromNode.getChildNodes();
652 }
653 for(TaxonNode childNode:childNodes){
654 TaxonNode clone = (TaxonNode) childNode.clone();
655 result.addUnChangedObject(clone);
656 if(childNode.hasChildNodes()){
657 copyAllChildrenToTaxonNode(childNode, clone, result);
658 }
659 //FIXME: citation from node instead of classification
660 // copyToNode.addChildNode(clone,classification.getCitation(), classification.getMicroReference());
661 copyToNode.addChildNode(clone, clone.getReference(), clone.getMicroReference());
662 }
663 return childNodes;
664 }
665
666 /**
667 * {@inheritDoc}
668 */
669 @Override
670 public ClassificationLookupDTO classificationLookup(Classification classification) {
671 return dao.classificationLookup(classification);
672 }
673
674
675 @Override
676 public DeleteResult delete(UUID classificationUuid, TaxonDeletionConfigurator config){
677 DeleteResult result = new DeleteResult();
678 Classification classification = dao.findByUuid(classificationUuid);
679 if (classification == null){
680 result.addException(new IllegalArgumentException("The classification does not exist in database."));
681 result.setAbort();
682 return result;
683 }
684 if (!classification.hasChildNodes()){
685 dao.delete(classification);
686 }
687 if (config.getTaxonNodeConfig().getChildHandling().equals(ChildHandling.DELETE) ){
688 TaxonNode root = classification.getRootNode();
689 taxonNodeDao.delete(root, true);
690 dao.delete(classification);
691 }
692
693
694 return result;
695 }
696
697 @Override
698 public List<GroupedTaxonDTO> groupTaxaByHigherTaxon(List<UUID> originalTaxonUuids, UUID classificationUuid, Rank minRank, Rank maxRank){
699 List<GroupedTaxonDTO> result = new ArrayList<>();
700
701 //get treeindex for each taxonUUID
702 Map<UUID, TreeIndex> taxonIdTreeIndexMap = dao.treeIndexForTaxonUuids(classificationUuid, originalTaxonUuids);
703
704 //build treeindex list (or tree)
705 //TODO make it work with TreeIndex or move there
706 List<String> treeIndexClosureStr = new ArrayList<>();
707 for (TreeIndex treeIndex : taxonIdTreeIndexMap.values()){
708 String[] splits = treeIndex.toString().substring(1).split(ITreeNode.separator);
709 String currentIndex = ITreeNode.separator;
710 for (String split : splits){
711 if (split.equals("")){
712 continue;
713 }
714 currentIndex += split + ITreeNode.separator;
715 if (!treeIndexClosureStr.contains(currentIndex) && !split.startsWith(ITreeNode.treePrefix)){
716 treeIndexClosureStr.add(currentIndex);
717 }
718 }
719 }
720
721 //get rank sortindex for all parent taxa with sortindex <= minRank and sortIndex >= maxRank (if available)
722 Integer minRankOrderIndex = minRank == null ? null : minRank.getOrderIndex();
723 Integer maxRankOrderIndex = maxRank == null ? null : maxRank.getOrderIndex();
724 List<TreeIndex> treeIndexClosure = TreeIndex.NewListInstance(treeIndexClosureStr);
725
726 Map<TreeIndex, Integer> treeIndexSortIndexMapTmp = taxonNodeDao.rankOrderIndexForTreeIndex(treeIndexClosure, minRankOrderIndex, maxRankOrderIndex);
727
728 //remove all treeindex with "exists child in above map(and child.sortindex > xxx)
729 List<TreeIndex> treeIndexList = TreeIndex.sort(treeIndexSortIndexMapTmp.keySet());
730
731 Map<TreeIndex, Integer> treeIndexSortIndexMap = new HashMap<>();
732 TreeIndex lastTreeIndex = null;
733 for (TreeIndex treeIndex : treeIndexList){
734 if (lastTreeIndex != null && lastTreeIndex.hasChild(treeIndex)){
735 treeIndexSortIndexMap.remove(lastTreeIndex);
736 }
737 treeIndexSortIndexMap.put(treeIndex, treeIndexSortIndexMapTmp.get(treeIndex));
738 lastTreeIndex = treeIndex;
739 }
740
741 //get taxonID for treeIndexes
742 Map<TreeIndex, UuidAndTitleCache<?>> treeIndexTaxonIdMap = taxonNodeDao.taxonUuidsForTreeIndexes(treeIndexSortIndexMap.keySet());
743
744 //fill result list
745 for (UUID originalTaxonUuid : originalTaxonUuids){
746 GroupedTaxonDTO item = new GroupedTaxonDTO();
747 result.add(item);
748 item.setTaxonUuid(originalTaxonUuid);
749 TreeIndex groupTreeIndex = taxonIdTreeIndexMap.get(originalTaxonUuid);
750 String groupIndexX = TreeIndex.toString(groupTreeIndex);
751 while (groupTreeIndex != null){
752 if (treeIndexTaxonIdMap.get(groupTreeIndex) != null){
753 UuidAndTitleCache<?> uuidAndLabel = treeIndexTaxonIdMap.get(groupTreeIndex);
754 item.setGroupTaxonUuid(uuidAndLabel.getUuid());
755 item.setGroupTaxonName(uuidAndLabel.getTitleCache());
756 break;
757 }else{
758 groupTreeIndex = groupTreeIndex.parent();
759 // int index = groupIndex.substring(0, groupIndex.length()-1).lastIndexOf(ITreeNode.separator);
760 // groupIndex = index < 0 ? null : groupIndex.substring(0, index+1);
761 }
762 }
763 }
764
765 return result;
766 }
767
768 /**
769 * {@inheritDoc}
770 */
771 @Override
772 public List<GroupedTaxonDTO> groupTaxaByMarkedParents(List<UUID> originalTaxonUuids, UUID classificationUuid,
773 MarkerType markerType, Boolean flag) {
774
775 List<GroupedTaxonDTO> result = new ArrayList<>();
776
777 //get treeindex for each taxonUUID
778 Map<UUID, TreeIndex> taxonIdTreeIndexMap = dao.treeIndexForTaxonUuids(classificationUuid, originalTaxonUuids);
779
780 //get all marked tree indexes
781 Set<TreeIndex> markedTreeIndexes = dao.getMarkedTreeIndexes(markerType, flag);
782
783
784 Map<TreeIndex, TreeIndex> groupedMap = TreeIndex.group(markedTreeIndexes, taxonIdTreeIndexMap.values());
785 Set<TreeIndex> notNullGroups = new HashSet<>(groupedMap.values());
786 notNullGroups.remove(null);
787
788 //get taxonInfo for treeIndexes
789 Map<TreeIndex, UuidAndTitleCache<?>> treeIndexTaxonIdMap = taxonNodeDao.taxonUuidsForTreeIndexes(notNullGroups);
790
791 //fill result list
792 for (UUID originalTaxonUuid : originalTaxonUuids){
793 GroupedTaxonDTO item = new GroupedTaxonDTO();
794 result.add(item);
795 item.setTaxonUuid(originalTaxonUuid);
796
797 TreeIndex toBeGroupedTreeIndex = taxonIdTreeIndexMap.get(originalTaxonUuid);
798 TreeIndex groupTreeIndex = groupedMap.get(toBeGroupedTreeIndex);
799 UuidAndTitleCache<?> uuidAndLabel = treeIndexTaxonIdMap.get(groupTreeIndex);
800 if (uuidAndLabel != null){
801 item.setGroupTaxonUuid(uuidAndLabel.getUuid());
802 item.setGroupTaxonName(uuidAndLabel.getTitleCache());
803 }
804 }
805
806 return result;
807 }
808
809 /**
810 * {@inheritDoc}
811 */
812 @Override
813 public UUID getTaxonNodeUuidByTaxonUuid(UUID classificationUuid, UUID taxonUuid) {
814 Map<UUID, UUID> map = dao.getTaxonNodeUuidByTaxonUuid(classificationUuid, Arrays.asList(taxonUuid));
815 UUID taxonNodeUuid = map.get(taxonUuid);
816 return taxonNodeUuid;
817 }
818
819 /**
820 * {@inheritDoc}
821 */
822 @Override
823 public TaxonInContextDTO getTaxonInContext(UUID classificationUuid, UUID taxonBaseUuid,
824 Boolean doChildren, Boolean doSynonyms, List<UUID> ancestorMarkers,
825 NodeSortMode sortMode) {
826 TaxonInContextDTO result = new TaxonInContextDTO();
827
828 TaxonBase<?> taxonBase = taxonDao.load(taxonBaseUuid);
829 if (taxonBase == null){
830 throw new EntityNotFoundException("Taxon with uuid " + taxonBaseUuid + " not found in datasource");
831 }
832 boolean isSynonym = false;
833 Taxon acceptedTaxon;
834 if (taxonBase.isInstanceOf(Synonym.class)){
835 isSynonym = true;
836 Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class);
837 acceptedTaxon = synonym.getAcceptedTaxon();
838 if (acceptedTaxon == null) {
839 throw new EntityNotFoundException("Accepted taxon not found for synonym" );
840 }
841 TaxonStatus taxonStatus = TaxonStatus.Synonym;
842 if (synonym.getName()!= null && acceptedTaxon.getName() != null
843 && synonym.getName().getHomotypicalGroup().equals(acceptedTaxon.getName().getHomotypicalGroup())){
844 taxonStatus = TaxonStatus.SynonymObjective;
845 }
846 result.setTaxonStatus(taxonStatus);
847
848 }else{
849 acceptedTaxon = CdmBase.deproxy(taxonBase, Taxon.class);
850 result.setTaxonStatus(TaxonStatus.Accepted);
851 }
852 UUID acceptedTaxonUuid = acceptedTaxon.getUuid();
853
854 UUID taxonNodeUuid = getTaxonNodeUuidByTaxonUuid(classificationUuid, acceptedTaxonUuid);
855 if (taxonNodeUuid == null) {
856 throw new EntityNotFoundException("Taxon not found in classficiation with uuid " + classificationUuid + ". Either classification does not exist or does not contain taxon/synonym with uuid " + taxonBaseUuid );
857 }
858 result.setTaxonNodeUuid(taxonNodeUuid);
859
860 //TODO make it a dao call
861 Taxon parentTaxon = getParentTaxon(classificationUuid, acceptedTaxon);
862 if (parentTaxon != null){
863 result.setParentTaxonUuid(parentTaxon.getUuid());
864 result.setParentTaxonLabel(parentTaxon.getTitleCache());
865 if (parentTaxon.getName() != null){
866 result.setParentNameLabel(parentTaxon.getName().getTitleCache());
867 }
868 }
869
870
871 result.setTaxonUuid(taxonBaseUuid);
872 result.setClassificationUuid(classificationUuid);
873 if (taxonBase.getSec() != null){
874 result.setSecundumUuid(taxonBase.getSec().getUuid());
875 result.setSecundumLabel(taxonBase.getSec().getTitleCache());
876 }
877 result.setTaxonLabel(taxonBase.getTitleCache());
878
879 TaxonName name = taxonBase.getName();
880 result.setNameUuid(name.getUuid());
881 result.setNameLabel(name.getTitleCache());
882 result.setNameWithoutAuthor(name.getNameCache());
883 result.setGenusOrUninomial(name.getGenusOrUninomial());
884 result.setInfraGenericEpithet(name.getInfraGenericEpithet());
885 result.setSpeciesEpithet(name.getSpecificEpithet());
886 result.setInfraSpecificEpithet(name.getInfraSpecificEpithet());
887
888 result.setAuthorship(name.getAuthorshipCache());
889
890 Rank rank = name.getRank();
891 if (rank != null){
892 result.setRankUuid(rank.getUuid());
893 String rankLabel = rank.getAbbreviation();
894 if (StringUtils.isBlank(rankLabel)){
895 rankLabel = rank.getLabel();
896 }
897 result.setRankLabel(rankLabel);
898 }
899
900 boolean recursive = false;
901 Integer pageSize = null;
902 Integer pageIndex = null;
903 Pager<TaxonNodeDto> children = taxonNodeService.pageChildNodesDTOs(taxonNodeUuid, recursive, doSynonyms, sortMode, pageSize, pageIndex);
904
905 //children
906 if(! isSynonym) {
907 for (TaxonNodeDto childDto : children.getRecords()){
908 if (doChildren && childDto.getStatus().equals(TaxonStatus.Accepted)){
909 EntityDTO<Taxon> child = new EntityDTO<Taxon>(childDto.getTaxonUuid(), childDto.getTitleCache());
910 result.addChild(child);
911 }else if (doSynonyms && childDto.getStatus().isSynonym()){
912 EntityDTO<Synonym> child = new EntityDTO<Synonym>(childDto.getTaxonUuid(), childDto.getTitleCache());
913 result.addSynonym(child);
914 }
915 }
916 }else{
917 result.setAcceptedTaxonUuid(acceptedTaxonUuid);
918 String nameTitel = acceptedTaxon.getName() == null ? null : acceptedTaxon.getName().getTitleCache();
919 result.setAcceptedTaxonLabel(acceptedTaxon.getTitleCache());
920 result.setAcceptedNameLabel(nameTitel);
921 }
922
923 //marked ancestors
924 if (ancestorMarkers != null && !ancestorMarkers.isEmpty()){
925 List<DefinedTermBase> markerTypesTerms = termDao.list(ancestorMarkers, pageSize, null, null, null);
926 List<MarkerType> markerTypes = new ArrayList<>();
927 for (DefinedTermBase<?> term : markerTypesTerms){
928 if (term.isInstanceOf(MarkerType.class)){
929 markerTypes.add(CdmBase.deproxy(term, MarkerType.class));
930 }
931 }
932 if (! markerTypes.isEmpty()){
933 TaxonNode node = taxonNodeDao.findByUuid(taxonNodeUuid);
934 handleAncestorsForMarkersRecursive(result, markerTypes, node);
935 }
936 }
937
938 return result;
939 }
940
941 /**
942 * @param classificationUuid
943 * @param acceptedTaxon
944 * @return
945 */
946 private Taxon getParentTaxon(UUID classificationUuid, Taxon acceptedTaxon) {
947 if (classificationUuid == null){
948 return null;
949 }
950 TaxonNode parent = null;
951 for (TaxonNode node : acceptedTaxon.getTaxonNodes()){
952 if (classificationUuid.equals(node.getClassification().getUuid())){
953 parent = node.getParent();
954 }
955 }
956 if (parent != null){
957 return parent.getTaxon();
958 }
959 return null;
960 }
961
962 /**
963 * @param result
964 * @param markerTypes
965 * @param node
966 */
967 private void handleAncestorsForMarkersRecursive(TaxonInContextDTO result, List<MarkerType> markerTypes, TaxonNode node) {
968 for (MarkerType type : markerTypes){
969 Taxon taxon = node.getTaxon();
970 if (taxon != null && taxon.hasMarker(type, true)){
971 String label = taxon.getName() == null? taxon.getTitleCache() : taxon.getName().getTitleCache();
972 MarkedEntityDTO<Taxon> dto = new MarkedEntityDTO<>(type, true, taxon.getUuid(), label);
973 result.addMarkedAncestor(dto);
974 }
975 }
976 TaxonNode parentNode = node.getParent();
977 if (parentNode != null){
978 handleAncestorsForMarkersRecursive(result, markerTypes, parentNode);
979 }
980 }
981
982
983 }