Project

General

Profile

Download (16.8 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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

    
10
package eu.etaxonomy.cdm.remote.controller;
11

    
12
import java.io.IOException;
13
import java.util.ArrayList;
14
import java.util.Arrays;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Set;
18
import java.util.UUID;
19

    
20
import javax.persistence.EntityNotFoundException;
21
import javax.servlet.http.HttpServletRequest;
22
import javax.servlet.http.HttpServletResponse;
23

    
24
import org.apache.log4j.Logger;
25
import org.springframework.beans.factory.annotation.Autowired;
26
import org.springframework.stereotype.Controller;
27
import org.springframework.web.bind.WebDataBinder;
28
import org.springframework.web.bind.annotation.PathVariable;
29
import org.springframework.web.bind.annotation.RequestMapping;
30
import org.springframework.web.bind.annotation.RequestMethod;
31
import org.springframework.web.bind.annotation.RequestParam;
32
import org.springframework.web.servlet.ModelAndView;
33

    
34
import eu.etaxonomy.cdm.api.service.IDescriptionService;
35
import eu.etaxonomy.cdm.api.service.INameService;
36
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
37
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
38
import eu.etaxonomy.cdm.api.service.ITaxonService;
39
import eu.etaxonomy.cdm.api.service.ITermService;
40
import eu.etaxonomy.cdm.api.service.config.IncludedTaxonConfiguration;
41
import eu.etaxonomy.cdm.api.service.dto.IncludedTaxaDTO;
42
import eu.etaxonomy.cdm.api.service.pager.Pager;
43
import eu.etaxonomy.cdm.model.common.MarkerType;
44
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
45
import eu.etaxonomy.cdm.model.description.TaxonDescription;
46
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
47
import eu.etaxonomy.cdm.model.taxon.Classification;
48
import eu.etaxonomy.cdm.model.taxon.Taxon;
49
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
50
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
51
import eu.etaxonomy.cdm.model.taxon.TaxonNodeAgentRelation;
52
import eu.etaxonomy.cdm.persistence.query.OrderHint;
53
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
54
import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
55
import eu.etaxonomy.cdm.remote.editor.TermBasePropertyEditor;
56
import io.swagger.annotations.Api;
57

    
58
/**
59
 * TODO write controller documentation
60
 *
61
 * @author a.kohlbecker
62
 * @since 20.07.2009
63
 *
64
 */
65
@Controller
66
@Api("taxon")
67
@RequestMapping(value = {"/taxon/{uuid}"})
68
public class TaxonController extends AbstractIdentifiableController<TaxonBase, ITaxonService>
69
{
70
    public static final Logger logger = Logger.getLogger(TaxonController.class);
71

    
72
    @Autowired
73
    private IOccurrenceService occurrenceService;
74

    
75
    @Autowired
76
    private INameService nameService;
77

    
78
    @Autowired
79
    private ITaxonNodeService nodeService;
80

    
81
    @Autowired
82
    private IDescriptionService descriptionService;
83

    
84
    @Autowired
85
    private ITermService termService;
86

    
87
    protected static final List<String> TAXONNODE_INIT_STRATEGY = Arrays.asList(new String []{
88
            "taxonNodes"
89
    });
90

    
91
    public TaxonController(){
92
        super();
93
        setInitializationStrategy(Arrays.asList(new String[]{
94
                "$",
95
                "name.nomenclaturalReference"
96
                }
97
        ));
98
    }
99

    
100
    @Override
101
    @Autowired
102
    public void setService(ITaxonService service) {
103
        this.service = service;
104
    }
105

    
106
    /**
107
     * {@inheritDoc}
108
     */
109
    @Override
110
    public void initBinder(WebDataBinder binder) {
111
        super.initBinder(binder);
112
        binder.registerCustomEditor(MarkerType.class, new TermBasePropertyEditor<MarkerType>(termService));
113
    }
114

    
115
    protected List<String> getTaxonDescriptionInitStrategy() {
116
        return getInitializationStrategy();
117
    }
118

    
119
    protected List<String> getTaxonDescriptionElementInitStrategy() {
120
        return getInitializationStrategy();
121
    }
122

    
123
    /**
124
     * Get the set of accepted {@link Taxon} entities for a given
125
     * {@link TaxonBase} entity identified by the <code>{taxon-uuid}</code>.
126
     * <p>
127
     * URI: <b>&#x002F;{datasource-name}&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;accepted</b>
128
     *
129
     * @param request
130
     * @param response
131
     * @return a set on a list of {@link Taxon} entities which are initialized
132
     *         using the following initialization strategy:
133
     *         {@link #DEFAULT_INIT_STRATEGY}
134
     * @throws IOException
135
     */
136
    @RequestMapping(value = "accepted", method = RequestMethod.GET)
137
    public Taxon getAcceptedFor(
138
            @PathVariable("uuid") UUID uuid,
139
            @RequestParam(value = "classificationFilter", required = false) UUID classification_uuid,
140
            HttpServletRequest request,
141
            HttpServletResponse response)
142
            throws IOException {
143
        if(request != null){
144
            logger.info("getAcceptedFor() " + requestPathAndQuery(request));
145
        }
146

    
147
        Taxon result = null;
148
        try {
149
            result = service.findAcceptedTaxonFor(uuid, classification_uuid, getInitializationStrategy());
150
        } catch (EntityNotFoundException e){
151
            HttpStatusMessage.UUID_NOT_FOUND.send(response);
152
        }
153

    
154
        return result;
155
    }
156

    
157
    @RequestMapping(value = "classifications", method = RequestMethod.GET)
158
    public List<Classification> doGetClassifications(
159
            @PathVariable("uuid") UUID uuid,
160
            HttpServletRequest request,
161
            HttpServletResponse response) throws IOException {
162
        logger.info("doGetClassifications(): " + request.getRequestURI());
163
        TaxonBase<?> taxonBase = service.load(uuid);
164

    
165
        if (taxonBase == null){
166
            HttpStatusMessage.UUID_NOT_FOUND.send(response);
167
        }
168

    
169
        return service.listClassifications(taxonBase, null, null, getInitializationStrategy());
170
    }
171

    
172
    @RequestMapping(value = "taxonNodes", method = RequestMethod.GET)
173
    public Set<TaxonNode>  doGetTaxonNodes(
174
            @PathVariable("uuid") UUID uuid,
175
            HttpServletRequest request,
176
            HttpServletResponse response) throws IOException {
177

    
178
        TaxonBase<?> tb = service.load(uuid, TAXONNODE_INIT_STRATEGY);
179
        if(tb instanceof Taxon){
180
            return ((Taxon)tb).getTaxonNodes();
181
        } else {
182
            HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
183
            return null;
184
        }
185
    }
186

    
187
    /**
188
    *
189
    * See also {@link AgentController#doGetTaxonNodeAgentRelations(UUID, UUID, Integer, Integer, HttpServletRequest, HttpServletResponse)}
190
    *
191
    * @param uuid
192
    * @param classificationUuid
193
    * @param pageNumber
194
    * @param pageSize
195
    * @param request
196
    * @param response
197
    * @return
198
    * @throws IOException
199
    *
200
    */
201
    @RequestMapping(value = "taxonNodeAgentRelations/{classification_uuid}", method = RequestMethod.GET)
202
    public Pager<TaxonNodeAgentRelation>  doGetTaxonNodeAgentRelations(
203
            @PathVariable("uuid") UUID uuid,
204
            @PathVariable("classification_uuid") UUID classificationUuid,
205
            @RequestParam(value = "relType_uuid" , required = false) UUID relTypeUuid,
206
            @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
207
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
208
            HttpServletRequest request,
209
            HttpServletResponse response) throws IOException {
210

    
211
        PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
212
        pagerParams.normalizeAndValidate(response);
213

    
214
        Pager<TaxonNodeAgentRelation> pager = nodeService.pageTaxonNodeAgentRelations(uuid, classificationUuid,
215
                null, null, relTypeUuid, pagerParams.getPageSize(), pagerParams.getPageIndex(), null);
216
        return pager;
217
    }
218

    
219

    
220
    @RequestMapping(value = "specimensOrObservations", method = RequestMethod.GET)
221
    public ModelAndView doListSpecimensOrObservations(
222
            @PathVariable("uuid") UUID uuid,
223
            HttpServletRequest request,
224
            HttpServletResponse response) throws IOException {
225
        logger.info("doListSpecimensOrObservations() - " + request.getRequestURI());
226

    
227
        ModelAndView mv = new ModelAndView();
228

    
229
        TaxonBase<?> tb = service.load(uuid);
230

    
231
        List<OrderHint> orderHints = new ArrayList<OrderHint>();
232
        orderHints.add(new OrderHint("titleCache", SortOrder.DESCENDING));
233

    
234
        if(tb instanceof Taxon){
235
            List<SpecimenOrObservationBase<?>> specimensOrObservations = occurrenceService.listByAssociatedTaxon(null, null, (Taxon)tb, null, null, null, orderHints, null);
236
            mv.addObject(specimensOrObservations);
237
        } else {
238
            HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
239
            return null;
240
        }
241

    
242
        return mv;
243
    }
244

    
245
    @RequestMapping(value = "associatedFieldUnits", method = RequestMethod.GET)
246
    public Pager<SpecimenOrObservationBase> doGetFieldUnits(
247
            @PathVariable("uuid") UUID uuid,
248
            @RequestParam(value = "maxDepth", required = false) Integer maxDepth,
249
            @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
250
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
251
            HttpServletRequest request,
252
            HttpServletResponse response) throws IOException {
253
        logger.info("doGetFieldUnits() - " + request.getRequestURI());
254

    
255
        ModelAndView mv = new ModelAndView();
256

    
257
        TaxonBase<?> tb = service.load(uuid);
258

    
259
        List<OrderHint> orderHints = new ArrayList<OrderHint>();
260
        orderHints.add(new OrderHint("titleCache", SortOrder.ASCENDING));
261

    
262
        if(tb instanceof Taxon){
263
            PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
264
            pagerParams.normalizeAndValidate(response);
265

    
266
            return occurrenceService.pageFieldUnitsByAssociatedTaxon(null, (Taxon) tb, null, pagerParams.getPageSize(), pagerParams.getPageIndex(), orderHints, null);
267
        }
268
        return null;
269
    }
270

    
271
    @RequestMapping(value = "taggedName", method = RequestMethod.GET)
272
    public ModelAndView doGetTaggedName(
273
            @PathVariable("uuid") UUID uuid,
274
            HttpServletRequest request) {
275
        logger.info("doGetDescriptionElementsByType() - " + request.getRequestURI());
276

    
277
        ModelAndView mv = new ModelAndView();
278

    
279
        TaxonBase<?> tb = service.load(uuid, Arrays.asList(new String[] {"name"}));
280
        mv.addObject(nameService.getTaggedName(tb.getName().getUuid()));
281
        return mv;
282
    }
283

    
284
    /**
285
     * This webservice endpoint returns all taxa which are congruent or included in the taxon represented by the given taxon uuid.
286
     * The result also returns the path to these taxa represented by the uuids of the taxon relationships types and doubtful information.
287
     * If classificationUuids is set only taxa of classifications are returned which are included in the given classifications.
288
     * Also the path to these taxa may not include taxa from other classifications.
289
     *
290
     * @param taxonUUIDString
291
     * @param classificationStringList
292
     * @param includeDoubtful
293
     * @param onlyCongruent
294
     * @param response
295
     * @param request
296
     * @return
297
     * @throws IOException
298
     */
299

    
300
    @RequestMapping(value = { "includedTaxa" }, method = { RequestMethod.GET })
301
    public ModelAndView doGetIncludedTaxa(
302
            @PathVariable("uuid") UUID uuid,
303
            @RequestParam(value="classificationFilter", required=false) final List<String> classificationStringList,
304
            @RequestParam(value="includeDoubtful", required=false) final boolean includeDoubtful,
305
            @RequestParam(value="onlyCongruent", required=false) final boolean onlyCongruent,
306
            HttpServletResponse response,
307
            HttpServletRequest request) throws IOException {
308
            ModelAndView mv = new ModelAndView();
309
            /**
310
             * List<UUID> classificationFilter,
311
             * boolean includeDoubtful,
312
             * boolean onlyCongruent)
313
             */
314
            List<UUID> classificationFilter = null;
315
            if( classificationStringList != null ){
316
                classificationFilter = new ArrayList<UUID>();
317
                for(String classString :classificationStringList){
318
                    classificationFilter.add(UUID.fromString(classString));
319
                }
320
            }
321
            final IncludedTaxonConfiguration configuration =
322
                    new IncludedTaxonConfiguration(classificationFilter, includeDoubtful, onlyCongruent);
323
            IncludedTaxaDTO listIncludedTaxa = service.listIncludedTaxa(uuid, configuration);
324
            mv.addObject(listIncludedTaxa);
325
            return mv;
326
    }
327

    
328
    // TODO ================================================================================ //
329
    // move all description and descriptionElement related methods into the according
330
    // Description Controllers
331

    
332
    /**
333
     * Get the list of {@link TaxonDescription}s of the
334
     * {@link Taxon} instance identified by the <code>{taxon-uuid}</code>.
335
     * <p>
336
     * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;descriptions</b>
337
     *
338
     * @param request
339
     * @param response
340
     * @return a List of {@link TaxonDescription} entities which are initialized
341
     *         using the following initialization strategy:
342
     *         {@link #TAXONDESCRIPTION_INIT_STRATEGY}
343
     * @throws IOException
344
     */
345
    @RequestMapping(
346
            value = {"descriptions"},
347
            method = RequestMethod.GET)
348
    public Pager<TaxonDescription> doGetDescriptions(
349
            @PathVariable("uuid") UUID uuid,
350
            @RequestParam(value = "markerTypes", required = false) List<MarkerType> markerTypes,
351
            HttpServletRequest request,
352
            HttpServletResponse response)throws IOException {
353

    
354
        if(request != null){
355
            logger.info("doGetDescriptions()" + requestPathAndQuery(request));
356
        }
357

    
358
        Taxon t = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
359
        Set<MarkerType> markerTypesSet = new HashSet<MarkerType>();
360
        if (markerTypes != null) {
361
            markerTypesSet.addAll(markerTypes);
362
        }
363

    
364
        Pager<TaxonDescription> p = descriptionService.pageTaxonDescriptions(t, null, null, markerTypesSet, null, null, getTaxonDescriptionInitStrategy());
365

    
366
        return p;
367
    }
368

    
369
    @RequestMapping(value = "descriptions/elementsByType/{classSimpleName}", method = RequestMethod.GET)
370
    public ModelAndView doGetDescriptionElementsByType(
371
            @PathVariable("uuid") UUID uuid,
372
            @PathVariable("classSimpleName") String classSimpleName,
373
            @RequestParam(value = "markerTypes", required = false) List<MarkerType> markerTypes,
374
            @RequestParam(value = "count", required = false, defaultValue = "false") Boolean doCount,
375
            HttpServletRequest request,
376
            HttpServletResponse response) throws IOException {
377
        logger.info("doGetDescriptionElementsByType() - " + requestPathAndQuery(request));
378

    
379
        ModelAndView mv = new ModelAndView();
380

    
381
        List<DescriptionElementBase> allElements = new ArrayList<DescriptionElementBase>();
382
        List<DescriptionElementBase> elements;
383
        int count = 0;
384

    
385
        List<String> initStrategy = doCount ? null : getTaxonDescriptionElementInitStrategy();
386

    
387
        Taxon t = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
388

    
389
        Set<MarkerType> markerTypesSet = new HashSet<MarkerType>();
390
        if (markerTypes == null) {
391
            markerTypesSet.addAll(markerTypes);
392
        }
393

    
394
        List<TaxonDescription> taxonDescriptions = descriptionService.listTaxonDescriptions(t, null, null, markerTypesSet, null, null, null);
395
        try {
396
            Class type;
397
            type = Class.forName("eu.etaxonomy.cdm.model.description."
398
                    + classSimpleName);
399
            if (taxonDescriptions != null) {
400
                for (TaxonDescription description : taxonDescriptions) {
401
                    elements = descriptionService.listDescriptionElements(description, null, type, null, 0, initStrategy);
402
                    allElements.addAll(elements);
403
                    count += elements.size();
404
                }
405

    
406
            }
407
        } catch (ClassNotFoundException e) {
408
            HttpStatusMessage.create(e.getLocalizedMessage(), 400).send(response);
409
        }
410
        if(doCount){
411
            mv.addObject(count);
412
        } else {
413
            mv.addObject(allElements);
414
        }
415
        return mv;
416
    }
417

    
418
    // TODO ================================================================================ //
419

    
420
}
(56-56/67)