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