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