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