2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.e4
;
11 import java
.util
.ArrayList
;
12 import java
.util
.HashMap
;
13 import java
.util
.HashSet
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
19 import org
.eclipse
.nebula
.widgets
.nattable
.data
.IColumnPropertyAccessor
;
21 import eu
.etaxonomy
.cdm
.api
.service
.dto
.TaxonDistributionDTO
;
22 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
23 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementSource
;
24 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
25 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
26 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
27 import eu
.etaxonomy
.cdm
.model
.metadata
.DistributionDescription
;
28 import eu
.etaxonomy
.taxeditor
.event
.EventUtility
;
29 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
30 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
36 public class DistributionColumnAccessor
implements IColumnPropertyAccessor
<TaxonDistributionDTO
> {
38 public static final String DEFAULT_ENTRY
= "";
40 private DistributionEditor editor
;
42 public DistributionColumnAccessor(DistributionEditor editor
) {
47 public Object
getDataValue(TaxonDistributionDTO rowObject
, int columnIndex
) {
48 // editor.setActualNameCache(rowObject.getNameCache());
49 switch (columnIndex
) {
51 return rowObject
.getNameCache();
53 if (editor
.isShowRank()){
54 return rowObject
.getRankString();
56 return rowObject
.getConcatenatedSynonyms();
59 if (editor
.isShowRank()){
60 return rowObject
.getConcatenatedSynonyms();
67 NamedArea area
= editor
.getAreaToColumnIndexMap().get(columnIndex
);
69 //TODO: do not get the distribution objcts, but the label of the status.
70 Map
<NamedArea
, Set
<DescriptionElementBase
>> distributionMap
= editor
.taxonDistributionMap
.get(rowObject
.getTaxonUuid());
71 if (distributionMap
!= null){
72 Set
<DescriptionElementBase
> distributionsForArea
= editor
.taxonDistributionMap
.get(rowObject
.getTaxonUuid()).get(area
);
73 if (distributionsForArea
== null){
76 if (distributionsForArea
.size() == 1){
77 return distributionsForArea
.iterator().next();
79 if (distributionsForArea
.size() > 1){
80 List
<String
> labels
= new ArrayList();
81 distributionsForArea
.forEach(desc
-> labels
.add(((Distribution
)desc
).getStatus().getLabel()));
82 return distributionsForArea
;
90 public int getColumnCount() {
91 return editor
.getPropertyToLabelMap().size();
95 public String
getColumnProperty(int columnIndex
) {
96 return editor
.getPropertyToLabelMap().get(columnIndex
);
100 public int getColumnIndex(String propertyName
){
101 return editor
.getPropertyToLabelMap().indexOf(propertyName
);
105 public void setDataValue(TaxonDistributionDTO taxonWrapper
, int columnIndex
, Object newValue
) {
106 if (newValue
instanceof StatusHelper
){
107 NamedArea area
=editor
.getAreaToColumnIndexMap().get(columnIndex
);
108 Map
<NamedArea
, Set
<DescriptionElementBase
>> distributionMap
= editor
.taxonDistributionMap
.get(taxonWrapper
.getTaxonUuid());
109 if (distributionMap
== null){
110 distributionMap
= new HashMap();
111 editor
.taxonDistributionMap
.put(taxonWrapper
.getTaxonUuid(),distributionMap
);
113 Distribution dist
= null;
114 Set
<DescriptionElementBase
> distributions
= distributionMap
.get(area
);
115 if (distributions
!= null && !distributions
.isEmpty()){
116 DescriptionElementBase desc
= distributions
.iterator().next();
117 if (desc
instanceof Distribution
){
118 if (((StatusHelper
)newValue
).term
.getId() == 0){
119 desc
.getInDescription().removeElement(desc
);
120 distributions
.remove(desc
);
122 ((Distribution
)desc
).setStatus(((StatusHelper
)newValue
).term
);
126 if (((StatusHelper
)newValue
).term
.getId() == 0){
129 if (distributions
== null){
130 distributions
= new HashSet();
131 distributionMap
.put(area
, distributions
);
133 dist
= Distribution
.NewInstance(area
, ((StatusHelper
)newValue
).term
);
134 Set
<TaxonDescription
> descs
= taxonWrapper
.getDescriptionsWrapper().getDescriptions();
135 TaxonDescription desc
= null;
136 if (descs
.size() >= 1){
137 if (PreferencesUtil
.getDescriptionForChecklistEditor().equals(DistributionDescription
.AlwaysDefault
)){
138 Iterator
<TaxonDescription
> itDesc
= descs
.iterator();
139 while(itDesc
.hasNext()) {
140 TaxonDescription next
= itDesc
.next();
141 if (next
.isDefault()) {
148 if (PreferencesUtil
.getDescriptionForChecklistEditor().equals(DistributionDescription
.AlwaysDefault
)){
149 desc
= TaxonDescription
.NewInstance();
150 desc
.setDefault(true);
152 desc
= descs
.iterator().next();
156 desc
= TaxonDescription
.NewInstance();
157 desc
.setDefault(true);
159 taxonWrapper
.getDescriptionsWrapper().getDescriptions().add(desc
);
161 if (editor
.getDefaultSource() != null){
162 dist
.addSource(DescriptionElementSource
.NewPrimarySourceInstance(editor
.getDefaultSource(), null));
164 desc
.addElement(dist
);
165 if(desc
.isPersisted()){
166 editor
.part
.getCdmEntitySession().load(desc
, true);
168 distributions
.add(dist
);
169 // EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS_DISTRIBUTION, taxonWrapper.getNameCache());
172 editor
.setActualNameCache(taxonWrapper
.getNameCache());
174 editor
.descriptionsToSave
.add(taxonWrapper
);
177 EventUtility
.postEvent(WorkbenchEventConstants
.REFRESH_DETAILS
, true);