fix problems with distribution editor
[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.Iterator;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
20
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;
31
32 /**
33 * @author k.luther
34 * @since 28.11.2018
35 */
36 public class DistributionColumnAccessor implements IColumnPropertyAccessor<TaxonDistributionDTO> {
37
38 public static final String DEFAULT_ENTRY = "";
39
40 private DistributionEditor editor;
41
42 public DistributionColumnAccessor(DistributionEditor editor) {
43 this.editor = editor;
44 }
45
46 @Override
47 public Object getDataValue(TaxonDistributionDTO rowObject, int columnIndex) {
48 // editor.setActualNameCache(rowObject.getNameCache());
49 switch (columnIndex) {
50 case 0:
51 return rowObject.getNameCache();
52 case 1:
53 if (editor.isShowRank()){
54 return rowObject.getRankString();
55 }else{
56 return rowObject.getConcatenatedSynonyms();
57 }
58 case 2:
59 if (editor.isShowRank()){
60 return rowObject.getConcatenatedSynonyms();
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 distributionsForArea;
83 }
84 }
85 return null;
86
87 }
88
89 @Override
90 public int getColumnCount() {
91 return editor.getPropertyToLabelMap().size();
92 }
93
94 @Override
95 public String getColumnProperty(int columnIndex) {
96 return editor.getPropertyToLabelMap().get(columnIndex);
97 }
98
99 @Override
100 public int getColumnIndex(String propertyName){
101 return editor.getPropertyToLabelMap().indexOf(propertyName);
102 }
103
104 @Override
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);
112 }
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);
121 }else {
122 ((Distribution)desc).setStatus(((StatusHelper)newValue).term);
123 }
124 }
125 }else{
126 if (((StatusHelper)newValue).term.getId() == 0){
127 return;
128 }
129 if (distributions == null){
130 distributions = new HashSet();
131 distributionMap.put(area, distributions);
132 }
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()) {
142 desc = next;
143 break;
144 }
145 }
146 }
147 if (desc == null) {
148 if (PreferencesUtil.getDescriptionForChecklistEditor().equals(DistributionDescription.AlwaysDefault)){
149 desc = TaxonDescription.NewInstance();
150 desc.setDefault(true);
151 }else {
152 desc = descs.iterator().next();
153 }
154 }
155 }else {
156 desc = TaxonDescription.NewInstance();
157 desc.setDefault(true);
158
159 taxonWrapper.getDescriptionsWrapper().getDescriptions().add(desc);
160 }
161 if (editor.getDefaultSource() != null){
162 dist.addSource(DescriptionElementSource.NewPrimarySourceInstance(editor.getDefaultSource(), null));
163 }
164 desc.addElement(dist);
165 if(desc.isPersisted()){
166 editor.part.getCdmEntitySession().load(desc, true);
167 }
168 distributions.add(dist);
169 // EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS_DISTRIBUTION, taxonWrapper.getNameCache());
170
171 }
172 editor.setActualNameCache(taxonWrapper.getNameCache());
173
174 editor.descriptionsToSave.add(taxonWrapper);
175 }
176
177 EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS, true);
178 }
179 }