Project

General

Profile

Download (11.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.List;
16
import java.util.Set;
17
import java.util.UUID;
18
import java.util.stream.Collectors;
19

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

    
23
import org.apache.log4j.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.IDescriptionService;
32
import eu.etaxonomy.cdm.api.service.INameService;
33
import eu.etaxonomy.cdm.api.service.pager.Pager;
34
import eu.etaxonomy.cdm.database.UpdatableRoutingDataSource;
35
import eu.etaxonomy.cdm.model.common.CdmBase;
36
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
37
import eu.etaxonomy.cdm.model.name.NameRelationship;
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 io.swagger.annotations.Api;
43

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

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

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

    
73
    private static final EntityInitStrategy DEFAULT_INIT_STRATEGY =  new EntityInitStrategy(
74
            "$",
75
            "nomenclaturalSource.citation.inReference.inReference"
76
            );
77

    
78
    private static final List<String> TYPEDESIGNATION_INIT_STRATEGY = TypeDesignationPortalController.DEFAULT_INIT_STRATEGY;
79

    
80

    
81
    private static final List<String> NAMEDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
82
            "elements.$",
83
            "elements.multilanguageText",
84
            "elements.media",
85
            "elements.sources.citation.authorship",
86
            "elements.sources.nameUsedInSource",
87
    });
88

    
89
    private static final EntityInitStrategy NOMREF_INIT_STRATEGY = new EntityInitStrategy(
90
            "nomenclaturalSource.citation.authorship.$",
91
            "nomenclaturalSource.citation.inReference.authorship.$",
92
            "nomenclaturalSource.citation.inReference.inReference.authorship.$",
93
            "nomenclaturalSource.citation.inReference.inReference.inReference.authorship.$"
94
            );
95

    
96
    private static EntityInitStrategy nameRelationsInitStrategy = null;
97

    
98
    public static EntityInitStrategy getNameRelationsInitStrategy() {
99
        if(nameRelationsInitStrategy == null){
100
            nameRelationsInitStrategy = extendNameRelationsInitStrategies(NameController.NAME_RELATIONS_INIT_STRATEGY.getPropertyPaths(), true);
101
        }
102
        return nameRelationsInitStrategy;
103
    }
104

    
105
    public NamePortalController() {
106
        setInitializationStrategy(DEFAULT_INIT_STRATEGY.getPropertyPaths());
107
    }
108

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

    
113
        if(pathProperties == null){
114
            return pathProperties;
115
        }
116
        EntityInitStrategy initStrategy = extendNameRelationsInitStrategies(pathProperties, false);
117
        return initStrategy.getPropertyPaths();
118
    }
119

    
120
    static EntityInitStrategy extendNameRelationsInitStrategies(List<String> pathProperties, boolean addNomrefInitStrategy) {
121

    
122
        EntityInitStrategy initStrategy = new EntityInitStrategy(pathProperties);
123

    
124
        EntityInitStrategy nameRelInitExtendet = TaxonPortalController.NAMERELATIONSHIP_INIT_STRATEGY;
125

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

    
131
        List<String> transtientNameRelationsInitstrategies = initStrategy.getPropertyPaths()
132
                .stream()
133
                .filter(s -> s.startsWith("nameRelations"))
134
                .collect(Collectors.toList());
135
        if(!transtientNameRelationsInitstrategies.isEmpty()){
136
            // nameRelations is a transient property! replace it by relationsFromThisName and relationsToThisName
137
            for(String remove : transtientNameRelationsInitstrategies) {
138
                initStrategy.getPropertyPaths().remove(remove);
139
            }
140
            initStrategy.extend("relationsFromThisName", nameRelInitExtendet.getPropertyPaths(), true);
141
            initStrategy.extend("relationsToThisName", nameRelInitExtendet.getPropertyPaths(), true);
142
        } else {
143
            if(pathProperties.stream().anyMatch(s -> s.startsWith("relationsFromThisName"))){
144
                initStrategy.getPropertyPaths().remove("relationsFromThisName"); // remove the very simple one
145
                initStrategy.extend("relationsFromThisName", nameRelInitExtendet.getPropertyPaths(), true);
146
            }
147
            if(pathProperties.stream().anyMatch(s -> s.startsWith("relationsToThisName"))){
148
                initStrategy.getPropertyPaths().remove("relationsToThisName"); // remove the very simple one
149
                initStrategy.extend("relationsToThisName", nameRelInitExtendet.getPropertyPaths(), true);
150
            }
151
        }
152
        return initStrategy;
153
    }
154

    
155
    @Autowired
156
    @Override
157
    public void setService(INameService service) {
158
        this.service = service;
159
    }
160

    
161
    @Autowired
162
    private IDescriptionService descriptionService;
163

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

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

    
207
        if (request != null) {
208
            logger.info("doGetTypeDesignationsInHomotypicalGroup()" + requestPathAndQuery(request));
209
        }
210
        List<TypeDesignationBase> result = service.getTypeDesignationsInHomotypicalGroup(uuid,
211
                null, null, TYPEDESIGNATION_INIT_STRATEGY);
212
        return new ArrayList(RegistrableEntityFilter.newInstance(userHelper).filterPublishedOnly(result));
213
    }
214

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

    
235
        TaxonName tnb = getCdmBaseInstance(uuid, response, (List<String>)null);
236
        if(tnb == null){
237
            return null;
238
        }
239
        Pager<TaxonNameDescription> p = descriptionService.getTaxonNameDescriptions(tnb, null, null, NAMEDESCRIPTION_INIT_STRATEGY);
240
        return p.getRecords();
241

    
242
    }
243

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

    
259
        logger.info("doGetNameRelations" + requestPathAndQuery(request));
260
        TaxonName tnb = getCdmBaseInstance(uuid, response, getNameRelationsInitStrategy().getPropertyPaths());
261

    
262
        Set<NameRelationship> nameRelations = tnb.getNameRelations();
263

    
264
        if(nameRelations != null && nameRelations.size() > 0){
265
            Set<NameRelationship> nameRelationsFiltered = RegistrableEntityFilter.
266
                    newInstance(userHelper).filterPublishedOnly(tnb, nameRelations);
267
            return pageFromCollection(nameRelationsFiltered, pageIndex, pageSize, start, limit, response);
268
        }
269
        return null;
270
    }
271

    
272

    
273

    
274
}
(37-37/76)