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