Project

General

Profile

Download (27.3 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.Hashtable;
16
import java.util.Iterator;
17
import java.util.List;
18
import java.util.Map;
19
import java.util.Set;
20
import java.util.UUID;
21

    
22
import javax.servlet.http.HttpServletRequest;
23
import javax.servlet.http.HttpServletResponse;
24

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

    
36
import eu.etaxonomy.cdm.api.service.IClassificationService;
37
import eu.etaxonomy.cdm.api.service.INameService;
38
import eu.etaxonomy.cdm.api.service.ITaxonService;
39
import eu.etaxonomy.cdm.api.service.ITermService;
40
import eu.etaxonomy.cdm.api.service.util.TaxonRelationshipEdge;
41
import eu.etaxonomy.cdm.database.UpdatableRoutingDataSource;
42
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
43
import eu.etaxonomy.cdm.model.location.NamedArea;
44
import eu.etaxonomy.cdm.model.media.Media;
45
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
46
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
47
import eu.etaxonomy.cdm.model.media.MediaUtils;
48
import eu.etaxonomy.cdm.model.name.NameRelationship;
49
import eu.etaxonomy.cdm.model.taxon.Synonym;
50
import eu.etaxonomy.cdm.model.taxon.Taxon;
51
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
52
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
53
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
54
import eu.etaxonomy.cdm.persistence.query.MatchMode;
55
import eu.etaxonomy.cdm.remote.controller.util.ControllerUtils;
56
import eu.etaxonomy.cdm.remote.editor.CdmTypePropertyEditor;
57
import eu.etaxonomy.cdm.remote.editor.DefinedTermBaseList;
58
import eu.etaxonomy.cdm.remote.editor.MatchModePropertyEditor;
59
import eu.etaxonomy.cdm.remote.editor.NamedAreaPropertyEditor;
60
import eu.etaxonomy.cdm.remote.editor.TermBaseListPropertyEditor;
61
import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
62
import eu.etaxonomy.cdm.remote.editor.UuidList;
63
import io.swagger.annotations.Api;
64

    
65
/**
66
 * The TaxonPortalController class is a Spring MVC Controller.
67
 * <p>
68
 * The syntax of the mapped service URIs contains the the {datasource-name} path element.
69
 * The available {datasource-name}s are defined in a configuration file which
70
 * is loaded by the {@link UpdatableRoutingDataSource}. If the
71
 * UpdatableRoutingDataSource is not being used in the actual application
72
 * context any arbitrary {datasource-name} may be used.
73
 * <p>
74
 * Methods mapped at type level, inherited from super classes ({@link BaseController}):
75
 * <blockquote>
76
 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}</b>
77
 *
78
 * Get the {@link TaxonBase} instance identified by the <code>{taxon-uuid}</code>.
79
 * The returned Taxon is initialized by
80
 * the following strategy {@link #TAXON_INIT_STRATEGY}
81
 * </blockquote>
82
 *
83
 * @author a.kohlbecker
84
 * @since 20.07.2009
85
 *
86
 */
87
@Controller
88
@Api("portal_taxon")
89
@RequestMapping(value = {"/portal/taxon/{uuid}"})
90
public class TaxonPortalController extends TaxonController{
91

    
92
    public static final Logger logger = Logger.getLogger(TaxonPortalController.class);
93

    
94
    @Autowired
95
    private INameService nameService;
96

    
97
    @Autowired
98
    private IClassificationService classificationService;
99

    
100
    @Autowired
101
    private ITaxonService taxonService;
102

    
103
    @Autowired
104
    private ITermService termService;
105

    
106
    private static final List<String> TAXON_INIT_STRATEGY = Arrays.asList(new String []{
107
            "$",
108
            "sources",
109
            // taxon relations
110
//            "relationsToThisName.fromTaxon.name",
111
            // the name
112
            "name.$",
113
            "name.nomenclaturalReference.authorship",
114
            "name.nomenclaturalReference.inReference",
115
            "name.rank.representations",
116
            "name.status.type.representations",
117

    
118
//            "descriptions" // TODO remove
119

    
120
            });
121

    
122
    private static final List<String> TAXON_WITH_NODES_INIT_STRATEGY = Arrays.asList(new String []{
123
            "taxonNodes.$",
124
            "taxonNodes.classification.$",
125
            "taxonNodes.childNodes.$"
126
            });
127

    
128
    private static final List<String> SIMPLE_TAXON_INIT_STRATEGY = Arrays.asList(new String []{
129
            "$",
130
            // the name
131
            "name.$",
132
            "name.rank.representations",
133
            "name.status.type.representations",
134
            "name.nomenclaturalReference.authorship",
135
            "name.nomenclaturalReference.inReference",
136
            "taxonNodes.classification",
137
            });
138

    
139
    private static final List<String> SYNONYMY_INIT_STRATEGY = Arrays.asList(new String []{
140
            // initialize homotypical and heterotypical groups; needs synonyms
141
            "synonyms.$",
142
            "synonyms.name.status.type.representations",
143
            "synonyms.name.nomenclaturalReference.authorship",
144
            "synonyms.name.nomenclaturalReference.inReference",
145
//            "synonyms.name.homotypicalGroup.typifiedNames.$",
146
//            "synonyms.name.homotypicalGroup.typifiedNames.taxonBases.$",
147
            "synonyms.name.combinationAuthorship.$",
148

    
149
            "name.typeDesignations",
150

    
151
            "name.homotypicalGroup.$",
152
            "name.homotypicalGroup.typifiedNames.$",
153
            "name.homotypicalGroup.typifiedNames.nomenclaturalReference.authorship",
154
            "name.homotypicalGroup.typifiedNames.nomenclaturalReference.inReference",
155
//            "name.homotypicalGroup.typifiedNames.taxonBases.$"
156
    });
157

    
158

    
159
    private static final List<String> TAXONRELATIONSHIP_INIT_STRATEGY = Arrays.asList(new String []{
160
            "$",
161
            "type.inverseRepresentations",
162
            "fromTaxon.sec",
163
            "fromTaxon.name",
164
            "toTaxon.sec",
165
            "toTaxon.name"
166
    });
167

    
168
    private static final List<String> NAMERELATIONSHIP_INIT_STRATEGY = Arrays.asList(new String []{
169
            "$",
170
            "type.inverseRepresentations",
171
            "toName.$",
172
            "toName.nomenclaturalReference.authorship",
173
            "toName.nomenclaturalReference.inReference",
174
            "fromName.$",
175
            "fromName.nomenclaturalReference.authorship",
176
            "fromName.nomenclaturalReference.inReference",
177

    
178
    });
179

    
180
    protected static final List<String> TAXONDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String [] {
181
            "$",
182
            "elements.$",
183
            "elements.stateData.$",
184
            "elements.sources.citation.authorship",
185
            "elements.sources.nameUsedInSource",
186
            "elements.multilanguageText",
187
            "elements.media",
188
            "elements.modifyingText",
189
            "elements.modifiers",
190
            "elements.kindOfUnit",
191
            "name.$",
192
            "name.rank.representations",
193
            "name.status.type.representations",
194
            "sources.$",
195
    });
196

    
197
    protected static final List<String> DESCRIPTION_ELEMENT_INIT_STRATEGY = Arrays.asList(new String []{
198
            "$",
199
            "sources.citation.authorship",
200
            "sources.nameUsedInSource",
201
            "multilanguageText",
202
            "media",
203
    });
204

    
205

    
206
//	private static final List<String> NAMEDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
207
//			"uuid",
208
//			"feature",
209
//			"elements.$",
210
//			"elements.multilanguageText",
211
//			"elements.media",
212
//	});
213

    
214
    protected static final List<String> TAXONDESCRIPTION_MEDIA_INIT_STRATEGY = Arrays.asList(new String []{
215
            "elements.media"
216

    
217
    });
218

    
219
    private static final List<String> TYPEDESIGNATION_INIT_STRATEGY = Arrays.asList(new String []{
220
            "typeSpecimen.$",
221
            "citation.authorship.$",
222
            "typeName",
223
            "typeStatus"
224
    });
225

    
226
    protected static final List<String> TAXONNODE_WITHTAXON_INIT_STRATEGY = Arrays.asList(new String []{
227
            "childNodes.taxon",
228
    });
229

    
230
    protected static final List<String> TAXONNODE_INIT_STRATEGY = Arrays.asList(new String []{
231
            "taxonNodes.classification"
232
    });
233

    
234

    
235

    
236
    private static final String featureTreeUuidPattern = "^/taxon(?:(?:/)([^/?#&\\.]+))+.*";
237

    
238

    
239
    public TaxonPortalController(){
240
        super();
241
        setInitializationStrategy(TAXON_INIT_STRATEGY);
242
    }
243

    
244
    /* (non-Javadoc)
245
     * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
246
     */
247
    @Autowired
248
    @Override
249
    public void setService(ITaxonService service) {
250
        this.service = service;
251
    }
252

    
253
    @InitBinder
254
    @Override
255
    public void initBinder(WebDataBinder binder) {
256
        super.initBinder(binder);
257
        binder.registerCustomEditor(NamedArea.class, new NamedAreaPropertyEditor());
258
        binder.registerCustomEditor(MatchMode.class, new MatchModePropertyEditor());
259
        binder.registerCustomEditor(Class.class, new CdmTypePropertyEditor());
260
        binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
261
        binder.registerCustomEditor(DefinedTermBaseList.class, new TermBaseListPropertyEditor<NamedArea>(termService));
262

    
263
    }
264

    
265

    
266
    /* (non-Javadoc)
267
     * @see eu.etaxonomy.cdm.remote.controller.BaseController#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
268

    
269
    @Override
270
    @RequestMapping(method = RequestMethod.GET)
271
    public TaxonBase doGet(HttpServletRequest request, HttpServletResponse response)throws IOException {
272
        logger.info("doGet()");
273
        TaxonBase tb = getCdmBase(request, response, TAXON_INIT_STRATEGY, TaxonBase.class);
274
        return tb;
275
    }
276
     */
277

    
278

    
279

    
280
    /**
281
     * Get the synonymy for a taxon identified by the <code>{taxon-uuid}</code>.
282
     * The synonymy consists
283
     * of two parts: The group of homotypic synonyms of the taxon and the
284
     * heterotypic synonymy groups of the taxon. The synonymy is ordered
285
     * historically by the type designations and by the publication date of the
286
     * nomenclatural reference
287
     * <p>
288
     * URI:
289
     * <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;synonymy</b>
290
     *
291
     *
292
     * @param request
293
     * @param response
294
     * @return a Map with to entries which are mapped by the following keys:
295
     *         "homotypicSynonymsByHomotypicGroup", "heterotypicSynonymyGroups",
296
     *         containing lists of {@link Synonym}s which are initialized using the
297
     *         following initialization strategy: {@link #SYNONYMY_INIT_STRATEGY}
298
     *
299
     * @throws IOException
300
     */
301
    @RequestMapping(
302
            value = {"synonymy"},
303
            method = RequestMethod.GET)
304
    public ModelAndView doGetSynonymy(@PathVariable("uuid") UUID uuid,
305
            HttpServletRequest request, HttpServletResponse response)throws IOException {
306

    
307
        if(request != null){
308
            logger.info("doGetSynonymy() " + requestPathAndQuery(request));
309
        }
310
        ModelAndView mv = new ModelAndView();
311
        Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
312
        Map<String, List<?>> synonymy = new Hashtable<>();
313

    
314
        //new
315
        List<List<Synonym>> synonymyGroups = service.getSynonymsByHomotypicGroup(taxon, SYNONYMY_INIT_STRATEGY);
316
        synonymy.put("homotypicSynonymsByHomotypicGroup", synonymyGroups.get(0));
317
        synonymyGroups.remove(0);
318
        synonymy.put("heterotypicSynonymyGroups", synonymyGroups);
319

    
320

    
321

    
322
        //old
323
//        synonymy.put("homotypicSynonymsByHomotypicGroup", service.getHomotypicSynonymsByHomotypicGroup(taxon, SYNONYMY_INIT_STRATEGY));
324
//        synonymy.put("heterotypicSynonymyGroups", service.getHeterotypicSynonymyGroups(taxon, SYNONYMY_INIT_STRATEGY));
325

    
326

    
327
        mv.addObject(synonymy);
328
        return mv;
329
    }
330

    
331

    
332
    /**
333
     * {@inheritDoc}
334
     */
335
    @Override
336
    protected List<String> getTaxonDescriptionInitStrategy() {
337
        return TAXONDESCRIPTION_INIT_STRATEGY;
338
    }
339

    
340
    @Override
341
    protected List<String> getTaxonDescriptionElementInitStrategy() {
342
        return DESCRIPTION_ELEMENT_INIT_STRATEGY;
343
    }
344

    
345
    /**
346
     * Get the list of {@link TaxonRelationship}s for the given
347
     * {@link TaxonBase} instance identified by the <code>{taxon-uuid}</code>.
348
     * <p>
349
     * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;taxonRelationships</b>
350
     *
351
     * @param request
352
     * @param response
353
     * @return a List of {@link TaxonRelationship} entities which are initialized
354
     *         using the following initialization strategy:
355
     *         {@link #TAXONRELATIONSHIP_INIT_STRATEGY}
356
     * @throws IOException
357
     */
358
    @RequestMapping(
359
            value = {"taxonRelationships"},
360
            method = RequestMethod.GET)
361
    public List<TaxonRelationship> doGetTaxonRelations(@PathVariable("uuid") UUID uuid,
362
            HttpServletRequest request, HttpServletResponse response)throws IOException {
363

    
364
        logger.info("doGetTaxonRelations()" + requestPathAndQuery(request));
365
        Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
366
        List<TaxonRelationship> toRelationships = service.listToTaxonRelationships(taxon, null, null, null, null, TAXONRELATIONSHIP_INIT_STRATEGY);
367
        List<TaxonRelationship> fromRelationships = service.listFromTaxonRelationships(taxon, null, null, null, null, TAXONRELATIONSHIP_INIT_STRATEGY);
368

    
369
        List<TaxonRelationship> allRelationships = new ArrayList<TaxonRelationship>(toRelationships.size() + fromRelationships.size());
370
        allRelationships.addAll(toRelationships);
371
        allRelationships.addAll(fromRelationships);
372

    
373
        return allRelationships;
374
    }
375

    
376
    /**
377
     * Get the list of {@link NameRelationship}s of the Name associated with the
378
     * {@link TaxonBase} instance identified by the <code>{taxon-uuid}</code>.
379
     * <p>
380
     * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;nameRelationships</b>
381
     *
382
     * @param request
383
     * @param response
384
     * @return a List of {@link NameRelationship} entities which are initialized
385
     *         using the following initialization strategy:
386
     *         {@link #NAMERELATIONSHIP_INIT_STRATEGY}
387
     * @throws IOException
388
     */
389
    @RequestMapping(
390
            value = {"toNameRelationships"},
391
            method = RequestMethod.GET)
392
    public List<NameRelationship> doGetToNameRelations(@PathVariable("uuid") UUID uuid,
393
            HttpServletRequest request, HttpServletResponse response)throws IOException {
394
        logger.info("doGetNameRelations()" + request.getRequestURI());
395
        TaxonBase taxonBase = getCdmBaseInstance(TaxonBase.class, uuid, response, (List<String>)null);
396
        List<NameRelationship> list = nameService.listNameRelationships(taxonBase.getName(), Direction.relatedTo, null, null, 0, null, NAMERELATIONSHIP_INIT_STRATEGY);
397
        //List<NameRelationship> list = nameService.listToNameRelationships(taxonBase.getName(), null, null, null, null, NAMERELATIONSHIP_INIT_STRATEGY);
398
        return list;
399
    }
400

    
401
    /**
402
     * Get the list of {@link NameRelationship}s of the Name associated with the
403
     * {@link TaxonBase} instance identified by the <code>{taxon-uuid}</code>.
404
     * <p>
405
     * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;nameRelationships</b>
406
     *
407
     * @param request
408
     * @param response
409
     * @return a List of {@link NameRelationship} entities which are initialized
410
     *         using the following initialization strategy:
411
     *         {@link #NAMERELATIONSHIP_INIT_STRATEGY}
412
     * @throws IOException
413
     */
414
    @RequestMapping(
415
            value = {"fromNameRelationships"},
416
            method = RequestMethod.GET)
417
    public List<NameRelationship> doGetFromNameRelations(@PathVariable("uuid") UUID uuid,
418
            HttpServletRequest request, HttpServletResponse response)throws IOException {
419
        logger.info("doGetNameFromNameRelations()" + requestPathAndQuery(request));
420

    
421
        TaxonBase taxonbase = getCdmBaseInstance(TaxonBase.class, uuid, response, SIMPLE_TAXON_INIT_STRATEGY);
422
        List<NameRelationship> list = nameService.listNameRelationships(taxonbase.getName(), Direction.relatedFrom, null, null, 0, null, NAMERELATIONSHIP_INIT_STRATEGY);
423
        //List<NameRelationship> list = nameService.listFromNameRelationships(taxonbase.getName(), null, null, null, null, NAMERELATIONSHIP_INIT_STRATEGY);
424
        return list;
425
    }
426

    
427
    @Override
428
    @RequestMapping(value = "taxonNodes", method = RequestMethod.GET)
429
    public Set<TaxonNode>  doGetTaxonNodes(
430
            @PathVariable("uuid") UUID uuid,
431
            HttpServletRequest request,
432
            HttpServletResponse response) throws IOException {
433
        logger.info("doGetTaxonNodes" + requestPathAndQuery(request));
434
        TaxonBase taxon = service.load(uuid, TAXONNODE_INIT_STRATEGY);
435
        if(taxon instanceof Taxon){
436
            return ((Taxon)taxon).getTaxonNodes();
437
        } else {
438
            HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
439
            return null;
440
        }
441
    }
442

    
443
//	@RequestMapping(value = "specimens", method = RequestMethod.GET)
444
//	public ModelAndView doGetSpecimens(
445
//			@PathVariable("uuid") UUID uuid,
446
//			HttpServletRequest request,
447
//			HttpServletResponse response) throws IOException, ClassNotFoundException {
448
//		logger.info("doGetSpecimens() - " + request.getRequestURI());
449
//
450
//		ModelAndView mv = new ModelAndView();
451
//
452
//		List<DerivedUnitFacade> derivedUnitFacadeList = new ArrayList<DerivedUnitFacade>();
453
//
454
//		// find speciemens in the TaxonDescriptions
455
//		List<TaxonDescription> taxonDescriptions = doGetDescriptions(uuid, request, response);
456
//		if (taxonDescriptions != null) {
457
//
458
//			for (TaxonDescription description : taxonDescriptions) {
459
//				derivedUnitFacadeList.addAll( occurrenceService.listDerivedUnitFacades(description, null) );
460
//			}
461
//		}
462
//		// TODO find speciemens in the NameDescriptions ??
463
//
464
//		// TODO also find type specimens
465
//
466
//		mv.addObject(derivedUnitFacadeList);
467
//
468
//		return mv;
469
//	}
470

    
471
    /**
472
     * Get the {@link Media} attached to the {@link Taxon} instance
473
     * identified by the <code>{taxon-uuid}</code>.
474
     *
475
     * Usage &#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-
476
     * uuid}&#x002F;media&#x002F;{mime type
477
     * list}&#x002F;{size}[,[widthOrDuration}][,{height}]&#x002F;
478
     *
479
     * Whereas
480
     * <ul>
481
     * <li><b>{mime type list}</b>: a comma separated list of mime types, in the
482
     * order of preference. The forward slashes contained in the mime types must
483
     * be replaced by a colon. Regular expressions can be used. Each media
484
     * associated with this given taxon is being searched whereas the first
485
     * matching mime type matching a representation always rules.</li>
486
     * <li><b>{size},{widthOrDuration},{height}</b>: <i>not jet implemented</i>
487
     * valid values are an integer or the asterisk '*' as a wildcard</li>
488
     * </ul>
489
     *
490
     * @param request
491
     * @param response
492
     * @return a List of {@link Media} entities which are initialized
493
     *         using the following initialization strategy:
494
     *         {@link #TAXONDESCRIPTION_INIT_STRATEGY}
495
     * @throws IOException
496
     */
497
    @RequestMapping(
498
        value = {"media"},
499
        method = RequestMethod.GET)
500
    public List<Media> doGetMedia(
501
            @PathVariable("uuid") UUID uuid,
502
            @RequestParam(value = "type", required = false) Class<? extends MediaRepresentationPart> type,
503
            @RequestParam(value = "mimeTypes", required = false) String[] mimeTypes,
504
            @RequestParam(value = "relationships", required = false) UuidList relationshipUuids,
505
            @RequestParam(value = "relationshipsInvers", required = false) UuidList relationshipInversUuids,
506
            @RequestParam(value = "includeTaxonDescriptions", required = true) Boolean  includeTaxonDescriptions,
507
            @RequestParam(value = "includeOccurrences", required = true) Boolean  includeOccurrences,
508
            @RequestParam(value = "includeTaxonNameDescriptions", required = true) Boolean  includeTaxonNameDescriptions,
509
            @RequestParam(value = "widthOrDuration", required = false) Integer  widthOrDuration,
510
            @RequestParam(value = "height", required = false) Integer height,
511
            @RequestParam(value = "size", required = false) Integer size,
512
            HttpServletRequest request, HttpServletResponse response) throws IOException {
513

    
514
        logger.info("doGetMedia() " + requestPathAndQuery(request));
515

    
516
        Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
517

    
518
        Set<TaxonRelationshipEdge> includeRelationships = ControllerUtils.loadIncludeRelationships(relationshipUuids, relationshipInversUuids, termService);
519

    
520
        List<Media> returnMedia = getMediaForTaxon(taxon, includeRelationships,
521
                includeTaxonDescriptions, includeOccurrences, includeTaxonNameDescriptions,
522
                type, mimeTypes, widthOrDuration, height, size);
523
        return returnMedia;
524
    }
525

    
526
    @RequestMapping(
527
            value = {"subtree/media"},
528
            method = RequestMethod.GET)
529
        public List<Media> doGetSubtreeMedia(
530
                @PathVariable("uuid") UUID uuid,
531
                @RequestParam(value = "type", required = false) Class<? extends MediaRepresentationPart> type,
532
                @RequestParam(value = "mimeTypes", required = false) String[] mimeTypes,
533
                @RequestParam(value = "relationships", required = false) UuidList relationshipUuids,
534
                @RequestParam(value = "relationshipsInvers", required = false) UuidList relationshipInversUuids,
535
                @RequestParam(value = "includeTaxonDescriptions", required = true) Boolean  includeTaxonDescriptions,
536
                @RequestParam(value = "includeOccurrences", required = true) Boolean  includeOccurrences,
537
                @RequestParam(value = "includeTaxonNameDescriptions", required = true) Boolean  includeTaxonNameDescriptions,
538
                @RequestParam(value = "widthOrDuration", required = false) Integer  widthOrDuration,
539
                @RequestParam(value = "height", required = false) Integer height,
540
                @RequestParam(value = "size", required = false) Integer size,
541
                HttpServletRequest request, HttpServletResponse response)throws IOException {
542

    
543
        logger.info("doGetSubtreeMedia() " + requestPathAndQuery(request));
544

    
545
        Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, TAXON_WITH_NODES_INIT_STRATEGY);
546

    
547
        Set<TaxonRelationshipEdge> includeRelationships = ControllerUtils.loadIncludeRelationships(relationshipUuids, relationshipInversUuids, termService);
548

    
549
        List<Media> returnMedia = getMediaForTaxon(taxon, includeRelationships,
550
                includeTaxonDescriptions, includeOccurrences, includeTaxonNameDescriptions,
551
                type, mimeTypes, widthOrDuration, height, size);
552
        TaxonNode node;
553
        //looking for all medias of genus
554
        if (taxon.getTaxonNodes().size()>0){
555
            Set<TaxonNode> nodes = taxon.getTaxonNodes();
556
            Iterator<TaxonNode> iterator = nodes.iterator();
557
            //TaxonNode holen
558
            node = iterator.next();
559
            //Check if TaxonNode belongs to the current tree
560

    
561
            node = classificationService.loadTaxonNode(node, TAXONNODE_WITHTAXON_INIT_STRATEGY);
562
            List<TaxonNode> children = node.getChildNodes();
563
            Taxon childTaxon;
564
            for (TaxonNode child : children){
565
                childTaxon = child.getTaxon();
566
                if(childTaxon != null) {
567
                childTaxon = (Taxon)taxonService.load(childTaxon.getUuid(), null);
568
                    returnMedia.addAll(getMediaForTaxon(childTaxon, includeRelationships,
569
                            includeTaxonDescriptions, includeOccurrences, includeTaxonNameDescriptions,
570
                            type, mimeTypes, widthOrDuration, height, size));
571
                }
572
            }
573
        }
574
        return returnMedia;
575
    }
