1
|
/**
|
2
|
* Copyright (C) 2009 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.cdm.ext.geo;
|
10
|
|
11
|
import java.awt.Color;
|
12
|
import java.io.IOException;
|
13
|
import java.io.Reader;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.Arrays;
|
16
|
import java.util.Collection;
|
17
|
import java.util.EnumSet;
|
18
|
import java.util.HashSet;
|
19
|
import java.util.List;
|
20
|
import java.util.Map;
|
21
|
import java.util.Set;
|
22
|
import java.util.UUID;
|
23
|
|
24
|
import javax.persistence.EntityNotFoundException;
|
25
|
|
26
|
import org.apache.log4j.Logger;
|
27
|
import org.springframework.beans.factory.annotation.Autowired;
|
28
|
import org.springframework.stereotype.Service;
|
29
|
import org.springframework.transaction.annotation.Transactional;
|
30
|
|
31
|
import eu.etaxonomy.cdm.api.service.DistributionTree;
|
32
|
import eu.etaxonomy.cdm.api.service.dto.CondensedDistribution;
|
33
|
import eu.etaxonomy.cdm.api.service.dto.DistributionInfoDTO;
|
34
|
import eu.etaxonomy.cdm.api.service.dto.DistributionInfoDTO.InfoPart;
|
35
|
import eu.etaxonomy.cdm.api.utility.DescriptionUtility;
|
36
|
import eu.etaxonomy.cdm.api.utility.DistributionOrder;
|
37
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
38
|
import eu.etaxonomy.cdm.model.common.Language;
|
39
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
40
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
41
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
42
|
import eu.etaxonomy.cdm.model.description.Feature;
|
43
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
44
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
45
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
46
|
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
|
47
|
import eu.etaxonomy.cdm.model.location.Point;
|
48
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
49
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
50
|
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
|
51
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
52
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
53
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
54
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
55
|
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao;
|
56
|
import eu.etaxonomy.cdm.persistence.dao.occurrence.IOccurrenceDao;
|
57
|
import eu.etaxonomy.cdm.persistence.dao.term.IDefinedTermDao;
|
58
|
import eu.etaxonomy.cdm.persistence.dao.term.ITermVocabularyDao;
|
59
|
|
60
|
/**
|
61
|
* @author a.kohlbecker
|
62
|
* @since 18.06.2009
|
63
|
*
|
64
|
*/
|
65
|
@Service
|
66
|
@Transactional(readOnly = true)
|
67
|
public class EditGeoService implements IEditGeoService {
|
68
|
public static final Logger logger = Logger.getLogger(EditGeoService.class);
|
69
|
|
70
|
@Autowired
|
71
|
private IDescriptionDao dao;
|
72
|
|
73
|
@Autowired
|
74
|
private IGeoServiceAreaMapping areaMapping;
|
75
|
|
76
|
@Autowired
|
77
|
private ITermVocabularyDao vocabDao;
|
78
|
|
79
|
@Autowired
|
80
|
private IDefinedTermDao termDao;
|
81
|
|
82
|
@Autowired
|
83
|
private IOccurrenceDao occurrenceDao;
|
84
|
|
85
|
private Set<Feature> getDistributionFeatures() {
|
86
|
Set<Feature> distributionFeature = new HashSet<>();
|
87
|
Feature feature = (Feature) termDao.findByUuid(Feature.DISTRIBUTION().getUuid());
|
88
|
distributionFeature.add(feature);
|
89
|
return distributionFeature;
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* @param taxonDescriptions
|
94
|
* @return
|
95
|
*/
|
96
|
private Set<Distribution> getDistributionsOf(List<TaxonDescription> taxonDescriptions) {
|
97
|
Set<Distribution> result = new HashSet<>();
|
98
|
|
99
|
Set<Feature> features = getDistributionFeatures();
|
100
|
for (TaxonDescription taxonDescription : taxonDescriptions) {
|
101
|
List<Distribution> distributions;
|
102
|
if (taxonDescription.getId() > 0){
|
103
|
distributions = dao.getDescriptionElements(
|
104
|
taxonDescription,
|
105
|
null,
|
106
|
null /*features*/,
|
107
|
Distribution.class,
|
108
|
null,
|
109
|
null,
|
110
|
null);
|
111
|
}else{
|
112
|
distributions = new ArrayList<Distribution>();
|
113
|
for (DescriptionElementBase deb : taxonDescription.getElements()){
|
114
|
if (deb.isInstanceOf(Distribution.class)){
|
115
|
if (features == null || features.isEmpty()
|
116
|
|| features.contains(deb.getFeature())) {
|
117
|
distributions.add(CdmBase.deproxy(deb, Distribution.class));
|
118
|
}
|
119
|
}
|
120
|
}
|
121
|
}
|
122
|
result.addAll(distributions);
|
123
|
}
|
124
|
return result;
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* {@inheritDoc}
|
129
|
*/
|
130
|
@Override
|
131
|
public String getDistributionServiceRequestParameterString(List<TaxonDescription> taxonDescriptions,
|
132
|
boolean subAreaPreference,
|
133
|
boolean statusOrderPreference,
|
134
|
Set<MarkerType> hideMarkedAreas,
|
135
|
Map<PresenceAbsenceTerm, Color> presenceAbsenceTermColors,
|
136
|
List<Language> langs) {
|
137
|
|
138
|
Set<Distribution> distributions = getDistributionsOf(taxonDescriptions);
|
139
|
|
140
|
String uriParams = getDistributionServiceRequestParameterString(distributions,
|
141
|
subAreaPreference,
|
142
|
statusOrderPreference,
|
143
|
hideMarkedAreas,
|
144
|
presenceAbsenceTermColors,
|
145
|
langs);
|
146
|
|
147
|
return uriParams;
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* {@inheritDoc}
|
152
|
*/
|
153
|
@Override
|
154
|
public String getDistributionServiceRequestParameterString(
|
155
|
Set<Distribution> distributions,
|
156
|
boolean subAreaPreference,
|
157
|
boolean statusOrderPreference,
|
158
|
Set<MarkerType> hideMarkedAreas,
|
159
|
Map<PresenceAbsenceTerm, Color> presenceAbsenceTermColors,
|
160
|
List<Language> langs) {
|
161
|
|
162
|
Collection<Distribution> filteredDistributions = DescriptionUtility.filterDistributions(distributions,
|
163
|
hideMarkedAreas, true, statusOrderPreference, subAreaPreference);
|
164
|
|
165
|
|
166
|
String uriParams = EditGeoServiceUtilities.getDistributionServiceRequestParameterString(
|
167
|
filteredDistributions,
|
168
|
areaMapping,
|
169
|
presenceAbsenceTermColors,
|
170
|
null, langs);
|
171
|
return uriParams;
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* {@inheritDoc}
|
176
|
*/
|
177
|
@Override
|
178
|
@Deprecated
|
179
|
public String getDistributionServiceRequestParameterString(TaxonDescription taxonDescription,
|
180
|
boolean subAreaPreference,
|
181
|
boolean statusOrderPreference,
|
182
|
Set<MarkerType> hideMarkedAreas,
|
183
|
Map<PresenceAbsenceTerm, Color> presenceAbsenceTermColors,
|
184
|
List<Language> langs) {
|
185
|
|
186
|
List<TaxonDescription> taxonDescriptions = new ArrayList<>();
|
187
|
taxonDescriptions.add(taxonDescription);
|
188
|
|
189
|
return getDistributionServiceRequestParameterString(taxonDescriptions,
|
190
|
subAreaPreference,
|
191
|
statusOrderPreference,
|
192
|
hideMarkedAreas,
|
193
|
presenceAbsenceTermColors,
|
194
|
langs);
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* {@inheritDoc}
|
199
|
*/
|
200
|
@Override
|
201
|
public OccurrenceServiceRequestParameterDto getOccurrenceServiceRequestParameterString(
|
202
|
List<SpecimenOrObservationBase> specimensOrObersvations,
|
203
|
Map<SpecimenOrObservationType, Color> specimenOrObservationTypeColors) {
|
204
|
|
205
|
List<Point> fieldUnitPoints = new ArrayList<>();
|
206
|
List<Point> derivedUnitPoints = new ArrayList<>();
|
207
|
|
208
|
for (SpecimenOrObservationBase<?> specimenOrObservationBase : specimensOrObersvations) {
|
209
|
SpecimenOrObservationBase<?> specimenOrObservation = occurrenceDao
|
210
|
.load(specimenOrObservationBase.getUuid());
|
211
|
|
212
|
if (specimenOrObservation instanceof FieldUnit) {
|
213
|
GatheringEvent gatherEvent = ((FieldUnit) specimenOrObservation).getGatheringEvent();
|
214
|
if (gatherEvent != null && gatherEvent.getExactLocation() != null){
|
215
|
fieldUnitPoints.add(gatherEvent.getExactLocation());
|
216
|
}
|
217
|
}
|
218
|
if (specimenOrObservation instanceof DerivedUnit) {
|
219
|
registerDerivedUnitLocations((DerivedUnit) specimenOrObservation, derivedUnitPoints);
|
220
|
}
|
221
|
}
|
222
|
|
223
|
return EditGeoServiceUtilities.getOccurrenceServiceRequestParameterString(fieldUnitPoints,
|
224
|
derivedUnitPoints, specimenOrObservationTypeColors);
|
225
|
|
226
|
}
|
227
|
|
228
|
public CondensedDistribution getCondensedDistribution(List<TaxonDescription> taxonDescriptions,
|
229
|
boolean statusOrderPreference,
|
230
|
Set<MarkerType> hideMarkedAreas,
|
231
|
MarkerType fallbackAreaMarkerType,
|
232
|
CondensedDistributionRecipe recipe,
|
233
|
List<Language> langs) {
|
234
|
Set<Distribution> distributions = getDistributionsOf(taxonDescriptions);
|
235
|
return getCondensedDistribution(distributions, statusOrderPreference,
|
236
|
hideMarkedAreas, fallbackAreaMarkerType, recipe, langs);
|
237
|
}
|
238
|
|
239
|
@Override
|
240
|
public CondensedDistribution getCondensedDistribution(Set<Distribution> distributions,
|
241
|
boolean statusOrderPreference,
|
242
|
Set<MarkerType> hideMarkedAreas,
|
243
|
MarkerType fallbackAreaMarkerType,
|
244
|
CondensedDistributionRecipe recipe,
|
245
|
List<Language> langs) {
|
246
|
|
247
|
Collection<Distribution> filteredDistributions = DescriptionUtility.filterDistributions(
|
248
|
distributions, hideMarkedAreas, true, statusOrderPreference, false);
|
249
|
|
250
|
|
251
|
|
252
|
CondensedDistribution condensedDistribution = EditGeoServiceUtilities.getCondensedDistribution(
|
253
|
filteredDistributions,
|
254
|
recipe,
|
255
|
langs);
|
256
|
|
257
|
return condensedDistribution;
|
258
|
}
|
259
|
|
260
|
/**
|
261
|
* @param derivedUnit
|
262
|
* @param derivedUnitPoints
|
263
|
*/
|
264
|
private void registerDerivedUnitLocations(DerivedUnit derivedUnit, List<Point> derivedUnitPoints) {
|
265
|
|
266
|
Set<SpecimenOrObservationBase> originals = derivedUnit.getOriginals();
|
267
|
if (originals != null) {
|
268
|
for (SpecimenOrObservationBase<?> original : originals) {
|
269
|
if (original instanceof FieldUnit) {
|
270
|
if (((FieldUnit) original).getGatheringEvent() != null) {
|
271
|
Point point = ((FieldUnit) original).getGatheringEvent().getExactLocation();
|
272
|
if (point != null) {
|
273
|
// points with no longitude or latitude should not exist
|
274
|
// see #4173 ([Rule] Longitude and Latitude in Point must not be null)
|
275
|
if (point.getLatitude() == null || point.getLongitude() == null){
|
276
|
continue;
|
277
|
}
|
278
|
// FIXME: remove next statement after
|
279
|
// DerivedUnitFacade or ABCD import is fixed
|
280
|
//
|
281
|
if(point.getLatitude() == 0.0 || point.getLongitude() == 0.0) {
|
282
|
continue;
|
283
|
}
|
284
|
derivedUnitPoints.add(point);
|
285
|
}
|
286
|
}
|
287
|
} else {
|
288
|
registerDerivedUnitLocations((DerivedUnit) original, derivedUnitPoints);
|
289
|
}
|
290
|
}
|
291
|
}
|
292
|
|
293
|
}
|
294
|
|
295
|
|
296
|
/**
|
297
|
* {@inheritDoc}
|
298
|
*/
|
299
|
@Override
|
300
|
public void setMapping(NamedArea area, GeoServiceArea geoServiceArea) {
|
301
|
areaMapping.set(area, geoServiceArea);
|
302
|
|
303
|
}
|
304
|
|
305
|
/**
|
306
|
* {@inheritDoc}
|
307
|
*/
|
308
|
@Override
|
309
|
@Transactional(readOnly=false)
|
310
|
public Map<NamedArea, String> mapShapeFileToNamedAreas(Reader csvReader,
|
311
|
List<String> idSearchFields, String wmsLayerName, UUID areaVocabularyUuid,
|
312
|
Set<UUID> namedAreaUuids) throws IOException {
|
313
|
|
314
|
Set<NamedArea> areas = new HashSet<>();
|
315
|
|
316
|
if(areaVocabularyUuid != null){
|
317
|
TermVocabulary<NamedArea> areaVocabulary = vocabDao.load(areaVocabularyUuid);
|
318
|
if(areaVocabulary == null){
|
319
|
throw new EntityNotFoundException("No Vocabulary found for uuid " + areaVocabularyUuid);
|
320
|
}
|
321
|
areas.addAll(areaVocabulary.getTerms());
|
322
|
}
|
323
|
if(namedAreaUuids != null && !namedAreaUuids.isEmpty()){
|
324
|
for(DefinedTermBase<?> dtb : termDao.list(namedAreaUuids, null, null, null, null)){
|
325
|
areas.add((NamedArea)CdmBase.deproxy(dtb));
|
326
|
}
|
327
|
}
|
328
|
|
329
|
ShpAttributesToNamedAreaMapper mapper = new ShpAttributesToNamedAreaMapper(areas, areaMapping);
|
330
|
Map<NamedArea, String> resultMap = mapper.readCsv(csvReader, idSearchFields, wmsLayerName);
|
331
|
termDao.saveOrUpdateAll((Collection)areas);
|
332
|
return resultMap;
|
333
|
}
|
334
|
|
335
|
/**
|
336
|
* {@inheritDoc}
|
337
|
*/
|
338
|
@Override
|
339
|
public DistributionInfoDTO composeDistributionInfoFor(EnumSet<DistributionInfoDTO.InfoPart> parts, UUID taxonUUID,
|
340
|
boolean subAreaPreference, boolean statusOrderPreference, Set<MarkerType> hiddenAreaMarkerTypes,
|
341
|
Set<NamedAreaLevel> omitLevels, Map<PresenceAbsenceTerm, Color> presenceAbsenceTermColors,
|
342
|
List<Language> languages, List<String> propertyPaths, CondensedDistributionRecipe recipe,
|
343
|
DistributionOrder distributionOrder){
|
344
|
|
345
|
DistributionInfoDTO dto = new DistributionInfoDTO();
|
346
|
|
347
|
if (propertyPaths == null){
|
348
|
propertyPaths = Arrays.asList(new String []{});
|
349
|
}
|
350
|
// Adding default initStrategies to improve the performance of this method
|
351
|
// adding 'status' and 'area' has a good positive effect:
|
352
|
// filterDistributions() only takes 21% of the total method time (before it was 46%)
|
353
|
// at the same time the cost of the getDescriptionElementForTaxon is not increased at all!
|
354
|
//
|
355
|
// adding 'markers.markerType' is not improving the performance since it only
|
356
|
// moved the load from the filter method to the getDescriptionElementForTaxon()
|
357
|
// method.
|
358
|
// overall improvement by this means is by 42% (from 77,711 ms to 44,868 ms)
|
359
|
ArrayList<String> initStrategy = new ArrayList<>(propertyPaths);
|
360
|
if(!initStrategy.contains("status")) {
|
361
|
initStrategy.add("status");
|
362
|
}
|
363
|
if(!initStrategy.contains("area")) {
|
364
|
initStrategy.add("area");
|
365
|
}
|
366
|
if(!initStrategy.contains("markers.markerType")) {
|
367
|
initStrategy.add("markers.markerType");
|
368
|
}
|
369
|
if(omitLevels == null) {
|
370
|
omitLevels = new HashSet<>(0);
|
371
|
}
|
372
|
|
373
|
List<Distribution> distributions = dao.getDescriptionElementForTaxon(taxonUUID, null, Distribution.class, null, null, initStrategy);
|
374
|
|
375
|
// Apply the rules statusOrderPreference and hideMarkedAreas for textual distribution info
|
376
|
Set<Distribution> filteredDistributions = DescriptionUtility.filterDistributions(distributions, hiddenAreaMarkerTypes,
|
377
|
true, statusOrderPreference, false);
|
378
|
|
379
|
if(parts.contains(InfoPart.elements)) {
|
380
|
dto.setElements(filteredDistributions);
|
381
|
}
|
382
|
|
383
|
if(parts.contains(InfoPart.tree)) {
|
384
|
DistributionTree tree = DescriptionUtility.orderDistributions(termDao, omitLevels, filteredDistributions, hiddenAreaMarkerTypes,distributionOrder);
|
385
|
dto.setTree(tree);
|
386
|
}
|
387
|
|
388
|
if(parts.contains(InfoPart.condensedDistribution)) {
|
389
|
dto.setCondensedDistribution(EditGeoServiceUtilities.getCondensedDistribution(filteredDistributions, recipe, languages));
|
390
|
}
|
391
|
|
392
|
if (parts.contains(InfoPart.mapUriParams)) {
|
393
|
// only apply the subAreaPreference rule for the maps
|
394
|
Set<Distribution> filteredMapDistributions = DescriptionUtility.filterDistributions(filteredDistributions, null, false, false, subAreaPreference);
|
395
|
|
396
|
dto.setMapUriParams(EditGeoServiceUtilities.getDistributionServiceRequestParameterString(filteredMapDistributions,
|
397
|
areaMapping,
|
398
|
presenceAbsenceTermColors,
|
399
|
null, languages));
|
400
|
}
|
401
|
|
402
|
return dto;
|
403
|
}
|
404
|
}
|