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