a little bit documentation
[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.HashSet;
17 import java.util.List;
18 import java.util.Set;
19 import java.util.UUID;
20
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.servlet.ModelAndView;
31
32 import eu.etaxonomy.cdm.api.service.INameService;
33 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
34 import eu.etaxonomy.cdm.api.service.ITaxonService;
35 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
36 import eu.etaxonomy.cdm.model.taxon.Synonym;
37 import eu.etaxonomy.cdm.model.taxon.Taxon;
38 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
40 import eu.etaxonomy.cdm.persistence.query.OrderHint;
41 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
42
43 /**
44 * TODO write controller documentation
45 *
46 * @author a.kohlbecker
47 * @date 20.07.2009
48 *
49 */
50 @Controller
51 @RequestMapping(value = {"/taxon/{uuid}"})
52 public class TaxonController extends BaseController<TaxonBase, ITaxonService>
53 {
54 public static final Logger logger = Logger.getLogger(TaxonController.class);
55
56 @Autowired
57 private IOccurrenceService occurrenceService;
58 @Autowired
59 private INameService nameService;
60 @Autowired
61 private ITaxonService taxonService;
62
63
64 protected static final List<String> TAXONNODE_INIT_STRATEGY = Arrays.asList(new String []{
65 "taxonNodes"
66 });
67
68 public TaxonController(){
69 super();
70 setInitializationStrategy(Arrays.asList(new String[]{"$","name.nomenclaturalReference"}));
71 }
72
73
74 @Autowired
75 public void setService(ITaxonService service) {
76 this.service = service;
77 }
78
79
80 /**
81 * Get the set of accepted {@link Taxon} entities for a given
82 * {@link TaxonBase} entity identified by the <code>{taxon-uuid}</code>.
83 * <p>
84 * URI: <b>&#x002F;{datasource-name}&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;accepted</b>
85 *
86 * @param request
87 * @param response
88 * @return a set on a list of {@link Taxon} entities which are initialized
89 * using the following initialization strategy:
90 * {@link #DEFAULT_INIT_STRATEGY}
91 * @throws IOException
92 */
93 @RequestMapping(value = "accepted", method = RequestMethod.GET)
94 public Set<TaxonBase> doGetAccepted(
95 @PathVariable("uuid") UUID uuid,
96 HttpServletRequest request,
97 HttpServletResponse response) throws IOException {
98 logger.info("getAccepted() " + request.getServletPath());
99 TaxonBase tb = service.load(uuid);
100 HashSet<TaxonBase> resultset = new HashSet<TaxonBase>();
101 if(tb instanceof Taxon){
102 //the taxon already is accepted
103 //FIXME take the current view into account once views are implemented!!!
104 resultset.add((Taxon)tb);
105 } else {
106 Synonym syn = (Synonym)tb;
107 resultset.addAll(syn.getAcceptedTaxa());
108 //TODO init Synonyms
109 }
110 return resultset;
111 }
112
113 @RequestMapping(value = "taxonNodes", method = RequestMethod.GET)
114 public Set<TaxonNode> doGetTaxonNodes(
115 @PathVariable("uuid") UUID uuid,
116 HttpServletRequest request,
117 HttpServletResponse response) throws IOException {
118 TaxonBase tb = service.load(uuid, TAXONNODE_INIT_STRATEGY);
119 if(tb instanceof Taxon){
120 return ((Taxon)tb).getTaxonNodes();
121 } else {
122 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
123 return null;
124 }
125 }
126
127
128 @RequestMapping(value = "specimensOrObservations", method = RequestMethod.GET)
129 public ModelAndView doListSpecimensOrObservations(
130 @PathVariable("uuid") UUID uuid,
131 HttpServletRequest request,
132 HttpServletResponse response) throws IOException {
133 logger.info("doListSpecimensOrObservations() - " + request.getServletPath());
134
135 ModelAndView mv = new ModelAndView();
136
137 TaxonBase tb = service.load(uuid);
138
139 List<OrderHint> orderHints = new ArrayList<OrderHint>();
140 orderHints.add(new OrderHint("titleCache", SortOrder.DESCENDING));
141
142 if(tb instanceof Taxon){
143 List<SpecimenOrObservationBase> specimensOrObersvations = occurrenceService.listByAssociatedTaxon(null, null, (Taxon)tb, null, null, null, orderHints, null);
144 mv.addObject(specimensOrObersvations);
145 } else {
146 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
147 return null;
148 }
149
150 return mv;
151 }
152
153 @RequestMapping(value = "taggedName", method = RequestMethod.GET)
154 public ModelAndView doGetTaggedName(
155 @PathVariable("uuid") UUID uuid,
156 HttpServletRequest request,
157 HttpServletResponse response) throws IOException {
158 logger.info("doGetDescriptionElementsByType() - " + request.getServletPath());
159
160 ModelAndView mv = new ModelAndView();
161
162 TaxonBase tb = service.load(uuid, Arrays.asList(new String[] {"name"}));
163 mv.addObject(nameService.getTaggedName(tb.getName().getUuid()));
164 return mv;
165 }
166
167 /**
168 * FIXME change @RequestMapping to /taxon/findBestMatchingTaxon and also move into TaxonListController
169 */
170 @RequestMapping(value = "/bestMatchingTaxon/{taxonName}", method = RequestMethod.GET)
171 public TaxonBase doFindBestMatchingTaxon(
172 @PathVariable("taxonName") String taxonName,
173 HttpServletRequest request,
174 HttpServletResponse response)throws IOException {
175
176 Taxon bestMatchingTaxon = taxonService.findBestMatchingTaxon(taxonName);
177
178 return bestMatchingTaxon;
179
180
181 }
182
183 }