Project

General

Profile

Download (12.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.Arrays;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import javax.servlet.http.HttpServletRequest;
20
import javax.servlet.http.HttpServletResponse;
21

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

    
30
import eu.etaxonomy.cdm.api.service.IDescriptionService;
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.description.TaxonNameDescription;
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 io.swagger.annotations.Api;
42

    
43
/**
44
 * The NamePortalController class is a Spring MVC Controller.
45
 * <p>
46
 * The syntax of the mapped service URIs contains the the {datasource-name} path element.
47
 * The available {datasource-name}s are defined in a configuration file which
48
 * is loaded by the {@link UpdatableRoutingDataSource}. If the
49
 * UpdatableRoutingDataSource is not being used in the actual application
50
 * context any arbitrary {datasource-name} may be used.
51
 * <p>
52
 * Methods mapped at type level, inherited from super classes ({@link BaseController}):
53
 * <blockquote>
54
 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;name&#x002F;{name-uuid}</b>
55
 *
56
 * Get the {@link TaxonName} instance identified by the <code>{name-uuid}</code>.
57
 * The returned TaxonName is initialized by
58
 * the following strategy: -- NONE --
59
 * </blockquote>
60
 *
61
 * @author a.kohlbecker
62
 * @since 24.03.2009
63
 */
64

    
65
@Controller
66
@Api("portal_name")
67
@RequestMapping(value = {"/portal/name/{uuid}"})
68
public class NamePortalController extends BaseController<TaxonName, INameService> {
69

    
70
    private static final Logger logger = Logger.getLogger(NamePortalController.class);
71

    
72
    private static final List<String> TYPEDESIGNATION_INIT_STRATEGY = TypeDesignationPortalController.DEFAULT_INIT_STRATEGY;
73

    
74

    
75
    private static final List<String> NAMEDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
76
            "elements.$",
77
            "elements.multilanguageText",
78
            "elements.media",
79
    });
80

    
81
    private static final EntityInitStrategy NOMREF_INIT_STRATEGY = new EntityInitStrategy(
82
            "nomenclaturalReference.authorship.$",
83
            "nomenclaturalReference.inReference.authorship.$",
84
            "nomenclaturalReference.inReference.inReference.authorship.$",
85
            "nomenclaturalReference.inReference.inReference.inReference.authorship.$"
86
            );
87

    
88
    private static EntityInitStrategy nameRelationsInitStrategy = null;
89

    
90
    /**
91
     * @return the nameRelationsInitStrategy
92
     */
93
    public static EntityInitStrategy getNameRelationsInitStrategy() {
94
        if(nameRelationsInitStrategy == null){
95
            nameRelationsInitStrategy = extendNameRelationsInitStrategies(NameController.NAME_RELATIONS_INIT_STRATEGY.getPropertyPaths(), true);
96
        }
97
        return nameRelationsInitStrategy;
98
    }
99

    
100

    
101
    @Override
102
    protected <CDM_BASE extends CdmBase> List<String> complementInitStrategy(Class<CDM_BASE> clazz,
103
            List<String> pathProperties) {
104

    
105
        if(pathProperties == null){
106
            return pathProperties;
107
        }
108

    
109
        EntityInitStrategy initStrategy = extendNameRelationsInitStrategies(pathProperties, false);
110

    
111
        return initStrategy.getPropertyPaths();
112
    }
113

    
114
    /**
115
     * @param pathProperties
116
     * @return
117
     */
