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
.io
.IOException
;
14 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import javax
.servlet
.http
.HttpServletRequest
;
18 import javax
.servlet
.http
.HttpServletResponse
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.util
.Assert
;
22 import org
.springframework
.web
.bind
.WebDataBinder
;
23 import org
.springframework
.web
.bind
.annotation
.InitBinder
;
24 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
25 import org
.springframework
.web
.bind
.annotation
.RequestMethod
;
27 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 import eu
.etaxonomy
.cdm
.remote
.editor
.UUIDPropertyEditor
;
33 * based on org.cateproject.controller.common
35 * @author a.kohlbecker
41 public abstract class BaseController
<T
extends CdmBase
, SERVICE
extends IService
<T
>> extends AbstractController
{
43 public static final Logger logger
= Logger
.getLogger(BaseController
.class);
45 protected static final Integer DEFAULT_PAGE_SIZE
= 30;
47 protected SERVICE service
;
50 protected List
<String
> initializationStrategy
= DEFAULT_INIT_STRATEGY
;
52 public abstract void setService(SERVICE service
);
54 public void setInitializationStrategy(List
<String
> initializationStrategy
) {
55 this.initializationStrategy
= initializationStrategy
;
59 public void initBinder(WebDataBinder binder
) {
60 binder
.registerCustomEditor(UUID
.class, new UUIDPropertyEditor());
63 @SuppressWarnings("unchecked")
64 @RequestMapping(method
= RequestMethod
.GET
)
65 public T
doGet(HttpServletRequest request
, HttpServletResponse response
) throws IOException
{
66 logger
.info("doGet() " + request
.getServletPath());
67 T obj
= (T
) getCdmBase(request
, response
, initializationStrategy
, CdmBase
.class);
78 protected <CDM_BASE
> CDM_BASE
getCdmBase(HttpServletRequest request
, HttpServletResponse response
,
79 List
<String
> initStrategy
, Class
<CDM_BASE
> clazz
) throws IOException
{
82 UUID uuid
= readValueUuid(request
, null);
83 Assert
.notNull(uuid
, HttpStatusMessage
.UUID_MISSING
.toString());
85 if(initStrategy
== null){
86 // may be null is set to null via the setter
87 obj
= service
.find(uuid
);
89 obj
= service
.load(uuid
, initStrategy
);
91 Assert
.notNull(obj
, HttpStatusMessage
.UUID_NOT_FOUND
.toString());
93 } catch (IllegalArgumentException iae
) {
94 HttpStatusMessage
.fromString(iae
.getMessage()).send(response
);
100 } catch (Exception e
) {
101 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
108 private Validator validator;
110 private javax.validation.Validator javaxValidator;
112 @RequestMapping(method = RequestMethod.PUT, headers="content-type=multipart/form-data")
113 public T doPutForm(@PathVariable(value = "uuid") UUID uuid, @ModelAttribute("object") T object, BindingResult result) {
114 object.setUuid(uuid);
115 validator.validate(object, result);
116 if (result.hasErrors()) {
118 // set http status code depending upon what happened, possibly return
119 // the put object and errors so that they can be rendered into a suitable error response
121 // requires merging detached object ?gilead?
122 service.save(object);
128 @RequestMapping(method = RequestMethod.PUT, headers="content-type=text/json")
129 public T doPutJSON(@PathVariable(value = "uuid") UUID uuid, @RequestBody String jsonMessage) {
130 JSONObject jsonObject = JSONObject.fromObject(jsonMessage);
131 T object = (T)JSONObject.toBean(jsonObject, this.getClass());
134 Set<ConstraintViolation<T>> constraintViolations = javaxValidator.validate(object);
135 if (!constraintViolations.isEmpty()) {
137 // set http status code depending upon what happened, possibly return
138 // the put object and errors so that they can be rendered into a suitable error response
140 // requires merging detached object ?gilead?
141 service.save(object);
147 @RequestMapping(method = RequestMethod.PUT) // the cdm-server may not allow clients to specify the uuid for resources
148 public T doPut(@PathVariable(value = "uuid") UUID uuid, @ModelAttribute("object") T object, BindingResult result) {
149 validator.validate(object, result);
150 if (result.hasErrors()) {
151 // set http status code depending upon what happened, possibly return
152 // the put object and errors so that they can be rendered into a suitable error response
154 service.save(object);
158 @RequestMapping(method = RequestMethod.DELETE)
159 public void doDelete(@PathVariable(value = "uuid") UUID uuid) {
160 T object = service.find(uuid);
161 // provided the object exists
162 service.delete(uuid);
163 // might return 204 or 200