merge-update from 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.IOException;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
23 import org.apache.log4j.Logger;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.stereotype.Controller;
26 import org.springframework.web.bind.WebDataBinder;
27 import org.springframework.web.bind.annotation.InitBinder;
28 import org.springframework.web.bind.annotation.PathVariable;
29 import org.springframework.web.bind.annotation.RequestMapping;
30 import org.springframework.web.bind.annotation.RequestMethod;
31 import org.springframework.web.bind.annotation.RequestParam;
32 import org.springframework.web.servlet.ModelAndView;
33
34 import eu.etaxonomy.cdm.api.service.IDescriptionService;
35 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
36 import eu.etaxonomy.cdm.api.service.ITaxonService;
37 import eu.etaxonomy.cdm.api.service.ITermService;
38 import eu.etaxonomy.cdm.api.service.pager.Pager;
39 import eu.etaxonomy.cdm.api.service.util.TaxonRelationshipEdge;
40 import eu.etaxonomy.cdm.database.UpdatableRoutingDataSource;
41 import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
42 import eu.etaxonomy.cdm.model.common.DefinedTerm;
43 import eu.etaxonomy.cdm.model.common.Language;
44 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
45 import eu.etaxonomy.cdm.model.description.TaxonDescription;
46 import eu.etaxonomy.cdm.model.location.NamedArea;
47 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
48 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
49 import eu.etaxonomy.cdm.model.taxon.Taxon;
50 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
51 import eu.etaxonomy.cdm.persistence.query.OrderHint;
52 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
53 import eu.etaxonomy.cdm.remote.controller.BaseController;
54 import eu.etaxonomy.cdm.remote.controller.util.ControllerUtils;
55 import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
56 import eu.etaxonomy.cdm.remote.editor.UuidList;
57 import eu.etaxonomy.cdm.remote.l10n.LocaleContext;
58
59 /**
60 * The ExternalGeoController class is a Spring MVC Controller.
61 * <p>
62 * The syntax of the mapped service URIs contains the the {datasource-name} path element.
63 * The available {datasource-name}s are defined in a configuration file which
64 * is loaded by the {@link UpdatableRoutingDataSource}. If the
65 * UpdatableRoutingDataSource is not being used in the actual application
66 * context any arbitrary {datasource-name} may be used.
67 * <p>
68 * @author a.kohlbecker
69 * @date 18.06.2009
70 *
71 */
72 @Controller
73 @RequestMapping(value = { "ext/edit/mapServiceParameters/" })
74 public class ExternalGeoController extends BaseController<TaxonBase, ITaxonService> {
75
76 public static final Logger logger = Logger.getLogger(ExternalGeoController.class);
77
78 @Autowired
79 private IEditGeoService geoservice;
80
81 @Autowired
82 private IDescriptionService descriptionService;
83
84 @Autowired
85 private IOccurrenceService occurrenceService;
86
87 @Autowired
88 private ITermService termService;
89
90 @InitBinder
91 @Override
92 public void initBinder(WebDataBinder binder) {
93 super.initBinder(binder);
94 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
95 }
96
97 /*
98 * (non-Javadoc)
99 *
100 * @see
101 * eu.etaxonomy.cdm.remote.controller.BaseController#setService(eu.etaxonomy
102 * .cdm.api.service.IService)
103 */
104 @Autowired
105 @Override
106 public void setService(ITaxonService service) {
107 this.service = service;
108 }
109
110 /**
111 * Assembles and returns URI parameter Strings for the EDIT Map Service. The distribution areas for the
112 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code> are found and are translated into
113 * an valid URI parameter String. Higher level distribiution areas are expanded in order to include all
114 * nested sub-areas.
115 * <p>
116 * URI: <b>&#x002F;{datasource-name}&#x002F;geo&#x002F;map&#x002F;distribution&#x002F;{taxon-uuid}</b>
117 *
118 * @param request
119 * @param response
120 * @return URI parameter Strings for the EDIT Map Service
121 * @throws IOException TODO write controller method documentation
122 */
123 @RequestMapping(value = { "taxonDistributionFor/{uuid}" }, method = RequestMethod.GET)
124 public ModelAndView doGetDistributionMapUriParams(
125 @PathVariable("uuid") UUID uuid,
126 HttpServletRequest request,
127 HttpServletResponse response)
128 throws IOException {
129
130
131 int width = 0;
132 int height = 0;
133 String bbox = null;
134 String backLayer = null;
135
136 logger.info("doGetDistributionMapUriParams() " + request.getRequestURI());
137 ModelAndView mv = new ModelAndView();
138
139 // get the descriptions for the taxon
140 Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
141
142 Map<PresenceAbsenceTermBase<?>, Color> presenceAbsenceTermColors = null;
143 //languages
144 List<Language> langs = LocaleContext.getLanguages();
145
146 Set<DefinedTerm> scopes = null;
147 Set<NamedArea> geographicalScope = null;
148 Integer pageSize = null;
149 Integer pageNumber = null;
150 List<String> propertyPaths = null;
151 Pager<TaxonDescription> page = descriptionService.pageTaxonDescriptions(taxon, scopes, geographicalScope, pageSize, pageNumber, propertyPaths);
152
153 List<TaxonDescription> taxonDescriptions = page.getRecords();
154 String uriParams = geoservice.getDistributionServiceRequestParameterString(taxonDescriptions, presenceAbsenceTermColors, width, height, bbox,
155 backLayer, langs);
156 mv.addObject(uriParams);
157 return mv;
158 }
159
160
161 /**
162 * Assembles and returns URI parameter Strings for the EDIT Map Service. The distribution areas for the
163 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code> are found and are translated into
164 * an valid URI parameter String. Higher level distribiution areas are expanded in order to include all
165 * nested sub-areas.
166 * <p>
167 * URI: <b>&#x002F;{datasource-name}&#x002F;geo&#x002F;map&#x002F;distribution&#x002F;{taxon-uuid}</b>
168 *
169 * @param request
170 * @param response
171 * @return URI parameter Strings for the EDIT Map Service
172 * @throws IOException TODO write controller method documentation
173 */
174 @RequestMapping(value = { "taxonOccurrencesFor/{uuid}" }, method = RequestMethod.GET)
175 public ModelAndView doGetOccurrenceMapUriParams(
176 @PathVariable("uuid") UUID uuid,
177 @RequestParam(value = "relationships", required = false) UuidList relationshipUuids,
178 @RequestParam(value = "relationshipsInvers", required = false) UuidList relationshipInversUuids,
179 @RequestParam(value = "maxDepth", required = false) Integer maxDepth,
180 HttpServletRequest request,
181 HttpServletResponse response)
182 throws IOException {
183
184 Integer width = null;
185 Integer height = null;
186 String bbox = null;
187 String backLayer = null;
188 Boolean doReturnImage = null;
189 Map<SpecimenOrObservationType, Color> specimenOrObservationTypeColors = null;
190
191 logger.info("doGetOccurrenceMapUriParams() " + request.getRequestURI() + "?" + request.getQueryString());
192 ModelAndView mv = new ModelAndView();
193
194 Set<TaxonRelationshipEdge> includeRelationships = ControllerUtils.loadIncludeRelationships(relationshipUuids, relationshipInversUuids, termService);
195
196 Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
197
198 List<OrderHint> orderHints = new ArrayList<OrderHint>();
199 orderHints.add(new OrderHint("titleCache", SortOrder.DESCENDING));
200
201 List<SpecimenOrObservationBase> specimensOrObersvations = occurrenceService.listByAssociatedTaxon(null, includeRelationships, taxon, maxDepth, null, null, orderHints, null);
202
203 String uriParams = geoservice.getOccurrenceServiceRequestParameterString(specimensOrObersvations, specimenOrObservationTypeColors, doReturnImage, width , height , bbox , backLayer );
204 mv.addObject(uriParams);
205 return mv;
206 }
207
208 }