Project

General

Profile

Download (12.3 KB) Statistics
| Branch: | Tag: | Revision:
1 b8b8ed11 Andreas Müller
/**
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 707cb8bc Andreas Kohlbecker
import java.util.HashSet;
15 b8b8ed11 Andreas Müller
import java.util.List;
16 707cb8bc Andreas Kohlbecker
import java.util.Set;
17 b8b8ed11 Andreas Müller
import java.util.UUID;
18
19
import javax.servlet.http.HttpServletRequest;
20
import javax.servlet.http.HttpServletResponse;
21
22 0f2a8f74 Andreas Kohlbecker
import org.apache.log4j.Logger;
23 b8b8ed11 Andreas Müller
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 707cb8bc Andreas Kohlbecker
import org.springframework.web.bind.annotation.RequestParam;
29 b8b8ed11 Andreas Müller
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 941a7fd0 Andreas Kohlbecker
import eu.etaxonomy.cdm.model.common.CdmBase;
34 b8b8ed11 Andreas Müller
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
35 707cb8bc Andreas Kohlbecker
import eu.etaxonomy.cdm.model.name.NameRelationship;
36
import eu.etaxonomy.cdm.model.name.Registration;
37
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
38 9dc896c9 Andreas Müller
import eu.etaxonomy.cdm.model.name.TaxonName;
39 b8b8ed11 Andreas Müller
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
40 941a7fd0 Andreas Kohlbecker
import eu.etaxonomy.cdm.persistence.dao.initializer.EntityInitStrategy;
41 9dc896c9 Andreas Müller
import io.swagger.annotations.Api;
42 b8b8ed11 Andreas Müller
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 9dc896c9 Andreas Müller
 * Get the {@link TaxonName} instance identified by the <code>{name-uuid}</code>.
57
 * The returned TaxonName is initialized by
58 b8b8ed11 Andreas Müller
 * the following strategy: -- NONE --
59
 * </blockquote>
60
 *
61
 * @author a.kohlbecker
62 7fa325bc Andreas Müller
 * @since 24.03.2009
63 b8b8ed11 Andreas Müller
 */
