1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2017 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.vaadin.util;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Arrays;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.List;
|
17
|
import java.util.Map;
|
18
|
import java.util.Set;
|
19
|
import java.util.UUID;
|
20
|
|
21
|
import org.springframework.transaction.TransactionStatus;
|
22
|
import org.vaadin.addons.lazyquerycontainer.Query;
|
23
|
import org.vaadin.addons.lazyquerycontainer.QueryDefinition;
|
24
|
|
25
|
import com.vaadin.data.Item;
|
26
|
import com.vaadin.data.util.ObjectProperty;
|
27
|
import com.vaadin.data.util.PropertysetItem;
|
28
|
|
29
|
import eu.etaxonomy.cdm.api.application.CdmRepository;
|
30
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
31
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
32
|
import eu.etaxonomy.cdm.model.description.Feature;
|
33
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
34
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
35
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
36
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
37
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
38
|
|
39
|
/**
|
40
|
* @author freimeier
|
41
|
* @date 16.11.2017
|
42
|
*
|
43
|
*/
|
44
|
|
45
|
public class DistributionStatusQuery implements Query{
|
46
|
public static final String DTYPE_COLUMN = "DTYPE";
|
47
|
public static final String ID_COLUMN = "id";
|
48
|
public static final String UUID_COLUMN = "uuid";
|
49
|
public static final String CLASSIFICATION_COLUMN = "classification";
|
50
|
public static final String RANK_COLUMN = "Rang";
|
51
|
public static final String TAXON_COLUMN = "Taxon";
|
52
|
|
53
|
private CdmRepository repo;
|
54
|
private QueryDefinition definition;
|
55
|
private List<UUID> nodeUuids;
|
56
|
private List<NamedArea> namedAreas;
|
57
|
|
58
|
public DistributionStatusQuery(CdmRepository repo, List<UUID> nodeUuids, List<NamedArea> namedAreas, QueryDefinition definition) {
|
59
|
this.repo = repo;
|
60
|
this.nodeUuids = nodeUuids;
|
61
|
this.namedAreas = namedAreas;
|
62
|
this.definition = definition;
|
63
|
}
|
64
|
|
65
|
/* (non-Javadoc)
|
66
|
* @see org.vaadin.addons.lazyquerycontainer.Query#constructItem()
|
67
|
*/
|
68
|
@Override
|
69
|
public Item constructItem() {
|
70
|
return null;
|
71
|
}
|
72
|
|
73
|
/* (non-Javadoc)
|
74
|
* @see org.vaadin.addons.lazyquerycontainer.Query#deleteAllItems()
|
75
|
*/
|
76
|
@Override
|
77
|
public boolean deleteAllItems() {
|
78
|
return false;
|
79
|
}
|
80
|
|
81
|
/* (non-Javadoc)
|
82
|
* @see org.vaadin.addons.lazyquerycontainer.Query#loadItems(int, int)
|
83
|
*/
|
84
|
@Override
|
85
|
public List<Item> loadItems(int startIndex, int count) {
|
86
|
List<Item> items = new ArrayList<>();
|
87
|
for(int i=startIndex; i<startIndex+count; i++) {
|
88
|
if(i<this.size()) {
|
89
|
UUID nodeUuid = this.nodeUuids.get(i);
|
90
|
final TaxonNode taxonNode = CdmSpringContextHelper.getTaxonNodeService().load(nodeUuid, Arrays.asList("taxon"));
|
91
|
final Taxon taxon = taxonNode.getTaxon();
|
92
|
Item item = new PropertysetItem();
|
93
|
item.addItemProperty(UUID_COLUMN, new ObjectProperty<>(taxon.getUuid(), UUID.class));
|
94
|
item.addItemProperty(TAXON_COLUMN, new ObjectProperty<>(taxon.getTitleCache(), String.class));
|
95
|
Map<NamedArea,PresenceAbsenceTerm> distributionStatusMap = this.getAreaToDistributionStatusMap(taxon);
|
96
|
for(NamedArea namedArea : this.namedAreas) {
|
97
|
PresenceAbsenceTerm distributionStatus = distributionStatusMap.get(namedArea);
|
98
|
UUID distributionStatusUuid = (distributionStatus != null) ? distributionStatus.getUuid() : null;
|
99
|
item.addItemProperty(namedArea.getUuid(), new ObjectProperty<>(distributionStatusUuid, UUID.class));
|
100
|
}
|
101
|
if(definition.getFilters().parallelStream().allMatch(f -> f.passesFilter(nodeUuid, item))) {
|
102
|
items.add(item);
|
103
|
}
|
104
|
}
|
105
|
}
|
106
|
return items;
|
107
|
}
|
108
|
|
109
|
/* (non-Javadoc)
|
110
|
* @see org.vaadin.addons.lazyquerycontainer.Query#saveItems(java.util.List, java.util.List, java.util.List)
|
111
|
*/
|
112
|
@Override
|
113
|
public void saveItems(List<Item> addedItems, List<Item> modifiedItems, List<Item> removedItems) {
|
114
|
System.out.println("Save requested!");
|
115
|
for(Item item : modifiedItems) {
|
116
|
final UUID taxonUuid = (UUID) item.getItemProperty(UUID_COLUMN).getValue();
|
117
|
final Taxon taxon = (Taxon) CdmSpringContextHelper.getTaxonService().find(taxonUuid);
|
118
|
Map<NamedArea, UUID> areaToDistributionStatusUuidMap = new HashMap<>();
|
119
|
for(NamedArea namedArea : this.namedAreas) {
|
120
|
areaToDistributionStatusUuidMap.put(namedArea, (UUID) item.getItemProperty(namedArea.getUuid()).getValue());
|
121
|
}
|
122
|
this.updateDistributionStatus(taxon, areaToDistributionStatusUuidMap);
|
123
|
}
|
124
|
}
|
125
|
|
126
|
/* (non-Javadoc)
|
127
|
* @see org.vaadin.addons.lazyquerycontainer.Query#size()
|
128
|
*/
|
129
|
@Override
|
130
|
public int size() {
|
131
|
return this.nodeUuids.size();
|
132
|
}
|
133
|
|
134
|
private Map<NamedArea, PresenceAbsenceTerm> getAreaToDistributionStatusMap(Taxon taxon) {
|
135
|
Map<NamedArea, PresenceAbsenceTerm> distributionStatusMap = new HashMap<>();
|
136
|
|
137
|
List<Distribution> distributions = this.getDistributions(taxon);
|
138
|
for(Distribution dist : distributions){
|
139
|
if(dist.getArea()!=null){
|
140
|
distributionStatusMap.put(dist.getArea(), dist.getStatus());
|
141
|
}
|
142
|
}
|
143
|
|
144
|
return distributionStatusMap;
|
145
|
}
|
146
|
|
147
|
private void updateDistributionStatus(Taxon taxon, Map<NamedArea, UUID> areaToDistributionStatusUuidMap) {
|
148
|
CdmRepository repo = this.repo;
|
149
|
TransactionStatus tx = repo.startTransaction();
|
150
|
taxon = (Taxon)repo.getTaxonService().find(taxon.getUuid());
|
151
|
|
152
|
List<Distribution> distributions = this.getDistributions(taxon);
|
153
|
|
154
|
for(NamedArea namedArea : areaToDistributionStatusUuidMap.keySet()) {
|
155
|
Distribution distribution = null;
|
156
|
for(Distribution dist : distributions){
|
157
|
if(dist.getArea()!= null && dist.getArea().equals(namedArea)){
|
158
|
distribution = dist;
|
159
|
break;
|
160
|
}
|
161
|
}
|
162
|
|
163
|
PresenceAbsenceTerm distributionStatus = null;
|
164
|
try {
|
165
|
distributionStatus = (PresenceAbsenceTerm) CdmSpringContextHelper.getTermService().load(areaToDistributionStatusUuidMap.get(namedArea));
|
166
|
} catch (IllegalArgumentException iae) {
|
167
|
distributionStatus = null;
|
168
|
}
|
169
|
|
170
|
if(distribution==null){
|
171
|
//create new distribution
|
172
|
distribution = Distribution.NewInstance(namedArea, distributionStatus);
|
173
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
174
|
if (descriptions != null && !descriptions.isEmpty()) {
|
175
|
for (TaxonDescription desc : descriptions) {
|
176
|
// add to first taxon description
|
177
|
desc.addElement(distribution);
|
178
|
}
|
179
|
} else {// there are no TaxonDescription yet.
|
180
|
TaxonDescription taxonDescription = TaxonDescription.NewInstance(taxon);
|
181
|
taxonDescription.addElement(distribution);
|
182
|
}
|
183
|
}
|
184
|
else if(distributionStatus == null){
|
185
|
//delete descriptionElementBase
|
186
|
DescriptionBase<?> desc = distribution.getInDescription();
|
187
|
desc.removeElement(distribution);
|
188
|
}
|
189
|
else if(!distributionStatus.equals(distribution.getStatus())){
|
190
|
//update distribution
|
191
|
distribution.setStatus(distributionStatus);
|
192
|
repo.getCommonService().saveOrUpdate(distribution);
|
193
|
}
|
194
|
}
|
195
|
repo.commitTransaction(tx);
|
196
|
}
|
197
|
|
198
|
private List<Distribution> getDistributions(Taxon taxon) {
|
199
|
Set<Feature> setFeature = new HashSet<>(Arrays.asList(Feature.DISTRIBUTION()));
|
200
|
List<Distribution> listTaxonDescription = CdmSpringContextHelper.getDescriptionService()
|
201
|
.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
|
202
|
return listTaxonDescription;
|
203
|
}
|
204
|
|
205
|
private static final List<String> DESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
|
206
|
"$",
|
207
|
"elements.*",
|
208
|
"elements.sources.citation.authorship.$",
|
209
|
"elements.sources.nameUsedInSource.originalNameString",
|
210
|
"elements.area.level",
|
211
|
"elements.modifyingText",
|
212
|
"elements.states.*",
|
213
|
"elements.media",
|
214
|
"elements.multilanguageText",
|
215
|
"multilanguageText",
|
216
|
"stateData.$",
|
217
|
"annotations",
|
218
|
"markers",
|
219
|
"sources.citation.authorship",
|
220
|
"sources.nameUsedInSource",
|
221
|
"multilanguageText",
|
222
|
"media",
|
223
|
"name.$",
|
224
|
"name.rank.representations",
|
225
|
"name.status.type.representations",
|
226
|
"taxon2.name",
|
227
|
});
|
228
|
}
|