3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.remote
.controller
;
13 import java
.beans
.PropertyDescriptor
;
14 import java
.io
.IOException
;
15 import java
.lang
.reflect
.InvocationTargetException
;
16 import java
.lang
.reflect
.Method
;
17 import java
.lang
.reflect
.ParameterizedType
;
18 import java
.lang
.reflect
.Type
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Arrays
;
21 import java
.util
.Collection
;
22 import java
.util
.List
;
23 import java
.util
.UUID
;
25 import javax
.servlet
.http
.HttpServletRequest
;
26 import javax
.servlet
.http
.HttpServletResponse
;
28 import org
.apache
.commons
.beanutils
.PropertyUtils
;
29 import org
.apache
.commons
.io
.FilenameUtils
;
30 import org
.apache
.commons
.lang
.StringUtils
;
31 import org
.hibernate
.mapping
.Map
;
32 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
33 import org
.springframework
.web
.bind
.WebDataBinder
;
34 import org
.springframework
.web
.bind
.annotation
.InitBinder
;
35 import org
.springframework
.web
.bind
.annotation
.PathVariable
;
36 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
37 import org
.springframework
.web
.bind
.annotation
.RequestMethod
;
38 import org
.springframework
.web
.bind
.annotation
.RequestParam
;
39 import org
.springframework
.web
.servlet
.ModelAndView
;
41 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
42 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
43 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
44 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
45 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
46 import eu
.etaxonomy
.cdm
.model
.reference
.INomenclaturalReference
;
47 import eu
.etaxonomy
.cdm
.remote
.editor
.UUIDPropertyEditor
;
50 * based on org.cateproject.controller.common
52 * @author a.kohlbecker
58 public abstract class BaseController
<T
extends CdmBase
, SERVICE
extends IService
<T
>> extends AbstractController
{
60 protected SERVICE service
;
62 protected Class
<T
> baseClass
;
64 public abstract void setService(SERVICE service
);
66 public BaseController (){
68 Type superClass
= this.getClass().getGenericSuperclass();
69 if(superClass
instanceof ParameterizedType
){
70 ParameterizedType parametrizedSuperClass
= (ParameterizedType
) superClass
;
71 Type
[] typeArguments
= parametrizedSuperClass
.getActualTypeArguments();
73 if(typeArguments
.length
> 1 && typeArguments
[0] instanceof Class
<?
>){
74 baseClass
= (Class
<T
>) typeArguments
[0];
76 logger
.error("unable to find baseClass");
82 public void initBinder(WebDataBinder binder
) {
83 binder
.registerCustomEditor(UUID
.class, new UUIDPropertyEditor());
86 //TODO implement bulk version of this method
87 @RequestMapping(method
= RequestMethod
.GET
)
88 public T
doGet(@PathVariable("uuid") UUID uuid
,
89 HttpServletRequest request
,
90 HttpServletResponse response
) throws IOException
{
92 logger
.info("doGet() " + request
.getServletPath());
93 T obj
= (T
) getCdmBaseInstance(uuid
, response
, initializationStrategy
);
102 * @throws IOException
104 * TODO implement bulk version of this method
106 @RequestMapping(value
= "*", method
= RequestMethod
.GET
)
107 public ModelAndView
doGetMethod(
108 @PathVariable("uuid") UUID uuid
,
109 // doPage request parametes
110 @RequestParam(value
= "pageNumber", required
= false) Integer pageNumber
,
111 @RequestParam(value
= "pageSize", required
= false) Integer pageSize
,
112 // doList request parametes
113 @RequestParam(value
= "start", required
= false) Integer start
,
114 @RequestParam(value
= "limit", required
= false) Integer limit
,
115 HttpServletRequest request
,
116 HttpServletResponse response
) throws IOException
{
118 ModelAndView modelAndView
= new ModelAndView();
120 String servletPath
= request
.getServletPath();
121 String baseName
= FilenameUtils
.getBaseName(servletPath
);
124 logger
.info("doGetMethod()[doGet" + StringUtils
.capitalize(baseName
) + "] " + request
.getServletPath());
127 // T instance = getCdmBaseInstance(uuid, response, (List<String>)null);
129 //Class<?> propertyClass = propertyClass(instance, baseName);
131 Object objectFromProperty
= getCdmBaseProperty(uuid
, baseName
, response
);// invokeProperty(instance, baseName, response);
135 if(objectFromProperty
!= null){
137 if( Collection
.class.isAssignableFrom(objectFromProperty
.getClass())){
138 // Map types cannot be returned as list or in a pager!
140 Collection c
= (Collection
)objectFromProperty
;
143 limit
= (limit
== null ? DEFAULT_PAGE_SIZE
: limit
);
144 Collection sub_c
= subCollection(c
, start
, limit
);
145 modelAndView
.addObject(sub_c
);
148 //FIXME use real paging mechanism of according service class instead of subCollection()
149 //FIXME use BaseListController.normalizeAndValidatePagerParameters(pageNumber, pageSize, response);
150 pageSize
= (pageSize
== null ? DEFAULT_PAGE_SIZE
: pageSize
);
151 pageNumber
= (pageNumber
== null ?
0 : pageNumber
);
152 start
= pageNumber
* pageSize
;
153 List sub_c
= subCollection(c
, start
, pageSize
);
154 Pager p
= new DefaultPagerImpl(pageNumber
, c
.size(), pageSize
, sub_c
);
155 modelAndView
.addObject(p
);
159 modelAndView
.addObject(objectFromProperty
);
164 if(modelAndView
.isEmpty()){
172 public Object
getCdmBaseProperty(UUID uuid
, String property
, HttpServletResponse response
) throws IOException
{
174 T instance
= (T
) HibernateProxyHelper
.deproxy(getCdmBaseInstance(uuid
, response
, property
));
176 Object objectFromProperty
= invokeProperty(instance
, property
, response
);
178 return objectFromProperty
;
181 private Class
<?
> propertyClass(T instance
, String baseName
) {
182 PropertyDescriptor propertyDescriptor
= null;
185 propertyDescriptor
= PropertyUtils
.getPropertyDescriptor(instance
, baseName
);
186 if(propertyDescriptor
!= null){
187 c
= propertyDescriptor
.getClass();
189 } catch (IllegalAccessException e
) {
190 // TODO Auto-generated catch block
192 } catch (InvocationTargetException e
) {
193 // TODO Auto-generated catch block
195 } catch (NoSuchMethodException e
) {
196 // TODO Auto-generated catch block
207 * @param pathProperties
209 * @throws IOException
211 @SuppressWarnings("unchecked")
212 protected final <SUB_T
extends T
> SUB_T
getCdmBaseInstance(Class
<SUB_T
> clazz
, UUID uuid
, HttpServletResponse response
, List
<String
> pathProperties
)
215 CdmBase cdmBaseObject
= getCdmBaseInstance(uuid
, response
, pathProperties
);
216 if(!clazz
.isAssignableFrom(cdmBaseObject
.getClass())){
217 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
219 return (SUB_T
) cdmBaseObject
;
227 * @param pathProperty
229 * @throws IOException
231 @SuppressWarnings("unchecked")
232 protected final <SUB_T
extends T
> SUB_T
getCdmBaseInstance(Class
<SUB_T
> clazz
, UUID uuid
, HttpServletResponse response
, String pathProperty
)
235 CdmBase cdmBaseObject
= getCdmBaseInstance(uuid
, response
, pathProperty
);
236 if(!clazz
.isAssignableFrom(cdmBaseObject
.getClass())){
237 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
239 return (SUB_T
) cdmBaseObject
;
245 * @param pathProperty
247 * @throws IOException
249 protected final T
getCdmBaseInstance(UUID uuid
, HttpServletResponse response
, String pathProperty
)
251 return getCdmBaseInstance(baseClass
, uuid
, response
, Arrays
252 .asList(new String
[] { pathProperty
}));
259 * @param pathProperties
261 * @throws IOException
263 protected final T
getCdmBaseInstance(UUID uuid
, HttpServletResponse response
, List
<String
> pathProperties
)
265 return getCdmBaseInstance(baseClass
, service
, uuid
, response
, pathProperties
);
274 * @param pathProperties
276 * @throws IOException
278 protected final <CDM_BASE
extends CdmBase
> CDM_BASE
getCdmBaseInstance(Class
<CDM_BASE
> clazz
, IService
<CDM_BASE
> service
, UUID uuid
, HttpServletResponse response
, List
<String
> pathProperties
)
281 CDM_BASE cdmBaseObject
= service
.load(uuid
, pathProperties
);
282 if (cdmBaseObject
== null) {
283 HttpStatusMessage
.UUID_NOT_FOUND
.send(response
);
285 return cdmBaseObject
;
293 * @throws IOException
295 private final Object
invokeProperty(T instance
,
296 String baseName
, HttpServletResponse response
) throws IOException
{
298 Object result
= null;
300 PropertyDescriptor propertyDescriptor
= PropertyUtils
.getPropertyDescriptor(instance
, baseName
);
301 if(propertyDescriptor
== null){
302 throw new NoSuchMethodException("No such method: " + instance
.getClass().getSimpleName() + ".get" + baseName
);
304 Method method
= propertyDescriptor
.getReadMethod();
306 Class
<?
> returnType
= method
.getReturnType();
308 if(CdmBase
.class.isAssignableFrom(returnType
)
309 || Collection
.class.isAssignableFrom(returnType
)
310 || Map
.class.isAssignableFrom(returnType
)
311 || INomenclaturalReference
.class.isAssignableFrom(returnType
)){
313 result
= method
.invoke(instance
, (Object
[])null);
315 result
= HibernateProxyHelper
.deproxy(result
);
318 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
320 } catch (SecurityException e
) {
321 logger
.error("SecurityException: ", e
);
322 HttpStatusMessage
.INTERNAL_ERROR
.send(response
);
323 } catch (NoSuchMethodException e
) {
324 HttpStatusMessage
.PROPERTY_NOT_FOUND
.send(response
);
325 } catch (IllegalArgumentException e
) {
326 HttpStatusMessage
.PROPERTY_NOT_FOUND
.send(response
);
327 } catch (IllegalAccessException e
) {
328 HttpStatusMessage
.PROPERTY_NOT_FOUND
.send(response
);
329 } catch (InvocationTargetException e
) {
330 HttpStatusMessage
.PROPERTY_NOT_FOUND
.send(response
);
335 private <E
> List
<E
> subCollection(Collection
<?
extends E
> c
, Integer start
, Integer length
){
336 List
<E
> sub_c
= new ArrayList
<E
>(length
);
337 if(c
.size() > length
){
338 E
[] a
= (E
[]) c
.toArray();
339 for(int i
= start
; i
< start
+ length
; i
++){
352 private Validator validator;
354 private javax.validation.Validator javaxValidator;
356 @RequestMapping(method = RequestMethod.PUT, headers="content-type=multipart/form-data")
357 public T doPutForm(@PathVariable(value = "uuid") UUID uuid, @ModelAttribute("object") T object, BindingResult result) {
358 object.setUuid(uuid);
359 validator.validate(object, result);
360 if (result.hasErrors()) {
362 // set http status code depending upon what happened, possibly return
363 // the put object and errors so that they can be rendered into a suitable error response
365 // requires merging detached object ?gilead?
366 service.save(object);
372 @RequestMapping(method = RequestMethod.PUT, headers="content-type=text/json")
373 public T doPutJSON(@PathVariable(value = "uuid") UUID uuid, @RequestBody String jsonMessage) {
374 JSONObject jsonObject = JSONObject.fromObject(jsonMessage);
375 T object = (T)JSONObject.toBean(jsonObject, this.getClass());
378 Set<ConstraintViolation<T>> constraintViolations = javaxValidator.validate(object);
379 if (!constraintViolations.isEmpty()) {
381 // set http status code depending upon what happened, possibly return
382 // the put object and errors so that they can be rendered into a suitable error response
384 // requires merging detached object ?gilead?
385 service.save(object);
391 @RequestMapping(method = RequestMethod.PUT) // the cdm-server may not allow clients to specify the uuid for resources
392 public T doPut(@PathVariable(value = "uuid") UUID uuid, @ModelAttribute("object") T object, BindingResult result) {
393 validator.validate(object, result);
394 if (result.hasErrors()) {
395 // set http status code depending upon what happened, possibly return
396 // the put object and errors so that they can be rendered into a suitable error response
398 service.save(object);
402 @RequestMapping(method = RequestMethod.DELETE)
403 public void doDelete(@PathVariable(value = "uuid") UUID uuid) {
404 T object = service.find(uuid);
405 // provided the object exists
406 service.delete(uuid);
407 // might return 204 or 200