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 org
.eclipse
.nebula
.widgets
.nattable
.data
.IColumnPropertyAccessor
;
20 import eu
.etaxonomy
.cdm
.api
.service
.dto
.TaxonDistributionDTO
;
21 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
22 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementSource
;
23 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
24 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
25 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
26 import eu
.etaxonomy
.taxeditor
.event
.EventUtility
;
27 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
33 public class DistributionColumnAccessor
implements IColumnPropertyAccessor
<TaxonDistributionDTO
> {
35 public static final String DEFAULT_ENTRY
= "";
37 private DistributionEditor editor
;
39 public DistributionColumnAccessor(DistributionEditor editor
) {
44 public Object
getDataValue(TaxonDistributionDTO rowObject
, int columnIndex
) {
45 // editor.setActualNameCache(rowObject.getNameCache());
46 switch (columnIndex
) {
48 return rowObject
.getNameCache();
50 if (editor
.isShowRank()){
51 return rowObject
.getRankString();
53 return rowObject
.getConcatenatedSynonyms();
56 if (editor
.isShowRank()){
57 return rowObject
.getConcatenatedSynonyms();
64 NamedArea area
= editor
.getAreaToColumnIndexMap().get(columnIndex
);
66 //TODO: do not get the distribution objcts, but the label of the status.
67 Map
<NamedArea
, Set
<DescriptionElementBase
>> distributionMap
= editor
.taxonDistributionMap
.get(rowObject
.getTaxonUuid());
68 if (distributionMap
!= null){
69 Set
<DescriptionElementBase
> distributionsForArea
= editor
.taxonDistributionMap
.get(rowObject
.getTaxonUuid()).get(area
);
70 if (distributionsForArea
== null){
73 if (distributionsForArea
.size() == 1){
74 return distributionsForArea
.iterator().next();
76 if (distributionsForArea
.size() > 1){
77 List
<String
> labels
= new ArrayList();
78 distributionsForArea
.forEach(desc
-> labels
.add(((Distribution
)desc
).getStatus().getLabel()));
79 return distributionsForArea
;
87 public int getColumnCount() {
88 return editor
.getPropertyToLabelMap().size();
92 public String
getColumnProperty(int columnIndex
) {
93 return editor
.getPropertyToLabelMap().get(columnIndex
);
97 public int getColumnIndex(String propertyName
){
98 return editor
.getPropertyToLabelMap().indexOf(propertyName
);
102 public void setDataValue(TaxonDistributionDTO taxonWrapper
, int columnIndex
, Object newValue
) {
103 if (newValue
instanceof StatusHelper
){
104 NamedArea area
=editor
.getAreaToColumnIndexMap().get(columnIndex
);
105 Map
<NamedArea
, Set
<DescriptionElementBase
>> distributionMap
= editor
.taxonDistributionMap
.get(taxonWrapper
.getTaxonUuid());
106 if (distributionMap
== null){
107 distributionMap
= new HashMap();
108 editor
.taxonDistributionMap
.put(taxonWrapper
.getTaxonUuid(),distributionMap
);
110 Distribution dist
= null;
111 Set
<DescriptionElementBase
> distributions
= distributionMap
.get(area
);
112 if (distributions
!= null && !distributions
.isEmpty()){
113 DescriptionElementBase desc
= distributions
.iterator().next();
114 if (desc
instanceof Distribution
){
115 if (((StatusHelper
)newValue
).term
.getId() == 0){
116 desc
.getInDescription().removeElement(desc
);
117 distributions
.remove(desc
);
119 ((Distribution
)desc
).setStatus(((StatusHelper
)newValue
).term
);
123 if (((StatusHelper
)newValue
).term
.getId() == 0){
126 if (distributions
== null){
127 distributions
= new HashSet();
128 distributionMap
.put(area
, distributions
);
130 dist
= Distribution
.NewInstance(area
, ((StatusHelper
)newValue
).term
);
131 Set
<TaxonDescription
> descs
= taxonWrapper
.getDescriptionsWrapper().getDescriptions();
132 TaxonDescription desc
;
133 if (descs
.size() >= 1){
134 desc
= descs
.iterator().next();
136 desc
= TaxonDescription
.NewInstance();
137 taxonWrapper
.getDescriptionsWrapper().getDescriptions().add(desc
);
139 if (editor
.getDefaultSource() != null){
140 dist
.addSource(DescriptionElementSource
.NewPrimarySourceInstance(editor
.getDefaultSource(), null));
142 desc
.addElement(dist
);
143 if(desc
.isPersited()){
144 editor
.part
.getCdmEntitySession().load(desc
, true);
146 distributions
.add(dist
);
147 // EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS_DISTRIBUTION, taxonWrapper.getNameCache());
150 editor
.setActualNameCache(taxonWrapper
.getNameCache());
152 editor
.descriptionsToSave
.add(taxonWrapper
);
155 EventUtility
.postEvent(WorkbenchEventConstants
.REFRESH_DETAILS
, true);