Merge branch 'release/5.45.0'
[cdmlib.git] / cdmlib-ext / src / main / java / eu / etaxonomy / cdm / ext / geo / EditGeoService.java
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.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16
17 import org.apache.logging.log4j.LogManager;
18 import org.apache.logging.log4j.Logger;
19 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.stereotype.Service;
21 import org.springframework.transaction.annotation.Transactional;
22
23 import de.micromata.opengis.kml.v_2_2_0.Kml;
24 import eu.etaxonomy.cdm.ext.geo.kml.KMLDocumentBuilder;
25 import eu.etaxonomy.cdm.model.location.Point;
26 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
27 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
28 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
29 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
30 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
31 import eu.etaxonomy.cdm.persistence.dao.occurrence.IOccurrenceDao;
32
33 /**
34 * @author a.kohlbecker
35 * @since 18.06.2009
36 */
37 @Service
38 @Transactional(readOnly = true)
39 public class EditGeoService implements IEditGeoService {
40
41 @SuppressWarnings("unused")
42 private static final Logger logger = LogManager.getLogger();
43
44 @Autowired
45 private IOccurrenceDao occurrenceDao;
46
47 @Override
48 public OccurrenceServiceRequestParameterDto getOccurrenceServiceRequestParameters(
49 List<SpecimenOrObservationBase> specimensOrObservations,
50 Map<SpecimenOrObservationType, Color> specimenOrObservationTypeColors) {
51
52 List<Point> fieldUnitPoints = new ArrayList<>();
53 List<Point> derivedUnitPoints = new ArrayList<>();
54
55 for (SpecimenOrObservationBase<?> specimenOrObservationBase : specimensOrObservations) {
56 SpecimenOrObservationBase<?> specimensOrObservation = occurrenceDao
57 .load(specimenOrObservationBase.getUuid());
58
59 if (specimensOrObservation instanceof FieldUnit) {
60 GatheringEvent gatherEvent = ((FieldUnit) specimensOrObservation).getGatheringEvent();
61 if (gatherEvent != null && gatherEvent.getExactLocation() != null){
62 fieldUnitPoints.add(gatherEvent.getExactLocation());
63 }
64 }
65 if (specimensOrObservation instanceof DerivedUnit) {
66 registerDerivedUnitLocations((DerivedUnit) specimensOrObservation, derivedUnitPoints);
67 }
68 }
69
70 return EditGeoServiceUtilities.getOccurrenceServiceRequestParameterString(fieldUnitPoints,
71 derivedUnitPoints, specimenOrObservationTypeColors);
72 }
73
74 @Override
75 public Kml occurrencesToKML(
76 @SuppressWarnings("rawtypes") List<SpecimenOrObservationBase> specimensOrObservations,
77 Map<SpecimenOrObservationType, Color> specimenOrObservationTypeColors) {
78
79 KMLDocumentBuilder builder = new KMLDocumentBuilder();
80
81 for (SpecimenOrObservationBase<?> specimenOrObservationBase : specimensOrObservations) {
82 builder.addSpecimenOrObservationBase(occurrenceDao.load(specimenOrObservationBase.getUuid()));
83 }
84
85 Kml kml = builder.build();
86
87 return kml;
88 }
89
90
91 private void registerDerivedUnitLocations(DerivedUnit derivedUnit, List<Point> derivedUnitPoints) {
92
93 @SuppressWarnings("rawtypes")
94 Set<SpecimenOrObservationBase> originals = derivedUnit.getOriginals();
95 for (SpecimenOrObservationBase<?> original : originals) {
96 if (original instanceof FieldUnit) {
97 if (((FieldUnit) original).getGatheringEvent() != null) {
98 Point point = ((FieldUnit) original).getGatheringEvent().getExactLocation();
99 if (point != null) {
100 // points with no longitude or latitude should not exist
101 // see #4173 ([Rule] Longitude and Latitude in Point must not be null)
102 if (point.getLatitude() == null || point.getLongitude() == null){
103 continue;
104 }
105 // FIXME: remove next statement after
106 // DerivedUnitFacade or ABCD import is fixed
107 //
108 if(point.getLatitude() == 0.0 || point.getLongitude() == 0.0) {
109 continue;
110 }
111 derivedUnitPoints.add(point);
112 }
113 }
114 } else {
115 registerDerivedUnitLocations((DerivedUnit) original, derivedUnitPoints);
116 }
117 }
118 }
119
120 }