2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.remote
.controller
.ext
;
11 import java
.awt
.Color
;
12 import java
.io
.IOException
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Arrays
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
19 import java
.util
.stream
.Collectors
;
21 import javax
.servlet
.http
.HttpServletRequest
;
22 import javax
.servlet
.http
.HttpServletResponse
;
24 import org
.apache
.logging
.log4j
.LogManager
;
25 import org
.apache
.logging
.log4j
.Logger
;
26 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
27 import org
.springframework
.http
.HttpStatus
;
28 import org
.springframework
.http
.ResponseEntity
;
29 import org
.springframework
.stereotype
.Controller
;
30 import org
.springframework
.web
.bind
.WebDataBinder
;
31 import org
.springframework
.web
.bind
.annotation
.CrossOrigin
;
32 import org
.springframework
.web
.bind
.annotation
.InitBinder
;
33 import org
.springframework
.web
.bind
.annotation
.PathVariable
;
34 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
35 import org
.springframework
.web
.bind
.annotation
.RequestMethod
;
36 import org
.springframework
.web
.bind
.annotation
.RequestParam
;
38 import de
.micromata
.opengis
.kml
.v_2_2_0
.Kml
;
39 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
40 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
41 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
42 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
43 import eu
.etaxonomy
.cdm
.api
.util
.TaxonRelationshipEdge
;
44 import eu
.etaxonomy
.cdm
.database
.UpdatableRoutingDataSource
;
45 import eu
.etaxonomy
.cdm
.ext
.geo
.IEditGeoService
;
46 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
47 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
48 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationBase
;
49 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
50 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
52 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
53 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
54 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
.SortOrder
;
55 import eu
.etaxonomy
.cdm
.remote
.controller
.BaseController
;
56 import eu
.etaxonomy
.cdm
.remote
.controller
.OptionsController
;
57 import eu
.etaxonomy
.cdm
.remote
.controller
.util
.ControllerUtils
;
58 import eu
.etaxonomy
.cdm
.remote
.editor
.DefinedTermBaseList
;
59 import eu
.etaxonomy
.cdm
.remote
.editor
.TermBaseListPropertyEditor
;
60 import eu
.etaxonomy
.cdm
.remote
.editor
.UUIDListPropertyEditor
;
61 import eu
.etaxonomy
.cdm
.remote
.editor
.UuidList
;
62 import io
.swagger
.annotations
.Api
;
65 * The ExternalGeoController class is a Spring MVC Controller.
67 * The syntax of the mapped service URIs contains the the {datasource-name} path element.
68 * The available {datasource-name}s are defined in a configuration file which
69 * is loaded by the {@link UpdatableRoutingDataSource}. If the
70 * UpdatableRoutingDataSource is not being used in the actual application
71 * context any arbitrary {datasource-name} may be used.
73 * @author a.kohlbecker
76 @CrossOrigin(origins
= "*")
78 @Api(value
="mapServiceParameters")
79 @RequestMapping(value
= { "kml" })
80 public class KmlController
extends BaseController
<TaxonBase
, ITaxonService
> {
82 private static final Logger logger
= LogManager
.getLogger();
85 private IEditGeoService geoservice
;
88 private IOccurrenceService occurrenceService
;
91 private INameService nameService
;
94 private ITermService termService
;
98 public void initBinder(WebDataBinder binder
) {
99 super.initBinder(binder
);
100 binder
.registerCustomEditor(UuidList
.class, new UUIDListPropertyEditor());
101 binder
.registerCustomEditor(DefinedTermBaseList
.class, new TermBaseListPropertyEditor
<MarkerType
>(termService
));
106 public void setService(ITaxonService service
) {
107 this.service
= service
;
111 * TODO This controller method replaces the general {@link OptionsController} which has been disabled temporarily.
112 * The {@link OptionsController} was causing problems in some situations by blocking POST requests to other controllers.
113 * This mainly happened in the taxeditor project where the integration test could not be run due to the cdm remote instance which
114 * did not allow POST requests to the /remoting-public/user.service
118 method
= RequestMethod
.OPTIONS
120 public ResponseEntity
options() {
121 return new ResponseEntity(HttpStatus
.OK
);
125 * Assembles and returns URI parameter Strings for the EDIT Map Service. The distribution areas for the
126 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code> are found and are translated into
127 * an valid URI parameter String. Higher level distribution areas are expanded in order to include all
130 * URI: <b>/{datasource-name}/geo/map/distribution/{taxon-uuid}</b>
134 * @return URI parameter Strings for the EDIT Map Service
135 * @throws IOException TODO write controller method documentation
137 @RequestMapping(value
= { "specimensOrOccurences/{uuid-list}" }, method
= RequestMethod
.GET
)
138 public Kml
doGetSpecimensOrOccurencesKml(
139 @PathVariable("uuid-list") UuidList uuidList
,
140 HttpServletRequest request
,
141 HttpServletResponse response
)
145 logger
.info("doGetSpecimensOrOccurencesKml() " + requestPathAndQuery(request
));
147 Map
<SpecimenOrObservationType
, Color
> specimenOrObservationTypeColors
= null;
149 List
<SpecimenOrObservationBase
> specimensOrObersvations
= occurrenceService
.load(uuidList
, null);
151 specimensOrObersvations
= specimensOrObersvations
.stream().filter(s
-> s
!= null).collect(Collectors
.toList());
153 Kml kml
= geoservice
.occurrencesToKML(specimensOrObersvations
, specimenOrObservationTypeColors
);
159 * Assembles and returns URI parameter Strings for the EDIT Map Service. The distribution areas for the
160 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code> are found and are translated into
161 * an valid URI parameter String. Higher level distribution areas are expanded in order to include all
164 * URI: <b>/{datasource-name}/geo/map/distribution/{taxon-uuid}</b>
168 * @return URI parameter Strings for the EDIT Map Service
169 * @throws IOException TODO write controller method documentation
171 @RequestMapping(value
= { "typeDesignations/{uuid-list}" }, method
= RequestMethod
.GET
)
172 public Kml
doGetTypeDesignationsKml(
173 @PathVariable("uuid-list") UuidList uuidList
,
174 HttpServletRequest request
,
175 HttpServletResponse response
)
179 logger
.info("doGetTypeDesignationsKml() " + requestPathAndQuery(request
));
181 Map
<SpecimenOrObservationType
, Color
> specimenOrObservationTypeColors
= null;
183 List
<TypeDesignationBase
<?
>> typeDesignations
= nameService
.loadTypeDesignations(uuidList
, Arrays
.asList("typeSpecimen"));
185 List
<SpecimenOrObservationBase
> specimensOrObersvations
= typeDesignations
.stream()
186 .filter(td
-> td
!= null && td
instanceof SpecimenTypeDesignation
)
187 .map(SpecimenTypeDesignation
.class::cast
)
188 .map(SpecimenTypeDesignation
::getTypeSpecimen
)
189 .filter(s
-> s
!= null)
190 .collect(Collectors
.toList());
192 Kml kml
= geoservice
.occurrencesToKML(specimensOrObersvations
, specimenOrObservationTypeColors
);
198 * Assembles and returns URI parameter Strings for the EDIT Map Service. The distribution areas for the
199 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code> are found and are translated into
200 * an valid URI parameter String. Higher level distribution areas are expanded in order to include all
203 * URI: <b>/{datasource-name}/geo/map/distribution/{taxon-uuid}</b>
207 * @return URI parameter Strings for the EDIT Map Service
208 * @throws IOException TODO write controller method documentation
210 @RequestMapping(value
= { "taxonOccurrencesFor/{uuid}" }, method
= RequestMethod
.GET
)
211 public Kml
doGetTaxonOccurrenceKml(
212 @PathVariable("uuid") UUID uuid
,
213 @RequestParam(value
= "relationships", required
= false) UuidList relationshipUuids
,
214 @RequestParam(value
= "relationshipsInvers", required
= false) UuidList relationshipInversUuids
,
215 @RequestParam(value
= "maxDepth", required
= false) Integer maxDepth
,
216 HttpServletRequest request
,
217 HttpServletResponse response
)
221 logger
.info("doGetTaxonOccurrenceKml() " + requestPathAndQuery(request
));
223 Map
<SpecimenOrObservationType
, Color
> specimenOrObservationTypeColors
= null;
225 List
<SpecimenOrObservationBase
> specimensOrObersvations
= occurencesForTaxon(uuid
, relationshipUuids
,
226 relationshipInversUuids
, maxDepth
, response
);
228 Kml kml
= geoservice
.occurrencesToKML(specimensOrObersvations
, specimenOrObservationTypeColors
);
233 private List
<SpecimenOrObservationBase
> occurencesForTaxon(UUID taxonUuid
, UuidList relationshipUuids
,
234 UuidList relationshipInversUuids
, Integer maxDepth
, HttpServletResponse response
) throws IOException
{
235 Set
<TaxonRelationshipEdge
> includeRelationships
= ControllerUtils
.loadIncludeRelationships(
236 relationshipUuids
, relationshipInversUuids
, termService
);
238 Taxon taxon
= getCdmBaseInstance(Taxon
.class, taxonUuid
, response
, (List
<String
>)null);
240 List
<OrderHint
> orderHints
= new ArrayList
<>();
241 orderHints
.add(new OrderHint("titleCache", SortOrder
.DESCENDING
));
243 List
<SpecimenOrObservationBase
> specimensOrObersvations
= occurrenceService
.listByAssociatedTaxon(
244 null, includeRelationships
, taxon
, maxDepth
, null, null, orderHints
, null);
245 return specimensOrObersvations
;