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 protected SERVICE service
;
45 public abstract void setService(SERVICE service
);
48 public void initBinder(WebDataBinder binder
) {
49 binder
.registerCustomEditor(UUID
.class, new UUIDPropertyEditor());
52 @SuppressWarnings("unchecked")
53 @RequestMapping(method
= RequestMethod
.GET
)
54 public T
doGet(HttpServletRequest request
, HttpServletResponse response
) throws IOException
{
55 logger
.info("doGet() " + request
.getServletPath());
56 T obj
= (T
) getCdmBase(request
, response
, initializationStrategy
, CdmBase
.class);
67 protected <CDM_BASE
> CDM_BASE
getCdmBase(HttpServletRequest request
, HttpServletResponse response
,
68 List
<String
> initStrategy
, Class
<CDM_BASE
> clazz
) throws IOException
{
71 UUID uuid
= readValueUuid(request
, null);
72 Assert
.notNull(uuid
, HttpStatusMessage
.UUID_MISSING
.toString());
74 if(initStrategy
== null){
75 // may be null is set to null via the setter
76 obj
= service
.find(uuid
);
78 obj
= service
.load(uuid
, initStrategy
);
80 Assert
.notNull(obj
, HttpStatusMessage
.UUID_NOT_FOUND
.toString());
82 } catch (IllegalArgumentException iae
) {
83 HttpStatusMessage
.fromString(iae
.getMessage()).send(response
);
89 } catch (Exception e
) {
90 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
97 private Validator validator;
99 private javax.validation.Validator javaxValidator;
101 @RequestMapping(method = RequestMethod.PUT, headers="content-type=multipart/form-data")
102 public T doPutForm(@PathVariable(value = "uuid") UUID uuid, @ModelAttribute("object") T object, BindingResult result) {
103 object.setUuid(uuid);
104 validator.validate(object, result);
105 if (result.hasErrors()) {
107 // set http status code depending upon what happened, possibly return
108 // the put object and errors so that they can be rendered into a suitable error response
110 // requires merging detached object ?gilead?
111 service.save(object);
117 @RequestMapping(method = RequestMethod.PUT, headers="content-type=text/json")
118 public T doPutJSON(@PathVariable(value = "uuid") UUID uuid, @RequestBody String jsonMessage) {
119 JSONObject jsonObject = JSONObject.fromObject(jsonMessage);
120 T object = (T)JSONObject.toBean(jsonObject, this.getClass());
123 Set<ConstraintViolation<T>> constraintViolations = javaxValidator.validate(object);
124 if (!constraintViolations.isEmpty()) {
126 // set http status code depending upon what happened, possibly return
127 // the put object and errors so that they can be rendered into a suitable error response
129 // requires merging detached object ?gilead?
130 service.save(object);
136 @RequestMapping(method = RequestMethod.PUT) // the cdm-server may not allow clients to specify the uuid for resources
137 public T doPut(@PathVariable(value = "uuid") UUID uuid, @ModelAttribute("object") T object, BindingResult result) {
138 validator.validate(object, result);
139 if (result.hasErrors()) {
140 // set http status code depending upon what happened, possibly return
141 // the put object and errors so that they can be rendered into a suitable error response
143 service.save(object);
147 @RequestMapping(method = RequestMethod.DELETE)
148 public void doDelete(@PathVariable(value = "uuid") UUID uuid) {
149 T object = service.find(uuid);
150 // provided the object exists
151 service.delete(uuid);
152 // might return 204 or 200