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