refactoring taxon controllers, reducing code duplication
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / ClassificationController.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
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.HttpServletResponse;
18
19 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.stereotype.Controller;
21 import org.springframework.web.bind.WebDataBinder;
22 import org.springframework.web.bind.annotation.InitBinder;
23 import org.springframework.web.bind.annotation.PathVariable;
24 import org.springframework.web.bind.annotation.RequestMapping;
25 import org.springframework.web.bind.annotation.RequestMethod;
26
27 import eu.etaxonomy.cdm.api.service.IClassificationService;
28 import eu.etaxonomy.cdm.api.service.ITermService;
29 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
30 import eu.etaxonomy.cdm.model.name.Rank;
31 import eu.etaxonomy.cdm.model.taxon.Classification;
32 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
33 import eu.etaxonomy.cdm.remote.editor.RankPropertyEditor;
34
35 /**
36 * @author a.kohlbecker
37 * @date 03.06.2010
38 *
39 */
40 @Controller
41 @RequestMapping(value = {"/classification/{uuid}"})
42 public class ClassificationController extends BaseController<Classification,IClassificationService> {
43
44
45 private ITermService termService;
46
47 /* (non-Javadoc)
48 * @see eu.etaxonomy.cdm.remote.controller.BaseController#setService(eu.etaxonomy.cdm.api.service.IService)
49 */
50 @Override
51 @Autowired
52 public void setService(IClassificationService service) {
53 this.service = service;
54 }
55
56 @Autowired
57 public void setTermService(ITermService termService) {
58 this.termService = termService;
59 }
60
61
62 @InitBinder
63 @Override
64 public void initBinder(WebDataBinder binder) {
65 super.initBinder(binder);
66 binder.registerCustomEditor(Rank.class, new RankPropertyEditor());
67 }
68
69 private List<String> NODE_INIT_STRATEGY(){
70 return Arrays.asList(new String[]{
71 "taxon.sec",
72 "taxon.name",
73 "classification"
74 });}
75
76 /**
77 * @param classificationUuid
78 * @param response
79 * @return
80 * @throws IOException
81 */
82 @RequestMapping(
83 value = {"childNodes"},
84 method = RequestMethod.GET)
85 public List<TaxonNode> getChildNodes(
86 @PathVariable("uuid") UUID classificationUuid,
87 HttpServletResponse response
88 ) throws IOException {
89
90 return getChildNodesAtRank(classificationUuid, null, response);
91 }
92
93 @RequestMapping(
94 value = {"childNodesAt/{rankUuid}"},
95 method = RequestMethod.GET)
96 public List<TaxonNode> getChildNodesAtRank(
97 @PathVariable("uuid") UUID classificationUuid,
98 @PathVariable("rankUuid") UUID rankUuid,
99 HttpServletResponse response
100 ) throws IOException {
101
102 logger.info("getChildNodesAtRank()");
103 Classification tree = null;
104 Rank rank = null;
105 if(classificationUuid != null){
106 // get view and rank
107 tree = service.find(classificationUuid);
108
109 if(tree == null) {
110 response.sendError(404 , "Classification not found using " + classificationUuid );
111 return null;
112 }
113 }
114 rank = findRank(rankUuid);
115
116 return service.listRankSpecificRootNodes(tree, rank, null, null, NODE_INIT_STRATEGY());
117 }
118
119 private Rank findRank(UUID rankUuid) {
120 Rank rank = null;
121 if(rankUuid != null){
122 DefinedTermBase definedTermBase = termService.find(rankUuid);
123 if(definedTermBase instanceof Rank){
124 rank = (Rank) definedTermBase;
125 } else {
126 new IllegalArgumentException("DefinedTermBase is not a Rank");
127 }
128 }
129 return rank;
130 }
131 }