8e7b54ce5dc85b033d33b658d0869046002cc1f7
[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.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.UUID;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
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.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.geo.IDistributionService;
41 import eu.etaxonomy.cdm.api.util.TaxonRelationshipEdge;
42 import eu.etaxonomy.cdm.database.UpdatableRoutingDataSource;
43 import eu.etaxonomy.cdm.ext.geo.EditGeoServiceUtilities;
44 import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
45 import eu.etaxonomy.cdm.ext.geo.OccurrenceServiceRequestParameterDto;
46 import eu.etaxonomy.cdm.model.common.MarkerType;
47 import eu.etaxonomy.cdm.model.location.NamedArea;
48 import eu.etaxonomy.cdm.model.location.Point;
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.util.ControllerUtils;
57 import eu.etaxonomy.cdm.remote.editor.DefinedTermBaseList;
58 import eu.etaxonomy.cdm.remote.editor.TermBaseListPropertyEditor;
59 import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
60 import eu.etaxonomy.cdm.remote.editor.UuidList;
61 import io.swagger.annotations.Api;
62
63 /**
64 * The ExternalGeoController class is a Spring MVC Controller.
65 * <p>
66 * The syntax of the mapped service URIs contains the the {datasource-name} path element.
67 * The available {datasource-name}s are defined in a configuration file which
68 * is loaded by the {@link UpdatableRoutingDataSource}. If the
69 * UpdatableRoutingDataSource is not being used in the actual application
70 * context any arbitrary {datasource-name} may be used.
71 * <p>
72 * @author a.kohlbecker
73 * @since 18.06.2009
74 *
75 */
76
77 @Controller
78 @Api(value="mapServiceParameters")
79 @RequestMapping(value = { "ext/edit/mapServiceParameters/" })
80 public class ExternalGeoController extends BaseController<TaxonBase, ITaxonService> {
81
82 private static final Logger logger = LogManager.getLogger();
83
84 @Autowired
85 private IEditGeoService geoService;
86
87 @Autowired
88 private IDistributionService distributionService;
89
90 @Autowired
91 private IDescriptionService descriptionService;
92
93 @Autowired
94 private IOccurrenceService occurrenceService;
95
96 @Autowired
97 private ITermService termService;
98
99 @InitBinder
100 @Override
101 public void initBinder(WebDataBinder binder) {
102 super.initBinder(binder);
103 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
104 binder.registerCustomEditor(DefinedTermBaseList.class, new TermBaseListPropertyEditor<MarkerType>(termService));
105 }
106
107 @Autowired
108 @Override
109 public void setService(ITaxonService service) {
110 this.service = service;
111 }
112
113 /**
114 * Assembles and returns URI parameter Strings for the EDIT Map Service. The distribution areas for the
115 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code> are found and are translated into
116 * an valid URI parameter String. Higher level distribution areas are expanded in order to include all
117 * nested sub-areas.
118 * <p>
119 * URI: <b>&#x002F;{datasource-name}&#x002F;geo&#x002F;map&#x002F;distribution&#x002F;{taxon-uuid}</b>
120 *
121 * @param request
122 * @param response
123 * @return URI parameter Strings for the EDIT Map Service
124 * @throws IOException TODO write controller method documentation
125 */
126 @RequestMapping(value = { "taxonOccurrencesFor/{uuid}" }, method = RequestMethod.GET)
127 public OccurrenceServiceRequestParameterDto doGetOccurrenceMapUriParams(
128 @PathVariable("uuid") UUID uuid,
129 @RequestParam(value = "relationships", required = false) UuidList relationshipUuids,
130 @RequestParam(value = "relationshipsInvers", required = false) UuidList relationshipInversUuids,
131 @RequestParam(value = "maxDepth", required = false) Integer maxDepth,
132 HttpServletRequest request,
133 HttpServletResponse response)
134 throws IOException {
135
136
137 logger.info("doGetOccurrenceMapUriParams() " + requestPathAndQuery(request));
138
139 Map<SpecimenOrObservationType, Color> specimenOrObservationTypeColors = null;
140
141 List<SpecimenOrObservationBase> specimensOrObersvations = occurencesForTaxon(uuid, relationshipUuids,
142 relationshipInversUuids, maxDepth, response);
143
144 OccurrenceServiceRequestParameterDto dto = geoService.getOccurrenceServiceRequestParameters(specimensOrObersvations,
145 specimenOrObservationTypeColors );
146
147 return dto;
148 }
149
150 private List<SpecimenOrObservationBase> occurencesForTaxon(UUID taxonUuid, UuidList relationshipUuids,
151 UuidList relationshipInversUuids, Integer maxDepth, HttpServletResponse response) throws IOException {
152 Set<TaxonRelationshipEdge> includeRelationships = ControllerUtils.loadIncludeRelationships(
153 relationshipUuids, relationshipInversUuids, termService);
154
155 Taxon taxon = getCdmBaseInstance(Taxon.class, taxonUuid, response, (List<String>)null);
156
157 List<OrderHint> orderHints = new ArrayList<>();
158 orderHints.add(new OrderHint("titleCache", SortOrder.DESCENDING));
159
160 List<SpecimenOrObservationBase> specimensOrObersvations = occurrenceService.listByAssociatedTaxon(
161 null, includeRelationships, taxon, maxDepth, null, null, orderHints, null);
162 return specimensOrObersvations;
163 }
164
165 /**
166 * Assembles and returns URI parameter Strings for the EDIT Map Service. The distribution areas for the
167 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code> are found and are translated into
168 * an valid URI parameter String. Higher level distribution areas are expanded in order to include all
169 * nested sub-areas.
170 * <p>
171 * URI: <b>&#x002F;{datasource-name}&#x002F;geo&#x002F;map&#x002F;distribution&#x002F;{taxon-uuid}</b>
172 *
173 * @param request
174 * @param response
175 * @return URI parameter Strings for the EDIT Map Service
176 * @throws IOException TODO write controller method documentation
177 */
178 @RequestMapping(value = { "taxonOccurrencesForX" }, method = RequestMethod.GET)
179 public ModelAndView doGetOccurrenceXMapUriParams(
180 @RequestParam(value = "fieldUnitUuidList", required = false) UuidList fieldUnitUuids,
181 HttpServletRequest request,
182 HttpServletResponse response)
183 throws IOException {
184
185 Map<SpecimenOrObservationType, Color> specimenOrObservationTypeColors = null;
186
187 logger.info("doGetOccurrenceMapUriParams() " + requestPathAndQuery(request));
188 ModelAndView mv = new ModelAndView();
189
190 List<Point> fieldUnitPoints = occurrenceService.findPointsForFieldUnitList(fieldUnitUuids);
191
192 OccurrenceServiceRequestParameterDto dto = EditGeoServiceUtilities.getOccurrenceServiceRequestParameterString(fieldUnitPoints,
193 null, specimenOrObservationTypeColors);
194 mv.addObject(dto);
195 return mv;
196 }
197
198 /**
199 * EXPERIMENTAL !!!!!
200 * DO NOT USE !!!!!
201 *
202 * @param vocabUuid
203 * @param request
204 * @param response
205 * @return
206 * @throws IOException
207 *
208 * @author a.kohlbecker
209 */
210 @RequestMapping(value = { "mapShapeFileToNamedAreas" }, method = RequestMethod.GET)
211 public ModelAndView doMapShapeFileToNamedAreas(
212 @RequestParam(required=false, value="vocabularyUuid") UUID vocabUuid,
213 @RequestParam(required=false, value="namedAreaUuids") UuidList namedAreaUuids,
214 @RequestParam(required=true, value="localFile") String localFile,
215 @RequestParam(required=true, value="idSearchField") List<String> idSearchFields,
216 @RequestParam(required=true, value="wmsLayerName") String wmsLayerName,
217 HttpServletRequest request,
218 HttpServletResponse response)
219 throws IOException {
220
221 logger.info("doMapShapeFileToNamedAreas() " + requestPathAndQuery(request));
222 ModelAndView mv = new ModelAndView();
223
224 FileReader reader = new FileReader(localFile);
225
226 Set<UUID> areaUuidSet = null;
227 if(namedAreaUuids != null) {
228 areaUuidSet = namedAreaUuids.asSet();
229 }
230 Map<NamedArea, String> resultMap = distributionService.mapShapeFileToNamedAreas(
231 reader, idSearchFields , wmsLayerName , vocabUuid, areaUuidSet);
232 Map<String, String> flatResultMap = new HashMap<String, String>(resultMap.size());
233 for(NamedArea area : resultMap.keySet()){
234 flatResultMap.put(area.getTitleCache() + " [" + area.getUuid() + "]", resultMap.get(area));
235 }
236 mv.addObject(flatResultMap);
237 return mv;
238
239 }
240
241
242 }