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