Project

General

Profile

Download (10.8 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 EDIT
3
* 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 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
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.persistence.EntityNotFoundException;
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.common.MarkerType;
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 AbstractIdentifiableController<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> doPageChildNodesByTaxon(
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

    
193
            HttpServletRequest request,
194
            HttpServletResponse response
195
            ) throws IOException {
196

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

    
199
        Classification classification = service.find(classificationUuid);
200
        if(classification == null) {
201
            response.sendError(404 , "Classification not found using " + classificationUuid );
202
            return null;
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
     * @param classificationUuid
217
     * @param response
218
     * @return
219
     * @throws IOException
220
     */
221
    @RequestMapping(
222
            value = {"groupedTaxaByMarker"},
223
            method = RequestMethod.GET)
224
    public List<GroupedTaxonDTO> getGroupedTaxaByMarkedParents(
225
            @PathVariable("uuid") UUID classificationUuid,
226
            @RequestParam(value = "taxonUuids", required = true) UuidList taxonUuids,
227
            @RequestParam(value = "markerTypeUuid", required = false) UUID markerTypeUuid,
228
            @RequestParam(value = "flag", required = false) Boolean flag,
229

    
230
            HttpServletRequest request,
231
            HttpServletResponse response
232
            ) throws IOException {
233

    
234
        logger.info("getGroupedTaxaByHigherTaxon() - " + request.getRequestURI());
235

    
236
        Classification classification = service.find(classificationUuid);
237
        if(classification == null) {
238
            response.sendError(404 , "Classification not found using " + classificationUuid );
239
            return null;
240
        }
241

    
242
        MarkerType markerType = findMarkerType(markerTypeUuid);
243
//        long start = System.currentTimeMillis();
244
        List<GroupedTaxonDTO> result = service.groupTaxaByMarkedParents(taxonUuids, classificationUuid, markerType, flag);
245
//        System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
246

    
247
        return result;
248
    }
249

    
250

    
251
    private Rank findRank(UUID rankUuid) {
252
        Rank rank = null;
253
        if(rankUuid != null){
254
            DefinedTermBase<?> definedTermBase =  termService.find(rankUuid);
255
            if(definedTermBase instanceof Rank){
256
                rank = (Rank) definedTermBase;
257
            } else {
258
               throw new IllegalArgumentException("DefinedTermBase is not a Rank");
259
            }
260
        }
261
        return rank;
262
    }
263

    
264
    private MarkerType findMarkerType(UUID markerTypeUuid) {
265
        MarkerType markerType = null;
266
        if(markerTypeUuid != null){
267
            DefinedTermBase<?> definedTermBase =  termService.find(markerTypeUuid);
268
            if(definedTermBase instanceof MarkerType){
269
                markerType = (MarkerType) definedTermBase;
270
            } else {
271
               throw new IllegalArgumentException("DefinedTermBase is not a MarkerType");
272
            }
273
        }
274
        return markerType;
275
    }
276

    
277

    
278
   @RequestMapping(
279
           value = {"taxonInContext/{taxonUuid}"},
280
           method = RequestMethod.GET)
281
   public TaxonInContextDTO getTaxonInContext(
282
           @PathVariable("uuid") UUID classificationUuid,
283
           @PathVariable("taxonUuid") UUID taxonUuid,
284
           @RequestParam(value = "doChildren", defaultValue = "false") Boolean doChildren,
285
           @RequestParam(value = "doSynonyms", defaultValue = "false") Boolean doSynonyms,
286
           @RequestParam(value = "sortMode", defaultValue="AlphabeticalOrder") NodeSortMode sortMode,
287
           @RequestParam(value = "ancestorMarker", required = false) List<UUID> ancestorMarkers,
288
           HttpServletResponse response
289
           ) throws IOException {
290

    
291
       try {
292
           TaxonInContextDTO taxonInContextDTO = service.getTaxonInContext(classificationUuid, taxonUuid, doChildren, doSynonyms, ancestorMarkers, sortMode);
293
           return taxonInContextDTO;
294
       } catch (EntityNotFoundException e) {
295
           HttpStatusMessage.UUID_NOT_FOUND.send(response);
296
           return null;
297
       }
298
   }
299

    
300

    
301
}
(11-11/66)