#5397 replacing swagger-springmvc by springfox-swagger2
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / ext / ExternalGeoController.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.remote.controller.ext;
11
12 import io.swagger.annotations.Api;
13
14 import java.awt.Color;
15 import java.io.FileReader;
16 import java.io.IOException;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.UUID;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.apache.log4j.Logger;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Controller;
31 import org.springframework.web.bind.WebDataBinder;
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;
37 import org.springframework.web.servlet.ModelAndView;
38
39 import eu.etaxonomy.cdm.api.service.IDescriptionService;
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.service.pager.Pager;
44 import eu.etaxonomy.cdm.api.service.util.TaxonRelationshipEdge;
45 import eu.etaxonomy.cdm.api.utility.DescriptionUtility;
46 import eu.etaxonomy.cdm.database.UpdatableRoutingDataSource;
47 import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
48 import eu.etaxonomy.cdm.ext.geo.OccurrenceServiceRequestParameterDto;
49 import eu.etaxonomy.cdm.model.common.DefinedTerm;
50 import eu.etaxonomy.cdm.model.common.Language;
51 import eu.etaxonomy.cdm.model.common.Marker;
52 import eu.etaxonomy.cdm.model.common.MarkerType;
53 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
54 import eu.etaxonomy.cdm.model.description.TaxonDescription;
55 import eu.etaxonomy.cdm.model.location.NamedArea;
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.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 eu.etaxonomy.cdm.remote.l10n.LocaleContext;
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 * @date 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 * EXPERIMENTAL !!!!!
230 * DO NOT USE !!!!!
231 *
232 * @param vocabUuid
233 * @param request
234 * @param response
235 * @return
236 * @throws IOException
237 *
238 * @author a.kohlbecker
239 */
240 @RequestMapping(value = { "mapShapeFileToNamedAreas" }, method = RequestMethod.GET)
241 public ModelAndView doMapShapeFileToNamedAreas(
242 @RequestParam(required=false, value="vocabularyUuid") UUID vocabUuid,
243 @RequestParam(required=false, value="namedAreaUuids") UuidList namedAreaUuids,
244 @RequestParam(required=true, value="localFile") String localFile,
245 @RequestParam(required=true, value="idSearchField") List<String> idSearchFields,
246 @RequestParam(required=true, value="wmsLayerName") String wmsLayerName,
247 HttpServletRequest request,
248 HttpServletResponse response)
249 throws IOException {
250
251 logger.info("doMapShapeFileToNamedAreas() " + requestPathAndQuery(request));
252 ModelAndView mv = new ModelAndView();
253
254 FileReader reader = new FileReader(localFile);
255
256 Set<UUID> areaUuidSet = null;
257 if(namedAreaUuids != null) {
258 areaUuidSet = namedAreaUuids.asSet();
259 }
260 Map<NamedArea, String> resultMap = geoservice.mapShapeFileToNamedAreas(
261 reader, idSearchFields , wmsLayerName , vocabUuid, areaUuidSet);
262 Map<String, String> flatResultMap = new HashMap<String, String>(resultMap.size());
263 for(NamedArea area : resultMap.keySet()){
264 flatResultMap.put(area.getTitleCache() + " [" + area.getUuid() + "]", resultMap.get(area));
265 }
266 mv.addObject(flatResultMap);
267 return mv;
268
269 }
270
271
272 }