2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.remote
.controller
;
12 import java
.util
.UUID
;
13 import java
.util
.regex
.Matcher
;
14 import java
.util
.regex
.Pattern
;
16 import javax
.servlet
.http
.HttpServletRequest
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.springframework
.util
.Assert
;
20 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
21 import org
.springframework
.web
.bind
.annotation
.RequestMethod
;
23 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
24 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 * based on org.cateproject.controller.common
29 * @author a.kohlbecker
35 public abstract class BaseController
<T
extends CdmBase
, SERVICE
extends IService
<T
>> {
37 public static final Logger logger
= Logger
.getLogger(BaseController
.class);
39 protected static final Integer DEFAULT_PAGE_SIZE
= 30;
41 protected SERVICE service
;
43 protected Pattern uuidParameterPattern
= null;
45 protected void setUuidParameterPattern(String pattern
){
46 uuidParameterPattern
= Pattern
.compile(pattern
);
49 public abstract void setService(SERVICE service
);
52 public void initBinder(WebDataBinder binder) {
53 binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
54 //TODO do we need this one?: binder.registerCustomEditor(Class.class, new ClassPropertyEditor());
58 protected UUID
readValueUuid(HttpServletRequest request
) {
59 String path
= request
.getServletPath();
61 Matcher uuidMatcher
= uuidParameterPattern
.matcher(path
);
62 if(uuidMatcher
.matches() && uuidMatcher
.groupCount() > 0){
64 UUID uuid
= UUID
.fromString(uuidMatcher
.group(1));
66 } catch (Exception e
) {
67 logger
.warn(uuidMatcher
.group(1) + "is not a uuid");
74 @RequestMapping(method
= RequestMethod
.GET
)
75 public T
doGet(HttpServletRequest request
) {
77 UUID uuid
= readValueUuid(request
);
78 Assert
.notNull(uuid
, "no valid uuid");
82 return service
.findByUuid(uuid
);
87 @RequestMapping(method = RequestMethod.POST)
88 public T doPost(@PathVariable(value = "uuid") UUID uuid, @ModelAttribute("object") T object, BindingResult result) {
89 validator.validate(object, result);
90 if (result.hasErrors()) {
91 // set http status code depending upon what happened, possibly return
92 // the put object and errors so that they can be rendered into a suitable error response
94 // requires merging detached object ?gilead?
95 service.update(object);
99 @RequestMapping(method = RequestMethod.PUT) // the cdm-server may not allow clients to specify the uuid for resources
100 public T doPut(@PathVariable(value = "uuid") UUID uuid, @ModelAttribute("object") T object, BindingResult result) {
101 validator.validate(object, result);
102 if (result.hasErrors()) {
103 // set http status code depending upon what happened, possibly return
104 // the put object and errors so that they can be rendered into a suitable error response
106 service.save(object);
110 @RequestMapping(method = RequestMethod.DELETE)
111 public void doDelete(@PathVariable(value = "uuid") UUID uuid) {
112 T object = service.find(uuid);
113 // provided the object exists
114 service.delete(uuid);
115 // might return 204 or 200