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 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.HttpServletResponse;
|
20
|
|
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.ITermService;
|
31
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
32
|
import eu.etaxonomy.cdm.model.name.Rank;
|
33
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
34
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
35
|
import eu.etaxonomy.cdm.remote.editor.RankPropertyEditor;
|
36
|
|
37
|
/**
|
38
|
* @author a.kohlbecker
|
39
|
* @date 03.06.2010
|
40
|
*
|
41
|
*/
|
42
|
@Controller
|
43
|
@Api("classification")
|
44
|
@RequestMapping(value = {"/classification/{uuid}"})
|
45
|
public class ClassificationController extends BaseController<Classification,IClassificationService> {
|
46
|
|
47
|
|
48
|
private ITermService termService;
|
49
|
|
50
|
/* (non-Javadoc)
|
51
|
* @see eu.etaxonomy.cdm.remote.controller.BaseController#setService(eu.etaxonomy.cdm.api.service.IService)
|
52
|
*/
|
53
|
@Override
|
54
|
@Autowired
|
55
|
public void setService(IClassificationService service) {
|
56
|
this.service = service;
|
57
|
}
|
58
|
|
59
|
@Autowired
|
60
|
public void setTermService(ITermService termService) {
|
61
|
this.termService = termService;
|
62
|
}
|
63
|
|
64
|
|
65
|
@InitBinder
|
66
|
@Override
|
67
|
public void initBinder(WebDataBinder binder) {
|
68
|
super.initBinder(binder);
|
69
|
binder.registerCustomEditor(Rank.class, new RankPropertyEditor());
|
70
|
}
|
71
|
|
72
|
private List<String> NODE_INIT_STRATEGY(){
|
73
|
return Arrays.asList(new String[]{
|
74
|
"taxon.name"
|
75
|
});}
|
76
|
|
77
|
/**
|
78
|
* @param classificationUuid
|
79
|
* @param response
|
80
|
* @return
|
81
|
* @throws IOException
|
82
|
*/
|
83
|
@RequestMapping(
|
84
|
value = {"childNodes"},
|
85
|
method = RequestMethod.GET)
|
86
|
public List<TaxonNode> getChildNodes(
|
87
|
@PathVariable("uuid") UUID classificationUuid,
|
88
|
HttpServletResponse response
|
89
|
) throws IOException {
|
90
|
|
91
|
return getChildNodesAtRank(classificationUuid, null, response);
|
92
|
}
|
93
|
|
94
|
@RequestMapping(
|
95
|
value = {"childNodesAt/{rankUuid}"},
|
96
|
method = RequestMethod.GET)
|
97
|
public List<TaxonNode> getChildNodesAtRank(
|
98
|
@PathVariable("uuid") UUID classificationUuid,
|
99
|
@PathVariable("rankUuid") UUID rankUuid,
|
100
|
HttpServletResponse response
|
101
|
) throws IOException {
|
102
|
|
103
|
logger.info("getChildNodesAtRank()");
|
104
|
Classification tree = null;
|
105
|
Rank rank = null;
|
106
|
if(classificationUuid != null){ // FIXME this never should happen!!!!
|
107
|
// get view and rank
|
108
|
tree = service.find(classificationUuid);
|
109
|
|
110
|
if(tree == null) {
|
111
|
response.sendError(404 , "Classification not found using " + classificationUuid );
|
112
|
return null;
|
113
|
}
|
114
|
}
|
115
|
rank = findRank(rankUuid);
|
116
|
|
117
|
// long start = System.currentTimeMillis();
|
118
|
List<TaxonNode> rootNodes = service.listRankSpecificRootNodes(tree, rank, null, null, NODE_INIT_STRATEGY());
|
119
|
// System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
|
120
|
|
121
|
return rootNodes;
|
122
|
}
|
123
|
|
124
|
private Rank findRank(UUID rankUuid) {
|
125
|
Rank rank = null;
|
126
|
if(rankUuid != null){
|
127
|
DefinedTermBase definedTermBase = termService.find(rankUuid);
|
128
|
if(definedTermBase instanceof Rank){
|
129
|
rank = (Rank) definedTermBase;
|
130
|
} else {
|
131
|
new IllegalArgumentException("DefinedTermBase is not a Rank");
|
132
|
}
|
133
|
}
|
134
|
return rank;
|
135
|
}
|
136
|
}
|