ref #7854: minor
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / checklist / e4 / DistributionColumnAccessor.java
1 /**
2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.taxeditor.editor.view.checklist.e4;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17
18 import javax.inject.Inject;
19
20 import org.eclipse.e4.core.services.events.IEventBroker;
21 import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
22
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.PresenceAbsenceTerm;
28 import eu.etaxonomy.cdm.model.description.TaxonDescription;
29 import eu.etaxonomy.cdm.model.location.NamedArea;
30 import eu.etaxonomy.taxeditor.event.EventUtility;
31 import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
32
33
34 /**
35 * @author k.luther
36 * @since 28.11.2018
37 *
38 */
39 public class DistributionColumnAccessor implements IColumnPropertyAccessor<TaxonDistributionDTO> {
40 private DistributionEditor editor;
41 public static final String DEFAULT_ENTRY = "";
42 @Inject
43 private IEventBroker eventBroker;
44
45 public DistributionColumnAccessor(DistributionEditor editor) {
46 this.editor = editor;
47 }
48
49
50 /**
51 * {@inheritDoc}
52 */
53 @Override
54 public Object getDataValue(TaxonDistributionDTO rowObject, int columnIndex) {
55 switch (columnIndex) {
56 case 0:
57 return rowObject.getNameCache();
58 case 1:
59 if (editor.isShowRank()){
60 return rowObject.getRankString();
61 }else{
62 break;
63 }
64 default:
65 break;
66 }
67 NamedArea area = editor.getAreaToColumnIndexMap().get(columnIndex);
68
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){
74 return null;
75 }
76 if (distributionsForArea.size() == 1){
77 return distributionsForArea.iterator().next();
78 }
79 if (distributionsForArea.size() > 1){
80 List<String> labels = new ArrayList();
81 distributionsForArea.forEach(desc -> labels.add(((Distribution)desc).getStatus().getLabel()));
82 return labels;
83 }
84 }
85 return null;
86
87 }
88
89
90 /**
91 * {@inheritDoc}
92 */
93 @Override
94 public int getColumnCount() {
95 return editor.getPropertyToLabelMap().size();
96 }
97
98 /**
99 * {@inheritDoc}
100 */
101 @Override
102 public String getColumnProperty(int columnIndex) {
103 return editor.getPropertyToLabelMap().get(columnIndex);
104 }
105
106 /**
107 * {@inheritDoc}
108 */
109 @Override
110 public int getColumnIndex(String propertyName){
111 return editor.getPropertyToLabelMap().indexOf(propertyName);
112 }
113
114
115 /**
116 * {@inheritDoc}
117 */
118 @Override
119 public void setDataValue(TaxonDistributionDTO taxonWrapper, int columnIndex, Object newValue) {
120 if (newValue instanceof PresenceAbsenceTerm){
121 NamedArea area =editor.getAreaToColumnIndexMap().get(columnIndex);
122 Map<NamedArea, Set<DescriptionElementBase>> distributionMap = editor.taxonDistributionMap.get(taxonWrapper.getTaxonUuid());
123 if (distributionMap == null){
124 distributionMap = new HashMap();
125 editor.taxonDistributionMap.put(taxonWrapper.getTaxonUuid(),distributionMap);
126 }
127 Distribution dist = null;
128 Set<DescriptionElementBase> distributions = distributionMap.get(area);
129 if (distributions != null && !distributions.isEmpty()){
130 DescriptionElementBase desc = distributions.iterator().next();
131 if (desc instanceof Distribution){
132 if (((PresenceAbsenceTerm)newValue).getId() == 0){
133 desc.getInDescription().removeElement(desc);
134 distributions.remove(desc);
135 }else {
136 ((Distribution)desc).setStatus((PresenceAbsenceTerm)newValue);
137 }
138 }
139 }else{
140 if (distributions == null){
141 distributions = new HashSet();
142 distributionMap.put(area, distributions);
143 }
144 dist = Distribution.NewInstance(area, (PresenceAbsenceTerm)newValue);
145 Set<TaxonDescription> descs = taxonWrapper.getDescriptionsWrapper().getDescriptions();
146 TaxonDescription desc;
147 if (descs.size() >= 1){
148 desc = descs.iterator().next();
149 }else {
150 desc = TaxonDescription.NewInstance();
151 taxonWrapper.getDescriptionsWrapper().getDescriptions().add(desc);
152 }
153 if (editor.getDefaultSource() != null){
154 dist.addSource(DescriptionElementSource.NewPrimarySourceInstance(editor.getDefaultSource(), null));
155 }
156 desc.addElement(dist);
157 editor.part.getCdmEntitySession().load(desc, true);
158 distributions.add(dist);
159
160
161 }
162
163
164 }
165 EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS, true);
166 }
167
168 }