79c446370d76373dac7a0a39551b8d426281364d
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / TaxonController.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.remote.controller;
11
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19 import java.util.UUID;
20 import java.util.stream.Collectors;
21
22 import javax.persistence.EntityNotFoundException;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.apache.log4j.Logger;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Controller;
29 import org.springframework.web.bind.WebDataBinder;
30 import org.springframework.web.bind.annotation.PathVariable;
31 import org.springframework.web.bind.annotation.RequestMapping;
32 import org.springframework.web.bind.annotation.RequestMethod;
33 import org.springframework.web.bind.annotation.RequestParam;
34 import org.springframework.web.servlet.ModelAndView;
35
36 import eu.etaxonomy.cdm.api.service.IDescriptionService;
37 import eu.etaxonomy.cdm.api.service.INameService;
38 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
39 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
40 import eu.etaxonomy.cdm.api.service.ITaxonService;
41 import eu.etaxonomy.cdm.api.service.ITermService;
42 import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
43 import eu.etaxonomy.cdm.api.service.config.IncludedTaxonConfiguration;
44 import eu.etaxonomy.cdm.api.service.dto.FieldUnitDTO;
45 import eu.etaxonomy.cdm.api.service.dto.IncludedTaxaDTO;
46 import eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO;
47 import eu.etaxonomy.cdm.api.service.dto.TaxonRelationshipsDTO;
48 import eu.etaxonomy.cdm.api.service.pager.Pager;
49 import eu.etaxonomy.cdm.exception.UnpublishedException;
50 import eu.etaxonomy.cdm.model.common.CdmBase;
51 import eu.etaxonomy.cdm.model.common.MarkerType;
52 import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
53 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
54 import eu.etaxonomy.cdm.model.description.DescriptionType;
55 import eu.etaxonomy.cdm.model.description.TaxonDescription;
56 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
57 import eu.etaxonomy.cdm.model.taxon.Classification;
58 import eu.etaxonomy.cdm.model.taxon.Synonym;
59 import eu.etaxonomy.cdm.model.taxon.Taxon;
60 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
61 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
62 import eu.etaxonomy.cdm.model.taxon.TaxonNodeAgentRelation;
63 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
64 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
65 import eu.etaxonomy.cdm.persistence.dao.initializer.EntityInitStrategy;
66 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
67 import eu.etaxonomy.cdm.persistence.query.OrderHint;
68 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
69 import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
70 import eu.etaxonomy.cdm.remote.dto.common.StringResultDTO;
71 import eu.etaxonomy.cdm.remote.editor.TermBasePropertyEditor;
72 import eu.etaxonomy.cdm.remote.editor.UuidList;
73 import io.swagger.annotations.Api;
74
75 /**
76 * TODO write controller documentation
77 *
78 * @author a.kohlbecker
79 * @since 20.07.2009
80 */
81 @Controller
82 @Api("taxon")
83 @RequestMapping(value = {"/taxon/{uuid}"})
84 public class TaxonController extends AbstractIdentifiableController<TaxonBase, ITaxonService>{
85
86 public static final Logger logger = Logger.getLogger(TaxonController.class);
87
88 @Autowired
89 private IOccurrenceService occurrenceService;
90
91 @Autowired
92 private INameService nameService;
93
94 @Autowired
95 private ITaxonNodeService nodeService;
96
97 @Autowired
98 private IDescriptionService descriptionService;
99
100 @Autowired
101 private ITermService termService;
102
103 protected static final EntityInitStrategy TAXONNODE_INIT_STRATEGY = new EntityInitStrategy(Arrays.asList(new String []{
104 "taxonNodes.classification",
105 "taxonNodes.parent",
106 "taxonNodes.parent.childNodes", //currently needed to compute sortindex //TODO improve
107 "taxonNodes.taxon.name",
108 "taxonNodes.taxon.secSource.citation",
109 "taxonNodes.statusNote",
110 "acceptedTaxon.taxonNodes.classification"
111 }));
112
113 public TaxonController(){
114 super();
115 setInitializationStrategy(Arrays.asList(new String[]{
116 "$",
117 "name.nomenclaturalSource.citation"
118 }
119 ));
120 }
121
122 @Override
123 @Autowired
124 public void setService(ITaxonService service) {
125 this.service = service;
126 }
127
128 @Override
129 public void initBinder(WebDataBinder binder) {
130 super.initBinder(binder);
131 binder.registerCustomEditor(MarkerType.class, new TermBasePropertyEditor<>(termService));
132 }
133
134 protected List<String> getTaxonDescriptionInitStrategy() {
135 return getInitializationStrategy(); // return Arrays.asList("$", "")
136 }
137
138 protected List<String> getTaxonDescriptionElementInitStrategy() {
139 return getInitializationStrategy();
140 }
141
142 @RequestMapping(params="subtree", method = RequestMethod.GET)
143 public TaxonBase<?> doGet(@PathVariable("uuid") UUID uuid,
144 @RequestParam(value = "subtree", required = true) UUID subtreeUuid, //if subtree does not exist the base class method is used, therefore required
145 HttpServletRequest request,
146 HttpServletResponse response) throws IOException {
147 if(request != null) {
148 logger.info("doGet() " + requestPathAndQuery(request));
149 }
150 //TODO do we want to allow Synonyms at all? Maybe needs initialization
151 EntityInitStrategy initStrategy = new EntityInitStrategy(getInitializationStrategy());
152 initStrategy.extend(null, getTaxonNodeInitStrategy(), false);
153 TaxonBase<?> taxonBase = getCdmBaseInstance(uuid, response, initStrategy.getPropertyPaths());
154 //TODO we should move subtree check down to service or persistence
155 TaxonNode subtree = getSubtreeOrError(subtreeUuid, nodeService, response);
156 taxonBase = checkExistsSubtreeAndAccess(taxonBase, subtree, NO_UNPUBLISHED, response);
157 return taxonBase;
158 }
159
160 /**
161 * Checks if a {@link TaxonBase taxonBase} is public and belongs to a {@link TaxonNode subtree}
162 * as accepted taxon or synonym.
163 * If not the according {@link HttpStatusMessage http messages} are send to response.
164 * <BR>
165 * Not (yet) checked is the relation to a subtree via a concept relationship.
166 * @param taxonBase
167 * @param includeUnpublished
168 * @param response
169 * @return
170 * @throws IOException
171 */
172 protected <S extends TaxonBase<?>> S checkExistsSubtreeAndAccess(S taxonBase,
173 TaxonNode subtree, boolean includeUnpublished,
174 HttpServletResponse response) throws IOException {
175 taxonBase = checkExistsAndAccess(taxonBase, NO_UNPUBLISHED, response);
176 if (subtree == null){
177 return taxonBase;
178 }else if(taxonBase != null){
179 //TODO synonyms maybe can not be initialized
180 Taxon taxon = taxonBase.isInstanceOf(Synonym.class)?
181 CdmBase.deproxy(taxonBase, Synonym.class).getAcceptedTaxon():
182 CdmBase.deproxy(taxonBase, Taxon.class);
183 //check if taxon has any node that is a descendant of subtree
184 for (TaxonNode taxonNode :taxon.getTaxonNodes()){
185 if (subtree.isAncestor(taxonNode)){
186 return taxonBase;
187 }
188 }
189 HttpStatusMessage.ACCESS_DENIED.send(response);
190 }
191 return null;
192 }
193
194
195 /**
196 * Get the accepted {@link Taxon} for a given
197 * {@link TaxonBase} entity identified by the <code>{taxon-uuid}</code>.
198 * <p>
199 * URI: <b>&#x002F;{datasource-name}&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;accepted</b>
200 *
201 * @param request
202 * @param response
203 * @return a set on a list of {@link Taxon} entities which are initialized
204 * using the following initialization strategy:
205 * {@link #DEFAULT_INIT_STRATEGY}
206 * @throws IOException
207 */
208 @RequestMapping(value = "accepted", method = RequestMethod.GET)
209 public Taxon doGetAcceptedFor(
210 @PathVariable("uuid") UUID uuid,
211 @RequestParam(value = "classificationFilter", required = false) UUID classification_uuid,
212 HttpServletRequest request,
213 HttpServletResponse response)
214 throws IOException {
215 if(request != null){
216 logger.info("doGetAcceptedFor() " + requestPathAndQuery(request));
217 }
218
219 try {
220 boolean includeUnpublished = NO_UNPUBLISHED;
221 Taxon result = service.findAcceptedTaxonFor(uuid, classification_uuid, includeUnpublished, getInitializationStrategy());
222 result = checkExistsAndAccess(result, includeUnpublished, response);
223
224 return result;
225 } catch (EntityNotFoundException e){
226 HttpStatusMessage.UUID_NOT_FOUND.send(response, e.getMessage());
227 } catch (UnpublishedException e) {
228 HttpStatusMessage.ACCESS_DENIED.send(response, e.getMessage());
229 }
230 return null;
231
232 }
233
234 @RequestMapping(value = "classifications", method = RequestMethod.GET)
235 public List<Classification> doGetClassifications(
236 @PathVariable("uuid") UUID uuid,
237 HttpServletRequest request,
238 HttpServletResponse response) throws IOException {
239
240 boolean includeUnpublished = NO_UNPUBLISHED;
241
242 logger.info("doGetClassifications(): " + request.getRequestURI());
243 TaxonBase<?> taxonBase = service.load(uuid);
244 taxonBase = checkExistsAndAccess(taxonBase, includeUnpublished, response);
245
246 return service.listClassifications(taxonBase, null, null, getInitializationStrategy());
247 }
248
249 @RequestMapping(value = "taxonNodes", method = RequestMethod.GET)
250 public Collection<TaxonNodeDto> doGetTaxonNodes(
251 @PathVariable("uuid") UUID taxonUuid,
252 @RequestParam(value = "subtree", required = false) UUID subtreeUuid,
253 HttpServletRequest request,
254 HttpServletResponse response) throws IOException {
255
256 logger.info("doGetTaxonNodes" + requestPathAndQuery(request));
257 TaxonBase<?> taxonBase = null;
258 if (subtreeUuid != null){
259 taxonBase = doGet(taxonUuid, subtreeUuid, request, response);
260 }else{
261 // taxonBase = service.load(taxonUuid, NO_UNPUBLISHED, getTaxonNodeInitStrategy().getPropertyPaths());
262 }
263 if(taxonBase != null && taxonBase instanceof Taxon){
264 return ((Taxon)taxonBase).getTaxonNodes().stream().map(e -> new TaxonNodeDto(e)).collect(Collectors.toSet());
265 }else {
266 try {
267 return nodeService.getTaxonNodeDtosFromTaxon(taxonUuid);
268 }catch(Exception e) {
269 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
270 return null;
271 }
272
273 }
274
275
276
277 }
278
279 protected EntityInitStrategy getTaxonNodeInitStrategy() {
280 return TAXONNODE_INIT_STRATEGY;
281 }
282
283 /**
284 * See also {@link AgentController#doGetTaxonNodeAgentRelations(UUID, UUID, Integer, Integer, HttpServletRequest, HttpServletResponse)}
285 */
286 @RequestMapping(value = "taxonNodeAgentRelations/{classification_uuid}", method = RequestMethod.GET)
287 public Pager<TaxonNodeAgentRelation> doGetTaxonNodeAgentRelations(
288 @PathVariable("uuid") UUID uuid,
289 @PathVariable("classification_uuid") UUID classificationUuid,
290 @RequestParam(value = "relType_uuid" , required = false) UUID relTypeUuid,
291 @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
292 @RequestParam(value = "pageSize", required = false) Integer pageSize,
293 HttpServletRequest request,
294 HttpServletResponse response) throws IOException {
295
296 PagerParameters pagerParams = new PagerParameters(pageSize, pageIndex);
297 pagerParams.normalizeAndValidate(response);
298
299 Pager<TaxonNodeAgentRelation> pager = nodeService.pageTaxonNodeAgentRelations(uuid, classificationUuid,
300 null, null, relTypeUuid, pagerParams.getPageSize(), pagerParams.getPageIndex(), null);
301 return pager;
302 }
303
304
305 @RequestMapping(value = "specimensOrObservationsCount", method = RequestMethod.GET)
306 public StringResultDTO doCountSpecimensOrObservations(
307 @PathVariable("uuid") UUID uuid,
308 HttpServletRequest request,
309 HttpServletResponse response) {
310 logger.info("doListSpecimensOrObservations() - " + request.getRequestURI());
311
312 List<OrderHint> orderHints = new ArrayList<>();
313 orderHints.add(new OrderHint("titleCache", SortOrder.DESCENDING));
314 FindOccurrencesConfigurator config = new FindOccurrencesConfigurator();
315 config.setAssociatedTaxonUuid(uuid);
316 long countSpecimen = occurrenceService.countOccurrences(config);
317 return new StringResultDTO(String.valueOf(countSpecimen));
318 }
319
320 /**
321 * @deprecated replaced by rootUnitDTOs
322 */
323 @Deprecated
324 @RequestMapping(value = "fieldUnitDTOs", method = RequestMethod.GET)
325 public List<SpecimenOrObservationBaseDTO> doListFieldUnitDTOs(
326 @PathVariable("uuid") UUID uuid,
327 HttpServletRequest request,
328 HttpServletResponse response) {
329 logger.info("doListFieldUnitDTOs() - " + request.getRequestURI());
330
331 List<SpecimenOrObservationBaseDTO> rootUnitDtos = occurrenceService.listRootUnitDTOsByAssociatedTaxon(null, uuid, OccurrenceController.DERIVED_UNIT_INIT_STRATEGY);
332 return rootUnitDtos.stream().filter(dto -> dto instanceof FieldUnitDTO).collect(Collectors.toList());
333 }
334
335 @RequestMapping(value = "rootUnitDTOs", method = RequestMethod.GET)
336 public List<SpecimenOrObservationBaseDTO> doListRooUnitDTOs(
337 @PathVariable("uuid") UUID uuid,
338 HttpServletRequest request,
339 HttpServletResponse response) {
340 logger.info("rootUnitDTOs() - " + request.getRequestURI());
341
342 List<SpecimenOrObservationBaseDTO> rootUnitDtos = occurrenceService.listRootUnitDTOsByAssociatedTaxon(null, uuid, OccurrenceController.DERIVED_UNIT_INIT_STRATEGY);
343 // List<SpecimenOrObservationBase<?>> specimensOrObservations = occurrenceService.listByAssociatedTaxon(null, null, (Taxon)tb, null, null, null, orderHints, null);
344 return rootUnitDtos;
345 }
346
347 @RequestMapping(value = "specimensOrObservations", method = RequestMethod.GET)
348 public List<SpecimenOrObservationBase<?>> doListSpecimensOrObservations(
349 @PathVariable("uuid") UUID uuid,
350 HttpServletRequest request,
351 HttpServletResponse response) throws IOException {
352 logger.info("doListSpecimensOrObservations() - " + request.getRequestURI());
353
354 TaxonBase<?> tb = service.load(uuid);
355 List<OrderHint> orderHints = new ArrayList<>();
356 orderHints.add(new OrderHint("titleCache", SortOrder.DESCENDING));
357 if(tb instanceof Taxon){
358 List<SpecimenOrObservationBase<?>> specimensOrObservations = occurrenceService.listByAssociatedTaxon(null, null, (Taxon)tb, null, null, null, orderHints, null);
359 return specimensOrObservations;
360 } else {
361 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
362 return null;
363 }
364 }
365
366 @RequestMapping(value = "associatedRootUnits", method = RequestMethod.GET)
367 public Pager<SpecimenOrObservationBase> doGetAssociatedRootUnits(
368 @PathVariable("uuid") UUID uuid,
369 @RequestParam(value = "maxDepth", required = false) Integer maxDepth,
370 @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
371 @RequestParam(value = "pageSize", required = false) Integer pageSize,
372 HttpServletRequest request,
373 HttpServletResponse response) throws IOException {
374 logger.info("doGetAssociatedRootUnits() - " + request.getRequestURI());
375
376 TaxonBase<?> taxonBase = service.load(uuid);
377 taxonBase = checkExistsAndAccess(taxonBase, NO_UNPUBLISHED, response);
378
379 List<OrderHint> orderHints = new ArrayList<>();
380 orderHints.add(new OrderHint("titleCache", SortOrder.ASCENDING));
381
382 if(taxonBase instanceof Taxon){
383 PagerParameters pagerParams = new PagerParameters(pageSize, pageIndex);
384 pagerParams.normalizeAndValidate(response);
385
386 return occurrenceService.pageRootUnitsByAssociatedTaxon(null, null, (Taxon) taxonBase, maxDepth, pagerParams.getPageSize(), pagerParams.getPageIndex(), orderHints, null);
387 }else{
388 // FIXME proper HTTP code response
389 return null;
390 }
391 }
392
393 @RequestMapping(value = "taggedName", method = RequestMethod.GET)
394 public ModelAndView doGetTaggedName(
395 @PathVariable("uuid") UUID uuid,
396 HttpServletRequest request) {
397 logger.info("doGetDescriptionElementsByType() - " + request.getRequestURI());
398
399 ModelAndView mv = new ModelAndView();
400
401 TaxonBase<?> tb = service.load(uuid, NO_UNPUBLISHED, Arrays.asList(new String[] {"name"}));
402 mv.addObject(nameService.getTaggedName(tb.getName().getUuid()));
403 return mv;
404 }
405
406 /**
407 * This webservice endpoint returns all taxa which are congruent or included in the taxon represented by the given taxon uuid.
408 * The result also returns the path to these taxa represented by the uuids of the taxon relationships types and doubtful information.
409 * If classificationUuids is set only taxa of classifications are returned which are included in the given classifications.
410 * Also the path to these taxa may not include taxa from other classifications.
411 */
412 @RequestMapping(value = { "includedTaxa" }, method = { RequestMethod.GET })
413 public IncludedTaxaDTO doGetIncludedTaxa(
414 @PathVariable("uuid") UUID uuid,
415 @RequestParam(value="classificationFilter", required=false) final List<String> classificationStringList,
416 @RequestParam(value="includeDoubtful", required=false) final boolean includeDoubtful,
417 @RequestParam(value="onlyCongruent", required=false) final boolean onlyCongruent,
418 HttpServletResponse response,
419 HttpServletRequest request) {
420
421
422 if(request != null){
423 logger.info("doGetIncludedTaxa()" + requestPathAndQuery(request));
424 }
425
426 List<UUID> classificationFilter = null;
427 if( classificationStringList != null ){
428 classificationFilter = new ArrayList<>();
429 for(String classString :classificationStringList){
430 classificationFilter.add(UUID.fromString(classString));
431 }
432 }
433 IncludedTaxonConfiguration configuration =
434 new IncludedTaxonConfiguration(classificationFilter, includeDoubtful, onlyCongruent);
435 IncludedTaxaDTO listIncludedTaxa = service.listIncludedTaxa(uuid, configuration);
436 return listIncludedTaxa;
437 }
438
439 // TODO ================================================================================ //
440 // move all description and descriptionElement related methods into the according
441 // Description Controllers
442
443 /**
444 * Get the list of {@link TaxonDescription}s of the
445 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code>.
446 * <p>
447 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;descriptions</b>
448 *
449 * @param request
450 * @param response
451 * @return a List of {@link TaxonDescription} entities which are initialized
452 * using the following initialization strategy:
453 * {@link #TAXONDESCRIPTION_INIT_STRATEGY}
454 * @throws IOException
455 */
456 @RequestMapping(
457 value = {"descriptions"},
458 method = RequestMethod.GET)
459 public Pager<TaxonDescription> doGetDescriptions(
460 @PathVariable("uuid") UUID uuid,
461 @RequestParam(value = "markerTypes", required = false) List<MarkerType> markerTypes,
462 @RequestParam(value = "descriptionTypes", required = false) List<DescriptionType> descriptionTypes,
463 HttpServletRequest request,
464 HttpServletResponse response)throws IOException {
465
466 if(request != null){
467 logger.info("doGetDescriptions()" + requestPathAndQuery(request));
468 }
469
470 Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
471 taxon = checkExistsAndAccess(taxon, NO_UNPUBLISHED, response);
472
473 Set<MarkerType> markerTypesSet = new HashSet<>();
474 if (markerTypes != null) {
475 markerTypesSet.addAll(markerTypes);
476 }
477 Set<DescriptionType> descriptionTypesSet = new HashSet<>();
478 if (descriptionTypes != null) {
479 descriptionTypesSet.addAll(descriptionTypes);
480 }
481
482 List<String> taxonDescriptionInitStrategy = getTaxonDescriptionInitStrategy();
483 Pager<TaxonDescription> p = descriptionService.pageTaxonDescriptions(taxon, null, null, markerTypesSet, descriptionTypesSet, null, null, taxonDescriptionInitStrategy);
484
485 return p;
486 }
487
488 @RequestMapping(value = "descriptions/elementsByType/{classSimpleName}", method = RequestMethod.GET)
489 public ModelAndView doGetDescriptionElementsByType(
490 @PathVariable("uuid") UUID uuid,
491 @PathVariable("classSimpleName") String classSimpleName,
492 @RequestParam(value = "markerTypes", required = false) List<MarkerType> markerTypes,
493 @RequestParam(value = "descriptionTypes", required = false) List<DescriptionType> descriptionTypes,
494 @RequestParam(value = "count", required = false, defaultValue = "false") Boolean doCount,
495 HttpServletRequest request,
496 HttpServletResponse response) throws IOException {
497 logger.info("doGetDescriptionElementsByType() - " + requestPathAndQuery(request));
498
499
500 boolean includeUnpublished = NO_UNPUBLISHED;
501
502 ModelAndView mv = new ModelAndView();
503
504 List<DescriptionElementBase> allElements = new ArrayList<>();
505 List<DescriptionElementBase> elements;
506 int count = 0;
507
508 List<String> initStrategy = doCount ? null : getTaxonDescriptionElementInitStrategy();
509
510 Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
511
512 taxon = checkExistsAndAccess(taxon, includeUnpublished, response);
513
514
515 Set<MarkerType> markerTypesSet = new HashSet<>();
516 if (markerTypes != null) {
517 markerTypesSet.addAll(markerTypes);
518 }
519 Set<DescriptionType> descriptionTypesSet = new HashSet<>();
520 if (descriptionTypes != null) {
521 descriptionTypesSet.addAll(descriptionTypes);
522 }
523
524 List<TaxonDescription> taxonDescriptions = descriptionService.listTaxonDescriptions(
525 taxon, null, null, markerTypesSet, descriptionTypesSet, null, null, null);
526 try {
527 Class type;
528 type = Class.forName("eu.etaxonomy.cdm.model.description."
529 + classSimpleName);
530 if (taxonDescriptions != null) {
531 for (TaxonDescription description : taxonDescriptions) {
532 elements = descriptionService.listDescriptionElements(description, null, type, null, 0, initStrategy);
533 allElements.addAll(elements);
534 count += elements.size();
535 }
536
537 }
538 } catch (ClassNotFoundException e) {
539 HttpStatusMessage.create(e.getLocalizedMessage(), 400).send(response);
540 }
541 if(doCount){
542 mv.addObject(count);
543 } else {
544 mv.addObject(allElements);
545 }
546 return mv;
547 }
548
549 @RequestMapping(value = "taxonRelationshipsDTO", method = RequestMethod.GET)
550 public TaxonRelationshipsDTO doGetTaxonRelationshipsDTO(
551 @PathVariable("uuid") UUID taxonUuid,
552 @RequestParam(value = "directTypes", required = false) UuidList directTypeUuids,
553 @RequestParam(value = "inversTypes", required = false) UuidList inversTypeUuids,
554 @RequestParam(value = "direction", required = false) Direction direction,
555 @RequestParam(value="groupMisapplications", required=false, defaultValue="false") final boolean groupMisapplications,
556 HttpServletRequest request,
557 HttpServletResponse response) throws IOException {
558
559 boolean includeUnpublished = NO_UNPUBLISHED;
560
561 logger.info("doGetTaxonRelationshipDTOs(): " + request.getRequestURI());
562 TaxonBase<?> taxonBase = service.load(taxonUuid);
563 checkExistsAccessType(taxonBase, includeUnpublished, Taxon.class, response);
564
565 Set<TaxonRelationshipType> directTypes = getTermsByUuidSet(TaxonRelationshipType.class, directTypeUuids);
566 Set<TaxonRelationshipType> inversTypes = getTermsByUuidSet(TaxonRelationshipType.class, inversTypeUuids);
567
568 // Set<TaxonRelationshipType> inversTypes = null;
569 // if (directTypeUuids != null && !directTypeUuids.isEmpty()){
570 // types = new HashSet<>();
571 // List<TaxonRelationshipType> typeList = termService.find(TaxonRelationshipType.class, new HashSet<>(directTypeUuids));
572 // types.addAll(typeList);
573 // //TODO should we handle missing uuids as error response
574 //// HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
575 // }
576
577
578
579 // boolean deduplicateMisapplications = true;
580 Integer pageSize = null;
581 Integer pageNumber = null;
582 return service.listTaxonRelationships(taxonUuid, directTypes, inversTypes, direction, groupMisapplications,
583 includeUnpublished, pageSize, pageNumber);
584 }
585
586 /**
587 * @param directTypeUuids
588 * @return
589 */
590 protected <T extends DefinedTermBase<T>> Set<T> getTermsByUuidSet(Class<T> clazz, UuidList directTypeUuids) {
591 Set<T> directTypes = null;
592
593 if (directTypeUuids != null && !directTypeUuids.isEmpty()){
594 directTypes = new HashSet<>();
595 List<T> typeList = termService.find(clazz, new HashSet<>(directTypeUuids));
596 directTypes.addAll(typeList);
597 //TODO should we handle missing uuids as error response
598 // HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
599 }
600 return directTypes;
601 }
602
603 // TODO ================================================================================ //
604
605 }