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
.List
;
18 import javax
.inject
.Inject
;
20 import org
.eclipse
.e4
.core
.services
.events
.IEventBroker
;
21 import org
.eclipse
.nebula
.widgets
.nattable
.data
.IColumnPropertyAccessor
;
23 import eu
.etaxonomy
.cdm
.api
.service
.dto
.TaxonDistributionDTO
;
24 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
25 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementSource
;
26 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
27 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
28 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
29 import eu
.etaxonomy
.taxeditor
.event
.EventUtility
;
30 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
38 public class DistributionColumnAccessor
implements IColumnPropertyAccessor
<TaxonDistributionDTO
> {
39 private DistributionEditor editor
;
40 public static final String DEFAULT_ENTRY
= "";
42 private IEventBroker eventBroker
;
44 public DistributionColumnAccessor(DistributionEditor editor
) {
53 public Object
getDataValue(TaxonDistributionDTO rowObject
, int columnIndex
) {
54 // editor.setActualNameCache(rowObject.getNameCache());
55 switch (columnIndex
) {
57 return rowObject
.getNameCache();
59 if (editor
.isShowRank()){
60 return rowObject
.getRankString();
62 return rowObject
.getConcatenatedSynonyms();
65 if (editor
.isShowRank()){
66 return rowObject
.getConcatenatedSynonyms();
73 NamedArea area
= editor
.getAreaToColumnIndexMap().get(columnIndex
);
75 //TODO: do not get the distribution objcts, but the label of the status.
76 Map
<NamedArea
, Set
<DescriptionElementBase
>> distributionMap
= editor
.taxonDistributionMap
.get(rowObject
.getTaxonUuid());
77 if (distributionMap
!= null){
78 Set
<DescriptionElementBase
> distributionsForArea
= editor
.taxonDistributionMap
.get(rowObject
.getTaxonUuid()).get(area
);
79 if (distributionsForArea
== null){
82 if (distributionsForArea
.size() == 1){
83 return distributionsForArea
.iterator().next();
85 if (distributionsForArea
.size() > 1){
86 List
<String
> labels
= new ArrayList();
87 distributionsForArea
.forEach(desc
-> labels
.add(((Distribution
)desc
).getStatus().getLabel()));
88 return distributionsForArea
;
100 public int getColumnCount() {
101 return editor
.getPropertyToLabelMap().size();
108 public String
getColumnProperty(int columnIndex
) {
109 return editor
.getPropertyToLabelMap().get(columnIndex
);
116 public int getColumnIndex(String propertyName
){
117 return editor
.getPropertyToLabelMap().indexOf(propertyName
);
125 public void setDataValue(TaxonDistributionDTO taxonWrapper
, int columnIndex
, Object newValue
) {
126 if (newValue
instanceof StatusHelper
){
127 NamedArea area
=editor
.getAreaToColumnIndexMap().get(columnIndex
);
128 Map
<NamedArea
, Set
<DescriptionElementBase
>> distributionMap
= editor
.taxonDistributionMap
.get(taxonWrapper
.getTaxonUuid());
129 if (distributionMap
== null){
130 distributionMap
= new HashMap();
131 editor
.taxonDistributionMap
.put(taxonWrapper
.getTaxonUuid(),distributionMap
);
133 Distribution dist
= null;
134 Set
<DescriptionElementBase
> distributions
= distributionMap
.get(area
);
135 if (distributions
!= null && !distributions
.isEmpty()){
136 DescriptionElementBase desc
= distributions
.iterator().next();
137 if (desc
instanceof Distribution
){
138 if (((StatusHelper
)newValue
).term
.getId() == 0){
139 desc
.getInDescription().removeElement(desc
);
140 distributions
.remove(desc
);
142 ((Distribution
)desc
).setStatus(((StatusHelper
)newValue
).term
);
146 if (((StatusHelper
)newValue
).term
.getId() == 0){
149 if (distributions
== null){
150 distributions
= new HashSet();
151 distributionMap
.put(area
, distributions
);
153 dist
= Distribution
.NewInstance(area
, ((StatusHelper
)newValue
).term
);
154 Set
<TaxonDescription
> descs
= taxonWrapper
.getDescriptionsWrapper().getDescriptions();
155 TaxonDescription desc
;
156 if (descs
.size() >= 1){
157 desc
= descs
.iterator().next();
159 desc
= TaxonDescription
.NewInstance();
160 taxonWrapper
.getDescriptionsWrapper().getDescriptions().add(desc
);
162 if (editor
.getDefaultSource() != null){
163 dist
.addSource(DescriptionElementSource
.NewPrimarySourceInstance(editor
.getDefaultSource(), null));
165 desc
.addElement(dist
);
166 if(desc
.isPersited()){
167 editor
.part
.getCdmEntitySession().load(desc
, true);
169 distributions
.add(dist
);
170 // EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS_DISTRIBUTION, taxonWrapper.getNameCache());
173 editor
.setActualNameCache(taxonWrapper
.getNameCache());
175 editor
.descriptionsToSave
.add(taxonWrapper
);
178 EventUtility
.postEvent(WorkbenchEventConstants
.REFRESH_DETAILS
, true);