576

    
577
    /**
578
     *
579
     * @param taxon
580
     * @param includeRelationships
581
     * @param type
582
     * @param mimeTypes
583
     * @param widthOrDuration
584
     * @param height
585
     * @param size
586
     * @return
587
     */
588
    private List<Media> getMediaForTaxon(Taxon taxon, Set<TaxonRelationshipEdge> includeRelationships,
589
            Boolean includeTaxonDescriptions, Boolean includeOccurrences, Boolean includeTaxonNameDescriptions,
590
            Class<? extends MediaRepresentationPart> type, String[] mimeTypes, Integer widthOrDuration,
591
            Integer height, Integer size) {
592

    
593
        // list the media
594
        logger.trace("getMediaForTaxon() - list the media");
595
        List<Media> taxonGalleryMedia = service.listMedia(taxon, includeRelationships,
596
                false, includeTaxonDescriptions, includeOccurrences, includeTaxonNameDescriptions, null);
597

    
598
        // filter by preferred size and type
599

    
600
        logger.trace("getMediaForTaxon() - filter the media");
601
        Map<Media, MediaRepresentation> mediaRepresentationMap = MediaUtils.findPreferredMedia(
602
                taxonGalleryMedia, type, mimeTypes, null, widthOrDuration, height, size);
603

    
604
        List<Media> filteredMedia = new ArrayList<>(mediaRepresentationMap.size());
605
        for (Media media : mediaRepresentationMap.keySet()) {
606
            media.getRepresentations().clear();
607
            media.addRepresentation(mediaRepresentationMap.get(media));
608
            filteredMedia.add(media);
609
        }
610

    
611
        logger.trace("getMediaForTaxon() - END ");
612

    
613
        return filteredMedia;
614
    }
