#4313 implementing full stack of methods to expose TaxonNodeAgentRelations via remote...
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / TaxonController.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.cdm.remote.controller;
12
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import javax.persistence.EntityNotFoundException;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
24 import org.apache.log4j.Logger;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.stereotype.Controller;
27 import org.springframework.web.bind.annotation.PathVariable;
28 import org.springframework.web.bind.annotation.RequestMapping;
29 import org.springframework.web.bind.annotation.RequestMethod;
30 import org.springframework.web.bind.annotation.RequestParam;
31 import org.springframework.web.servlet.ModelAndView;
32
33 import com.wordnik.swagger.annotations.Api;
34
35 import eu.etaxonomy.cdm.api.service.INameService;
36 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
37 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
38 import eu.etaxonomy.cdm.api.service.ITaxonService;
39 import eu.etaxonomy.cdm.api.service.config.IncludedTaxonConfiguration;
40 import eu.etaxonomy.cdm.api.service.dto.IncludedTaxaDTO;
41 import eu.etaxonomy.cdm.api.service.pager.Pager;
42 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
43 import eu.etaxonomy.cdm.model.taxon.Classification;
44 import eu.etaxonomy.cdm.model.taxon.Taxon;
45 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
46 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
47 import eu.etaxonomy.cdm.model.taxon.TaxonNodeAgentRelation;
48 import eu.etaxonomy.cdm.persistence.query.OrderHint;
49 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
50 import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
51
52 /**
53 * TODO write controller documentation
54 *
55 * @author a.kohlbecker
56 * @date 20.07.2009
57 *
58 */
59 @Controller
60 @Api("taxon")
61 @RequestMapping(value = {"/taxon/{uuid}"})
62 public class TaxonController extends BaseController<TaxonBase, ITaxonService>
63 {
64 public static final Logger logger = Logger.getLogger(TaxonController.class);
65
66 @Autowired
67 private IOccurrenceService occurrenceService;
68 @Autowired
69 private INameService nameService;
70 @Autowired
71 private ITaxonNodeService nodeService;
72
73 protected static final List<String> TAXONNODE_INIT_STRATEGY = Arrays.asList(new String []{
74 "taxonNodes"
75 });
76
77 public TaxonController(){
78 super();
79 setInitializationStrategy(Arrays.asList(new String[]{
80 "$",
81 "name.nomenclaturalReference"
82 }
83 ));
84 }
85
86
87 @Override
88 @Autowired
89 public void setService(ITaxonService service) {
90 this.service = service;
91 }
92
93
94 /**
95 * Get the set of accepted {@link Taxon} entities for a given
96 * {@link TaxonBase} entity identified by the <code>{taxon-uuid}</code>.
97 * <p>
98 * URI: <b>&#x002F;{datasource-name}&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;accepted</b>
99 *
100 * @param request
101 * @param response
102 * @return a set on a list of {@link Taxon} entities which are initialized
103 * using the following initialization strategy:
104 * {@link #DEFAULT_INIT_STRATEGY}
105 * @throws IOException
106 */
107 /**
108 * Get the set of accepted {@link Taxon} entities for a given
109 * {@link TaxonBase} entity identified by the <code>{taxon-uuid}</code>.
110 * <p>
111 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;accepted</b>
112 *
113 * @param request
114 * @param response
115 * @return a Set of {@link Taxon} entities which are initialized
116 * using the following initialization strategy:
117 * {@link #SYNONYMY_INIT_STRATEGY}
118 * @throws IOException
119 * @Deprecated use getAcceptedFor() instead
120 */
121 @Deprecated
122 @RequestMapping(value = "accepted/{classification_uuid}", method = RequestMethod.GET)
123 public List<Taxon> getAcceptedForWithClassificationFilter(
124 @PathVariable("uuid") UUID uuid,
125 @PathVariable("classification_uuid") UUID classification_uuid,
126 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
127 @RequestParam(value = "pageSize", required = false) Integer pageSize,
128 HttpServletRequest request,
129 HttpServletResponse response)
130 throws IOException {
131
132
133 return getAcceptedFor(uuid, classification_uuid, pageNumber, pageSize, request, response);
134 }
135
136 @RequestMapping(value = "accepted", method = RequestMethod.GET)
137 public List<Taxon> getAcceptedFor(
138 @PathVariable("uuid") UUID uuid,
139 @RequestParam(value = "classificationFilter", required = false) UUID classification_uuid,
140 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
141 @RequestParam(value = "pageSize", required = false) Integer pageSize,
142 HttpServletRequest request,
143 HttpServletResponse response)
144 throws IOException {
145 if(request != null){
146 logger.info("getAccepted() " + requestPathAndQuery(request));
147 }
148
149 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
150 pagerParams.normalizeAndValidate(response);
151
152 List<Taxon> resultset = new ArrayList<Taxon>();
153 try {
154 resultset = service.listAcceptedTaxaFor(uuid, classification_uuid, pagerParams.getPageSize(), pagerParams.getPageIndex(), null, getInitializationStrategy());
155 } catch (EntityNotFoundException e){
156 HttpStatusMessage.UUID_NOT_FOUND.send(response);
157 }
158
159 return resultset;
160 }
161
162 @RequestMapping(value = "classifications", method = RequestMethod.GET)
163 public List<Classification> doGetClassifications(
164 @PathVariable("uuid") UUID uuid,
165 HttpServletRequest request,
166 HttpServletResponse response) throws IOException {
167 logger.info("doGetClassifications(): " + request.getRequestURI());
168 TaxonBase<?> taxonBase = service.load(uuid);
169
170 if (taxonBase == null){
171 HttpStatusMessage.UUID_NOT_FOUND.send(response);
172 }
173
174 return service.listClassifications(taxonBase, null, null, getInitializationStrategy());
175 }
176
177 @RequestMapping(value = "taxonNodes", method = RequestMethod.GET)
178 public Set<TaxonNode> doGetTaxonNodes(
179 @PathVariable("uuid") UUID uuid,
180 HttpServletRequest request,
181 HttpServletResponse response) throws IOException {
182
183 TaxonBase<?> tb = service.load(uuid, TAXONNODE_INIT_STRATEGY);
184 if(tb instanceof Taxon){
185 return ((Taxon)tb).getTaxonNodes();
186 } else {
187 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
188 return null;
189 }
190 }
191
192 @RequestMapping(value = "taxonNodeAgentRelations/{classification_uuid}", method = RequestMethod.GET)
193 public Pager<TaxonNodeAgentRelation> doGetTaxonNodeAgentRelations(
194 @PathVariable("uuid") UUID uuid,
195 @PathVariable("classification_uuid") UUID classificationUuid,
196 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
197 @RequestParam(value = "pageSize", required = false) Integer pageSize,
198 HttpServletRequest request,
199 HttpServletResponse response) throws IOException {
200
201 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
202 pagerParams.normalizeAndValidate(response);
203
204 Pager<TaxonNodeAgentRelation> pager = nodeService.pageTaxonNodeAgentRelations(uuid, classificationUuid,
205 pagerParams.getPageSize(), pagerParams.getPageIndex(), null);
206 return pager;
207
208
209 }
210
211
212 @RequestMapping(value = "specimensOrObservations", method = RequestMethod.GET)
213 public ModelAndView doListSpecimensOrObservations(
214 @PathVariable("uuid") UUID uuid,
215 HttpServletRequest request,
216 HttpServletResponse response) throws IOException {
217 logger.info("doListSpecimensOrObservations() - " + request.getRequestURI());
218
219 ModelAndView mv = new ModelAndView();
220
221 TaxonBase<?> tb = service.load(uuid);
222
223 List<OrderHint> orderHints = new ArrayList<OrderHint>();
224 orderHints.add(new OrderHint("titleCache", SortOrder.DESCENDING));
225
226 if(tb instanceof Taxon){
227 List<SpecimenOrObservationBase<?>> specimensOrObservations = occurrenceService.listByAssociatedTaxon(null, null, (Taxon)tb, null, null, null, orderHints, null);
228 mv.addObject(specimensOrObservations);
229 } else {
230 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
231 return null;
232 }
233
234 return mv;
235 }
236
237 @RequestMapping(value = "associatedFieldUnits", method = RequestMethod.GET)
238 public Pager<SpecimenOrObservationBase> doGetFieldUnits(
239 @PathVariable("uuid") UUID uuid,
240 @RequestParam(value = "maxDepth", required = false) Integer maxDepth,
241 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
242 @RequestParam(value = "pageSize", required = false) Integer pageSize,
243 HttpServletRequest request,
244 HttpServletResponse response) throws IOException {
245 logger.info("doGetFieldUnits() - " + request.getRequestURI());
246
247 ModelAndView mv = new ModelAndView();
248
249 TaxonBase<?> tb = service.load(uuid);
250
251 List<OrderHint> orderHints = new ArrayList<OrderHint>();
252 orderHints.add(new OrderHint("titleCache", SortOrder.ASCENDING));
253
254 if(tb instanceof Taxon){
255 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
256 pagerParams.normalizeAndValidate(response);
257
258 return occurrenceService.pageFieldUnitsByAssociatedTaxon(null, (Taxon) tb, null, pagerParams.getPageSize(), pagerParams.getPageIndex(), orderHints, null);
259 }
260 return null;
261 }
262
263 @RequestMapping(value = "taggedName", method = RequestMethod.GET)
264 public ModelAndView doGetTaggedName(
265 @PathVariable("uuid") UUID uuid,
266 HttpServletRequest request) {
267 logger.info("doGetDescriptionElementsByType() - " + request.getRequestURI());
268
269 ModelAndView mv = new ModelAndView();
270
271 TaxonBase<?> tb = service.load(uuid, Arrays.asList(new String[] {"name"}));
272 mv.addObject(nameService.getTaggedName(tb.getName().getUuid()));
273 return mv;
274 }
275
276 /**
277 * This webservice endpoint returns all taxa which are congruent or included in the taxon represented by the given taxon uuid.
278 * The result also returns the path to these taxa represented by the uuids of the taxon relationships types and doubtful information.
279 * If classificationUuids is set only taxa of classifications are returned which are included in the given classifications.
280 * Also the path to these taxa may not include taxa from other classifications.
281 *
282 * @param taxonUUIDString
283 * @param classificationStringList
284 * @param includeDoubtful
285 * @param onlyCongruent
286 * @param response
287 * @param request
288 * @return
289 * @throws IOException
290 */
291
292 @RequestMapping(value = { "includedTaxa" }, method = { RequestMethod.GET })
293 public ModelAndView doGetIncludedTaxa(
294 @PathVariable("uuid") UUID uuid,
295 @RequestParam(value="classificationFilter", required=false) final List<String> classificationStringList,
296 @RequestParam(value="includeDoubtful", required=false) final boolean includeDoubtful,
297 @RequestParam(value="onlyCongruent", required=false) final boolean onlyCongruent,
298 HttpServletResponse response,
299 HttpServletRequest request) throws IOException {
300 ModelAndView mv = new ModelAndView();
301 /**
302 * List<UUID> classificationFilter,
303 * boolean includeDoubtful,
304 * boolean onlyCongruent)
305 */
306 List<UUID> classificationFilter = null;
307 if( classificationStringList != null ){
308 classificationFilter = new ArrayList<UUID>();
309 for(String classString :classificationStringList){
310 classificationFilter.add(UUID.fromString(classString));
311 }
312 }
313 final IncludedTaxonConfiguration configuration = new IncludedTaxonConfiguration(classificationFilter, includeDoubtful, onlyCongruent);
314 IncludedTaxaDTO listIncludedTaxa = service.listIncludedTaxa(uuid, configuration);
315 mv.addObject(listIncludedTaxa);
316 return mv;
317 }
318
319 }