Merge branch 'release/5.8.0'
[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 switch (columnIndex) {
55 case 0:
56 return rowObject.getNameCache();
57 case 1:
58 if (editor.isShowRank()){
59 return rowObject.getRankString();
60 }else{
61 break;
62 }
63 default:
64 break;
65 }
66 NamedArea area = editor.getAreaToColumnIndexMap().get(columnIndex);
67
68 //TODO: do not get the distribution objcts, but the label of the status.
69 Map<NamedArea, Set<DescriptionElementBase>> distributionMap = editor.taxonDistributionMap.get(rowObject.getTaxonUuid());
70 if (distributionMap != null){
71 Set<DescriptionElementBase> distributionsForArea = editor.taxonDistributionMap.get(rowObject.getTaxonUuid()).get(area);
72 if (distributionsForArea == null){
73 return null;
74 }
75 if (distributionsForArea.size() == 1){
76 return distributionsForArea.iterator().next();
77 }
78 if (distributionsForArea.size() > 1){
79 List<String> labels = new ArrayList();
80 distributionsForArea.forEach(desc -> labels.add(((Distribution)desc).getStatus().getLabel()));
81 return labels;
82 }
83 }
84 return null;
85
86 }
87
88
89 /**
90 * {@inheritDoc}
91 */
92 @Override
93 public int getColumnCount() {
94 return editor.getPropertyToLabelMap().size();
95 }
96
97 /**
98 * {@inheritDoc}
99 */
100 @Override
101 public String getColumnProperty(int columnIndex) {
102 return editor.getPropertyToLabelMap().get(columnIndex);
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 @Override
109 public int getColumnIndex(String propertyName){
110 return editor.getPropertyToLabelMap().indexOf(propertyName);
111 }
112
113
114 /**
115 * {@inheritDoc}
116 */
117 @Override
118 public void setDataValue(TaxonDistributionDTO taxonWrapper, int columnIndex, Object newValue) {
119 if (newValue instanceof StatusHelper){
120 NamedArea area =editor.getAreaToColumnIndexMap().get(columnIndex);
121 Map<NamedArea, Set<DescriptionElementBase>> distributionMap = editor.taxonDistributionMap.get(taxonWrapper.getTaxonUuid());
122 if (distributionMap == null){
123 distributionMap = new HashMap();
124 editor.taxonDistributionMap.put(taxonWrapper.getTaxonUuid(),distributionMap);
125 }
126 Distribution dist = null;
127 Set<DescriptionElementBase> distributions = distributionMap.get(area);
128 if (distributions != null && !distributions.isEmpty()){
129 DescriptionElementBase desc = distributions.iterator().next();
130 if (desc instanceof Distribution){
131 if (((StatusHelper)newValue).term.getId() == 0){
132 desc.getInDescription().removeElement(desc);
133 distributions.remove(desc);
134 }else {
135 ((Distribution)desc).setStatus(((StatusHelper)newValue).term);
136 }
137 }
138 }else{
139 if (((StatusHelper)newValue).term.getId() == 0){
140 return;
141 }
142 if (distributions == null){
143 distributions = new HashSet();
144 distributionMap.put(area, distributions);
145 }
146 dist = Distribution.NewInstance(area, ((StatusHelper)newValue).term);
147 Set<TaxonDescription> descs = taxonWrapper.getDescriptionsWrapper().getDescriptions();
148 TaxonDescription desc;
149 if (descs.size() >= 1){
150 desc = descs.iterator().next();
151 }else {
152 desc = TaxonDescription.NewInstance();
153 taxonWrapper.getDescriptionsWrapper().getDescriptions().add(desc);
154 }
155 if (editor.getDefaultSource() != null){
156 dist.addSource(DescriptionElementSource.NewPrimarySourceInstance(editor.getDefaultSource(), null));
157 }
158 desc.addElement(dist);
159 if(desc.isPersited()){
160 editor.part.getCdmEntitySession().load(desc, true);
161 }
162 distributions.add(dist);
163
164
165 }
166
167 editor.descriptionsToSave.add(taxonWrapper);
168 }
169 EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS, true);
170 }
171
172 }