Project

General

Profile

Download (12.2 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.NodeSortMode;
35
import eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO;
36
import eu.etaxonomy.cdm.api.service.dto.TaxonInContextDTO;
37
import eu.etaxonomy.cdm.api.service.pager.Pager;
38
import eu.etaxonomy.cdm.model.common.MarkerType;
39
import eu.etaxonomy.cdm.model.name.Rank;
40
import eu.etaxonomy.cdm.model.taxon.Classification;
41
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
42
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
43
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
44
import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
45
import eu.etaxonomy.cdm.remote.editor.RankPropertyEditor;
46
import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
47
import eu.etaxonomy.cdm.remote.editor.UuidList;
48
import io.swagger.annotations.Api;
49

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

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

    
62
    private ITermService termService;
63
    private ITaxonNodeService taxonNodeService;
64

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

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

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

    
84

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

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

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

    
115
        return getChildNodesAtRank(classificationUuid, null, subtreeUuid, request, response);
116
    }
117

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

    
129
        logger.info("getChildNodesAtRank() - " + request.getRequestURI());
130

    
131
        Classification classification = service.find(classificationUuid);
132

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

    
138
        TaxonNode subtree = getSubtreeOrError(subtreeUuid, taxonNodeService, response);
139

    
140
        Rank rank = findRank(rankUuid);
141

    
142
        boolean includeUnpublished = NO_UNPUBLISHED;
143
//        long start = System.currentTimeMillis();
144
        List<TaxonNode> rootNodes = service.listRankSpecificRootNodes(classification, subtree, rank,
145
                includeUnpublished, null, null, NODE_INIT_STRATEGY());
146
//        System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
147

    
148
        return rootNodes;
149
    }
150

    
151

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

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

    
177
       PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex);
178
       pagerParameters.normalizeAndValidate(response);
179

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

    
191
       return pager;
192
   }
193

    
194

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

    
210
            HttpServletRequest request,
211
            HttpServletResponse response
212
            ) throws IOException {
213

    
214
        logger.info("getGroupedTaxaByHigherTaxon() - " + request.getRequestURI());
215

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

    
222
        Rank minRank = findRank(minRankUuid);
223
        Rank maxRank = findRank(maxRankUuid);
224

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

    
229
        return result;
230
    }
231

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

    
247
            HttpServletRequest request,
248
            HttpServletResponse response
249
            ) throws IOException {
250

    
251
        logger.info("getGroupedTaxaByHigherTaxon() - " + request.getRequestURI());
252

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

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

    
264
        return result;
265
    }
266

    
267

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

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

    
294

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

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

    
319
   @RequestMapping(value = { "classificationRootNode" }, method = RequestMethod.GET)
320
   public ModelAndView getClassificationRootNode(@PathVariable("uuid") UUID uuid, HttpServletRequest request,
321
           HttpServletResponse response) throws IOException {
322

    
323
       ModelAndView mv = new ModelAndView();
324
       mv.addObject(service.getRootNode(uuid));
325
       return mv;
326
   }
327

    
328

    
329
}
(11-11/70)