Project

General

Profile

Download (10.1 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2009 EDIT European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version
7
 * 1.1 See LICENSE.TXT at the top of this package for the full license terms.
8
 */
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.HttpServletRequest;
20
import javax.servlet.http.HttpServletResponse;
21

    
22
import org.apache.log4j.Logger;
23
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.stereotype.Controller;
25
import org.springframework.web.bind.WebDataBinder;
26
import org.springframework.web.bind.annotation.InitBinder;
27
import org.springframework.web.bind.annotation.PathVariable;
28
import org.springframework.web.bind.annotation.RequestMapping;
29
import org.springframework.web.bind.annotation.RequestMethod;
30

    
31
import eu.etaxonomy.cdm.api.service.IClassificationService;
32
import eu.etaxonomy.cdm.api.service.ITaxonService;
33
import eu.etaxonomy.cdm.api.service.ITermService;
34
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
35
import eu.etaxonomy.cdm.model.name.Rank;
36
import eu.etaxonomy.cdm.model.taxon.Classification;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
39
import eu.etaxonomy.cdm.remote.editor.RankPropertyEditor;
40

    
41
/**
42
 * The ClassificationController class is a Spring MVC Controller.
43
 * @author a.kohlbecker
44
 * @date 20.03.2009
45
 */
46
@Controller
47
@Api("portal_classification")
48
@RequestMapping(value="/portal/classification")
49
public class ClassificationPortalListController extends IdentifiableListController<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

    
154
//        long start = System.currentTimeMillis();
155
        List<TaxonNode> rootNodes = service.listRankSpecificRootNodes(tree, rank, null, null, NODE_INIT_STRATEGY);
156
//        System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
157
        return rootNodes;
158
    }
159

    
160

    
161

    
162

    
163
    /**
164
     * Lists all child-{@link TaxonNode}s of the specified {@link Taxon} in the {@link Classification}. The
165
     * a given {@link Rank} is ignored in this method but for consistency reasons it has been allowed to included it into the URI.
166
     * <p>
167
     * URI: <b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;childNodesOf&#x002F;{taxonUuid}</b>
168
     * <p>
169
     * <b>URI elements:</b>
170
     * <ul>
171
     * <li><b>{tree-uuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
172
     * <li><b>{taxon-uuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
173
     * </ul>
174
     *
175
     * @param response
176
     * @param request
177
     * @return a List of {@link TaxonNode} entities initialized by
178
     *         the {@link #NODE_INIT_STRATEGY}
179
     */
180
    @RequestMapping(
181
            value = {"{treeUuid}/childNodesOf/{taxonUuid}"},
182
            method = RequestMethod.GET)
183
    public List<TaxonNode> getChildNodesOfTaxon(
184
            @PathVariable("treeUuid") UUID treeUuid,
185
            @PathVariable("taxonUuid") UUID taxonUuid,
186
            HttpServletRequest request,
187
            HttpServletResponse response) throws IOException {
188
        logger.info("getChildNodesOfTaxon() " + request.getRequestURI());
189

    
190

    
191
        List<TaxonNode> childs = service.listChildNodesOfTaxon(taxonUuid, treeUuid, null, null, NODE_INIT_STRATEGY);
192
        return childs;
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
        //FIXME return pager
207
        List<TaxonNode> childs = service.listSiblingsOfTaxon(taxonUuid, treeUuid, null, null, NODE_INIT_STRATEGY);
208
        return childs;
209

    
210
    }
211

    
212
    /**
213
     * Provides path of {@link TaxonNode}s from the base node to the node of the specified taxon.
214
     * <p>
215
     * URI:<b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;pathFrom&#x002F;{taxonUuid}&#x002F;toRank&#x002F;{rankUuid}</b>
216
     * <p>
217
     * <b>URI elements:</b>
218
     * <ul>
219
     * <li><b>{treeUuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
220
     * <li><b>{taxonUuid}</b> identifies the {@link Rank}
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
     */
229
    @RequestMapping(
230
            value = {"{treeUuid}/pathFrom/{taxonUuid}/toRank/{rankUuid}"},
231
            method = RequestMethod.GET)
232
    public List<TaxonNode> getPathFromTaxonToRank(
233
            @PathVariable("treeUuid") UUID treeUuid,
234
            @PathVariable("taxonUuid") UUID taxonUuid,
235
            @PathVariable("rankUuid") UUID rankUuid,
236
            HttpServletRequest request,
237
            HttpServletResponse response) throws IOException {
238
        logger.info("getPathFromTaxonToRank() " + request.getRequestURI());
239

    
240
        Classification tree = service.find(treeUuid);
241
        Rank rank = findRank(rankUuid);
242
        Taxon taxon = (Taxon) taxonService.load(taxonUuid);
243

    
244
        return service.loadTreeBranchToTaxon(taxon, tree, rank, NODE_INIT_STRATEGY);
245
    }
246

    
247
    /**
248
     * Provides path of {@link TaxonNode}s from the base node to the node of the specified taxon.
249
     * <p>
250
     * URI:<b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;pathFrom&#x002F;{taxonUuid}</b>
251
     * <p>
252
     * <b>URI elements:</b>
253
     * <ul>
254
     * <li><b>{treeUuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
255
     * <li><b>{rankUuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
256
     * </ul>
257
     *
258
     * @param response
259
     * @param request
260
     * @return a List of {@link TaxonNode} entities initialized by
261
     *         the {@link #NODE_INIT_STRATEGY}
262
     */
263
    @RequestMapping(
264
            value = {"{treeUuid}/pathFrom/{taxonUuid}"},
265
            method = RequestMethod.GET)
266
    public List<TaxonNode> getPathFromTaxon(
267
            @PathVariable("treeUuid") UUID treeUuid,
268
            @PathVariable("taxonUuid") UUID taxonUuid,
269
            HttpServletRequest request,
270
            HttpServletResponse response) throws IOException {
271

    
272
        return getPathFromTaxonToRank(treeUuid, taxonUuid, null, request, response);
273
    }
274

    
275

    
276
    private Rank findRank(UUID rankUuid) {
277
        Rank rank = null;
278
        if(rankUuid != null){
279
            DefinedTermBase dt =  termService.find(rankUuid);
280
            if(dt instanceof Rank){
281
                rank = (Rank)dt;
282
            } else {
283
               new IllegalArgumentException("DefinedTermBase is not a Rank");
284
            }
285
        }
286
        return rank;
287
    }
288

    
289

    
290
}
(12-12/63)