Project

General

Profile

Download (8.58 KB) Statistics
| Branch: | Tag: | Revision:
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.dto.TaxonInContextDTO;
35
import eu.etaxonomy.cdm.api.service.pager.Pager;
36
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
37
import eu.etaxonomy.cdm.model.name.Rank;
38
import eu.etaxonomy.cdm.model.taxon.Classification;
39
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
40
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
41
import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
42
import eu.etaxonomy.cdm.remote.editor.RankPropertyEditor;
43
import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
44
import eu.etaxonomy.cdm.remote.editor.UuidList;
45
import io.swagger.annotations.Api;
46

    
47
/**
48
 * @author a.kohlbecker
49
 * @date 03.06.2010
50
 *
51
 */
52
@Controller
53
@Api("classification")
54
@RequestMapping(value = {"/classification/{uuid}"})
55
public class ClassificationController extends BaseController<Classification,IClassificationService> {
56

    
57

    
58
    private ITermService termService;
59
    private ITaxonNodeService taxonNodeService;
60

    
61
    @Override
62
    @Autowired
63
    public void setService(IClassificationService service) {
64
        this.service = service;
65
    }
66

    
67
    @Autowired
68
    public void setTermService(ITermService termService) {
69
        this.termService = termService;
70
    }
71

    
72
    @Autowired
73
    public void setTaxonNodeService(ITaxonNodeService taxonNodeService) {
74
        this.taxonNodeService = taxonNodeService;
75
    }
76

    
77

    
78
    @InitBinder
79
    @Override
80
    public void initBinder(WebDataBinder binder) {
81
        super.initBinder(binder);
82
        binder.registerCustomEditor(Rank.class, new RankPropertyEditor());
83
        binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
84
    }
85

    
86
    private List<String> NODE_INIT_STRATEGY(){
87
        return Arrays.asList(new String[]{
88
            "taxon.name"
89
    });}
90

    
91
    /**
92
     * @param classificationUuid
93
     * @param response
94
     * @return
95
     * @throws IOException
96
     *
97
     */
98
    @RequestMapping(
99
            value = {"childNodes"},
100
            method = RequestMethod.GET)
101
    public List<TaxonNode> getChildNodes(
102
            @PathVariable("uuid") UUID classificationUuid,
103
            HttpServletRequest request,
104
            HttpServletResponse response
105
            ) throws IOException {
106

    
107
        return getChildNodesAtRank(classificationUuid, null, request, response);
108
    }
109

    
110
    @RequestMapping(
111
            value = {"childNodesAt/{rankUuid}"},
112
            method = RequestMethod.GET)
113
    public List<TaxonNode> getChildNodesAtRank(
114
            @PathVariable("uuid") UUID classificationUuid,
115
            @PathVariable("rankUuid") UUID rankUuid,
116
            HttpServletRequest request,
117
            HttpServletResponse response
118
            ) throws IOException {
119

    
120
        logger.info("getChildNodesAtRank() - " + request.getRequestURI());
121

    
122
        Classification classification = service.find(classificationUuid);
123

    
124
        if(classification == null) {
125
            response.sendError(404 , "Classification not found using " + classificationUuid );
126
            return null;
127
        }
128
        Rank rank = findRank(rankUuid);
129

    
130
//        long start = System.currentTimeMillis();
131
        List<TaxonNode> rootNodes = service.listRankSpecificRootNodes(classification, rank, null, null, NODE_INIT_STRATEGY());
132
//        System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
133

    
134
        return rootNodes;
135
    }
136

    
137
    /**
138
    *
139
    * @param uuid
140
    * @param pageIndex
141
    * @param pageSize
142
    * @param sortMode
143
    * @param response
144
    * @return
145
    * @throws IOException
146
    */
147
   @RequestMapping(
148
           value = {"childNodesByTaxon/{taxonUuid}"},
149
           method = RequestMethod.GET)
150
   public Pager<TaxonNodeDto> doPageChildNodes(
151
           @PathVariable("uuid") UUID classificationUuid,
152
           @PathVariable("taxonUuid") UUID taxonUuid,
153
           @RequestParam(value = "pageNumber", required = false) Integer pageIndex,
154
           @RequestParam(value = "pageSize", required = false) Integer pageSize,
155
           @RequestParam(value = "sortMode", defaultValue="AlphabeticalOrder") NodeSortMode sortMode,
156
           @RequestParam(value = "doSynonyms", defaultValue="false") Boolean doSynonyms,
157
           HttpServletResponse response
158
           ) throws IOException {
159

    
160
       PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex);
161
       pagerParameters.normalizeAndValidate(response);
162

    
163
//       service.startTransaction();
164
       boolean recursive = false;
165
       UUID taxonNodeUuid = service.getTaxonNodeUuidByTaxonUuid(classificationUuid, taxonUuid) ;
166
       Pager<TaxonNodeDto> pager = taxonNodeService.pageChildNodesDTOs(taxonNodeUuid, recursive, doSynonyms, sortMode, pageSize, pageIndex);
167
//       service.commitTransaction()
168

    
169
       return pager;
170
   }
