Implement generic comparator for OriginalSourceBase #3185
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 13 Apr 2016 14:54:12 +0000 (16:54 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 13 Apr 2016 14:54:12 +0000 (16:54 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/DescriptionElementSourceSection.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/SourceComparator.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/supplemental/SourceSection.java

index 314810d764fc78cee73a9f21975338442d1f958c..6c74da87009652102f6c3a523691da51f28c8569 100644 (file)
@@ -6,14 +6,12 @@ package eu.etaxonomy.taxeditor.ui.section.description;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.List;
 
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.model.common.OriginalSourceType;
 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
 import java.util.List;
 
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.model.common.OriginalSourceType;
 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
-import eu.etaxonomy.cdm.model.reference.Reference;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
 import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
 import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection;
@@ -62,51 +60,7 @@ public class DescriptionElementSourceSection extends
     @Override
     public Collection<DescriptionElementSource> getCollection(DescriptionElementBase entity) {
         List<DescriptionElementSource> sources = new ArrayList<DescriptionElementSource>(entity.getSources());
     @Override
     public Collection<DescriptionElementSource> getCollection(DescriptionElementBase entity) {
         List<DescriptionElementSource> sources = new ArrayList<DescriptionElementSource>(entity.getSources());
-        Collections.sort(sources, new Comparator<DescriptionElementSource>() {
-
-            @Override
-            public int compare(DescriptionElementSource o1, DescriptionElementSource o2) {
-                int id1 = o1.getId();
-                int id2 = o2.getId();
-                OriginalSourceType type1 = o1.getType();
-                OriginalSourceType type2 = o2.getType();
-                Reference citation1 = o1.getCitation();
-                Reference citation2 = o2.getCitation();
-
-                // the newly created should always be on top
-                if (id1 == 0 && id2!=0) {
-                    return -1;
-                }
-                if (id1 != 0 && id2==0) {
-                    return 1;
-                }
-
-                // sort by type (Primary taxonomic > Primary Media > others
-                // alphabetically by reference title cache)
-                if (type1 != null && type1.equals(OriginalSourceType.PrimaryTaxonomicSource)
-                               && type2 != null && !type2.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
-                       return -1;
-                }
-                if (type1 != null && !type1.equals(OriginalSourceType.PrimaryTaxonomicSource)
-                               && type2 != null && type2.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
-                       return 1;
-                }
-                if (type1 != null && type1.equals(OriginalSourceType.PrimaryMediaSource)
-                        && type2!=null && !type2.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
-                    return -1;
-                }
-                if (type2 != null && type2.equals(OriginalSourceType.PrimaryMediaSource)
-                        && type1!=null && !type1.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
-                    return 1;
-                }
-
-                //sort by citation title cache
-                if(citation1!=null && citation2!=null){
-                    return citation1.getTitleCache().compareTo(citation2.getTitleCache());
-                }
-                return o2.getCreated().compareTo(o1.getCreated());
-            }
-        });
+        Collections.sort(sources, new SourceComparator());
         return sources;
     }
 
         return sources;
     }
 
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/SourceComparator.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/SourceComparator.java
new file mode 100644 (file)
index 0000000..5353ec3
--- /dev/null
@@ -0,0 +1,58 @@
+package eu.etaxonomy.taxeditor.ui.section.description;
+
+
+import java.util.Comparator;
+
+import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
+import eu.etaxonomy.cdm.model.common.OriginalSourceType;
+import eu.etaxonomy.cdm.model.reference.Reference;
+
+/**
+ * @author pplitzner
+ * @date Apr 13, 2016
+ *
+ */
+public class SourceComparator implements Comparator<OriginalSourceBase> {
+    @Override
+    public int compare(OriginalSourceBase o1, OriginalSourceBase o2) {
+        int id1 = o1.getId();
+        int id2 = o2.getId();
+        OriginalSourceType type1 = o1.getType();
+        OriginalSourceType type2 = o2.getType();
+        Reference citation1 = o1.getCitation();
+        Reference citation2 = o2.getCitation();
+
+        // the newly created should always be on top
+        if (id1 == 0 && id2!=0) {
+            return -1;
+        }
+        if (id1 != 0 && id2==0) {
+            return 1;
+        }
+
+        // sort by type (Primary taxonomic > Primary Media > others
+        // alphabetically by reference title cache)
+        if (type1 != null && type1.equals(OriginalSourceType.PrimaryTaxonomicSource)
+                && type2 != null && !type2.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
+            return -1;
+        }
+        if (type1 != null && !type1.equals(OriginalSourceType.PrimaryTaxonomicSource)
+                && type2 != null && type2.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
+            return 1;
+        }
+        if (type1 != null && type1.equals(OriginalSourceType.PrimaryMediaSource)
+                && type2!=null && !type2.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
+            return -1;
+        }
+        if (type2 != null && type2.equals(OriginalSourceType.PrimaryMediaSource)
+                && type1!=null && !type1.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
+            return 1;
+        }
+
+        //sort by citation title cache
+        if(citation1!=null && citation2!=null){
+            return citation1.getTitleCache().compareTo(citation2.getTitleCache());
+        }
+        return o2.getCreated().compareTo(o1.getCreated());
+    }
+}
\ No newline at end of file
index 8e644d609e016c13415200dee58e420fa4a9ef21..ae4b637d59aa7fc0c2a88e03a5f2530243f2bd6f 100644 (file)
@@ -3,7 +3,10 @@
  */
 package eu.etaxonomy.taxeditor.ui.section.supplemental;
 
  */
 package eu.etaxonomy.taxeditor.ui.section.supplemental;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
 
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
 
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
@@ -13,6 +16,7 @@ import eu.etaxonomy.cdm.model.media.Media;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
 import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
 import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection;
+import eu.etaxonomy.taxeditor.ui.section.description.SourceComparator;
 
 /**
  * <p>SourceSection class.</p>
 
 /**
  * <p>SourceSection class.</p>
@@ -54,7 +58,9 @@ public class SourceSection extends AbstractEntityCollectionSection<IIdentifiable
        @Override
        public Collection<IdentifiableSource> getCollection(
                        IIdentifiableEntity entity) {
        @Override
        public Collection<IdentifiableSource> getCollection(
                        IIdentifiableEntity entity) {
-               return entity.getSources();
+               List<IdentifiableSource> sources = new ArrayList<IdentifiableSource>(entity.getSources());
+               Collections.sort(sources, new SourceComparator());
+        return sources;
        }
 
        /** {@inheritDoc} */
        }
 
        /** {@inheritDoc} */