Project

General

Profile

Download (8.9 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.persistence.EntityNotFoundException;
18
import javax.servlet.http.HttpServletRequest;
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
import org.springframework.web.bind.annotation.RequestParam;
29

    
30
import eu.etaxonomy.cdm.api.service.IClassificationService;
31
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
32
import eu.etaxonomy.cdm.api.service.ITermService;
33
import eu.etaxonomy.cdm.api.service.NodeSortMode;
34
import eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO;
35
import eu.etaxonomy.cdm.api.service.dto.TaxonInContextDTO;
36
import eu.etaxonomy.cdm.api.service.pager.Pager;
37
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
38
import eu.etaxonomy.cdm.model.name.Rank;
39
import eu.etaxonomy.cdm.model.taxon.Classification;
40
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
41
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
42
import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
43
import eu.etaxonomy.cdm.remote.editor.RankPropertyEditor;
44
import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
45
import eu.etaxonomy.cdm.remote.editor.UuidList;
46
import io.swagger.annotations.Api;
47

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

    
58

    
59
    private ITermService termService;
60
    private ITaxonNodeService taxonNodeService;
61

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

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

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

    
78

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

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

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

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

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

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

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

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

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

    
135
        return rootNodes;
136
    }
137

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

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

    
164
//       service.startTransaction();
165
       boolean recursive = false;
166
       UUID taxonNodeUuid = service.getTaxonNodeUuidByTaxonUuid(classificationUuid, taxonUuid) ;
167
       if (taxonNodeUuid == null){
168
           HttpStatusMessage.UUID_NOT_FOUND.send(response);
169
           return null;
170
       }
171
       Pager<TaxonNodeDto> pager = taxonNodeService.pageChildNodesDTOs(taxonNodeUuid, recursive, doSynonyms, sortMode, pageSize, pageIndex);
172
//       service.commitTransaction()
173

    
174
       return pager;
175
   }
176

    
177

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

    
196
        logger.info("getGroupedTaxaByHigherTaxon() - " + request.getRequestURI());
197

    
198
        Classification classification = service.find(classificationUuid);
199
        if(classification == null) {
200
            response.sendError(404 , "Classification not found using " + classificationUuid );
201
            return null;
202
        }
203

    
204

    
205
        Rank minRank = findRank(minRankUuid);
206
        Rank maxRank = findRank(maxRankUuid);
207

    
208
//        long start = System.currentTimeMillis();
209
        List<GroupedTaxonDTO> result = service.groupTaxaByHigherTaxon(taxonUuids, classificationUuid, minRank, maxRank);
210
//        System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
211

    
212
        return result;
213
    }
214

    
215

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

    
229

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

    
243
       try {
244
           TaxonInContextDTO taxonInContextDTO = service.getTaxonInContext(classificationUuid, taxonUuid, doChildren, doSynonyms, ancestorMarkers, sortMode);
245
           return taxonInContextDTO;
246
       } catch (EntityNotFoundException e) {
247
           HttpStatusMessage.UUID_NOT_FOUND.send(response);
248
           return null;
249
       }
250
   }
251

    
252

    
253
}
(9-9/63)