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.Arrays;
|
13
|
import java.util.List;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import javax.servlet.http.HttpServletRequest;
|
17
|
import javax.servlet.http.HttpServletResponse;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
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.ITaxonService;
|
32
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
33
|
import eu.etaxonomy.cdm.api.service.TaxonNodeDtoSortMode;
|
34
|
import eu.etaxonomy.cdm.exception.FilterException;
|
35
|
import eu.etaxonomy.cdm.exception.UnpublishedException;
|
36
|
import eu.etaxonomy.cdm.model.name.Rank;
|
37
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
38
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
39
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
40
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
41
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
|
42
|
import eu.etaxonomy.cdm.remote.editor.RankPropertyEditor;
|
43
|
import io.swagger.annotations.Api;
|
44
|
|
45
|
/**
|
46
|
* The ClassificationController class is a Spring MVC Controller.
|
47
|
* @author a.kohlbecker
|
48
|
* @since 20.03.2009
|
49
|
*/
|
50
|
@Controller
|
51
|
@Api("portal_classification")
|
52
|
@RequestMapping(value="/portal/classification")
|
53
|
public class ClassificationPortalListController extends AbstractIdentifiableListController<Classification,IClassificationService> {
|
54
|
|
55
|
public static final Logger logger = Logger.getLogger(ClassificationPortalListController.class);
|
56
|
|
57
|
private static final List<String> CLASSIFICATION_INIT_STRATEGY = Arrays.asList(new String[]{
|
58
|
"reference.authorship"
|
59
|
});
|
60
|
|
61
|
private static final List<String> NODE_INIT_STRATEGY = Arrays.asList(new String[]{
|
62
|
"taxon.name.rank",
|
63
|
"taxon.sec"
|
64
|
});
|
65
|
|
66
|
private ITaxonService taxonService;
|
67
|
private ITaxonNodeService taxonNodeService;
|
68
|
|
69
|
private ITermService termService;
|
70
|
|
71
|
public ClassificationPortalListController() {
|
72
|
setInitializationStrategy(CLASSIFICATION_INIT_STRATEGY);
|
73
|
}
|
74
|
|
75
|
@Override
|
76
|
@Autowired
|
77
|
public void setService(IClassificationService service) {
|
78
|
this.service = service;
|
79
|
}
|
80
|
|
81
|
@Autowired
|
82
|
public void setTermService(ITermService termService) {
|
83
|
this.termService = termService;
|
84
|
}
|
85
|
|
86
|
@Autowired
|
87
|
public void setTaxonService(ITaxonService taxonService) {
|
88
|
this.taxonService = taxonService;
|
89
|
}
|
90
|
|
91
|
@Autowired
|
92
|
public void setTaxonNodeService(ITaxonNodeService taxonNodeService) {
|
93
|
this.taxonNodeService = taxonNodeService;
|
94
|
}
|
95
|
|
96
|
|
97
|
@InitBinder
|
98
|
@Override
|
99
|
public void initBinder(WebDataBinder binder) {
|
100
|
super.initBinder(binder);
|
101
|
binder.registerCustomEditor(Rank.class, new RankPropertyEditor());
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Lists all child-{@link TaxonNode}s of the specified {@link Taxon} in the {@link Classification}. The
|
106
|
* a given {@link Rank} is ignored in this method but for consistency reasons it has been allowed to included it into the URI.
|
107
|
* <p>
|
108
|
* URI: <b>/portal/classification/{treeUuid}/childNodesOf/{taxonUuid}</b>
|
109
|
* <p>
|
110
|
* <b>URI elements:</b>
|
111
|
* <ul>
|
112
|
* <li><b>{tree-uuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
|
113
|
* <li><b>{taxon-uuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
|
114
|
* </ul>
|
115
|
*
|
116
|
* @param response
|
117
|
* @param request
|
118
|
* @return a List of {@link TaxonNode} entities initialized by
|
119
|
* the {@link #NODE_INIT_STRATEGY}
|
120
|
*/
|
121
|
@RequestMapping(
|
122
|
value = {"{treeUuid}/childNodesOf/{taxonUuid}"},
|
123
|
method = RequestMethod.GET)
|
124
|
public List<TaxonNodeDto> getChildNodesOfTaxon(
|
125
|
@PathVariable("treeUuid") UUID treeUuid,
|
126
|
@PathVariable("taxonUuid") UUID taxonUuid,
|
127
|
@RequestParam(value = "subtree", required = false) UUID subtreeUuid,
|
128
|
@RequestParam(value = "sortMode", required = false, defaultValue = "AlphabeticalOrder") TaxonNodeDtoSortMode sortMode,
|
129
|
HttpServletRequest request,
|
130
|
HttpServletResponse response) throws IOException {
|
131
|
logger.info("getChildNodesOfTaxon() " + request.getRequestURI());
|
132
|
|
133
|
boolean includeUnpublished = NO_UNPUBLISHED; //for now we do not allow any remote service to publish unpublished data
|
134
|
|
135
|
List<TaxonNodeDto> children;
|
136
|
try {
|
137
|
children = service.listChildNodeDtosOfTaxon(taxonUuid, treeUuid, subtreeUuid,
|
138
|
includeUnpublished, null, null, sortMode, NODE_INIT_STRATEGY);
|
139
|
} catch (FilterException e) {
|
140
|
HttpStatusMessage.SUBTREE_FILTER_INVALID.send(response);
|
141
|
return null;
|
142
|
}
|
143
|
return children;
|
144
|
|
145
|
}
|
146
|
|
147
|
@RequestMapping(
|
148
|
value = {"{treeUuid}/siblingsOf/{taxonUuid}"},
|
149
|
method = RequestMethod.GET)
|
150
|
public List<TaxonNode> getSiblingsOfTaxon(
|
151
|
@PathVariable("treeUuid") UUID classificationUuid,
|
152
|
@PathVariable("taxonUuid") UUID taxonUuid,
|
153
|
HttpServletRequest request,
|
154
|
@SuppressWarnings("unused") HttpServletResponse response) {
|
155
|
logger.info("getSiblingsOfTaxon() " + request.getRequestURI());
|
156
|
|
157
|
boolean includeUnpublished = NO_UNPUBLISHED;
|
158
|
//FIXME return pager
|
159
|
List<TaxonNode> childs = service.listSiblingsOfTaxon(taxonUuid, classificationUuid, includeUnpublished, null, null, NODE_INIT_STRATEGY);
|
160
|
return childs;
|
161
|
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Provides path of {@link TaxonNode}s from the base node to the node of the specified taxon.
|
166
|
* <p>
|
167
|
* URI:<b>/portal/classification/{treeUuid}/pathFrom/{taxonUuid}/toRank/{rankUuid}</b>
|
168
|
* <p>
|
169
|
* <b>URI elements:</b>
|
170
|
* <ul>
|
171
|
* <li><b>{treeUuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
|
172
|
* <li><b>{taxonUuid}</b> identifies the {@link Rank}
|
173
|
* <li><b>{rankUuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
|
174
|
* </ul>
|
175
|
*
|
176
|
* @param response
|
177
|
* @param request
|
178
|
* @return a List of {@link TaxonNode} entities initialized by
|
179
|
* the {@link #NODE_INIT_STRATEGY}
|
180
|
* @throws IOException
|
181
|
*/
|
182
|
@RequestMapping(
|
183
|
value = {"{treeUuid}/pathFrom/{taxonUuid}/toRank/{rankUuid}"},
|
184
|
method = RequestMethod.GET)
|
185
|
public List<TaxonNodeDto> getPathFromTaxonToRank(
|
186
|
@PathVariable("treeUuid") UUID classificationUuid,
|
187
|
@PathVariable("taxonUuid") UUID taxonUuid,
|
188
|
@PathVariable("rankUuid") UUID rankUuid,
|
189
|
@RequestParam(value = "subtree", required = false) UUID subtreeUuid,
|
190
|
HttpServletRequest request,
|
191
|
HttpServletResponse response) throws IOException {
|
192
|
logger.info("getPathFromTaxonToRank() " + request.getRequestURI());
|
193
|
|
194
|
boolean includeUnpublished = NO_UNPUBLISHED;
|
195
|
|
196
|
Classification classification = service.find(classificationUuid);
|
197
|
TaxonNode subtree = getSubtreeOrError(subtreeUuid, taxonNodeService, response);
|
198
|
Rank rank = findRank(rankUuid);
|
199
|
Taxon taxon = (Taxon) taxonService.load(taxonUuid);
|
200
|
if(classification == null){
|
201
|
HttpStatusMessage.UUID_INVALID.send(response, "Classification uuid does not exist.");
|
202
|
return null;
|
203
|
}
|
204
|
try {
|
205
|
List<TaxonNodeDto> result = service.loadTreeBranchDTOsToTaxon(taxon, classification, subtree, rank, includeUnpublished, NODE_INIT_STRATEGY);
|
206
|
return result;
|
207
|
} catch (UnpublishedException e) {
|
208
|
HttpStatusMessage.ACCESS_DENIED.send(response);
|
209
|
return null;
|
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>/portal/classification/{treeUuid}/pathFrom/{taxonUuid}</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>{rankUuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
|
222
|
* </ul>
|
223
|
*
|
224
|
* @param response
|
225
|
* @param request
|
226
|
* @return a List of {@link TaxonNode} entities initialized by
|
227
|
* the {@link #NODE_INIT_STRATEGY}
|
228
|
* @throws IOException
|
229
|
*/
|
230
|
@RequestMapping(
|
231
|
value = {"{treeUuid}/pathFrom/{taxonUuid}"},
|
232
|
method = RequestMethod.GET)
|
233
|
public List<TaxonNodeDto> getPathFromTaxon(
|
234
|
@PathVariable("treeUuid") UUID classificationUuid,
|
235
|
@PathVariable("taxonUuid") UUID taxonUuid,
|
236
|
@RequestParam(value = "subtree", required = false) UUID subtreeUuid,
|
237
|
HttpServletRequest request,
|
238
|
HttpServletResponse response) throws IOException {
|
239
|
|
240
|
return getPathFromTaxonToRank(classificationUuid, taxonUuid, null, subtreeUuid, request, response);
|
241
|
}
|
242
|
|
243
|
|
244
|
private Rank findRank(UUID rankUuid) {
|
245
|
Rank rank = null;
|
246
|
if(rankUuid != null){
|
247
|
DefinedTermBase<?> dt = termService.find(rankUuid);
|
248
|
if(dt instanceof Rank){
|
249
|
rank = (Rank)dt;
|
250
|
} else {
|
251
|
throw new IllegalArgumentException("DefinedTermBase is not a Rank");
|
252
|
}
|
253
|
}
|
254
|
return rank;
|
255
|
}
|
256
|
|
257
|
|
258
|
}
|