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.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.compare.taxon.TaxonNodeSortMode;
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
@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
    public static final String DEFAULT_TAXONNODEDTO_SORT_MODE = "RankAndAlphabeticalOrder";
66

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

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

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

    
86

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

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

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

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

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

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

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

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

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

    
144
        Rank rank = findRank(rankUuid);
145

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

    
152
        return rootNodes;
153
    }
154

    
155
   @RequestMapping(
156
           value = {"childNodesByTaxon/{taxonUuid}"},
157
           method = RequestMethod.GET)
158
   public Pager<TaxonNodeDto> doPageChildNodesByTaxon(
159
           @PathVariable("uuid") UUID classificationUuid,
160
           @PathVariable("taxonUuid") UUID taxonUuid,
161
           @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
162
           @RequestParam(value = "pageSize", required = false) Integer pageSize,
163
           @RequestParam(value = "sortMode", defaultValue = DEFAULT_TAXONNODEDTO_SORT_MODE) TaxonNodeSortMode sortMode,
164
           @RequestParam(value = "doSynonyms", defaultValue = "false") Boolean doSynonyms,
165
           HttpServletResponse response
166
           ) throws IOException {
167

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

    
170
       PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex);
171
       pagerParameters.normalizeAndValidate(response);
172

    
173
//       service.startTransaction();
174
       boolean recursive = false;
175
       UUID taxonNodeUuid = service.getTaxonNodeUuidByTaxonUuid(classificationUuid, taxonUuid) ;
176
       if (taxonNodeUuid == null){
177
           HttpStatusMessage.UUID_NOT_FOUND.send(response);
178
           return null;
179
       }
180
       Pager<TaxonNodeDto> pager = taxonNodeService.pageChildNodesDTOs(taxonNodeUuid, recursive, includeUnpublished,
181
               doSynonyms, sortMode, pageSize, pageIndex);
182
//       service.commitTransaction()
183

    
184
       return pager;
185
   }
186

    
187

    
188
    /**
189
     * @param classificationUuid
190
     * @param response
191
     * @return
192
     * @throws IOException
193
     */
194
    @RequestMapping(
195
            value = {"groupedTaxa"},
196
            method = RequestMethod.GET)
197
    public List<GroupedTaxonDTO> getGroupedTaxaByHigherTaxon(
198
            @PathVariable("uuid") UUID classificationUuid,
199
            @RequestParam(value = "taxonUuids", required = true) UuidList taxonUuids,
200
            @RequestParam(value = "minRankUuid", required = false) UUID minRankUuid,
201
            @RequestParam(value = "maxRankUuid", required = false) UUID maxRankUuid,
202

    
203
            HttpServletRequest request,
204
            HttpServletResponse response
205
            ) throws IOException {
206

    
207
        logger.info("getGroupedTaxaByHigherTaxon() - " + request.getRequestURI());
208

    
209
        Classification classification = service.find(classificationUuid);
210
        if(classification == null) {
211
            response.sendError(404 , "Classification not found using " + classificationUuid );
212
            return null;
213
        }
214

    
215
        Rank minRank = findRank(minRankUuid);
216
        Rank maxRank = findRank(maxRankUuid);
217

    
218
//        long start = System.currentTimeMillis();
219
        List<GroupedTaxonDTO> result = service.groupTaxaByHigherTaxon(taxonUuids, classificationUuid, minRank, maxRank);
220
//        System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
221

    
222
        return result;
223
    }
224

    
225
    /**
226
     * @param classificationUuid
227
     * @param response
228
     * @return
229
     * @throws IOException
230
     */
231
    @RequestMapping(
232
            value = {"groupedTaxaByMarker"},
233
            method = RequestMethod.GET)
