Project

General

Profile

Download (12.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.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.name.NameRelationship;
35
import eu.etaxonomy.cdm.model.name.Registration;
36
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
37
import eu.etaxonomy.cdm.model.name.TaxonName;
38
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
39
import eu.etaxonomy.cdm.persistence.dao.initializer.EntityInitStrategy;
40
import eu.etaxonomy.cdm.strategy.cache.TaggedText;
41
import io.swagger.annotations.Api;
42

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

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

    
55
    private static Logger logger = Logger.getLogger(NameController.class);
56

    
57
    public static final EntityInitStrategy TYPEDESIGNATION_INIT_STRATEGY = new EntityInitStrategy(Arrays.asList(new String []{
58
            "typeStatus.representations",
59
            "typifiedNames",
60
            "typeSpecimen",
61
            "typeName",
62
            "citation",
63
            "citation.authorship.$",
64
            "text"
65
    }));
66

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

    
78
    public static final EntityInitStrategy NAME_RELATIONS_INIT_STRATEGY = new EntityInitStrategy(Arrays.asList(new String []{
79
            "$",
80
            "relationsFromThisName.$",
81
            "relationsFromThisName.toName.registrations",
82
            "relationsToThisName.$",
83
            "relationsToThisName.fromName.registrations"
84
    }));
85

    
86
    public  static final EntityInitStrategy NAME_CACHE_INIT_STRATEGY = new EntityInitStrategy(Arrays.asList(new String []{
87

    
88
    }));
89

    
90
    public static final EntityInitStrategy NAME_REGISTRATIONS_INIT_STRATEGY = new EntityInitStrategy(Arrays.asList(new String []{
91
            "registrations.typeDesignations.$",
92
            "registrations.institution"
93
    }));
94

    
95
    public NameController(){
96
        super();
97
        setInitializationStrategy(Arrays.asList(new String[]{"$"})); //TODO still needed????
98
    }
99

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

    
106

    
107
    @Override
108
    protected <CDM_BASE extends CdmBase> List<String> complementInitStrategy(Class<CDM_BASE> clazz,
109
            List<String> pathProperties) {
110

    
111
        if(pathProperties == null){
112
            return pathProperties;
113
        }
114

    
115
        EntityInitStrategy initStrategy = new EntityInitStrategy(pathProperties);
116

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

    
133
        return initStrategy.getPropertyPaths();
134
    }
135

    
136

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

    
154
        if (request != null) {
155
            logger.info("doGetTypeDesignations()" + requestPathAndQuery(request));
156
        }
157
        TaxonName tnb = getCdmBaseInstance(uuid, response, (List<String>)null);
158
        if(tnb == null){
159
            return null;
160
        }
161
        Pager<TypeDesignationBase> pager = service.getTypeDesignations(tnb, null,
162
                null, null, TYPEDESIGNATION_INIT_STRATEGY.getPropertyPaths());
163
        return pager.getRecords();
164
    }
165

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

    
183
        if (request != null) {
184
            logger.info("doGetTypeDesignationsInHomotypicalGroup()" + requestPathAndQuery(request));
185
        }
186
        List<TypeDesignationBase> result = service.getTypeDesignationsInHomotypicalGroup(uuid,
187
                null, null, TYPEDESIGNATION_INIT_STRATEGY.getPropertyPaths());
188
        return result;
189
    }
190

    
191

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

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

    
208
    }
209

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

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

    
226
        TaxonName name = service.load(uuid, FULL_TITLE_CACHE_INIT_STRATEGY.getPropertyPaths());
227
        return name.getTaggedFullTitle();
228
    }
229

    
230
    @RequestMapping(
231
            value = "registrations",
232
            method = RequestMethod.GET)
233
    public Set<Registration> doGetRegistrations(@PathVariable("uuid") UUID uuid,
234
            HttpServletRequest request, HttpServletResponse response)throws IOException {
235

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

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

    
267
        logger.info("doGetNameRelations" + requestPathAndQuery(request));
268
        TaxonName tnb = getCdmBaseInstance(uuid, response, NAME_RELATIONS_INIT_STRATEGY.getPropertyPaths());
269

    
270
        Set<NameRelationship> nameRelations = tnb.getNameRelations();
271

    
272
        if(nameRelations != null && nameRelations.size() > 0){
273
        Set<NameRelationship> nameRelationsFiltered = new HashSet<>(nameRelations.size());
274

    
275
        if(!userHelper.userIsAutheticated() || userHelper.userIsAnnonymous()){
276
            // need to filter out unpublished related names in this case
277
                for(NameRelationship rel : nameRelations){
278
                    // check if the name has been published ba any registration
279
                    Set<Registration> regsToCheck = new HashSet<>();
280
                    if(rel.getToName().equals(tnb) && rel.getFromName().getRegistrations() != null) {
281
                        regsToCheck.addAll(rel.getFromName().getRegistrations());
282
                    }
283
                    if(rel.getFromName().equals(tnb) && rel.getToName().getRegistrations() != null) {
284
                        regsToCheck.addAll(rel.getToName().getRegistrations());
285
                    }
286
                    // if there is no registration for this name we assume that it is published
287
                    boolean nameIsPublished = regsToCheck.size() == 0;
288
                    nameIsPublished |= regsToCheck.stream().anyMatch(reg -> reg.getStatus().equals(RegistrationStatus.PUBLISHED));
289
                    if(nameIsPublished){
290
                        nameRelationsFiltered.add(rel);
291
                    }
292
                }
293
            }  else {
294
                // no filtering needed
295
                nameRelationsFiltered = nameRelations;
296
            }
297
            return pageFromCollection(nameRelationsFiltered, pageNumber, pageSize, start, limit, response);
298
        }
299
        return null;
300
    }
301

    
302
}
(35-35/75)