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