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