118
    static EntityInitStrategy extendNameRelationsInitStrategies(List<String> pathProperties, boolean addNomrefInitStrategy) {
119

    
120
        EntityInitStrategy initStrategy = new EntityInitStrategy(pathProperties);
121

    
122
        EntityInitStrategy nameRelInitExtendet = TaxonPortalController.NAMERELATIONSHIP_INIT_STRATEGY;
123

    
124
        if(addNomrefInitStrategy){
125
            nameRelInitExtendet.extend("toName", NOMREF_INIT_STRATEGY.getPropertyPaths(), false);
126
            nameRelInitExtendet.extend("fromName", NOMREF_INIT_STRATEGY.getPropertyPaths(), false);
127
        }
128

    
129
        if(pathProperties.contains("nameRelations")){
130
            // nameRelations is a transient property! replace it by relationsFromThisName and relationsToThisName
131
            initStrategy.getPropertyPaths().remove("nameRelations");
132
            initStrategy.extend("relationsFromThisName", nameRelInitExtendet.getPropertyPaths(), true);
133
            initStrategy.extend("relationsToThisName", nameRelInitExtendet.getPropertyPaths(), true);
134
        } else {
135
            if(pathProperties.contains("relationsFromThisName")){
136
                initStrategy.getPropertyPaths().remove("relationsFromThisName");
137
                initStrategy.extend("relationsFromThisName", nameRelInitExtendet.getPropertyPaths(), true);
138
            }
139
            if(pathProperties.contains("relationsToThisName")){
140
                initStrategy.getPropertyPaths().remove("relationsToThisName");
141
                initStrategy.extend("relationsToThisName", nameRelInitExtendet.getPropertyPaths(), true);
142
            }
143
        }
144
        return initStrategy;
145
    }
146

    
147
    /* (non-Javadoc)
148
     * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
149
     */
150
    @Autowired
151
    @Override
152
    public void setService(INameService service) {
153
        this.service = service;
154
    }
155

    
156
    @Autowired
157
    private IDescriptionService descriptionService;
158

    
159
    /**
160
     * Get the list of {@link TypeDesignationBase}s of the
161
     * {@link TaxonName} instance identified by the <code>{name-uuid}</code>.
162
     * <p>
163
     * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;name&#x002F;{name-uuid}&#x002F;typeDesignations</b>
164
     *
165
     * @param request
166
     * @param response
167
     * @return a List of {@link TypeDesignationBase} entities which are initialized
168
     *         using the following initialization strategy:
169
     *         {@link #TYPEDESIGNATION_INIT_STRATEGY}
170
     * @throws IOException
171
     */
172
    @RequestMapping(
173
            value = {"typeDesignations"},
174
            method = RequestMethod.GET)
175
    public List<TypeDesignationBase> doGetTypeDesignations(@PathVariable("uuid") UUID uuid,
176
            HttpServletRequest request, HttpServletResponse response)throws IOException {
177
        TaxonName tnb = getCdmBaseInstance(uuid, response, (List<String>)null);
178
        if(tnb == null){
179
            return null;
180
        }
181
        Pager<TypeDesignationBase> p = service.getTypeDesignations(tnb,  null, null, null, TYPEDESIGNATION_INIT_STRATEGY);
182
        return p.getRecords();
183
    }
184

    
185
    /**
186
     * Get the list of {@link TypeDesignationBase}s associated to any name in the same homotypical group to which
187
     * the {@link TaxonName} identified by the <code>{name-uuid}</code> belongs.
188
     * <p>
189
     * URI: <b>&#x002F;{datasource-name}&#x002F;name&#x002F;{name-uuid}&#x002F;typeDesignations</b>
190
     *
191
     * @param request
192
     * @param response
193
     * @return a List of {@link TypeDesignationBase} entities which are initialized
194
     *         using the {@link #TYPEDESIGNATION_INIT_STRATEGY}
195
     * @throws IOException
196
     */
197
    @RequestMapping(value = { "typeDesignationsInHomotypicalGroup" }, method = RequestMethod.GET)
198
    public List<TypeDesignationBase> doGetTypeDesignationsInHomotypicalGroup(
199
            @PathVariable("uuid") UUID uuid, HttpServletRequest request,
200
            HttpServletResponse response) throws IOException {
201

    
202
        if (request != null) {
203
            logger.info("doGetTypeDesignationsInHomotypicalGroup()" + requestPathAndQuery(request));
204
        }
205
        List<TypeDesignationBase> result = service.getTypeDesignationsInHomotypicalGroup(uuid,
206
                null, null, TYPEDESIGNATION_INIT_STRATEGY);
207
        return result;
208
    }
