ref #10410: create new default description of not exist
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / checklist / e4 / DistributionColumnAccessor.java
index 05e3f374487fed1d0965e498f6f49f27a5839290..e67d890b9c35a144a65fda337985bf097616a2d5 100755 (executable)
@@ -11,53 +11,53 @@ package eu.etaxonomy.taxeditor.editor.view.checklist.e4;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import javax.inject.Inject;
-
-import org.eclipse.e4.core.services.events.IEventBroker;
 import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
 
 import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
 import eu.etaxonomy.cdm.model.description.Distribution;
-import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
 import eu.etaxonomy.cdm.model.location.NamedArea;
+import eu.etaxonomy.cdm.model.metadata.DistributionDescription;
 import eu.etaxonomy.taxeditor.event.EventUtility;
 import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
-
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 
 /**
  * @author k.luther
  * @since 28.11.2018
- *
  */
 public class DistributionColumnAccessor implements IColumnPropertyAccessor<TaxonDistributionDTO> {
-    private DistributionEditor editor;
+
     public static final String DEFAULT_ENTRY = "";
-    @Inject
-    private IEventBroker eventBroker;
+
+    private DistributionEditor editor;
 
     public DistributionColumnAccessor(DistributionEditor editor) {
         this.editor = editor;
     }
 
-
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public Object getDataValue(TaxonDistributionDTO rowObject, int columnIndex) {
+//     editor.setActualNameCache(rowObject.getNameCache());
      switch (columnIndex) {
             case 0:
                 return rowObject.getNameCache();
             case 1:
                 if (editor.isShowRank()){
                     return rowObject.getRankString();
+                }else{
+                    return rowObject.getConcatenatedSynonyms();
+                }
+            case 2:
+                if (editor.isShowRank()){
+                    return rowObject.getConcatenatedSynonyms();
                 }else{
                     break;
                 }
@@ -79,45 +79,31 @@ public class DistributionColumnAccessor implements IColumnPropertyAccessor<Taxon
             if (distributionsForArea.size() > 1){
                 List<String> labels = new ArrayList();
                 distributionsForArea.forEach(desc -> labels.add(((Distribution)desc).getStatus().getLabel()));
-                return labels;
+                return distributionsForArea;
             }
         }
         return null;
 
     }
 
-
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public int getColumnCount() {
         return editor.getPropertyToLabelMap().size();
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public String getColumnProperty(int columnIndex) {
         return editor.getPropertyToLabelMap().get(columnIndex);
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public int getColumnIndex(String propertyName){
         return editor.getPropertyToLabelMap().indexOf(propertyName);
     }
 
-
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public void setDataValue(TaxonDistributionDTO taxonWrapper, int columnIndex, Object newValue) {
-           if (newValue instanceof PresenceAbsenceTerm){
+        if (newValue instanceof StatusHelper){
                 NamedArea area =editor.getAreaToColumnIndexMap().get(columnIndex);
                 Map<NamedArea, Set<DescriptionElementBase>> distributionMap = editor.taxonDistributionMap.get(taxonWrapper.getTaxonUuid());
                 if (distributionMap == null){
@@ -129,40 +115,65 @@ public class DistributionColumnAccessor implements IColumnPropertyAccessor<Taxon
                 if (distributions != null && !distributions.isEmpty()){
                     DescriptionElementBase desc = distributions.iterator().next();
                     if (desc instanceof Distribution){
-                        if (((PresenceAbsenceTerm)newValue).getId() == 0){
+                        if (((StatusHelper)newValue).term.getId() == 0){
                             desc.getInDescription().removeElement(desc);
                             distributions.remove(desc);
                         }else {
-                            ((Distribution)desc).setStatus((PresenceAbsenceTerm)newValue);
+                            ((Distribution)desc).setStatus(((StatusHelper)newValue).term);
                         }
                     }
                 }else{
+                    if (((StatusHelper)newValue).term.getId() == 0){
+                        return;
+                    }
                     if (distributions == null){
                         distributions = new HashSet();
                         distributionMap.put(area, distributions);
                     }
-                    dist = Distribution.NewInstance(area, (PresenceAbsenceTerm)newValue);
+                    dist = Distribution.NewInstance(area, ((StatusHelper)newValue).term);
                     Set<TaxonDescription> descs = taxonWrapper.getDescriptionsWrapper().getDescriptions();
-                    TaxonDescription desc;
+                    TaxonDescription desc = null;
                     if (descs.size() >= 1){
-                        desc = descs.iterator().next();
+                        Iterator<TaxonDescription> itDesc = descs.iterator();
+                        while(itDesc.hasNext()) {
+                            TaxonDescription next = itDesc.next();
+                            if (next.isDefault()) {
+                                desc = next;
+                                break;
+                            }
+                        }
+
+                        if (desc == null) {
+                            if (PreferencesUtil.getDescriptionForChecklistEditor().equals(DistributionDescription.AlwaysDefault)){
+                                desc = TaxonDescription.NewInstance();
+                                desc.setDefault(true);
+                                taxonWrapper.getDescriptionsWrapper().getDescriptions().add(desc);
+                            }else {
+                                desc = descs.iterator().next();
+                            }
+                        }
                     }else {
                         desc = TaxonDescription.NewInstance();
+                        desc.setDefault(true);
+
                         taxonWrapper.getDescriptionsWrapper().getDescriptions().add(desc);
                     }
                     if (editor.getDefaultSource() != null){
                       dist.addSource(DescriptionElementSource.NewPrimarySourceInstance(editor.getDefaultSource(), null));
                     }
                     desc.addElement(dist);
-                    editor.part.getCdmEntitySession().load(desc, true);
+                    if(desc.isPersisted()){
+                        editor.part.getCdmEntitySession().load(desc, true);
+                    }
                     distributions.add(dist);
-
+//                    EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS_DISTRIBUTION, taxonWrapper.getNameCache());
 
                 }
+                editor.setActualNameCache(taxonWrapper.getNameCache());
 
                 editor.descriptionsToSave.add(taxonWrapper);
             }
+
             EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS, true);
     }
-
-}
+}
\ No newline at end of file