64
65
@Controller
66
@Api("portal_name")
67
@RequestMapping(value = {"/portal/name/{uuid}"})
68 0f2a8f74 Andreas Kohlbecker
public class NamePortalController extends BaseController<TaxonName, INameService> {
69
70
    private static final Logger logger = Logger.getLogger(NamePortalController.class);
71 b8b8ed11 Andreas Müller
72 28702a86 Andreas Kohlbecker
    private static final List<String> TYPEDESIGNATION_INIT_STRATEGY = TypeDesignationPortalController.DEFAULT_INIT_STRATEGY;
73 b8b8ed11 Andreas Müller
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 db026829 Andreas Kohlbecker
    private static final EntityInitStrategy NOMREF_INIT_STRATEGY = new EntityInitStrategy(
82 eae56a30 Andreas Müller
            "nomenclaturalSource.citation.authorship.$",
83
            "nomenclaturalSource.citation.inReference.authorship.$",
84
            "nomenclaturalSource.citation.inReference.inReference.authorship.$",
85
            "nomenclaturalSource.citation.inReference.inReference.inReference.authorship.$"
86 db026829 Andreas Kohlbecker
            );
87
88 707cb8bc Andreas Kohlbecker
    private static EntityInitStrategy nameRelationsInitStrategy = null;
89
90
    /**
91
     * @return the nameRelationsInitStrategy
92
     */
93
    public static EntityInitStrategy getNameRelationsInitStrategy() {
94
        if(nameRelationsInitStrategy == null){
95 db026829 Andreas Kohlbecker
            nameRelationsInitStrategy = extendNameRelationsInitStrategies(NameController.NAME_RELATIONS_INIT_STRATEGY.getPropertyPaths(), true);
96 707cb8bc Andreas Kohlbecker
        }
97
        return nameRelationsInitStrategy;
98
    }
99
100
101 941a7fd0 Andreas Kohlbecker
    @Override
102
    protected <CDM_BASE extends CdmBase> List<String> complementInitStrategy(Class<CDM_BASE> clazz,
103
            List<String> pathProperties) {
104
105 73f51846 Andreas Kohlbecker
        if(pathProperties == null){
106
            return pathProperties;
107
        }
108
109 db026829 Andreas Kohlbecker
        EntityInitStrategy initStrategy = extendNameRelationsInitStrategies(pathProperties, false);
110 707cb8bc Andreas Kohlbecker
111
        return initStrategy.getPropertyPaths();
112
    }
113
114
    /**
115
     * @param pathProperties
116
     * @return
117
     */
118 db026829 Andreas Kohlbecker
    static EntityInitStrategy extendNameRelationsInitStrategies(List<String> pathProperties, boolean addNomrefInitStrategy) {
119
120 941a7fd0 Andreas Kohlbecker
        EntityInitStrategy initStrategy = new EntityInitStrategy(pathProperties);
121
122 db026829 Andreas Kohlbecker
        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 941a7fd0 Andreas Kohlbecker
        if(pathProperties.contains("nameRelations")){
130 db026829 Andreas Kohlbecker
            // nameRelations is a transient property! replace it by relationsFromThisName and relationsToThisName
131 941a7fd0 Andreas Kohlbecker
            initStrategy.getPropertyPaths().remove("nameRelations");
132 db026829 Andreas Kohlbecker
            initStrategy.extend("relationsFromThisName", nameRelInitExtendet.getPropertyPaths(), true);
133
            initStrategy.extend("relationsToThisName", nameRelInitExtendet.getPropertyPaths(), true);
134 941a7fd0 Andreas Kohlbecker
        } else {
135
            if(pathProperties.contains("relationsFromThisName")){
136
                initStrategy.getPropertyPaths().remove("relationsFromThisName");
137 db026829 Andreas Kohlbecker
                initStrategy.extend("relationsFromThisName", nameRelInitExtendet.getPropertyPaths(), true);
138 941a7fd0 Andreas Kohlbecker
            }
139
            if(pathProperties.contains("relationsToThisName")){
140
                initStrategy.getPropertyPaths().remove("relationsToThisName");
141 db026829 Andreas Kohlbecker
                initStrategy.extend("relationsToThisName", nameRelInitExtendet.getPropertyPaths(), true);
142 941a7fd0 Andreas Kohlbecker
            }
143
        }
144 707cb8bc Andreas Kohlbecker
        return initStrategy;
145 941a7fd0 Andreas Kohlbecker
    }
146
147 b8b8ed11 Andreas Müller
    /* (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 9dc896c9 Andreas Müller
     * {@link TaxonName} instance identified by the <code>{name-uuid}</code>.
162 b8b8ed11 Andreas Müller
     * <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 9dc896c9 Andreas Müller
        TaxonName tnb = getCdmBaseInstance(uuid, response, (List<String>)null);
178 cc3ce6e3 Andreas Kohlbecker
        if(tnb == null){
179
            return null;
180
        }
181 b8b8ed11 Andreas Müller
        Pager<TypeDesignationBase> p = service.getTypeDesignations(tnb,  null, null, null, TYPEDESIGNATION_INIT_STRATEGY);
182
        return p.getRecords();
183
    }
184
185 7dde6846 Andreas Kohlbecker
    /**
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 dd4347b6 Andreas Kohlbecker
210 b8b8ed11 Andreas Müller
    /**
211
     * Get the list of {@link TaxonNameDescription}s of the Name associated with the
212 9dc896c9 Andreas Müller
     * {@link TaxonName} instance identified by the <code>{name-uuid}</code>.
213 b8b8ed11 Andreas Müller
     * <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 cc3ce6e3 Andreas Kohlbecker
230
        TaxonName tnb = getCdmBaseInstance(uuid, response, (List<String>)null);
231
        if(tnb == null){
232
            return null;
233
        }
234 b8b8ed11 Andreas Müller
        Pager<TaxonNameDescription> p = descriptionService.getTaxonNameDescriptions(tnb, null, null, NAMEDESCRIPTION_INIT_STRATEGY);
235
        return p.getRecords();
236 cc3ce6e3 Andreas Kohlbecker
237 b8b8ed11 Andreas Müller
    }
238
239 707cb8bc Andreas Kohlbecker
    // 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 b8b8ed11 Andreas Müller
292
}