171

    
172

    
173
    /**
174
     * @param classificationUuid
175
     * @param response
176
     * @return
177
     * @throws IOException
178
     */
179
    @RequestMapping(
180
            value = {"groupedTaxa"},
181
            method = RequestMethod.GET)
182
    public List<GroupedTaxonDTO> getGroupedTaxaByHigherTaxon(
183
            @PathVariable("uuid") UUID classificationUuid,
184
            @RequestParam(value = "taxonUuids", required = true) UuidList taxonUuids,
185
            @RequestParam(value = "minRankUuid", required = false) UUID minRankUuid,
186
            @RequestParam(value = "maxRankUuid", required = false) UUID maxRankUuid,
187
            HttpServletRequest request,
188
            HttpServletResponse response
189
            ) throws IOException {
190

    
191
        logger.info("getGroupedTaxaByHigherTaxon() - " + request.getRequestURI());
192

    
193
        Classification classification = service.find(classificationUuid);
194
        if(classification == null) {
195
            response.sendError(404 , "Classification not found using " + classificationUuid );
196
            return null;
197
        }
198

    
199

    
200
        Rank minRank = findRank(minRankUuid);
201
        Rank maxRank = findRank(maxRankUuid);
202

    
203
//        long start = System.currentTimeMillis();
204
        List<GroupedTaxonDTO> result = service.groupTaxaByHigherTaxon(taxonUuids, classificationUuid, minRank, maxRank);
205
//        System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
206

    
207
        return result;
208
    }
209

    
210

    
211
    private Rank findRank(UUID rankUuid) {
212
        Rank rank = null;
213
        if(rankUuid != null){
214
            DefinedTermBase<?> definedTermBase =  termService.find(rankUuid);
215
            if(definedTermBase instanceof Rank){
216
                rank = (Rank) definedTermBase;
217
            } else {
218
               throw new IllegalArgumentException("DefinedTermBase is not a Rank");
219
            }
220
        }
221
        return rank;
222
    }
223

    
224

    
225
   @RequestMapping(
226
           value = {"taxonInContext/{taxonUuid}"},
227
           method = RequestMethod.GET)
228
   public TaxonInContextDTO getTaxonInContext(
229
           @PathVariable("uuid") UUID classificationUuid,
230
           @PathVariable("taxonUuid") UUID taxonUuid,
231
           @RequestParam(value = "doChildren", defaultValue = "false") Boolean doChildren,
232
           @RequestParam(value = "doSynonyms", defaultValue = "false") Boolean doSynonyms,
233
           @RequestParam(value = "sortMode", defaultValue="AlphabeticalOrder") NodeSortMode sortMode,
234
           @RequestParam(value = "ancestorMarker", required = false) List<UUID> ancestorMarkers,
235
           HttpServletResponse response
236
           ) throws IOException {
237

    
238
       TaxonInContextDTO taxonInContextDTO = service.getTaxonInContext(classificationUuid, taxonUuid, doChildren, doSynonyms, ancestorMarkers, sortMode) ;
239

    
240
       return taxonInContextDTO;
241
   }
242

    
243

    
244
}
(9-9/63)