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