Merge branch 'hotfix/5.18.2'
[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.TaxonDescription;
28 import eu.etaxonomy.cdm.model.location.NamedArea;
29 import eu.etaxonomy.taxeditor.event.EventUtility;
30 import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
31
32
33 /**
34 * @author k.luther
35 * @since 28.11.2018
36 *
37 */
38 public class DistributionColumnAccessor implements IColumnPropertyAccessor<TaxonDistributionDTO> {
39 private DistributionEditor editor;
40 public static final String DEFAULT_ENTRY = "";
41 @Inject
42 private IEventBroker eventBroker;
43
44 public DistributionColumnAccessor(DistributionEditor editor) {
45 this.editor = editor;
46 }
47
48
49 /**
50 * {@inheritDoc}
51 */
52 @Override
53 public Object getDataValue(TaxonDistributionDTO rowObject, int columnIndex) {
54 // editor.setActualNameCache(rowObject.getNameCache());
55 switch (columnIndex) {
56 case 0:
57 return rowObject.getNameCache();
58 case 1:
59 if (editor.isShowRank()){
60 return rowObject.getRankString();
61 }else{
62 return rowObject.getConcatenatedSynonyms();
63 }
64 case 2:
65 if (editor.isShowRank()){
66 return rowObject.getConcatenatedSynonyms();
67 }else{
68 break;
69 }
70 default:
71 break;
72 }
73 NamedArea area = editor.getAreaToColumnIndexMap().get(columnIndex);
74
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){
80 return null;
81 }
82 if (distributionsForArea.size() == 1){
83 return distributionsForArea.iterator().next();
84 }
85 if (distributionsForArea.size() > 1){
86 List<String> labels = new ArrayList();
87 distributionsForArea.forEach(desc -> labels.add(((Distribution)desc).getStatus().getLabel()));
88 return distributionsForArea;
89 }
90 }
91 return null;
92
93 }
94
95
96 /**
97 * {@inheritDoc}
98 */
99 @Override
100 public int getColumnCount() {
101 return editor.getPropertyToLabelMap().size();
102 }
103
104 /**
105 * {@inheritDoc}
106 */
107 @Override
108 public String getColumnProperty(int columnIndex) {
109 return editor.getPropertyToLabelMap().get(columnIndex);
110 }
111
112 /**
113 * {@inheritDoc}
114 */
115 @Override
116 public int getColumnIndex(String propertyName){
117 return editor.getPropertyToLabelMap().indexOf(propertyName);
118 }
119
120
121 /**
122 * {@inheritDoc}
123 */
124 @Override
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);
132 }
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);
141 }else {
142 ((Distribution)desc).setStatus(((StatusHelper)newValue).term);
143 }
144 }
145 }else{
146 if (((StatusHelper)newValue).term.getId() == 0){
147 return;
148 }
149 if (distributions == null){
150 distributions = new HashSet();
151 distributionMap.put(area, distributions);
152 }
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();
158 }else {
159 desc = TaxonDescription.NewInstance();
160 taxonWrapper.getDescriptionsWrapper().getDescriptions().add(desc);
161 }
162 if (editor.getDefaultSource() != null){
163 dist.addSource(DescriptionElementSource.NewPrimarySourceInstance(editor.getDefaultSource(), null));
164 }
165 desc.addElement(dist);
166 if(desc.isPersited()){
167 editor.part.getCdmEntitySession().load(desc, true);
168 }
169 distributions.add(dist);
170 // EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS_DISTRIBUTION, taxonWrapper.getNameCache());
171
172 }
173 editor.setActualNameCache(taxonWrapper.getNameCache());
174
175 editor.descriptionsToSave.add(taxonWrapper);
176 }
177
178 EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS, true);
179
180 }
181
182 }