ref #6066 rename childNodes to childNodesByTaxon in ClassificationController
[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.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
20 import org.springframework.beans.factory.annotation.Autowired;
21 import org.springframework.stereotype.Controller;
22 import org.springframework.web.bind.WebDataBinder;
23 import org.springframework.web.bind.annotation.InitBinder;
24 import org.springframework.web.bind.annotation.PathVariable;
25 import org.springframework.web.bind.annotation.RequestMapping;
26 import org.springframework.web.bind.annotation.RequestMethod;
27 import org.springframework.web.bind.annotation.RequestParam;
28
29 import eu.etaxonomy.cdm.api.service.IClassificationService;
30 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
31 import eu.etaxonomy.cdm.api.service.ITermService;
32 import eu.etaxonomy.cdm.api.service.NodeSortMode;
33 import eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO;
34 import eu.etaxonomy.cdm.api.service.pager.Pager;
35 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
36 import eu.etaxonomy.cdm.model.name.Rank;
37 import eu.etaxonomy.cdm.model.taxon.Classification;
38 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
39 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
40 import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
41 import eu.etaxonomy.cdm.remote.editor.RankPropertyEditor;
42 import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
43 import eu.etaxonomy.cdm.remote.editor.UuidList;
44 import io.swagger.annotations.Api;
45
46 /**
47 * @author a.kohlbecker
48 * @date 03.06.2010
49 *
50 */
51 @Controller
52 @Api("classification")
53 @RequestMapping(value = {"/classification/{uuid}"})
54 public class ClassificationController extends BaseController<Classification,IClassificationService> {
55
56
57 private ITermService termService;
58 private ITaxonNodeService taxonNodeService;
59
60 @Override
61 @Autowired
62 public void setService(IClassificationService service) {
63 this.service = service;
64 }
65
66 @Autowired
67 public void setTermService(ITermService termService) {
68 this.termService = termService;
69 }
70
71 @Autowired
72 public void setTaxonNodeService(ITaxonNodeService taxonNodeService) {
73 this.taxonNodeService = taxonNodeService;
74 }
75
76
77 @InitBinder
78 @Override
79 public void initBinder(WebDataBinder binder) {
80 super.initBinder(binder);
81 binder.registerCustomEditor(Rank.class, new RankPropertyEditor());
82 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
83 }
84
85 private List<String> NODE_INIT_STRATEGY(){
86 return Arrays.asList(new String[]{
87 "taxon.name"
88 });}
89
90 /**
91 * @param classificationUuid
92 * @param response
93 * @return
94 * @throws IOException
95 *
96 */
97 @RequestMapping(
98 value = {"childNodes"},
99 method = RequestMethod.GET)
100 public List<TaxonNode> getChildNodes(
101 @PathVariable("uuid") UUID classificationUuid,
102 HttpServletRequest request,
103 HttpServletResponse response
104 ) throws IOException {
105
106 return getChildNodesAtRank(classificationUuid, null, request, response);
107 }
108
109 @RequestMapping(
110 value = {"childNodesAt/{rankUuid}"},
111 method = RequestMethod.GET)
112 public List<TaxonNode> getChildNodesAtRank(
113 @PathVariable("uuid") UUID classificationUuid,
114 @PathVariable("rankUuid") UUID rankUuid,
115 HttpServletRequest request,
116 HttpServletResponse response
117 ) throws IOException {
118
119 logger.info("getChildNodesAtRank() - " + request.getRequestURI());
120
121 Classification classification = service.find(classificationUuid);
122
123 if(classification == null) {
124 response.sendError(404 , "Classification not found using " + classificationUuid );
125 return null;
126 }
127 Rank rank = findRank(rankUuid);
128
129 // long start = System.currentTimeMillis();
130 List<TaxonNode> rootNodes = service.listRankSpecificRootNodes(classification, rank, null, null, NODE_INIT_STRATEGY());
131 // System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
132
133 return rootNodes;
134 }
135
136 /**
137 *
138 * @param uuid
139 * @param pageIndex
140 * @param pageSize
141 * @param sortMode
142 * @param response
143 * @return
144 * @throws IOException
145 */
146 @RequestMapping(
147 value = {"childNodesByTaxon"},
148 method = RequestMethod.GET)
149 public Pager<TaxonNodeDto> doPageChildNodes(
150 @PathVariable("uuid") UUID classificationUuid,
151 @PathVariable("taxonUuid") UUID taxonUuid,
152 @RequestParam(value = "pageNumber", required = false) Integer pageIndex,
153 @RequestParam(value = "pageSize", required = false) Integer pageSize,
154 @RequestParam(value = "sortMode", defaultValue="AlphabeticalOrder") NodeSortMode sortMode,
155 @RequestParam(value = "doSynonyms", defaultValue="false") Boolean doSynonyms,
156 HttpServletResponse response
157 ) throws IOException {
158
159 PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex);
160 pagerParameters.normalizeAndValidate(response);
161
162 // service.startTransaction();
163 boolean recursive = false;
164 UUID taxonNodeUuid = service.getTaxonNodeUuidByTaxonUuid(classificationUuid, taxonUuid) ;
165 Pager<TaxonNodeDto> pager = taxonNodeService.pageChildNodesDTOs(taxonNodeUuid, recursive, doSynonyms, sortMode, pageSize, pageIndex);
166 // service.commitTransaction()
167
168 return pager;
169 }
170
171
172 /**
173 * @param classificationUuid
174 * @param response
175 * @return
176 * @throws IOException
177 */
178 @RequestMapping(
179 value = {"groupedTaxa"},
180 method = RequestMethod.GET)
181 public List<GroupedTaxonDTO> getGroupedTaxaByHigherTaxon(
182 @PathVariable("uuid") UUID classificationUuid,
183 @RequestParam(value = "taxonUuids", required = true) UuidList taxonUuids,
184 @RequestParam(value = "minRankUuid", required = false) UUID minRankUuid,
185 @RequestParam(value = "maxRankUuid", required = false) UUID maxRankUuid,
186 HttpServletRequest request,
187 HttpServletResponse response
188 ) throws IOException {
189
190 logger.info("getGroupedTaxaByHigherTaxon() - " + request.getRequestURI());
191
192 Classification classification = service.find(classificationUuid);
193 if(classification == null) {
194 response.sendError(404 , "Classification not found using " + classificationUuid );
195 return null;
196 }
197
198
199 Rank minRank = findRank(minRankUuid);
200 Rank maxRank = findRank(maxRankUuid);
201
202 // long start = System.currentTimeMillis();
203 List<GroupedTaxonDTO> result = service.groupTaxaByHigherTaxon(taxonUuids, classificationUuid, minRank, maxRank);
204 // System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
205
206 return result;
207 }
208
209
210 private Rank findRank(UUID rankUuid) {
211 Rank rank = null;
212 if(rankUuid != null){
213 DefinedTermBase<?> definedTermBase = termService.find(rankUuid);
214 if(definedTermBase instanceof Rank){
215 rank = (Rank) definedTermBase;
216 } else {
217 throw new IllegalArgumentException("DefinedTermBase is not a Rank");
218 }
219 }
220 return rank;
221 }
222 }