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