cleanup
[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 java.io.IOException;
12 import java.util.ArrayList;
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.exception.UnpublishedException;
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 import io.swagger.annotations.Api;
40
41 /**
42 * The ClassificationController class is a Spring MVC Controller.
43 * @author a.kohlbecker
44 * @since 20.03.2009
45 */
46 @Controller
47 @Api("portal_classification")
48 @RequestMapping(value="/portal/classification")
49 public class ClassificationPortalListController extends AbstractIdentifiableListController<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 boolean includeUnpublished = NO_UNPUBLISHED;
154 // long start = System.currentTimeMillis();
155 List<TaxonNode> rootNodes = service.listRankSpecificRootNodes(tree, rank, includeUnpublished, null, null, NODE_INIT_STRATEGY);
156 // System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
157 return rootNodes;
158 }
159
160
161 /**
162 * Lists all child-{@link TaxonNode}s of the specified {@link Taxon} in the {@link Classification}. The
163 * a given {@link Rank} is ignored in this method but for consistency reasons it has been allowed to included it into the URI.
164 * <p>
165 * URI: <b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;childNodesOf&#x002F;{taxonUuid}</b>
166 * <p>
167 * <b>URI elements:</b>
168 * <ul>
169 * <li><b>{tree-uuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
170 * <li><b>{taxon-uuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
171 * </ul>
172 *
173 * @param response
174 * @param request
175 * @return a List of {@link TaxonNode} entities initialized by
176 * the {@link #NODE_INIT_STRATEGY}
177 */
178 @RequestMapping(
179 value = {"{treeUuid}/childNodesOf/{taxonUuid}"},
180 method = RequestMethod.GET)
181 public List<TaxonNode> getChildNodesOfTaxon(
182 @PathVariable("treeUuid") UUID treeUuid,
183 @PathVariable("taxonUuid") UUID taxonUuid,
184 HttpServletRequest request,
185 HttpServletResponse response) throws IOException {
186 logger.info("getChildNodesOfTaxon() " + request.getRequestURI());
187
188 boolean includeUnpublished = NO_UNPUBLISHED; //for now we do not allow any remote service to publish unpublished data
189
190 List<TaxonNode> children = service.listChildNodesOfTaxon(taxonUuid, treeUuid,
191 includeUnpublished, null, null, NODE_INIT_STRATEGY);
192 return children;
193
194 }
195
196 @RequestMapping(
197 value = {"{treeUuid}/siblingsOf/{taxonUuid}"},
198 method = RequestMethod.GET)
199 public List<TaxonNode> getSiblingsOfTaxon(
200 @PathVariable("treeUuid") UUID treeUuid,
201 @PathVariable("taxonUuid") UUID taxonUuid,
202 HttpServletRequest request,
203 HttpServletResponse response) throws IOException {
204 logger.info("getSiblingsOfTaxon() " + request.getRequestURI());
205
206 boolean includeUnpublished = NO_UNPUBLISHED;
207 //FIXME return pager
208 List<TaxonNode> childs = service.listSiblingsOfTaxon(taxonUuid, treeUuid, includeUnpublished, null, null, NODE_INIT_STRATEGY);
209 return childs;
210
211 }
212
213 /**
214 * Provides path of {@link TaxonNode}s from the base node to the node of the specified taxon.
215 * <p>
216 * URI:<b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;pathFrom&#x002F;{taxonUuid}&#x002F;toRank&#x002F;{rankUuid}</b>
217 * <p>
218 * <b>URI elements:</b>
219 * <ul>
220 * <li><b>{treeUuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
221 * <li><b>{taxonUuid}</b> identifies the {@link Rank}
222 * <li><b>{rankUuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
223 * </ul>
224 *
225 * @param response
226 * @param request
227 * @return a List of {@link TaxonNode} entities initialized by
228 * the {@link #NODE_INIT_STRATEGY}
229 */
230 @RequestMapping(
231 value = {"{treeUuid}/pathFrom/{taxonUuid}/toRank/{rankUuid}"},
232 method = RequestMethod.GET)
233 public List<TaxonNode> getPathFromTaxonToRank(
234 @PathVariable("treeUuid") UUID treeUuid,
235 @PathVariable("taxonUuid") UUID taxonUuid,
236 @PathVariable("rankUuid") UUID rankUuid,
237 HttpServletRequest request,
238 HttpServletResponse response) {
239 logger.info("getPathFromTaxonToRank() " + request.getRequestURI());
240
241 boolean includeUnpublished = NO_UNPUBLISHED;
242
243 Classification tree = service.find(treeUuid);
244 Rank rank = findRank(rankUuid);
245 Taxon taxon = (Taxon) taxonService.load(taxonUuid);
246
247 try {
248 return service.loadTreeBranchToTaxon(taxon, tree, rank, includeUnpublished, NODE_INIT_STRATEGY);
249 } catch (UnpublishedException e) {
250 return new ArrayList<>();
251 }
252 }
253
254 /**
255 * Provides path of {@link TaxonNode}s from the base node to the node of the specified taxon.
256 * <p>
257 * URI:<b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;pathFrom&#x002F;{taxonUuid}</b>
258 * <p>
259 * <b>URI elements:</b>
260 * <ul>
261 * <li><b>{treeUuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
262 * <li><b>{rankUuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
263 * </ul>
264 *
265 * @param response
266 * @param request
267 * @return a List of {@link TaxonNode} entities initialized by
268 * the {@link #NODE_INIT_STRATEGY}
269 */
270 @RequestMapping(
271 value = {"{treeUuid}/pathFrom/{taxonUuid}"},
272 method = RequestMethod.GET)
273 public List<TaxonNode> getPathFromTaxon(
274 @PathVariable("treeUuid") UUID treeUuid,
275 @PathVariable("taxonUuid") UUID taxonUuid,
276 HttpServletRequest request,
277 HttpServletResponse response) {
278
279 return getPathFromTaxonToRank(treeUuid, taxonUuid, null, request, response);
280 }
281
282
283 private Rank findRank(UUID rankUuid) {
284 Rank rank = null;
285 if(rankUuid != null){
286 DefinedTermBase<?> dt = termService.find(rankUuid);
287 if(dt instanceof Rank){
288 rank = (Rank)dt;
289 } else {
290 new IllegalArgumentException("DefinedTermBase is not a Rank");
291 }
292 }
293 return rank;
294 }
295
296
297 }