fix #8395 avoiding NPE in NameCatalogueController.getDatasetIdName()
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / NamePortalController.java
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.List;
15 import java.util.UUID;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
20 import org.apache.log4j.Logger;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.stereotype.Controller;
23 import org.springframework.web.bind.annotation.PathVariable;
24 import org.springframework.web.bind.annotation.RequestMapping;
25 import org.springframework.web.bind.annotation.RequestMethod;
26
27 import eu.etaxonomy.cdm.api.service.IDescriptionService;
28 import eu.etaxonomy.cdm.api.service.INameService;
29 import eu.etaxonomy.cdm.api.service.pager.Pager;
30 import eu.etaxonomy.cdm.model.common.CdmBase;
31 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
32 import eu.etaxonomy.cdm.model.name.TaxonName;
33 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
34 import eu.etaxonomy.cdm.persistence.dao.initializer.EntityInitStrategy;
35 import io.swagger.annotations.Api;
36
37 /**
38 * The NamePortalController class is a Spring MVC Controller.
39 * <p>
40 * The syntax of the mapped service URIs contains the the {datasource-name} path element.
41 * The available {datasource-name}s are defined in a configuration file which
42 * is loaded by the {@link UpdatableRoutingDataSource}. If the
43 * UpdatableRoutingDataSource is not being used in the actual application
44 * context any arbitrary {datasource-name} may be used.
45 * <p>
46 * Methods mapped at type level, inherited from super classes ({@link BaseController}):
47 * <blockquote>
48 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;name&#x002F;{name-uuid}</b>
49 *
50 * Get the {@link TaxonName} instance identified by the <code>{name-uuid}</code>.
51 * The returned TaxonName is initialized by
52 * the following strategy: -- NONE --
53 * </blockquote>
54 *
55 * @author a.kohlbecker
56 * @since 24.03.2009
57 */
58
59 @Controller
60 @Api("portal_name")
61 @RequestMapping(value = {"/portal/name/{uuid}"})
62 public class NamePortalController extends BaseController<TaxonName, INameService> {
63
64 private static final Logger logger = Logger.getLogger(NamePortalController.class);
65
66 private static final List<String> TYPEDESIGNATION_INIT_STRATEGY = TypeDesignationPortalController.DEFAULT_INIT_STRATEGY;
67
68
69 private static final List<String> NAMEDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
70 "elements.$",
71 "elements.multilanguageText",
72 "elements.media",
73 });
74
75 @Override
76 protected <CDM_BASE extends CdmBase> List<String> complementInitStrategy(Class<CDM_BASE> clazz,
77 List<String> pathProperties) {
78
79 if(pathProperties == null){
80 return pathProperties;
81 }
82
83 EntityInitStrategy initStrategy = new EntityInitStrategy(pathProperties);
84
85 if(pathProperties.contains("nameRelations")){
86 // nameRelations is a transient property!
87 initStrategy.getPropertyPaths().remove("nameRelations");
88 initStrategy.extend("relationsFromThisName", TaxonPortalController.NAMERELATIONSHIP_INIT_STRATEGY, true);
89 initStrategy.extend("relationsToThisName", TaxonPortalController.NAMERELATIONSHIP_INIT_STRATEGY, true);
90 } else {
91 if(pathProperties.contains("relationsFromThisName")){
92 initStrategy.getPropertyPaths().remove("relationsFromThisName");
93 initStrategy.extend("relationsFromThisName", TaxonPortalController.NAMERELATIONSHIP_INIT_STRATEGY, true);
94 }
95 if(pathProperties.contains("relationsToThisName")){
96 initStrategy.getPropertyPaths().remove("relationsToThisName");
97 initStrategy.extend("relationsToThisName", TaxonPortalController.NAMERELATIONSHIP_INIT_STRATEGY, true);
98 }
99 }
100
101 return initStrategy.getPropertyPaths();
102 }
103
104 /* (non-Javadoc)
105 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
106 */
107 @Autowired
108 @Override
109 public void setService(INameService service) {
110 this.service = service;
111 }
112
113 @Autowired
114 private IDescriptionService descriptionService;
115
116 /**
117 * Get the list of {@link TypeDesignationBase}s of the
118 * {@link TaxonName} instance identified by the <code>{name-uuid}</code>.
119 * <p>
120 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;name&#x002F;{name-uuid}&#x002F;typeDesignations</b>
121 *
122 * @param request
123 * @param response
124 * @return a List of {@link TypeDesignationBase} entities which are initialized
125 * using the following initialization strategy:
126 * {@link #TYPEDESIGNATION_INIT_STRATEGY}
127 * @throws IOException
128 */
129 @RequestMapping(
130 value = {"typeDesignations"},
131 method = RequestMethod.GET)
132 public List<TypeDesignationBase> doGetTypeDesignations(@PathVariable("uuid") UUID uuid,
133 HttpServletRequest request, HttpServletResponse response)throws IOException {
134 TaxonName tnb = getCdmBaseInstance(uuid, response, (List<String>)null);
135 Pager<TypeDesignationBase> p = service.getTypeDesignations(tnb, null, null, null, TYPEDESIGNATION_INIT_STRATEGY);
136 return p.getRecords();
137 }
138
139 /**
140 * Get the list of {@link TypeDesignationBase}s associated to any name in the same homotypical group to which
141 * the {@link TaxonName} identified by the <code>{name-uuid}</code> belongs.
142 * <p>
143 * URI: <b>&#x002F;{datasource-name}&#x002F;name&#x002F;{name-uuid}&#x002F;typeDesignations</b>
144 *
145 * @param request
146 * @param response
147 * @return a List of {@link TypeDesignationBase} entities which are initialized
148 * using the {@link #TYPEDESIGNATION_INIT_STRATEGY}
149 * @throws IOException
150 */
151 @RequestMapping(value = { "typeDesignationsInHomotypicalGroup" }, method = RequestMethod.GET)
152 public List<TypeDesignationBase> doGetTypeDesignationsInHomotypicalGroup(
153 @PathVariable("uuid") UUID uuid, HttpServletRequest request,
154 HttpServletResponse response) throws IOException {
155
156 if (request != null) {
157 logger.info("doGetTypeDesignationsInHomotypicalGroup()" + requestPathAndQuery(request));
158 }
159 List<TypeDesignationBase> result = service.getTypeDesignationsInHomotypicalGroup(uuid,
160 null, null, TYPEDESIGNATION_INIT_STRATEGY);
161 return result;
162 }
163
164 /**
165 * Get the list of {@link TaxonNameDescription}s of the Name associated with 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;descriptions</b>
169 *
170 * @param request
171 * @param response
172 * @return a List of {@link TaxonNameDescription} entities which are initialized
173 * using the following initialization strategy:
174 * {@link #NAMEDESCRIPTION_INIT_STRATEGY}
175 * @throws IOException
176 */
177 @RequestMapping(
178 value = {"taxonNameDescriptions"},
179 method = RequestMethod.GET)
180 public List<TaxonNameDescription> doGetNameDescriptions(@PathVariable("uuid") UUID uuid,
181 HttpServletRequest request, HttpServletResponse response)throws IOException {
182 logger.info("doGetNameDescriptions()" + request.getRequestURI());
183 TaxonName tnb = service.load(uuid, null);
184 Pager<TaxonNameDescription> p = descriptionService.getTaxonNameDescriptions(tnb, null, null, NAMEDESCRIPTION_INIT_STRATEGY);
185 return p.getRecords();
186 }
187
188
189 }