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