209

    
210
    /**
211
     * Get the list of {@link TaxonNameDescription}s of the Name associated with the
212
     * {@link TaxonName} instance identified by the <code>{name-uuid}</code>.
213
     * <p>
214
     * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;name&#x002F;{name-uuid}&#x002F;descriptions</b>
215
     *
216
     * @param request
217
     * @param response
218
     * @return a List of {@link TaxonNameDescription} entities which are initialized
219
     *         using the following initialization strategy:
220
     *         {@link #NAMEDESCRIPTION_INIT_STRATEGY}
221
     * @throws IOException
222
     */
223
    @RequestMapping(
224
            value = {"taxonNameDescriptions"},
225
            method = RequestMethod.GET)
226
    public List<TaxonNameDescription> doGetNameDescriptions(@PathVariable("uuid") UUID uuid,
227
            HttpServletRequest request, HttpServletResponse response)throws IOException {
228
        logger.info("doGetNameDescriptions()" + request.getRequestURI());
229

    
230
        TaxonName tnb = getCdmBaseInstance(uuid, response, (List<String>)null);
231
        if(tnb == null){
232
            return null;
233
        }
234
        Pager<TaxonNameDescription> p = descriptionService.getTaxonNameDescriptions(tnb, null, null, NAMEDESCRIPTION_INIT_STRATEGY);
235
        return p.getRecords();
236

    
237
    }
238

    
239
    // TODO this is a copy of the same method in NameController --> this class should extend  NameController !!!
240
    @RequestMapping(
241
            value = "nameRelations",
242
            method = RequestMethod.GET)
243
    public Object doGetNameRelations(
244
            @PathVariable("uuid") UUID uuid,
245
            // doPage request parameters
246
            @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
247
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
248
            // doList request parameters
249
            @RequestParam(value = "start", required = false) Integer start,
250
            @RequestParam(value = "limit", required = false) Integer limit,
251
            HttpServletRequest request,
252
            HttpServletResponse response) throws IOException {
253

    
254
        logger.info("doGetNameRelations" + requestPathAndQuery(request));
255
        TaxonName tnb = getCdmBaseInstance(uuid, response, getNameRelationsInitStrategy().getPropertyPaths());
256

    
257
        Set<NameRelationship> nameRelations = tnb.getNameRelations();
258

    
259
        if(nameRelations != null && nameRelations.size() > 0){
260
        Set<NameRelationship> nameRelationsFiltered = new HashSet<>(nameRelations.size());
261

    
262
        if(userHelper.userIsAnnonymous()){
263
            // need to filter out unpublished related names in this case
264
                for(NameRelationship rel : nameRelations){
265
                    // check if the name has been published ba any registration
266
                    Set<Registration> regsToCheck = new HashSet<>();
267
                    if(rel.getToName().equals(tnb) && rel.getFromName().getRegistrations() != null) {
268
                        regsToCheck.addAll(rel.getFromName().getRegistrations());
269
                    }
270
                    if(rel.getFromName().equals(tnb) && rel.getToName().getRegistrations() != null) {
271
                        regsToCheck.addAll(rel.getToName().getRegistrations());
272
                    }
273
                    // if there is no registration for this name we assume that it is published
274
                    boolean nameIsPublished = regsToCheck.size() == 0;
275
                    nameIsPublished |= regsToCheck.stream().anyMatch(reg -> reg.getStatus().equals(RegistrationStatus.PUBLISHED));
276
                    if(nameIsPublished){
277
                        nameRelationsFiltered.add(rel);
278
                    } else {
279
                        logger.debug("Hiding NameRelationship " + rel);
280
                    }
281
                }
282
            }  else {
283
                // no filtering needed
284
                nameRelationsFiltered = nameRelations;
285
            }
286
            return pageFromCollection(nameRelationsFiltered, pageNumber, pageSize, start, limit, response);
287
        }
288
        return null;
289
    }
290

    
291

    
292
}
(37-37/75)