Project

General

Profile

Download (10.1 KB) Statistics
| Branch: | Tag: | Revision:
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
            "source.citation.authorship"
59
    });
60

    
61
    private static final List<String> NODE_INIT_STRATEGY = Arrays.asList(new String[]{
62
            "taxon.name.rank",
63
            "taxon.secSource.citation"
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
    @InitBinder
97
    @Override
98
    public void initBinder(WebDataBinder binder) {
99
        super.initBinder(binder);
100
        binder.registerCustomEditor(Rank.class, new RankPropertyEditor());
101
    }
102

    
103
    /**
104
     * Lists all child-{@link TaxonNode}s of the specified {@link Taxon} in the {@link Classification}. The
105
     * a given {@link Rank} is ignored in this method but for consistency reasons it has been allowed to included it into the URI.
106
     * <p>
107
     * URI: <b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;childNodesOf&#x002F;{taxonUuid}</b>
108
     * <p>
109
     * <b>URI elements:</b>
110
     * <ul>
111
     * <li><b>{tree-uuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
112
     * <li><b>{taxon-uuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
113
     * </ul>
114
     *
115
     * @param response
116
     * @param request
117
     * @return a List of {@link TaxonNode} entities initialized by
118
     *         the {@link #NODE_INIT_STRATEGY}
119
     */
120
    @RequestMapping(
121
            value = {"{treeUuid}/childNodesOf/{taxonUuid}"},
122
            method = RequestMethod.GET)
123
    public List<TaxonNodeDto> getChildNodesOfTaxon(
124
            @PathVariable("treeUuid") UUID treeUuid,
125
            @PathVariable("taxonUuid") UUID taxonUuid,
126
            @RequestParam(value = "subtree", required = false) UUID subtreeUuid,
127
            @RequestParam(value = "sortMode", required = false, defaultValue = ClassificationController.DEFAULT_TAXONNODEDTO_SORT_MODE) TaxonNodeDtoSortMode sortMode,
128
            HttpServletRequest request,
129
            HttpServletResponse response) throws IOException {
130
        logger.info("getChildNodesOfTaxon() " + request.getRequestURI());
131

    
132
        boolean includeUnpublished = NO_UNPUBLISHED;  //for now we do not allow any remote service to publish unpublished data
133

    
134
        List<TaxonNodeDto> children;
135
        try {
136
            children = service.listChildNodeDtosOfTaxon(taxonUuid, treeUuid, subtreeUuid,
137
                    includeUnpublished, null, null, sortMode, NODE_INIT_STRATEGY);
138
        } catch (FilterException e) {
139
            HttpStatusMessage.SUBTREE_FILTER_INVALID.send(response);
140
            return null;
141
        }
142
        return children;
143

    
144
    }
145

    
146
    @RequestMapping(
147
            value = {"{treeUuid}/siblingsOf/{taxonUuid}"},
148
            method = RequestMethod.GET)
149
    public List<TaxonNode> getSiblingsOfTaxon(
150
            @PathVariable("treeUuid") UUID classificationUuid,
151
            @PathVariable("taxonUuid") UUID taxonUuid,
152
            HttpServletRequest request,
153
            @SuppressWarnings("unused") HttpServletResponse response) {
154
        logger.info("getSiblingsOfTaxon() " + request.getRequestURI());
155

    
156
        boolean includeUnpublished = NO_UNPUBLISHED;
157
        //FIXME return pager
158
        List<TaxonNode> childs = service.listSiblingsOfTaxon(taxonUuid, classificationUuid, includeUnpublished, null, null, NODE_INIT_STRATEGY);
159
        return childs;
160
    }
161

    
162
    /**
163
     * Provides path of {@link TaxonNode}s from the base node to the node of the specified taxon.
164
     * <p>
165
     * URI:<b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;pathFrom&#x002F;{taxonUuid}&#x002F;toRank&#x002F;{rankUuid}</b>
166
     * <p>
167
     * <b>URI elements:</b>
168
     * <ul>
169
     * <li><b>{treeUuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
170
     * <li><b>{taxonUuid}</b> identifies the {@link Rank}
171
     * <li><b>{rankUuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
172
     * </ul>
173
     *
174
     * @param response
175
     * @param request
176
     * @return a List of {@link TaxonNode} entities initialized by
177
     *         the {@link #NODE_INIT_STRATEGY}
178
     * @throws IOException
179
     */
180
    @RequestMapping(
181
            value = {"{treeUuid}/pathFrom/{taxonUuid}/toRank/{rankUuid}"},
182
            method = RequestMethod.GET)
183
    public List<TaxonNodeDto> getPathFromTaxonToRank(
184
            @PathVariable("treeUuid") UUID classificationUuid,
185
            @PathVariable("taxonUuid") UUID taxonUuid,
186
            @PathVariable("rankUuid") UUID rankUuid,
187
            @RequestParam(value = "subtree", required = false) UUID subtreeUuid,
188
            HttpServletRequest request,
189
            HttpServletResponse response) throws IOException {
190
        logger.info("getPathFromTaxonToRank() " + request.getRequestURI());
191

    
192
        boolean includeUnpublished = NO_UNPUBLISHED;
193

    
194
        Classification classification = service.find(classificationUuid);
195
        TaxonNode subtree = getSubtreeOrError(subtreeUuid, taxonNodeService, response);
196
        Rank rank = findRank(rankUuid);
197
        Taxon taxon = (Taxon) taxonService.load(taxonUuid);
198
        if(classification == null){
199
            HttpStatusMessage.UUID_INVALID.send(response, "Classification uuid does not exist.");
200
            return null;
201
        }
202
        try {
203
            List<TaxonNodeDto> result = service.loadTreeBranchDTOsToTaxon(taxon, classification, subtree, rank, includeUnpublished, NODE_INIT_STRATEGY);
204
            return result;
205
        } catch (UnpublishedException e) {
206
            HttpStatusMessage.ACCESS_DENIED.send(response);
207
            return null;
208
        }
209
    }
210

    
211
    /**
212
     * Provides path of {@link TaxonNode}s from the base node to the node of the specified taxon.
213
     * <p>
214
     * URI:<b>&#x002F;portal&#x002F;classification&#x002F;{treeUuid}&#x002F;pathFrom&#x002F;{taxonUuid}</b>
215
     * <p>
216
     * <b>URI elements:</b>
217
     * <ul>
218
     * <li><b>{treeUuid}</b> identifies the {@link Classification} by its UUID - <i>required</i>.
219
     * <li><b>{rankUuid}</b> identifies the {@link Taxon} by its UUID. - <i>required</i>.
220
     * </ul>
221
     *
222
     * @param response
223
     * @param request
224
     * @return a List of {@link TaxonNode} entities initialized by
225
     *         the {@link #NODE_INIT_STRATEGY}
226
     * @throws IOException
227
     */
228
    @RequestMapping(
229
            value = {"{treeUuid}/pathFrom/{taxonUuid}"},
230
            method = RequestMethod.GET)
231
    public List<TaxonNodeDto> getPathFromTaxon(
232
            @PathVariable("treeUuid") UUID classificationUuid,
233
            @PathVariable("taxonUuid") UUID taxonUuid,
234
            @RequestParam(value = "subtree", required = false) UUID subtreeUuid,
235
            HttpServletRequest request,
236
            HttpServletResponse response) throws IOException {
237

    
238
        return getPathFromTaxonToRank(classificationUuid, taxonUuid, null, subtreeUuid, request, response);
239
    }
240

    
241
    private Rank findRank(UUID rankUuid) {
242
        Rank rank = null;
243
        if(rankUuid != null){
244
            DefinedTermBase<?> dt =  termService.find(rankUuid);
245
            if(dt instanceof Rank){
246
                rank = (Rank)dt;
247
            } else {
248
               throw new IllegalArgumentException("DefinedTermBase is not a Rank");
249
            }
250
        }
251
        return rank;
252
    }
253
}
(15-15/76)