Merge branch 'master' into abcd
[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 java.awt.Color;
13 import java.io.FileReader;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.UUID;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.apache.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 com.wordnik.swagger.annotations.Api;
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 @RequestParam(value = "fallbackAreaMarkerType", required = false) MarkerType fallbackAreaMarkerType,
148 HttpServletRequest request,
149 HttpServletResponse response)
150 throws IOException {
151
152 logger.info("doGetDistributionMapUriParams() " + request.getRequestURI());
153 ModelAndView mv = new ModelAndView();
154
155 // get the descriptions for the taxon
156 Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
157
158 Map<PresenceAbsenceTerm, Color> presenceAbsenceTermColors = null;
159 //languages
160 List<Language> langs = LocaleContext.getLanguages();
161
162 Set<MarkerType> hideMarkedAreas = null;
163 if(hideMarkedAreasList != null){
164 hideMarkedAreas = hideMarkedAreasList.asSet();
165 }
166
167 Set<DefinedTerm> scopes = null;
168 Set<NamedArea> geographicalScope = null;
169 Integer pageSize = null;
170 Integer pageNumber = null;
171 List<String> propertyPaths = null;
172 Pager<TaxonDescription> page = descriptionService.pageTaxonDescriptions(taxon, scopes, geographicalScope, pageSize, pageNumber, propertyPaths);
173
174 List<TaxonDescription> taxonDescriptions = page.getRecords();
175 String uriParams = geoservice.getDistributionServiceRequestParameterString(taxonDescriptions,
176 subAreaPreference, statusOrderPreference,
177 hideMarkedAreas, fallbackAreaMarkerType, presenceAbsenceTermColors, langs);
178 mv.addObject(uriParams);
179
180 return mv;
181 }
182
183
184 /**
185 * Assembles and returns URI parameter Strings for the EDIT Map Service. The distribution areas for the
186 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code> are found and are translated into
187 * an valid URI parameter String. Higher level distribution areas are expanded in order to include all
188 * nested sub-areas.
189 * <p>
190 * URI: <b>&#x002F;{datasource-name}&#x002F;geo&#x002F;map&#x002F;distribution&#x002F;{taxon-uuid}</b>
191 *
192 * @param request
193 * @param response
194 * @return URI parameter Strings for the EDIT Map Service
195 * @throws IOException TODO write controller method documentation
196 */
197 @RequestMapping(value = { "taxonOccurrencesFor/{uuid}" }, method = RequestMethod.GET)
198 public ModelAndView doGetOccurrenceMapUriParams(
199 @PathVariable("uuid") UUID uuid,
200 @RequestParam(value = "relationships", required = false) UuidList relationshipUuids,
201 @RequestParam(value = "relationshipsInvers", required = false) UuidList relationshipInversUuids,
202 @RequestParam(value = "maxDepth", required = false) Integer maxDepth,
203 HttpServletRequest request,
204 HttpServletResponse response)
205 throws IOException {
206
207 Map<SpecimenOrObservationType, Color> specimenOrObservationTypeColors = null;
208
209 logger.info("doGetOccurrenceMapUriParams() " + requestPathAndQuery(request));
210 ModelAndView mv = new ModelAndView();
211
212 Set<TaxonRelationshipEdge> includeRelationships = ControllerUtils.loadIncludeRelationships(
213 relationshipUuids, relationshipInversUuids, termService);
214
215 Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
216
217 List<OrderHint> orderHints = new ArrayList<OrderHint>();
218 orderHints.add(new OrderHint("titleCache", SortOrder.DESCENDING));
219
220 List<SpecimenOrObservationBase> specimensOrObersvations = occurrenceService.listByAssociatedTaxon(
221 null, includeRelationships, taxon, maxDepth, null, null, orderHints, null);
222
223 OccurrenceServiceRequestParameterDto dto = geoservice.getOccurrenceServiceRequestParameterString(specimensOrObersvations,
224 specimenOrObservationTypeColors );
225 mv.addObject(dto);
226 return mv;
227 }
228
229 /**
230 * EXPERIMENTAL !!!!!
231 * DO NOT USE !!!!!
232 *
233 * @param vocabUuid
234 * @param request
235 * @param response
236 * @return
237 * @throws IOException
238 *
239 * @author a.kohlbecker
240 */
241 @RequestMapping(value = { "mapShapeFileToNamedAreas" }, method = RequestMethod.GET)
242 public ModelAndView doMapShapeFileToNamedAreas(
243 @RequestParam(required=false, value="vocabularyUuid") UUID vocabUuid,
244 @RequestParam(required=false, value="namedAreaUuids") UuidList namedAreaUuids,
245 @RequestParam(required=true, value="localFile") String localFile,
246 @RequestParam(required=true, value="idSearchField") List<String> idSearchFields,
247 @RequestParam(required=true, value="wmsLayerName") String wmsLayerName,
248 HttpServletRequest request,
249 HttpServletResponse response)
250 throws IOException {
251
252 logger.info("doMapShapeFileToNamedAreas() " + requestPathAndQuery(request));
253 ModelAndView mv = new ModelAndView();
254
255 FileReader reader = new FileReader(localFile);
256
257 Set<UUID> areaUuidSet = null;
258 if(namedAreaUuids != null) {
259 areaUuidSet = namedAreaUuids.asSet();
260 }
261 Map<NamedArea, String> resultMap = geoservice.mapShapeFileToNamedAreas(
262 reader, idSearchFields , wmsLayerName , vocabUuid, areaUuidSet);
263 Map<String, String> flatResultMap = new HashMap<String, String>(resultMap.size());
264 for(NamedArea area : resultMap.keySet()){
265 flatResultMap.put(area.getTitleCache() + " [" + area.getUuid() + "]", resultMap.get(area));
266 }
267 mv.addObject(flatResultMap);
268 return mv;
269
270 }
271
272
273 }