ref #7854: fix exceptions in distribution editor when saving new created descriptions
[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 org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
19
20 import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
21 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
22 import eu.etaxonomy.cdm.model.description.Distribution;
23 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
24 import eu.etaxonomy.cdm.model.description.TaxonDescription;
25 import eu.etaxonomy.cdm.model.location.NamedArea;
26
27
28 /**
29 * @author k.luther
30 * @since 28.11.2018
31 *
32 */
33 public class DistributionColumnAccessor implements IColumnPropertyAccessor<TaxonDistributionDTO> {
34 private DistributionEditor editor;
35 public static final String DEFAULT_ENTRY = "";
36
37 public DistributionColumnAccessor(DistributionEditor editor) {
38 this.editor = editor;
39 }
40
41
42 /**
43 * {@inheritDoc}
44 */
45 @Override
46 public Object getDataValue(TaxonDistributionDTO rowObject, int columnIndex) {
47 switch (columnIndex) {
48 case 0:
49 return rowObject.getNameCache();
50 case 1:
51 if (editor.isShowRank()){
52 return rowObject.getRankString();
53 }else{
54 break;
55 }
56 default:
57 break;
58 }
59 NamedArea area = editor.getAreaToColumnIndexMap().get(columnIndex);
60
61 //TODO: do not get the distribution objcts, but the label of the status.
62 Map<NamedArea, Set<DescriptionElementBase>> distributionMap = editor.taxonDistributionMap.get(rowObject.getTaxonUuid());
63 if (distributionMap != null){
64 Set<DescriptionElementBase> distributionsForArea = editor.taxonDistributionMap.get(rowObject.getTaxonUuid()).get(area);
65 if (distributionsForArea == null){
66 return null;
67 }
68 if (distributionsForArea.size() == 1){
69 return distributionsForArea.iterator().next();
70 }
71 if (distributionsForArea.size() > 1){
72 List<String> labels = new ArrayList();
73 distributionsForArea.forEach(desc -> labels.add(((Distribution)desc).getStatus().getLabel()));
74 return labels;
75 }
76 }
77 return null;
78
79 }
80
81
82 /**
83 * {@inheritDoc}
84 */
85 @Override
86 public int getColumnCount() {
87 return editor.getPropertyToLabelMap().size();
88 }
89
90 /**
91 * {@inheritDoc}
92 */
93 @Override
94 public String getColumnProperty(int columnIndex) {
95 return editor.getPropertyToLabelMap().get(columnIndex);
96 }
97
98 /**
99 * {@inheritDoc}
100 */
101 @Override
102 public int getColumnIndex(String propertyName){
103 return editor.getPropertyToLabelMap().indexOf(propertyName);
104 }
105
106
107 /**
108 * {@inheritDoc}
109 */
110 @Override
111 public void setDataValue(TaxonDistributionDTO taxonWrapper, int columnIndex, Object newValue) {
112 if (newValue instanceof PresenceAbsenceTerm){
113 NamedArea area =editor.getAreaToColumnIndexMap().get(columnIndex);
114 Map<NamedArea, Set<DescriptionElementBase>> distributionMap = editor.taxonDistributionMap.get(taxonWrapper.getTaxonUuid());
115 if (distributionMap == null){
116 distributionMap = new HashMap();
117 editor.taxonDistributionMap.put(taxonWrapper.getTaxonUuid(),distributionMap);
118 }
119 Set<DescriptionElementBase> distributions = distributionMap.get(area);
120 if (distributions != null && !distributions.isEmpty()){
121 DescriptionElementBase desc = distributions.iterator().next();
122 if (desc instanceof Distribution){
123 if (((PresenceAbsenceTerm)newValue).getId() == 0){
124 desc.getInDescription().removeElement(desc);
125 distributions.remove(desc);
126 }else {
127 ((Distribution)desc).setStatus((PresenceAbsenceTerm)newValue);
128 }
129 }
130 }else{
131 if (distributions == null){
132 distributions = new HashSet();
133 distributionMap.put(area, distributions);
134 }
135 Distribution dist = Distribution.NewInstance(area, (PresenceAbsenceTerm)newValue);
136 Set<TaxonDescription> descs = taxonWrapper.getDescriptionsWrapper().getDescriptions();
137 TaxonDescription desc;
138 if (descs.size() >= 1){
139 desc = descs.iterator().next();
140 }else {
141 desc = TaxonDescription.NewInstance();
142 taxonWrapper.getDescriptionsWrapper().getDescriptions().add(desc);
143 }
144 desc.addElement(dist);
145 editor.part.getCdmEntitySession().load(desc, true);
146 distributions.add(dist);
147
148 }
149
150 }
151
152 }
153
154 }