refactoring taxon controllers, reducing code duplication
[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 @RequestMapping(value="/portal/classification")
46 public class ClassificationPortalListController extends IdentifiableListController<Classification,IClassificationService> {
47
48
49 private static final List<String> CLASSIFICATION_INIT_STRATEGY = Arrays.asList(new String[]{
50 "reference.authorTeam"
51
52 });
53
54 private static final List<String> NODE_INIT_STRATEGY = Arrays.asList(new String[]{
55 "taxon.sec",
56 "taxon.name.rank"
57 });
58
59
60 public static final Logger logger = Logger.getLogger(ClassificationPortalListController.class);
61
62 private ITaxonService taxonService;
63
64 private ITermService termService;
65
66 public ClassificationPortalListController() {
67 setInitializationStrategy(CLASSIFICATION_INIT_STRATEGY);
68 }
69
70 @Override
71 @Autowired
72 public void setService(IClassificationService service) {
73 this.service = service;
74 }
75
76 @Autowired
77 public void setTermService(ITermService termService) {
78 this.termService = termService;
79 }
80
81 @Autowired
82 public void setTaxonService(ITaxonService taxonService) {
83 this.taxonService = taxonService;
84 }
85
86
87 @InitBinder
88 @Override
89 public void initBinder(WebDataBinder binder) {
90 super.initBinder(binder);
91 binder.registerCustomEditor(Rank.class, new RankPropertyEditor());
92 }
93
94
95 /**
96 * @param treeUuid
97 * @param response
98 * @return
99 * @throws IOException
100 */
101 @RequestMapping(
102 value = {"{treeUuid}/childNodes"},
103 method = RequestMethod.GET)
104 public List<TaxonNode> getChildNodes(
105 @PathVariable("treeUuid") UUID treeUuid,
106 HttpServletRequest request,
107 HttpServletResponse response
108 ) throws IOException {
109
110 return getChildNodesAtRank(treeUuid, null, request, response);
111 }
112
113
114 @RequestMapping(
115 value = {"{treeUuid}/childNodesAt/{rankUuid}"},
116 method = RequestMethod.GET)
117 public List<TaxonNode> getChildNodesAtRank(
118 @PathVariable("treeUuid") UUID treeUuid,
119 @PathVariable("rankUuid") UUID rankUuid,
120 HttpServletRequest request,
121 HttpServletResponse response
122 ) throws IOException {
123
124 logger.info("getChildNodesAtRank() " + request.getRequestURI());
125 Classification tree = null;
126 Rank rank = null;
127 if(treeUuid != null){
128 // get view and rank
129 tree = service.find(treeUuid);
130
131 if(tree == null) {
132 response.sendError(404 , "Classification not found using " + treeUuid );
133 return null;
134 }
135 }
136 rank = findRank(rankUuid);
137
138 return service.listRankSpecificRootNodes(tree, rank, null, null, NODE_INIT_STRATEGY);
139 }
140
141
142
143
144 /**
145 * Lists all child-{@link TaxonNode}s of the specified {@link Taxon} in the {@link Classification}. The
146 * a given {@link Rank} is ignored in this method but for consistency reasons it has been allowed to included it into the URI.
147 * <p>
148 * URI: <b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;childNodesOf&#x002F;{taxonUuid}</b>
149 * <p>
150 * <b>URI elements:</b>
151 * <ul>
152 * <li><b>{tree-uuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
153 * <li><b>{taxon-uuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
154 * </ul>
155 *
156 * @param response
157 * @param request
158 * @return a List of {@link TaxonNode} entities initialized by
159 * the {@link #NODE_INIT_STRATEGY}
160 */
161 @RequestMapping(
162 value = {"{treeUuid}/childNodesOf/{taxonUuid}"},
163 method = RequestMethod.GET)
164 public List<TaxonNode> getChildNodesOfTaxon(
165 @PathVariable("treeUuid") UUID treeUuid,
166 @PathVariable("taxonUuid") UUID taxonUuid,
167 HttpServletRequest request,
168 HttpServletResponse response) throws IOException {
169 logger.info("getChildNodesOfTaxon() " + request.getRequestURI());
170
171
172 List<TaxonNode> childs = service.listChildNodesOfTaxon(taxonUuid, treeUuid, null, null, NODE_INIT_STRATEGY);
173 return childs;
174
175 }
176
177 /**
178 * Provides path of {@link TaxonNode}s from the base node to the node of the specified taxon.
179 * <p>
180 * URI:<b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;pathFrom&#x002F;{taxonUuid}&#x002F;toRank&#x002F;{rankUuid}</b>
181 * <p>
182 * <b>URI elements:</b>
183 * <ul>
184 * <li><b>{treeUuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
185 * <li><b>{taxonUuid}</b> identifies the {@link Rank}
186 * <li><b>{rankUuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
187 * </ul>
188 *
189 * @param response
190 * @param request
191 * @return a List of {@link TaxonNode} entities initialized by
192 * the {@link #NODE_INIT_STRATEGY}
193 */
194 @RequestMapping(
195 value = {"{treeUuid}/pathFrom/{taxonUuid}/toRank/{rankUuid}"},
196 method = RequestMethod.GET)
197 public List<TaxonNode> getPathFromTaxonToRank(
198 @PathVariable("treeUuid") UUID treeUuid,
199 @PathVariable("taxonUuid") UUID taxonUuid,
200 @PathVariable("rankUuid") UUID rankUuid,
201 HttpServletRequest request,
202 HttpServletResponse response) throws IOException {
203 logger.info("getPathFromTaxonToRank() " + request.getRequestURI());
204
205 Classification tree = service.find(treeUuid);
206 Rank rank = findRank(rankUuid);
207 Taxon taxon = (Taxon) taxonService.load(taxonUuid);
208
209 return service.loadTreeBranchToTaxon(taxon, tree, rank, NODE_INIT_STRATEGY);
210 }
211
212 /**
213 * Provides path of {@link TaxonNode}s from the base node to the node of the specified taxon.
214 * <p>
215 * URI:<b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;pathFrom&#x002F;{taxonUuid}</b>
216 * <p>
217 * <b>URI elements:</b>
218 * <ul>
219 * <li><b>{treeUuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
220 * <li><b>{rankUuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
221 * </ul>
222 *
223 * @param response
224 * @param request
225 * @return a List of {@link TaxonNode} entities initialized by
226 * the {@link #NODE_INIT_STRATEGY}
227 */
228 @RequestMapping(
229 value = {"{treeUuid}/pathFrom/{taxonUuid}"},
230 method = RequestMethod.GET)
231 public List<TaxonNode> getPathFromTaxon(
232 @PathVariable("treeUuid") UUID treeUuid,
233 @PathVariable("taxonUuid") UUID taxonUuid,
234 HttpServletRequest request,
235 HttpServletResponse response) throws IOException {
236
237 return getPathFromTaxonToRank(treeUuid, taxonUuid, null, request, response);
238 }
239
240
241 private Rank findRank(UUID rankUuid) {
242 Rank rank = null;
243 if(rankUuid != null){
244 DefinedTermBase dt = termService.find(rankUuid);
245 if(dt instanceof Rank){
246 rank = (Rank)dt;
247 } else {
248 new IllegalArgumentException("DefinedTermBase is not a Rank");
249 }
250 }
251 return rank;
252 }
253
254
255 }