Merge branch 'release/5.18.0'
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / dto / TaxonRelationshipsDTO.java
index 968aef6746a3d3c90bfeef955956d6a4b12007fc..a1e34ba00db20913b2af1385fd420a8ccad5e222 100644 (file)
@@ -19,10 +19,10 @@ import eu.etaxonomy.cdm.common.CdmUtils;
 import eu.etaxonomy.cdm.format.taxon.TaxonRelationshipFormatter;
 import eu.etaxonomy.cdm.model.common.Language;
 import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
-import eu.etaxonomy.cdm.model.common.Representation;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
+import eu.etaxonomy.cdm.model.term.Representation;
 import eu.etaxonomy.cdm.persistence.dto.TermDto;
 import eu.etaxonomy.cdm.strategy.cache.TagEnum;
 import eu.etaxonomy.cdm.strategy.cache.TaggedCacheHelper;
@@ -36,9 +36,10 @@ import eu.etaxonomy.cdm.strategy.cache.TaggedText;
  */
 public class TaxonRelationshipsDTO {
 
-    public class TaxonRelation{
+    private static final String SENSU_SEPARATOR = ", ";
+
+    public class TaxonRelationDTO{
 
-        private UUID relationUuid;
         private boolean doubtful = false;
         private boolean misapplication = false;
         private boolean synonym = false;
@@ -48,23 +49,29 @@ public class TaxonRelationshipsDTO {
         private List<TaggedText> taggedText;
         //TODO maybe this will be changed in future
         private TermDto type;
+        private UUID typeUuid;
+        private SourceDTO sec;
+        private SourceDTO relSec;
 
 
-        public TaxonRelation(TaxonRelationship relation, Direction direction, List<Language> languages) {
+        public TaxonRelationDTO(TaxonRelationship relation, Direction direction, List<Language> languages) {
             Taxon relatedTaxon = direction == Direction.relatedTo? relation.getToTaxon()
                     : relation.getFromTaxon();
             this.taxonUuid = relatedTaxon.getUuid();
             this.doubtful = relation.isDoubtful();
-            this.relationUuid = relation.getUuid();
             this.direction = direction;
             TaxonRelationshipType relType = relation.getType();
+
             if (relType != null){
-                this.misapplication = relType.isAnyMisappliedName();
+                this.misapplication = relType.isMisappliedName();
                 this.synonym = relType.isAnySynonym();
-                //TODO there must be a better DTO which also includes
+                this.typeUuid = relType.getUuid();
+//                TODO there must be a better DTO which also includes
                 Set<Representation> representations = direction.isDirect() ? relType.getRepresentations() : relType.getInverseRepresentations();
-                this.setType(new TermDto(relType.getUuid(), representations, relType.getOrderIndex()));
-                this.misapplication = relation.getType().isAnyMisappliedName();
+                TermDto termDto = TermDto.fromTerm(relType, representations);
+                this.type = termDto;
+//                TODO localize
+//                termDto.localize(representation_L10n);
             }
             List<TaggedText> tags = new TaxonRelationshipFormatter().getTaggedText(
                     relation, direction == Direction.relatedFrom, languages);
@@ -85,12 +92,6 @@ public class TaxonRelationshipsDTO {
         public void setDoubtful(boolean doubtful) {
             this.doubtful = doubtful;
         }
-        public UUID getRelationUuid() {
-            return relationUuid;
-        }
-        public void setRelationUuid(UUID relationUuid) {
-            this.relationUuid = relationUuid;
-        }
 
         public Direction getDirection() {
             return direction;
@@ -141,9 +142,25 @@ public class TaxonRelationshipsDTO {
             this.type = type;
         }
 
+
+        /**
+         * @return the typeUuid
+         */
+        public UUID getTypeUuid() {
+            return typeUuid;
+        }
+
+
+        /**
+         * @param typeUuid the typeUuid to set
+         */
+        public void setTypeUuid(UUID typeUuid) {
+            this.typeUuid = typeUuid;
+        }
+
     }
 
-    private List<TaxonRelation> relations = new ArrayList<>();
+    private List<TaxonRelationDTO> relations = new ArrayList<>();
 
     private List<List<TaggedText>> misapplications = new ArrayList<>();
 
@@ -160,39 +177,32 @@ public class TaxonRelationshipsDTO {
 
  // ************************** GETTER / SETTER  ***********************/
 
-    public List<TaxonRelation> getRelations() {
+    public List<TaxonRelationDTO> getRelations() {
         return relations;
     }
 
-    public void setIncludedTaxa(List<TaxonRelation> relations) {
+    public void setIncludedTaxa(List<TaxonRelationDTO> relations) {
         this.relations = relations;
     }
 
-    public void addRelation(TaxonRelation relation){
+    public void addRelation(TaxonRelationDTO relation){
         relations.add(relation);
     }
 
-    /**
-     * @param relation
-     * @param direction
-     */
-    public void addRelation(TaxonRelationship relation, Direction direction, List<Language> languages) {
-        TaxonRelation newRelation = new TaxonRelation(relation, direction, languages);
+    public TaxonRelationDTO addRelation(TaxonRelationship relation, Direction direction, List<Language> languages) {
+        TaxonRelationDTO newRelation = new TaxonRelationDTO(relation, direction, languages);
         relations.add(newRelation);
+        return newRelation;
     }
 
-
-    /**
-     *
-     */
     public void createMisapplicationString() {
         List<List<TaggedText>> result = new ArrayList<>();
 
-        for (TaxonRelation relation: relations){
+        for (TaxonRelationDTO relation: relations){
             if (relation.isMisapplication()){
                 List<TaggedText> tags = relation.getTaggedText();
 
-                boolean isDuplicate = false;;
+                boolean isDuplicate = false;
                 for (List<TaggedText> existing: result){
                     isDuplicate = mergeIfDuplicate(existing, tags);
                     if (isDuplicate){
@@ -220,53 +230,61 @@ public class TaxonRelationshipsDTO {
         int j = 0;
         int sensuEndInFirst = -1;
         int sensuStartInSecond = -1;
-        int lastEndInSecond = -1;
+        int senusEndInSecond = -1;
 
-        while (i < first.size() && j< second.size()){
+        boolean sensuHandled = false;
+        boolean isDuplicate = true;
+        while (isDuplicate && i < first.size() && j< second.size()){
             if (tagEqualsMisapplied(first.get(i), second.get(i))){
                 i++;j++;
-            }else{
+            }else if (!sensuHandled){
                 while (i < first.size() && tagIsSensu(first.get(i))){
                     i++;
+                    sensuEndInFirst = i;
                 }
-                sensuEndInFirst = i;
                 sensuStartInSecond = j;
                 while (j< second.size() && tagIsSensu(second.get(j))){
                     j++;
+                    senusEndInSecond = j;
                 }
-                lastEndInSecond = j;
+                sensuHandled = true;
+            }else{
+                isDuplicate = false;
+                i++;j++; //not really necessary
             }
+
         }
-        boolean isDuplicate = (i == first.size() || j == second.size());
+        isDuplicate = isDuplicate && i == first.size() && j == second.size();
         if (isDuplicate && sensuEndInFirst > -1 && sensuStartInSecond > -1){
-            first.addAll(sensuEndInFirst, second.subList(sensuStartInSecond, lastEndInSecond));
+            first.addAll(sensuEndInFirst, second.subList(sensuStartInSecond, senusEndInSecond));
+            first.add(sensuEndInFirst, TaggedText.NewSeparatorInstance(SENSU_SEPARATOR));
             return true;
         }else{
             return false;
         }
     }
 
-    /**
-     * @param taggedText
-     * @return
-     */
     private boolean tagIsSensu(TaggedText tag) {
-        if (tag.getType() == TagEnum.sensuReference ||
-                tag.getType() == TagEnum.sensuMicroReference){
+        if (tag.getType() == TagEnum.secReference ||
+                tag.getType() == TagEnum.secMicroReference ||
+                isSensuSeparator(tag)){
+            return true;
+        }
+        return false;
+    }
+
+    private boolean isSensuSeparator(TaggedText tag) {
+        if (SENSU_SEPARATOR.equals(tag.getText())
+                && tag.getType() == TagEnum.separator) {
             return true;
         }
         return false;
     }
 
-    /**
-     * @param x
-     * @param y
-     * @return
-     */
     private boolean tagEqualsMisapplied(TaggedText x, TaggedText y) {
         if (CdmUtils.nullSafeEqual(x.getText(),y.getText())
-                && x.getType().equals(y.getType())){
-            //TODO entity
+                && x.getType().equals(y.getType())
+                && CdmUtils.nullSafeEqual(x.getEntityReference(),y.getEntityReference())){
             return true;
         }else{
             return false;
@@ -285,7 +303,7 @@ public class TaxonRelationshipsDTO {
     }
 //
 //    public boolean contains(UUID taxonUuid) {
-//        for (TaxonRelation relation: relations){
+//        for (TaxonRelationDTO relation: relations){
 //            if (taxon.taxonUuid.equals(taxonUuid)){
 //                return true;
 //            }
@@ -296,7 +314,7 @@ public class TaxonRelationshipsDTO {
     @Override
     public String toString(){
         String result = "";
-        for (TaxonRelation relation : relations){
+        for (TaxonRelationDTO relation : relations){
             result += relation.toString() + ",";
         }
         if (result.length() > 0){
@@ -307,19 +325,10 @@ public class TaxonRelationshipsDTO {
         return result;
     }
 
-    /**
-     * @return the misapplications
-     */
     public List<List<TaggedText>> getMisapplications() {
         return misapplications;
     }
-
-    /**
-     * @param misapplications the misapplications to set
-     */
     public void setMisapplications(List<List<TaggedText>> misapplications) {
         this.misapplications = misapplications;
     }
-
-
 }