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