fixing controller name findByDescriptionElementFullText
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / ClassificationPortalListController.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT 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
7 * 1.1 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.Arrays;
14 import java.util.List;
15 import java.util.UUID;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
20 import org.apache.log4j.Logger;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.stereotype.Controller;
23 import org.springframework.web.bind.WebDataBinder;
24 import org.springframework.web.bind.annotation.InitBinder;
25 import org.springframework.web.bind.annotation.PathVariable;
26 import org.springframework.web.bind.annotation.RequestMapping;
27 import org.springframework.web.bind.annotation.RequestMethod;
28
29 import eu.etaxonomy.cdm.api.service.IClassificationService;
30 import eu.etaxonomy.cdm.api.service.ITaxonService;
31 import eu.etaxonomy.cdm.api.service.ITermService;
32 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
33 import eu.etaxonomy.cdm.model.name.Rank;
34 import eu.etaxonomy.cdm.model.taxon.Classification;
35 import eu.etaxonomy.cdm.model.taxon.Taxon;
36 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37 import eu.etaxonomy.cdm.remote.editor.RankPropertyEditor;
38
39 /**
40 * The ClassificationController class is a Spring MVC Controller.
41 * @author a.kohlbecker
42 * @date 20.03.2009
43 */
44 @Controller
45 public class ClassificationPortalListController extends IdentifiableListController<Classification,IClassificationService> {
46
47
48 private static final List<String> CLASSIFICATION_INIT_STRATEGY = Arrays.asList(new String[]{
49 "reference.authorTeam"
50 });
51
52 private static final List<String> NODE_INIT_STRATEGY = Arrays.asList(new String[]{
53 "taxon.sec",
54 "taxon.name"
55 });
56
57
58 public static final Logger logger = Logger.getLogger(ClassificationPortalListController.class);
59
60 private ITaxonService taxonService;
61
62 private IClassificationService service;
63
64 private ITermService termService;
65
66 @Autowired
67 public void setService(IClassificationService service) {
68 this.service = service;
69 }
70
71 @Autowired
72 public void setTermService(ITermService termService) {
73 this.termService = termService;
74 }
75
76 @Autowired
77 public void setTaxonService(ITaxonService taxonService) {
78 this.taxonService = taxonService;
79 }
80
81
82 @InitBinder
83 @Override
84 public void initBinder(WebDataBinder binder) {
85 super.initBinder(binder);
86 binder.registerCustomEditor(Rank.class, new RankPropertyEditor());
87 }
88
89
90 /**
91 * Lists all available {@link Classification}s.
92 * <p>
93 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;classification</b>
94 *
95 * @param request
96 * @param response
97 * @return a list of {@link Classification}s initialized by
98 * the {@link #CLASSIFICATION_INIT_STRATEGY}
99 * @throws IOException
100 */
101 @RequestMapping(value = { "/portal/classification" }, method = RequestMethod.GET)
102 public List<Classification> getClassifications(HttpServletRequest request, HttpServletResponse response)
103 throws IOException {
104 logger.info("getClassification() " + request.getServletPath());
105 return service.list(null, null, null,null, CLASSIFICATION_INIT_STRATEGY);
106 }
107
108
109 /**
110 * @param treeUuid
111 * @param response
112 * @return
113 * @throws IOException
114 */
115 @RequestMapping(
116 value = {"/portal/classification/{treeUuid}/childNodes"},
117 method = RequestMethod.GET)
118 public List<TaxonNode> getChildNodes(
119 @PathVariable("treeUuid") UUID treeUuid,
120 HttpServletRequest request,
121 HttpServletResponse response
122 ) throws IOException {
123
124 return getChildNodesAtRank(treeUuid, null, request, response);
125 }
126
127
128 @RequestMapping(
129 value = {"/portal/classification/{treeUuid}/childNodesAt/{rankUuid}"},
130 method = RequestMethod.GET)
131 public List<TaxonNode> getChildNodesAtRank(
132 @PathVariable("treeUuid") UUID treeUuid,
133 @PathVariable("rankUuid") UUID rankUuid,
134 HttpServletRequest request,
135 HttpServletResponse response
136 ) throws IOException {
137
138 logger.info("getChildNodesAtRank() " + request.getServletPath());
139 Classification tree = null;
140 Rank rank = null;
141 if(treeUuid != null){
142 // get view and rank
143 tree = service.find(treeUuid);
144
145 if(tree == null) {
146 response.sendError(404 , "Classification not found using " + treeUuid );
147 return null;
148 }
149 }
150 rank = findRank(rankUuid);
151
152 return service.loadRankSpecificRootNodes(tree, rank, NODE_INIT_STRATEGY);
153 }
154
155
156
157
158 /**
159 * Lists all child-{@link TaxonNode}s of the specified {@link Taxon} in the {@link Classification}. The
160 * a given {@link Rank} is ignored in this method but for consistency reasons it has been allowed to included it into the URI.
161 * <p>
162 * URI: <b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;childNodesOf&#x002F;{taxonUuid}</b>
163 * <p>
164 * <b>URI elements:</b>
165 * <ul>
166 * <li><b>{tree-uuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
167 * <li><b>{taxon-uuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
168 * </ul>
169 *
170 * @param response
171 * @param request
172 * @return a List of {@link TaxonNode} entities initialized by
173 * the {@link #NODE_INIT_STRATEGY}
174 */
175 @RequestMapping(
176 value = {"/portal/classification/{treeUuid}/childNodesOf/{taxonUuid}"},
177 method = RequestMethod.GET)
178 public List<TaxonNode> getChildNodesOfTaxon(
179 @PathVariable("treeUuid") UUID treeUuid,
180 @PathVariable("taxonUuid") UUID taxonUuid,
181 HttpServletRequest request,
182 HttpServletResponse response) throws IOException {
183 logger.info("getChildNodesOfTaxon() " + request.getServletPath());
184
185 Classification tree = service.find(treeUuid);
186 Taxon taxon = (Taxon) taxonService.load(taxonUuid);
187 List<TaxonNode> childs = service.loadChildNodesOfTaxon(taxon, tree, NODE_INIT_STRATEGY);
188 return childs;
189
190 }
191
192 /**
193 * Provides path of {@link TaxonNode}s from the base node to the node of the specified taxon.
194 * <p>
195 * URI:<b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;pathFrom&#x002F;{taxonUuid}&#x002F;toRank&#x002F;{rankUuid}</b>
196 * <p>
197 * <b>URI elements:</b>
198 * <ul>
199 * <li><b>{treeUuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
200 * <li><b>{taxonUuid}</b> identifies the {@link Rank}
201 * <li><b>{rankUuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
202 * </ul>
203 *
204 * @param response
205 * @param request
206 * @return a List of {@link TaxonNode} entities initialized by
207 * the {@link #NODE_INIT_STRATEGY}
208 */
209 @RequestMapping(
210 value = {"/portal/classification/{treeUuid}/pathFrom/{taxonUuid}/toRank/{rankUuid}"},
211 method = RequestMethod.GET)
212 public List<TaxonNode> getPathFromTaxonToRank(
213 @PathVariable("treeUuid") UUID treeUuid,
214 @PathVariable("taxonUuid") UUID taxonUuid,
215 @PathVariable("rankUuid") UUID rankUuid,
216 HttpServletRequest request,
217 HttpServletResponse response) throws IOException {
218 logger.info("getPathFromTaxonToRank() " + request.getServletPath());
219
220 Classification tree = service.find(treeUuid);
221 Rank rank = findRank(rankUuid);
222 Taxon taxon = (Taxon) taxonService.load(taxonUuid);
223
224 return service.loadTreeBranchToTaxon(taxon, tree, rank, NODE_INIT_STRATEGY);
225 }
226
227 /**
228 * Provides path of {@link TaxonNode}s from the base node to the node of the specified taxon.
229 * <p>
230 * URI:<b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;pathFrom&#x002F;{taxonUuid}</b>
231 * <p>
232 * <b>URI elements:</b>
233 * <ul>
234 * <li><b>{treeUuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
235 * <li><b>{rankUuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
236 * </ul>
237 *
238 * @param response
239 * @param request
240 * @return a List of {@link TaxonNode} entities initialized by
241 * the {@link #NODE_INIT_STRATEGY}
242 */
243 @RequestMapping(
244 value = {"/portal/classification/{treeUuid}/pathFrom/{taxonUuid}"},
245 method = RequestMethod.GET)
246 public List<TaxonNode> getPathFromTaxon(
247 @PathVariable("treeUuid") UUID treeUuid,
248 @PathVariable("taxonUuid") UUID taxonUuid,
249 HttpServletRequest request,
250 HttpServletResponse response) throws IOException {
251
252 return getPathFromTaxonToRank(treeUuid, taxonUuid, null, request, response);
253 }
254
255
256 private Rank findRank(UUID rankUuid) {
257 Rank rank = null;
258 if(rankUuid != null){
259 DefinedTermBase dt = termService.find(rankUuid);
260 if(dt instanceof Rank){
261 rank = (Rank)dt;
262 } else {
263 new IllegalArgumentException("DefinedTermBase is not a Rank");
264 }
265 }
266 return rank;
267 }
268
269
270 }