Project

General

Profile

Download (6.08 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.loadTypeDesignatio() 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
    /**
59
     * {@inheritDoc}
60
     */
61
    @Override
62
    @Autowired
63
    public void setService(INameService service) {
64
        this.service = service;
65
    }
66

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

    
73

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

    
95
        }
96
        return dtos;
97
    }
98

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

    
105
        String servletPath = request.getServletPath();
106
        String propertyName = FilenameUtils.getBaseName(servletPath);
107

    
108
        logger.info("doGet() - " + requestPathAndQuery(request));
109

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

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

    
123
        String servletPath = request.getServletPath();
124
        String propertyName = FilenameUtils.getBaseName(servletPath);
125

    
126
        logger.info("doGetAnnotations() - " + requestPathAndQuery(request));
127

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

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

    
144
        String servletPath = request.getServletPath();
145
        String propertyName = FilenameUtils.getBaseName(servletPath);
146

    
147
        logger.info("doGetAnnotations() - " + requestPathAndQuery(request));
148

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

    
157
}
(72-72/76)