dc57e97a29b71bfd8cfa7c8002ad5d81e3bc8348
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / ext / ExternalGeoController.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.remote.controller.ext;
10
11 import java.awt.Color;
12 import java.io.FileReader;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.UUID;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.stereotype.Controller;
28 import org.springframework.web.bind.WebDataBinder;
29 import org.springframework.web.bind.annotation.InitBinder;
30 import org.springframework.web.bind.annotation.PathVariable;
31 import org.springframework.web.bind.annotation.RequestMapping;
32 import org.springframework.web.bind.annotation.RequestMethod;
33 import org.springframework.web.bind.annotation.RequestParam;
34 import org.springframework.web.servlet.ModelAndView;
35
36 import eu.etaxonomy.cdm.api.service.IDescriptionService;
37 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
38 import eu.etaxonomy.cdm.api.service.ITaxonService;
39 import eu.etaxonomy.cdm.api.service.ITermService;
40 import eu.etaxonomy.cdm.api.service.l10n.LocaleContext;
41 import eu.etaxonomy.cdm.api.service.pager.Pager;
42 import eu.etaxonomy.cdm.api.util.DescriptionUtility;
43 import eu.etaxonomy.cdm.api.util.TaxonRelationshipEdge;
44 import eu.etaxonomy.cdm.database.UpdatableRoutingDataSource;
45 import eu.etaxonomy.cdm.ext.geo.EditGeoServiceUtilities;
46 import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
47 import eu.etaxonomy.cdm.ext.geo.OccurrenceServiceRequestParameterDto;
48 import eu.etaxonomy.cdm.model.common.Language;
49 import eu.etaxonomy.cdm.model.common.Marker;
50 import eu.etaxonomy.cdm.model.common.MarkerType;
51 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
52 import eu.etaxonomy.cdm.model.description.TaxonDescription;
53 import eu.etaxonomy.cdm.model.location.NamedArea;
54 import eu.etaxonomy.cdm.model.location.Point;
55 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
56 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
57 import eu.etaxonomy.cdm.model.taxon.Taxon;
58 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
59 import eu.etaxonomy.cdm.model.term.DefinedTerm;
60 import eu.etaxonomy.cdm.persistence.query.OrderHint;
61 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
62 import eu.etaxonomy.cdm.remote.controller.BaseController;
63 import eu.etaxonomy.cdm.remote.controller.util.ControllerUtils;
64 import eu.etaxonomy.cdm.remote.editor.DefinedTermBaseList;
65 import eu.etaxonomy.cdm.remote.editor.TermBaseListPropertyEditor;
66 import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
67 import eu.etaxonomy.cdm.remote.editor.UuidList;
68 import io.swagger.annotations.Api;
69
70 /**
71 * The ExternalGeoController class is a Spring MVC Controller.
72 * <p>
73 * The syntax of the mapped service URIs contains the the {datasource-name} path element.
74 * The available {datasource-name}s are defined in a configuration file which
75 * is loaded by the {@link UpdatableRoutingDataSource}. If the
76 * UpdatableRoutingDataSource is not being used in the actual application
77 * context any arbitrary {datasource-name} may be used.
78 * <p>
79 * @author a.kohlbecker
80 * @since 18.06.2009
81 *
82 */
83
84 @Controller
85 @Api(value="mapServiceParameters")
86 @RequestMapping(value = { "ext/edit/mapServiceParameters/" })
87 public class ExternalGeoController extends BaseController<TaxonBase, ITaxonService> {
88
89 public static final Logger logger = LogManager.getLogger(ExternalGeoController.class);
90
91 @Autowired
92 private IEditGeoService geoservice;
93
94 @Autowired
95 private IDescriptionService descriptionService;
96
97 @Autowired
98 private IOccurrenceService occurrenceService;
99
100 @Autowired
101 private ITermService termService;
102
103 @InitBinder
104 @Override
105 public void initBinder(WebDataBinder binder) {
106 super.initBinder(binder);
107 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
108 binder.registerCustomEditor(DefinedTermBaseList.class, new TermBaseListPropertyEditor<MarkerType>(termService));
109 }
110
111 @Autowired
112 @Override
113 public void setService(ITaxonService service) {
114 this.service = service;
115 }
116
117 /**
118 * Assembles and returns URI parameter Strings for the EDIT Map Service. The distribution areas for the
119 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code> are found and are translated into
120 * an valid URI parameter String. Higher level distribution areas are expanded in order to include all
121 * nested sub-areas.
122 * <p>
123 * URI: <b>&#x002F;{datasource-name}&#x002F;geo&#x002F;map&#x002F;distribution&#x002F;{taxon-uuid}</b>
124 *
125 *
126 * @param subAreaPreference
127 * enables the <b>Sub area preference rule</b> if set to true,
128 * see {@link DescriptionUtility#filterDistributions(Collection, boolean, boolean}
129 * @param statusOrderPreference
130 * enables the <b>Status order preference rule</b> if set to true,
131 * see {@link DescriptionUtility#filterDistributions(Collection, boolean, boolean}
132 * @param hideMarkedAreas
133 * comma separated list of {@link MarkerType} uuids,
134 * distributions where the area has a {@link Marker} with one of
135 * the specified {@link MarkerType}s will be skipped, see
136 * {@link DescriptionUtility#filterDistributions(Collection, boolean, boolean, Set)}
137 * @param request
138 * @param response
139 * @return URI parameter Strings for the EDIT Map Service
140 * @throws IOException
141 */
142 @RequestMapping(value = { "taxonDistributionFor/{uuid}" }, method = RequestMethod.GET)
143 public ModelAndView doGetDistributionMapUriParams(
144 @PathVariable("uuid") UUID uuid,
145 @RequestParam(value = "subAreaPreference", required = false) boolean subAreaPreference,
146 @RequestParam(value = "statusOrderPreference", required = false) boolean statusOrderPreference,
147 @RequestParam(value = "hideMarkedAreas", required = false) DefinedTermBaseList<MarkerType> hideMarkedAreasList,
148 HttpServletRequest request,
149 HttpServletResponse response)
150 throws IOException {
151
152 logger.info("doGetDistributionMapUriParams() " + request.getRequestURI());
153 ModelAndView mv = new ModelAndView();
154
155 // get the descriptions for the taxon
156 Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
157
158 Map<PresenceAbsenceTerm, Color> presenceAbsenceTermColors = null;
159 //languages
160 List<Language> langs = LocaleContext.getLanguages();
161
162 Set<MarkerType> hideMarkedAreas = null;
163 if(hideMarkedAreasList != null){
164 hideMarkedAreas = hideMarkedAreasList.asSet();
165 }
166
167 Set<DefinedTerm> scopes = null;
168 Set<NamedArea> geographicalScope = null;
169 Integer pageSize = null;
170 Integer pageNumber = null;
171 List<String> propertyPaths = null;
172 Pager<TaxonDescription> page = descriptionService.pageTaxonDescriptions(taxon, scopes, geographicalScope, pageSize, pageNumber, propertyPaths);
173
174 List<TaxonDescription> taxonDescriptions = page.getRecords();
175 String uriParams = geoservice.getDistributionServiceRequestParameterString(taxonDescriptions,
176 subAreaPreference, statusOrderPreference,
177 hideMarkedAreas, presenceAbsenceTermColors, langs);
178 mv.addObject(uriParams);
179
180 return mv;
181 }
182
183
184 /**
185 * Assembles and returns URI parameter Strings for the EDIT Map Service. The distribution areas for the
186 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code> are found and are translated into
187 * an valid URI parameter String. Higher level distribution areas are expanded in order to include all
188 * nested sub-areas.
189 * <p>
190 * URI: <b>&#x002F;{datasource-name}&#x002F;geo&#x002F;map&#x002F;distribution&#x002F;{taxon-uuid}</b>
191 *
192 * @param request
193 * @param response
194 * @return URI parameter Strings for the EDIT Map Service
195 * @throws IOException TODO write controller method documentation
196 */
197 @RequestMapping(value = { "taxonOccurrencesFor/{uuid}" }, method = RequestMethod.GET)
198 public OccurrenceServiceRequestParameterDto doGetOccurrenceMapUriParams(
199 @PathVariable("uuid") UUID uuid,
200 @RequestParam(value = "relationships", required = false) UuidList relationshipUuids,
201 @RequestParam(value = "relationshipsInvers", required = false) UuidList relationshipInversUuids,
202 @RequestParam(value = "maxDepth", required = false) Integer maxDepth,
203 HttpServletRequest request,
204 HttpServletResponse response)
205 throws IOException {
206
207
208 logger.info("doGetOccurrenceMapUriParams() " + requestPathAndQuery(request));
209
210 Map<SpecimenOrObservationType, Color> specimenOrObservationTypeColors = null;
211
212 List<SpecimenOrObservationBase> specimensOrObersvations = occurencesForTaxon(uuid, relationshipUuids,
213 relationshipInversUuids, maxDepth, response);
214
215 OccurrenceServiceRequestParameterDto dto = geoservice.getOccurrenceServiceRequestParameters(specimensOrObersvations,
216 specimenOrObservationTypeColors );
217
218 return dto;
219 }
220
221 private List<SpecimenOrObservationBase> occurencesForTaxon(UUID taxonUuid, UuidList relationshipUuids,
222 UuidList relationshipInversUuids, Integer maxDepth, HttpServletResponse response) throws IOException {
223 Set<TaxonRelationshipEdge> includeRelationships = ControllerUtils.loadIncludeRelationships(
224 relationshipUuids, relationshipInversUuids, termService);
225
226 Taxon taxon = getCdmBaseInstance(Taxon.class, taxonUuid, response, (List<String>)null);
227
228 List<OrderHint> orderHints = new ArrayList<>();
229 orderHints.add(new OrderHint("titleCache", SortOrder.DESCENDING));
230
231 List<SpecimenOrObservationBase> specimensOrObersvations = occurrenceService.listByAssociatedTaxon(
232 null, includeRelationships, taxon, maxDepth, null, null, orderHints, null);
233 return specimensOrObersvations;
234 }
235
236 /**
237 * Assembles and returns URI parameter Strings for the EDIT Map Service. The distribution areas for the
238 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code> are found and are translated into
239 * an valid URI parameter String. Higher level distribution areas are expanded in order to include all
240 * nested sub-areas.
241 * <p>
242 * URI: <b>&#x002F;{datasource-name}&#x002F;geo&#x002F;map&#x002F;distribution&#x002F;{taxon-uuid}</b>
243 *
244 * @param request
245 * @param response
246 * @return URI parameter Strings for the EDIT Map Service
247 * @throws IOException TODO write controller method documentation
248 */
249 @RequestMapping(value = { "taxonOccurrencesForX" }, method = RequestMethod.GET)
250 public ModelAndView doGetOccurrenceXMapUriParams(
251 @RequestParam(value = "fieldUnitUuidList", required = false) UuidList fieldUnitUuids,
252 HttpServletRequest request,
253 HttpServletResponse response)
254 throws IOException {
255
256 Map<SpecimenOrObservationType, Color> specimenOrObservationTypeColors = null;
257
258 logger.info("doGetOccurrenceMapUriParams() " + requestPathAndQuery(request));
259 ModelAndView mv = new ModelAndView();
260
261 List<Point> fieldUnitPoints = occurrenceService.findPointsForFieldUnitList(fieldUnitUuids);
262
263 OccurrenceServiceRequestParameterDto dto = EditGeoServiceUtilities.getOccurrenceServiceRequestParameterString(fieldUnitPoints,
264 null, specimenOrObservationTypeColors);
265 mv.addObject(dto);
266 return mv;
267 }
268
269 /**
270 * EXPERIMENTAL !!!!!
271 * DO NOT USE !!!!!
272 *
273 * @param vocabUuid
274 * @param request
275 * @param response
276 * @return
277 * @throws IOException
278 *
279 * @author a.kohlbecker
280 */
281 @RequestMapping(value = { "mapShapeFileToNamedAreas" }, method = RequestMethod.GET)
282 public ModelAndView doMapShapeFileToNamedAreas(
283 @RequestParam(required=false, value="vocabularyUuid") UUID vocabUuid,
284 @RequestParam(required=false, value="namedAreaUuids") UuidList namedAreaUuids,
285 @RequestParam(required=true, value="localFile") String localFile,
286 @RequestParam(required=true, value="idSearchField") List<String> idSearchFields,
287 @RequestParam(required=true, value="wmsLayerName") String wmsLayerName,
288 HttpServletRequest request,
289 HttpServletResponse response)
290 throws IOException {
291
292 logger.info("doMapShapeFileToNamedAreas() " + requestPathAndQuery(request));
293 ModelAndView mv = new ModelAndView();
294
295 FileReader reader = new FileReader(localFile);
296
297 Set<UUID> areaUuidSet = null;
298 if(namedAreaUuids != null) {
299 areaUuidSet = namedAreaUuids.asSet();
300 }
301 Map<NamedArea, String> resultMap = geoservice.mapShapeFileToNamedAreas(
302 reader, idSearchFields , wmsLayerName , vocabUuid, areaUuidSet);
303 Map<String, String> flatResultMap = new HashMap<String, String>(resultMap.size());
304 for(NamedArea area : resultMap.keySet()){
305 flatResultMap.put(area.getTitleCache() + " [" + area.getUuid() + "]", resultMap.get(area));
306 }
307 mv.addObject(flatResultMap);
308 return mv;
309
310 }
311
312
313 }