Project

General

Profile

Download (12.7 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.servlet.http.HttpServletRequest;
21
import javax.servlet.http.HttpServletResponse;
22

    
23
import org.jboss.logging.Logger;
24
import org.springframework.beans.factory.annotation.Autowired;
25
import org.springframework.stereotype.Controller;
26
import org.springframework.web.bind.annotation.PathVariable;
27
import org.springframework.web.bind.annotation.RequestMapping;
28
import org.springframework.web.bind.annotation.RequestMethod;
29
import org.springframework.web.bind.annotation.RequestParam;
30

    
31
import eu.etaxonomy.cdm.api.service.INameService;
32
import eu.etaxonomy.cdm.api.service.pager.Pager;
33
import eu.etaxonomy.cdm.model.common.CdmBase;
34
import eu.etaxonomy.cdm.model.media.ExternalLink;
35
import eu.etaxonomy.cdm.model.name.NameRelationship;
36
import eu.etaxonomy.cdm.model.name.Registration;
37
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
38
import eu.etaxonomy.cdm.model.name.TaxonName;
39
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
40
import eu.etaxonomy.cdm.persistence.dao.initializer.EntityInitStrategy;
41
import eu.etaxonomy.cdm.remote.service.RegistrableEntityFilter;
42
import eu.etaxonomy.cdm.strategy.cache.TaggedText;
43
import io.swagger.annotations.Api;
44

    
45
/**
46
 * TODO write controller documentation
47
 *
48
 * @author a.kohlbecker
49
 * @since 24.03.2009
50
 */
51

    
52
@Controller
53
@Api("name")
54
@RequestMapping(value = {"/name/{uuid}"})
55
public class NameController extends AbstractIdentifiableController<TaxonName, INameService>{
56

    
57
    private static Logger logger = Logger.getLogger(NameController.class);
58

    
59
    public static final EntityInitStrategy TYPEDESIGNATION_INIT_STRATEGY = new EntityInitStrategy(Arrays.asList(new String []{
60
            "typeStatus.representations",
61
            "typifiedNames",
62
            "typeSpecimen",
63
            "typeName",
64
            "designationSource.citation",
65
            "designationSource.citation.authorship.$",
66
            "registrations", // needed for access control
67
            "text"
68
    }));
69

    
70
    public static final EntityInitStrategy FULL_TITLE_CACHE_INIT_STRATEGY = new EntityInitStrategy(Arrays.asList(new String []{
71
            "$",
72
            "relationsFromThisName.$",
73
            "relationsToThisName.$",
74
            "status.$",
75
            "nomenclaturalSource.citation.authorship.$",
76
            "nomenclaturalSource.citation.inReference.authorship.$",
77
            "nomenclaturalSource.citation.inReference.inReference.authorship.$",
78
            "nomenclaturalSource.citation.inReference.inReference.inReference.authorship.$"
79
    }));
80

    
81
    public static final EntityInitStrategy NAME_RELATIONS_INIT_STRATEGY = new EntityInitStrategy(Arrays.asList(new String []{
82
            "$",
83
            "source.citation",
84
            "relationsFromThisName.$",
85
            "relationsFromThisName.toName.registrations",
86
            "relationsToThisName.$",
87
            "relationsToThisName.fromName.registrations"
88
    }));
89

    
90
    public  static final EntityInitStrategy NAME_CACHE_INIT_STRATEGY = new EntityInitStrategy(Arrays.asList(new String []{
91

    
92
    }));
93

    
94
    public static final EntityInitStrategy NAME_REGISTRATIONS_INIT_STRATEGY = new EntityInitStrategy(Arrays.asList(new String []{
95
            "registrations.typeDesignations.$",
96
            "registrations.institution"
97
    }));
98

    
99
    public NameController(){
100
        super();
101
        setInitializationStrategy(Arrays.asList(new String[]{"$"})); //TODO still needed????
102
    }
103

    
104
    @Autowired
105
    @Override
106
    public void setService(INameService service) {
107
        this.service = service;
108
    }
109

    
110

    
111
    @Override
112
    protected <CDM_BASE extends CdmBase> List<String> complementInitStrategy(Class<CDM_BASE> clazz,
113
            List<String> pathProperties) {
114

    
115
        if(pathProperties == null){
116
            return pathProperties;
117
        }
118

    
119
        EntityInitStrategy initStrategy = new EntityInitStrategy(pathProperties);
120

    
121
        if(pathProperties.contains("nameRelations")){
122
            // nameRelations is a transient property!
123
            initStrategy.getPropertyPaths().remove("nameRelations");
124
            initStrategy.extend("relationsFromThisName", TaxonPortalController.NAMERELATIONSHIP_INIT_STRATEGY.getPropertyPaths(), true);
125
            initStrategy.extend("relationsToThisName", TaxonPortalController.NAMERELATIONSHIP_INIT_STRATEGY.getPropertyPaths(), true);
126
        } else {
127
            if(pathProperties.contains("relationsFromThisName")){
128
                initStrategy.getPropertyPaths().remove("relationsFromThisName");
129
                initStrategy.extend("relationsFromThisName", TaxonPortalController.NAMERELATIONSHIP_INIT_STRATEGY.getPropertyPaths(), true);
130
            }
131
            if(pathProperties.contains("relationsToThisName")){
132
                initStrategy.getPropertyPaths().remove("relationsToThisName");
133
                initStrategy.extend("relationsToThisName", TaxonPortalController.NAMERELATIONSHIP_INIT_STRATEGY.getPropertyPaths(), true);
134
            }
135
        }
136

    
137
        return initStrategy.getPropertyPaths();
138
    }
139

    
140

    
141
    /**
142
     * Get the list of {@link TypeDesignationBase}s of the
143
     * {@link TaxonName} instance identified by the <code>{name-uuid}</code>.
144
     * <p>
145
     * URI: <b>&#x002F;{datasource-name}&#x002F;name&#x002F;{name-uuid}&#x002F;typeDesignations</b>
146
     *
147
     * @param request
148
     * @param response
149
     * @return a List of {@link TypeDesignationBase} entities which are initialized
150
     *         using the {@link #TYPEDESIGNATION_INIT_STRATEGY}
151
     * @throws IOException
152
     */
153
    @RequestMapping(value = { "typeDesignations" }, method = RequestMethod.GET)
154
    public List<TypeDesignationBase> doGetTypeDesignations(
155
            @PathVariable("uuid") UUID uuid, HttpServletRequest request,
156
            HttpServletResponse response) throws IOException {
157

    
158
        if (request != null) {
159
            logger.info("doGetTypeDesignations()" + requestPathAndQuery(request));
160
        }
161
        TaxonName tnb = getCdmBaseInstance(uuid, response, (List<String>)null);
162
        if(tnb == null){
163
            return null;
164
        }
165
        Pager<TypeDesignationBase> pager = service.getTypeDesignations(tnb, null,
166
                null, null, TYPEDESIGNATION_INIT_STRATEGY.getPropertyPaths());
167
        return new ArrayList(RegistrableEntityFilter.newInstance(userHelper).filterPublishedOnly(pager.getRecords()));
168
    }
169

    
170
    /**
171
     * Get the list of {@link TypeDesignationBase}s associated to any name in the same homotypical group to which
172
     * the {@link TaxonName} identified by the <code>{name-uuid}</code> belongs.
173
     * <p>
174
     * URI: <b>&#x002F;{datasource-name}&#x002F;name&#x002F;{name-uuid}&#x002F;typeDesignations</b>
175
     *
176
     * @param request
177
     * @param response
178
     * @return a List of {@link TypeDesignationBase} entities which are initialized
179
     *         using the {@link #TYPEDESIGNATION_INIT_STRATEGY}
180
     * @throws IOException
181
     */
182
    @RequestMapping(value = { "typeDesignationsInHomotypicalGroup" }, method = RequestMethod.GET)
183
    public List<TypeDesignationBase> doGetTypeDesignationsInHomotypicalGroup(
184
            @PathVariable("uuid") UUID uuid, HttpServletRequest request,
185
            HttpServletResponse response) throws IOException {
186

    
187
        if (request != null) {
188
            logger.info("doGetTypeDesignationsInHomotypicalGroup()" + requestPathAndQuery(request));
189
        }
190
        List<TypeDesignationBase> result = service.getTypeDesignationsInHomotypicalGroup(uuid,
191
                null, null, TYPEDESIGNATION_INIT_STRATEGY.getPropertyPaths());
192
        return new ArrayList(RegistrableEntityFilter.newInstance(userHelper).filterPublishedOnly(result));
193
    }
194

    
195

    
196
    @RequestMapping(
197
            value = {"nameCache"},
198
            method = RequestMethod.GET)
199
    public List<String> doGetNameCache(@PathVariable("uuid") UUID uuid,
200
            HttpServletRequest request, HttpServletResponse response)throws IOException {
201

    
202
        logger.info("doGetNameCache()" + requestPathAndQuery(request));
203
        TaxonName tnb = getCdmBaseInstance(uuid, response, NAME_CACHE_INIT_STRATEGY.getPropertyPaths());
204
        if(tnb == null){
205
            return null;
206
        }
207
        String nameCacheString = tnb.getNameCache();
208
        List<String> result = new ArrayList<>();
209
        result.add(nameCacheString);
210
        return result;
211

    
212
    }
213

    
214
    @RequestMapping(value = "taggedName", method = RequestMethod.GET)
215
    public List<TaggedText> doGetTaggedName(
216
            @PathVariable("uuid") UUID uuid,
217
            HttpServletRequest request,
218
            HttpServletResponse response) throws IOException {
219
        logger.info("doGetDescriptionElementsByType() - " + requestPathAndQuery(request));
220
        return service.getTaggedName(uuid);
221
    }
222

    
223
    @RequestMapping(value = "taggedFullTitle", method = RequestMethod.GET)
224
    public List<TaggedText> doGetTaggedFullTitle(
225
            @PathVariable("uuid") UUID uuid,
226
            HttpServletRequest request,
227
            HttpServletResponse response) throws IOException {
228
        logger.info("doGetTaggedFullTitle() - " + requestPathAndQuery(request));
229

    
230
        TaxonName name = service.load(uuid, FULL_TITLE_CACHE_INIT_STRATEGY.getPropertyPaths());
231
        return name.getTaggedFullTitle();
232
    }
233

    
234
    @RequestMapping(
235
            value = "registrations",
236
            method = RequestMethod.GET)
237
    public Set<Registration> doGetRegistrations(@PathVariable("uuid") UUID uuid,
238
            HttpServletRequest request, HttpServletResponse response)throws IOException {
239

    
240
        logger.info("doGetRegistrations" + requestPathAndQuery(request));
241
        TaxonName tnb = getCdmBaseInstance(uuid, response, NAME_REGISTRATIONS_INIT_STRATEGY.getPropertyPaths());
242
        Set<Registration> regs = tnb.getRegistrations();
243
        if(regs != null && regs.size() > 0){
244
            Set<Registration> regsFiltered = new HashSet<>(regs.size());
245
            for(Registration reg : regs){
246
                if(userHelper.userIsAutheticated() && reg.getStatus().equals(RegistrationStatus.PUBLISHED)) {
247
                    regsFiltered.add(reg);
248
                } else {
249
                    logger.debug("skipping unpublished registration");
250
                }
251
            }
252
            return regsFiltered;
253
        }
254
        return null;
255
    }
256

    
257
    @RequestMapping(
258
            value = "nameRelations",
259
            method = RequestMethod.GET)
260
    public Object doGetNameRelations(
261
            @PathVariable("uuid") UUID uuid,
262
            // doPage request parameters
263
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
264
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
265
            // doList request parameters
266
            @RequestParam(value = "start", required = false) Integer start,
267
            @RequestParam(value = "limit", required = false) Integer limit,
268
            HttpServletRequest request,
269
            HttpServletResponse response) throws IOException {
270

    
271
        logger.info("doGetNameRelations" + requestPathAndQuery(request));
272
        TaxonName tnb = getCdmBaseInstance(uuid, response, NAME_RELATIONS_INIT_STRATEGY.getPropertyPaths());
273

    
274
        Set<NameRelationship> nameRelations = tnb.getNameRelations();
275

    
276
        if(nameRelations != null && nameRelations.size() > 0){
277
            Set<NameRelationship> nameRelationsFiltered = RegistrableEntityFilter.
278
                newInstance(userHelper).filterPublishedOnly(tnb, nameRelations);
279
            return pageFromCollection(nameRelationsFiltered, pageIndex, pageSize, start, limit, response);
280
        }
281
        return null;
282
    }
283

    
284
    /**
285
     * Provides the  „Protologue / original publication“ of the names nomenclatural reference.
286
     *
287
     */
288
    @RequestMapping(value = "protologueLinks", method = RequestMethod.GET)
289
    public Set<ExternalLink> doGetProtologueLinks(
290
            @PathVariable("uuid") UUID uuid,
291
            HttpServletRequest request,
292
            HttpServletResponse response) throws IOException {
293
        logger.info("doGetProtologueLinks() - " + requestPathAndQuery(request));
294
        TaxonName name = service.load(uuid, Arrays.asList("nomenclaturalSource.links"));
295
        if(name.getNomenclaturalSource() != null) {
296
            return name.getNomenclaturalSource().getLinks();
297
        }
298
        return null;
299
    }
300

    
301
}
(35-35/76)