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