234
    public List<GroupedTaxonDTO> getGroupedTaxaByMarkedParents(
235
            @PathVariable("uuid") UUID classificationUuid,
236
            @RequestParam(value = "taxonUuids", required = true) UuidList taxonUuids,
237
            @RequestParam(value = "markerTypeUuid", required = false) UUID markerTypeUuid,
238
            @RequestParam(value = "flag", required = false) Boolean flag,
239

    
240
            HttpServletRequest request,
241
            HttpServletResponse response
242
            ) throws IOException {
243

    
244
        logger.info("getGroupedTaxaByHigherTaxon() - " + request.getRequestURI());
245

    
246
        Classification classification = service.find(classificationUuid);
247
        if(classification == null) {
248
            response.sendError(404 , "Classification not found using " + classificationUuid );
249
            return null;
250
        }
251

    
252
        MarkerType markerType = findMarkerType(markerTypeUuid);
253
//        long start = System.currentTimeMillis();
254
        List<GroupedTaxonDTO> result = service.groupTaxaByMarkedParents(taxonUuids, classificationUuid, markerType, flag);
255
//        System.err.println("service.listRankSpecificRootNodes() " + (System.currentTimeMillis() - start));
256

    
257
        return result;
258
    }
259

    
260

    
261
    private Rank findRank(UUID rankUuid) {
262
        Rank rank = null;
263
        if(rankUuid != null){
264
            DefinedTermBase<?> definedTermBase =  termService.find(rankUuid);
265
            if(definedTermBase instanceof Rank){
266
                rank = (Rank) definedTermBase;
267
            } else {
268
               throw new IllegalArgumentException("DefinedTermBase is not a Rank");
269
            }
270
        }
271
        return rank;
272
    }
273

    
274
    private MarkerType findMarkerType(UUID markerTypeUuid) {
275
        MarkerType markerType = null;
276
        if(markerTypeUuid != null){
277
            DefinedTermBase<?> definedTermBase =  termService.find(markerTypeUuid);
278
            if(definedTermBase instanceof MarkerType){
279
                markerType = (MarkerType) definedTermBase;
280
            } else {
281
               throw new IllegalArgumentException("DefinedTermBase is not a MarkerType");
282
            }
283
        }
284
        return markerType;
285
    }
286

    
287

    
288
   @RequestMapping(
289
           value = {"taxonInContext/{taxonUuid}"},
290
           method = RequestMethod.GET)
291
   public TaxonInContextDTO getTaxonInContext(
292
           @PathVariable("uuid") UUID classificationUuid,
293
           @PathVariable("taxonUuid") UUID taxonUuid,
294
           @RequestParam(value = "doChildren", defaultValue = "false") Boolean doChildren,
295
           @RequestParam(value = "doSynonyms", defaultValue = "false") Boolean doSynonyms,
296
           @RequestParam(value = "sortMode", defaultValue = DEFAULT_TAXONNODEDTO_SORT_MODE) TaxonNodeSortMode sortMode,
297
           @RequestParam(value = "ancestorMarker", required = false) List<UUID> ancestorMarkers,
298
           HttpServletResponse response
299
           ) throws IOException {
300

    
301
       try {
302
           boolean includeUnpublished = NO_UNPUBLISHED;  //for now we do not allow any remote service to publish unpublished data
303
           TaxonInContextDTO taxonInContextDTO = service.getTaxonInContext(classificationUuid, taxonUuid, doChildren, includeUnpublished,
304
                   doSynonyms, ancestorMarkers, sortMode);
305
           return taxonInContextDTO;
306
       } catch (EntityNotFoundException e) {
307
           HttpStatusMessage.UUID_NOT_FOUND.send(response);
308
           return null;
309
       }
310
   }
311

    
312
   @RequestMapping(value = { "classificationRootNode" }, method = RequestMethod.GET)
313
   public ModelAndView getClassificationRootNode(
314
           @PathVariable("uuid") UUID uuid,
315
           @SuppressWarnings("unused") HttpServletRequest request,
316
           @SuppressWarnings("unused") HttpServletResponse response) {
317

    
318
       ModelAndView mv = new ModelAndView();
319
       mv.addObject(service.getRootNode(uuid));
320
       return mv;
321
   }
322

    
323

    
324
}
(12-12/76)