Project

General

Profile

Download (12.6 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.apache.log4j.Logger;
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
import org.springframework.web.servlet.ModelAndView;
30

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

    
51
/**
52
 * @author a.kohlbecker
53
 * @since 03.06.2010
54
 *
55
 */
56
@Controller
57
@Api("classification")
58
@RequestMapping(value = {"/classification/{uuid}"})
59
public class ClassificationController extends AbstractIdentifiableController<Classification,IClassificationService> {
60

    
61
    private static final Logger logger = Logger.getLogger(ClassificationController.class);
62

    
63
    private ITermService termService;
64
    private ITaxonNodeService taxonNodeService;
65

    
66
    @Override
67
    @Autowired
68
    public void setService(IClassificationService service) {
69
        this.service = service;
70
    }
71

    
72
    @Autowired
73
    public void setTermService(ITermService termService) {
74
        this.termService = termService;
75
    }
76

    
77
    @Autowired
78
    public void setTaxonNodeService(ITaxonNodeService taxonNodeService) {
79
        this.taxonNodeService = taxonNodeService;
80
    }
81
    protected ITaxonNodeService getTaxonNodeService() {
82
        return this.taxonNodeService;
83
    }
84

    
85

    
86
    @InitBinder
87
    @Override
88
    public void initBinder(WebDataBinder binder) {
89
        super.initBinder(binder);
90
        binder.registerCustomEditor(Rank.class, new RankPropertyEditor());
91
        binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
92
    }
93

    
94
    private List<String> NODE_INIT_STRATEGY(){
95
        return Arrays.asList(new String[]{
96
            "taxon.name"
97
    });}
98

    
99
    /**
100
     * @param classificationUuid
101
     * @param response
102
     * @return
103
     * @throws IOException
104
     *
105
     */
106
    @RequestMapping(
107
            value = {"childNodes"},
108
            method = RequestMethod.GET)
109
    public List<TaxonNodeDto> getChildNodes(
110
            @PathVariable("uuid") UUID classificationUuid,
111
            @RequestParam(value = "subtree", required = false) UUID subtreeUuid,
112
            @RequestParam(value = "sortMode", required = false, defaultValue = "AlphabeticalOrder") TaxonNodeDtoSortMode sortMode,
113
            HttpServletRequest request,
114
            HttpServletResponse response
115
            ) throws IOException {
116

    
117
        return getChildNodesAtRank(classificationUuid, null, subtreeUuid, sortMode, request, response);
118
    }
119

    
120
    @RequestMapping(
121
            value = {"childNodesAt/{rankUuid}"},
122
            method = RequestMethod.GET)
123
    public List<TaxonNodeDto> getChildNodesAtRank(
124
            @PathVariable("uuid") UUID classificationUuid,
125
            @PathVariable("rankUuid") UUID rankUuid,
126
            @RequestParam(value = "subtree", required = false) UUID subtreeUuid,
127
            @RequestParam(value = "sortMode", required = false, defaultValue = "AlphabeticalOrder") TaxonNodeDtoSortMode sortMode,
128
            HttpServletRequest request,
129
            HttpServletResponse response
130
            ) throws IOException {
131

    
132
        logger.info("getChildNodesAtRank() - " + request.getRequestURI());
133

    
134
        Classification classification = service.find(classificationUuid);
135

    
136
        if(classification == null) {
137
            HttpStatusMessage.UUID_NOT_FOUND.send(response, "Classification not found using " + classificationUuid);
138
            return null;
139
        }
140

    
141
        TaxonNode subtree = getSubtreeOrError(subtreeUuid, taxonNodeService, response);
142

    
143
        Rank rank = findRank(rankUuid);
144

    
145
        boolean includeUnpublished = NO_UNPUBLISHED;
146
//        long start = System.currentTimeMillis();
147
        List<TaxonNodeDto> rootNodes = service.listRankSpecificRootNodeDtos(classification, subtree, rank,
148
                includeUnpublished, null, null, sortMode, NODE_INIT_STRATEGY());
149
//        System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
150

    
151
        return rootNodes;
152
    }
153

    
154

    
155
    /**
156
    *
157
    * @param uuid
158
    * @param pageIndex
159
    * @param pageSize
160
    * @param sortMode
161
    * @param response
162
    * @return
163
    * @throws IOException
164
    */
165
   @RequestMapping(
166
           value = {"childNodesByTaxon/{taxonUuid}"},
167
           method = RequestMethod.GET)
168
   public Pager<TaxonNodeDto> doPageChildNodesByTaxon(
169
           @PathVariable("uuid") UUID classificationUuid,
170
           @PathVariable("taxonUuid") UUID taxonUuid,
171
           @RequestParam(value = "pageNumber", required = false) Integer pageIndex,
172
           @RequestParam(value = "pageSize", required = false) Integer pageSize,
173
           @RequestParam(value = "sortMode", defaultValue="AlphabeticalOrder") TaxonNodeSortMode sortMode,
174
           @RequestParam(value = "doSynonyms", defaultValue="false") Boolean doSynonyms,
175
           HttpServletResponse response
176
           ) throws IOException {
177

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

    
180
       PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex);
181
       pagerParameters.normalizeAndValidate(response);
182

    
183
//       service.startTransaction();
184
       boolean recursive = false;
185
       UUID taxonNodeUuid = service.getTaxonNodeUuidByTaxonUuid(classificationUuid, taxonUuid) ;
186
       if (taxonNodeUuid == null){
187
           HttpStatusMessage.UUID_NOT_FOUND.send(response);
188
           return null;
189
       }
190
       Pager<TaxonNodeDto> pager = taxonNodeService.pageChildNodesDTOs(taxonNodeUuid, recursive, includeUnpublished,
191
               doSynonyms, sortMode, pageSize, pageIndex);
192
//       service.commitTransaction()
193

    
194
       return pager;
195
   }
196

    
197

    
198
    /**
199
     * @param classificationUuid
200
     * @param response
201
     * @return
202
     * @throws IOException
203
     */
204
    @RequestMapping(
205
            value = {"groupedTaxa"},
206
            method = RequestMethod.GET)
207
    public List<GroupedTaxonDTO> getGroupedTaxaByHigherTaxon(
208
            @PathVariable("uuid") UUID classificationUuid,
209
            @RequestParam(value = "taxonUuids", required = true) UuidList taxonUuids,
210
            @RequestParam(value = "minRankUuid", required = false) UUID minRankUuid,
211
            @RequestParam(value = "maxRankUuid", required = false) UUID maxRankUuid,
212

    
213
            HttpServletRequest request,
214
            HttpServletResponse response
215
            ) throws IOException {
216

    
217
        logger.info("getGroupedTaxaByHigherTaxon() - " + request.getRequestURI());
218

    
219
        Classification classification = service.find(classificationUuid);
220
        if(classification == null) {
221
            response.sendError(404 , "Classification not found using " + classificationUuid );
222
            return null;
223
        }
224

    
225
        Rank minRank = findRank(minRankUuid);
226
        Rank maxRank = findRank(maxRankUuid);
227

    
228
//        long start = System.currentTimeMillis();
229
        List<GroupedTaxonDTO> result = service.groupTaxaByHigherTaxon(taxonUuids, classificationUuid, minRank, maxRank);
230
//        System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
231

    
232
        return result;
233
    }
234

    
235
    /**
236
     * @param classificationUuid
237
     * @param response
238
     * @return
239
     * @throws IOException
240
     */
241
    @RequestMapping(
242
            value = {"groupedTaxaByMarker"},
243
            method = RequestMethod.GET)
244
    public List<GroupedTaxonDTO> getGroupedTaxaByMarkedParents(
245
            @PathVariable("uuid") UUID classificationUuid,
246
            @RequestParam(value = "taxonUuids", required = true) UuidList taxonUuids,
247
            @RequestParam(value = "markerTypeUuid", required = false) UUID markerTypeUuid,
248
            @RequestParam(value = "flag", required = false) Boolean flag,
249

    
250
            HttpServletRequest request,
251
            HttpServletResponse response
252
            ) throws IOException {
253

    
254
        logger.info("getGroupedTaxaByHigherTaxon() - " + request.getRequestURI());
255

    
256
        Classification classification = service.find(classificationUuid);
257
        if(classification == null) {
258
            response.sendError(404 , "Classification not found using " + classificationUuid );
259
            return null;
260
        }
261

    
262
        MarkerType markerType = findMarkerType(markerTypeUuid);
263
//        long start = System.currentTimeMillis();
264
        List<GroupedTaxonDTO> result = service.groupTaxaByMarkedParents(taxonUuids, classificationUuid, markerType, flag);
265
//        System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
266

    
267
        return result;
268
    }
269

    
270

    
271
    private Rank findRank(UUID rankUuid) {
272
        Rank rank = null;
273
        if(rankUuid != null){
274
            DefinedTermBase<?> definedTermBase =  termService.find(rankUuid);
275
            if(definedTermBase instanceof Rank){
276
                rank = (Rank) definedTermBase;
277
            } else {
278
               throw new IllegalArgumentException("DefinedTermBase is not a Rank");
279
            }
280
        }
281
        return rank;
282
    }
283

    
284
    private MarkerType findMarkerType(UUID markerTypeUuid) {
285
        MarkerType markerType = null;
286
        if(markerTypeUuid != null){
287
            DefinedTermBase<?> definedTermBase =  termService.find(markerTypeUuid);
288
            if(definedTermBase instanceof MarkerType){
289
                markerType = (MarkerType) definedTermBase;
290
            } else {
291
               throw new IllegalArgumentException("DefinedTermBase is not a MarkerType");
292
            }
293
        }
294
        return markerType;
295
    }
296

    
297

    
298
   @RequestMapping(
299
           value = {"taxonInContext/{taxonUuid}"},
300
           method = RequestMethod.GET)
301
   public TaxonInContextDTO getTaxonInContext(
302
           @PathVariable("uuid") UUID classificationUuid,
303
           @PathVariable("taxonUuid") UUID taxonUuid,
304
           @RequestParam(value = "doChildren", defaultValue = "false") Boolean doChildren,
305
           @RequestParam(value = "doSynonyms", defaultValue = "false") Boolean doSynonyms,
306
           @RequestParam(value = "sortMode", defaultValue="AlphabeticalOrder") TaxonNodeSortMode sortMode,
307
           @RequestParam(value = "ancestorMarker", required = false) List<UUID> ancestorMarkers,
308
           HttpServletResponse response
309
           ) throws IOException {
310

    
311
       try {
312
           boolean includeUnpublished = NO_UNPUBLISHED;  //for now we do not allow any remote service to publish unpublished data
313
           TaxonInContextDTO taxonInContextDTO = service.getTaxonInContext(classificationUuid, taxonUuid, doChildren, includeUnpublished,
314
                   doSynonyms, ancestorMarkers, sortMode);
315
           return taxonInContextDTO;
316
       } catch (EntityNotFoundException e) {
317
           HttpStatusMessage.UUID_NOT_FOUND.send(response);
318
           return null;
319
       }
320
   }
321

    
322
   @RequestMapping(value = { "classificationRootNode" }, method = RequestMethod.GET)
323
   public ModelAndView getClassificationRootNode(
324
           @PathVariable("uuid") UUID uuid,
325
           @SuppressWarnings("unused") HttpServletRequest request,
326
           @SuppressWarnings("unused") HttpServletResponse response) {
327

    
328
       ModelAndView mv = new ModelAndView();
329
       mv.addObject(service.getRootNode(uuid));
330
       return mv;
331
   }
332

    
333

    
334
}
(12-12/75)