615

    
616
// ---------------------- code snippet preserved for possible later use --------------------
617
//	@RequestMapping(
618
//			value = {"//*/portal/taxon/*/descriptions"}, // mapped as absolute path, see CdmAntPathMatcher
619
//			method = RequestMethod.GET)
620
//	public List<TaxonDescription> doGetDescriptionsbyFeatureTree(HttpServletRequest request, HttpServletResponse response)throws IOException {
621
//		TaxonBase tb = getCdmBase(request, response, null, Taxon.class);
622
//		if(tb instanceof Taxon){
623
//			//T O D O this is a quick and dirty implementation -> generalize
624
//			UUID featureTreeUuid = readValueUuid(request, featureTreeUuidPattern);
625
//
626
//			FeatureTree featureTree = descriptionService.getFeatureTreeByUuid(featureTreeUuid);
627
//			Pager<TaxonDescription> p = descriptionService.getTaxonDescriptions((Taxon)tb, null, null, null, null, TAXONDESCRIPTION_INIT_STRATEGY);
628
//			List<TaxonDescription> descriptions = p.getRecords();
629
//
630
//			if(!featureTree.isDescriptionSeparated()){
631
//
632
//				TaxonDescription superDescription = TaxonDescription.NewInstance();
633
//				//put all descriptionElements in superDescription and make it invisible
634
//				for(TaxonDescription description: descriptions){
635
//					for(DescriptionElementBase element: description.getElements()){
636
//						superDescription.addElement(element);
637
//					}
638
//				}
639
//				List<TaxonDescription> separatedDescriptions = new ArrayList<TaxonDescription>(descriptions.size());
640
//				separatedDescriptions.add(superDescription);
641
//				return separatedDescriptions;
642
//			}else{
643
//				return descriptions;
644
//			}
645
//		} else {
646
//			response.sendError(HttpServletResponse.SC_NOT_FOUND, "invalid type; Taxon expected but " + tb.getClass().getSimpleName() + " found.");
647
//			return null;
648
//		}
649
//	}
650

    
651
}
(60-60/67)