changes to fix the checklist editor
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / checklist / ChecklistLabelProvider.java
index 0ae5336b38e01875faa6eaa9678eb600bfc49587..56c931a562aff65981d42a965d4d86c92f236f51 100644 (file)
@@ -12,7 +12,9 @@ package eu.etaxonomy.taxeditor.editor.view.checklist;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
 import java.util.UUID;
@@ -29,6 +31,7 @@ import eu.etaxonomy.cdm.api.service.ITermService;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
 import eu.etaxonomy.cdm.model.common.Language;
+import eu.etaxonomy.cdm.model.common.TermIdInVocabularyComparator;
 import eu.etaxonomy.cdm.model.common.TermLanguageComparator;
 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
 import eu.etaxonomy.cdm.model.description.Distribution;
@@ -48,7 +51,7 @@ public class ChecklistLabelProvider extends LabelProvider implements ITableLabel
 
     public static final String DEFAULT_ENTRY = "";
     private IDescriptionService descriptionService;
-    private final List<DefinedTermBase<?>> namedAreas;
+    private final SortedSet<DefinedTermBase> namedAreas;
     TableViewer viewer = null;
 
     /**
@@ -92,17 +95,17 @@ public class ChecklistLabelProvider extends LabelProvider implements ITableLabel
         //TODO load areas by this list
 
         List<TaxonDescription> listTaxonDescriptions = descriptionService.listTaxonDescriptions(taxon, null, null, null, null, null, DESC_INIT_STRATEGY);;
-        NonViralName<?> nonVirlaName = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
+        NonViralName<?> nonViralName = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
 
         switch (columnIndex) {
         case 0:
             String taxonName = null;
-            taxonName = (nonVirlaName != null) ? nonVirlaName.getNameCache() : null;
+            taxonName = (nonViralName != null) ? nonViralName.getNameCache() : null;
             return (taxonName != null) ? taxonName : DEFAULT_ENTRY;
 
         case 1:
             String authorship = null;
-            authorship = (nonVirlaName != null) ? nonVirlaName.getAuthorshipCache() : null;
+            authorship = (nonViralName != null) ? nonViralName.getAuthorshipCache() : null;
             return (authorship != null) ? authorship : DEFAULT_ENTRY;
 //        case 2:
 //            String ref = null;
@@ -125,13 +128,24 @@ public class ChecklistLabelProvider extends LabelProvider implements ITableLabel
             for (TaxonDescription td : listTaxonDescriptions) {
                 for (DescriptionElementBase deb : td.getElements()) {
                     if (deb instanceof Distribution) {
-                        Distribution distribution = (Distribution) deb;
-                        String area = distribution.toString();
+                        Distribution distribution = HibernateProxyHelper.deproxy(deb, Distribution.class);
+                        String area = null;
+                        if (distribution.getArea() != null ){
+                            if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
+                                area = distribution.getArea().getIdInVocabulary();
+                            }else{
+                                area = distribution.getArea().getTitleCache();
+                            }
+                        }else{
+                            continue;
+                        }
+
                         if(viewer != null){
                                TableColumn column = viewer.getTable().getColumn(columnIndex);
                                if (area.equalsIgnoreCase(column.getText())) {
-                                       return (distribution.getStatus().getTitleCache() != null)?distribution.getStatus().getTitleCache():DEFAULT_ENTRY;
-                               }
+                                   return (distribution.getStatus().getTitleCache() != null)?distribution.getStatus().getTitleCache():DEFAULT_ENTRY;
+                            }
+
                         }
                     }
                 }
@@ -145,20 +159,24 @@ public class ChecklistLabelProvider extends LabelProvider implements ITableLabel
             "descriptions.*", "description.state" });
 
 
-    private List<DefinedTermBase<?>> loadNamedAreas() {
+    private SortedSet<DefinedTermBase> loadNamedAreas() {
         IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
         String values = preferenceStore.getString(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS);
         if (values != null && values != "") {
             String[] split = values.split(",");
             List<String> listValue = Arrays.asList(split);
-            ArrayList<DefinedTermBase<?>> termlist = new ArrayList<DefinedTermBase<?>>();
+            List<DefinedTermBase> termlist = new ArrayList<DefinedTermBase>();
+           Set<UUID> uuidList = new HashSet<UUID>();
+           UUID uuid;
             for(String s : listValue){
-                UUID uuid = UUID.fromString(s);
-                ITermService service = CdmStore.getService(ITermService.class);
-                DefinedTermBase definedTermBase = service.load(uuid);
-                termlist.add(definedTermBase);
+                uuid = UUID.fromString(s);
+                uuidList.add(uuid);
+
             }
-            return termlist;
+            ITermService service = CdmStore.getService(ITermService.class);
+            termlist = service.find(uuidList);
+
+            return getTermsOrderedByIdInVocabulary(termlist);
         }
         return null;
     }
@@ -176,7 +194,22 @@ public class ChecklistLabelProvider extends LabelProvider implements ITableLabel
     /**
      * @return the namedAreas
      */
-    public List<DefinedTermBase<?>> getNamedAreas() {
+    public SortedSet<DefinedTermBase> getNamedAreas() {
         return namedAreas;
     }
+
+    /**
+     * @param namedAreas
+     * @param defaultLanguage
+     * @return
+     */
+    public SortedSet<DefinedTermBase> getTermsOrderedByIdInVocabulary(List<DefinedTermBase> namedAreas) {
+        TermIdInVocabularyComparator comp = new TermIdInVocabularyComparator();
+
+        SortedSet result = new TreeSet(comp);
+        if(namedAreas != null){
+            result.addAll(namedAreas);
+        }
+        return result;
+    }
 }