Project

General

Profile

Download (6.04 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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.ArrayList;
13
import java.util.Arrays;
14
import java.util.Collection;
15
import java.util.UUID;
16

    
17
import javax.servlet.http.HttpServletRequest;
18
import javax.servlet.http.HttpServletResponse;
19

    
20
import org.apache.commons.io.FilenameUtils;
21
import org.apache.log4j.Logger;
22
import org.springframework.beans.factory.annotation.Autowired;
23
import org.springframework.stereotype.Controller;
24
import org.springframework.web.bind.annotation.PathVariable;
25
import org.springframework.web.bind.annotation.RequestMapping;
26
import org.springframework.web.bind.annotation.RequestMethod;
27
import org.springframework.web.bind.annotation.RequestParam;
28

    
29
import eu.etaxonomy.cdm.api.service.INameService;
30
import eu.etaxonomy.cdm.api.service.dto.MediaDTO;
31
import eu.etaxonomy.cdm.api.service.pager.Pager;
32
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
33
import eu.etaxonomy.cdm.model.common.Annotation;
34
import eu.etaxonomy.cdm.model.media.Media;
35
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
36
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
37
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
38
import eu.etaxonomy.cdm.model.name.TaxonName;
39
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
40
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
41
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
42
import io.swagger.annotations.Api;
43

    
44
/**
45
 * @author a.kohlbecker
46
 * @since Jul 31, 2018
47
 *
48
 */
49
@Controller
50
@Api(value = "typeDesignation")
51
@RequestMapping(value = {"/typedesignation/{uuid}"})
52
/* can not use BaseController since the getMethod needs to call service.loadTypeDesignation() which is generically not possible */
53
public class TypeDesignationController extends AbstractController<TaxonName, INameService>  {
54

    
55

    
56
    private static final Logger logger = Logger.getLogger(TypeDesignationController.class);
57

    
58
    @Override
59
    @Autowired
60
    public void setService(INameService service) {
61
        this.service = service;
62
    }
63

    
64
    @RequestMapping(value="media", method = RequestMethod.GET)
65
    public Collection<MediaDTO> doGetMedia(
66
            @PathVariable("uuid") UUID uuid,
67
            HttpServletRequest request,
68
            HttpServletResponse response) throws IOException {
69

    
70

    
71
        logger.info("doGetMediaUris() - " + requestPathAndQuery(request));
72
        ArrayList<MediaDTO> dtos = new ArrayList<>();
73
        TypeDesignationBase<?> td = service.loadTypeDesignation(uuid, Arrays.asList("typeSpecimen.mediaSpecimen.representations.mediaRepresentationParts"));
74
        if(td instanceof SpecimenTypeDesignation){
75
            SpecimenTypeDesignation std = (SpecimenTypeDesignation)td;
76
            DerivedUnit du = HibernateProxyHelper.deproxy(std.getTypeSpecimen(), DerivedUnit.class);
77
            if(du != null && du instanceof MediaSpecimen) {
78
                Media media = ((MediaSpecimen)du).getMediaSpecimen();
79
                if(media != null){
80
                    for(MediaRepresentation mrp : media.getRepresentations()){
81
                        for(MediaRepresentationPart p : mrp.getParts()){
82
                            if(p.getUri() != null){
83
                                MediaDTO dto = new MediaDTO(media.getUuid());
84
                                dto.setUri(p.getUri().toString());
85
                                dtos.add(dto);
86
                            }
87
                        }
88
                    }
89
                }
90
            }
91

    
92
        }
93
        return dtos;
94
    }
95

    
96
    @RequestMapping(method = RequestMethod.GET)
97
    public TypeDesignationBase<?> doGetMethod(
98
            @PathVariable("uuid") UUID uuid,
99
            HttpServletRequest request,
100
            HttpServletResponse response) throws IOException {
101

    
102
        String servletPath = request.getServletPath();
103
        String propertyName = FilenameUtils.getBaseName(servletPath);
104

    
105
        logger.info("doGet() - " + requestPathAndQuery(request));
106

    
107
        TypeDesignationBase<?> dtb = service.loadTypeDesignation(uuid, Arrays.asList("$"));
108
        if(dtb == null){
109
            HttpStatusMessage.UUID_NOT_FOUND.send(response);
110
        }
111
        return dtb;
112
    }
113

    
114
    @RequestMapping(method = RequestMethod.GET, value="annotations")
115
    public Collection<Annotation> doGetAnnotations(
116
            @PathVariable("uuid") UUID uuid,
117
            HttpServletRequest request,
118
            HttpServletResponse response) throws IOException {
119

    
120
        String servletPath = request.getServletPath();
121
        String propertyName = FilenameUtils.getBaseName(servletPath);
122

    
123
        logger.info("doGetAnnotations() - " + requestPathAndQuery(request));
124

    
125
        TypeDesignationBase<?> dtb = service.loadTypeDesignation(uuid, Arrays.asList("$", "annotations"));
126
        if(dtb == null){
127
            HttpStatusMessage.UUID_NOT_FOUND.send(response);
128
        }
129
        return dtb.getAnnotations();
130
    }
131

    
132
    @RequestMapping(method = RequestMethod.GET, value="annotations", params = {"pageIndex", "pageSize"})
133
    public Pager<Annotation> doGetAnnotations(
134
            @PathVariable("uuid") UUID uuid,
135
            // doPage request parameters
136
            @RequestParam(value = "pageIndex", required = true) Integer pageIndex,
137
            @RequestParam(value = "pageSize", required = true) Integer pageSize,
138
            HttpServletRequest request,
139
            HttpServletResponse response) throws IOException {
140

    
141
        String servletPath = request.getServletPath();
142
        String propertyName = FilenameUtils.getBaseName(servletPath);
143

    
144
        logger.info("doGetAnnotations() - " + requestPathAndQuery(request));
145

    
146
        TypeDesignationBase<?> dtb = service.loadTypeDesignation(uuid, Arrays.asList("$", "annotations"));
147
        if(dtb == null){
148
            HttpStatusMessage.UUID_NOT_FOUND.send(response);
149
        }
150
        Pager<Annotation> p = pagerForSubCollectionOf(dtb.getAnnotations(), pageIndex, pageSize, response);
151
        return p;
152
    }
153

    
154
}
(72-72/76)