large number of changes relating to new architecture
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / BaseController.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.util.UUID;
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
15
16 import javax.servlet.http.HttpServletRequest;
17
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;
22
23 import eu.etaxonomy.cdm.api.service.IService;
24 import eu.etaxonomy.cdm.model.common.CdmBase;
25
26 /**
27 * based on org.cateproject.controller.common
28 * @author b.clark
29 * @author a.kohlbecker
30 *
31 * @param <T>
32 * @param <SERVICE>
33 */
34
35 public abstract class BaseController<T extends CdmBase, SERVICE extends IService<T>> {
36
37 public static final Logger logger = Logger.getLogger(BaseController.class);
38
39 protected static final Integer DEFAULT_PAGE_SIZE = 30;
40
41 protected SERVICE service;
42
43 protected Pattern uuidParameterPattern = null;
44
45 protected void setUuidParameterPattern(String pattern){
46 uuidParameterPattern = Pattern.compile(pattern);
47 }
48
49 public abstract void setService(SERVICE service);
50
51 /**@InitBinder
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());
55 }
56 */
57
58 protected UUID readValueUuid(HttpServletRequest request) {
59 String path = request.getServletPath();
60 if(path != null) {
61 Matcher uuidMatcher = uuidParameterPattern.matcher(path);
62 if(uuidMatcher.matches() && uuidMatcher.groupCount() > 0){
63 try {
64 UUID uuid = UUID.fromString(uuidMatcher.group(1));
65 return uuid;
66 } catch (Exception e) {
67 logger.warn(uuidMatcher.group(1) + "is not a uuid");
68 }
69 }
70 }
71 return null;
72 }
73
74 @RequestMapping(method = RequestMethod.GET)
75 public T doGet(HttpServletRequest request) {
76
77 UUID uuid = readValueUuid(request);
78 Assert.notNull(uuid, "no valid uuid");
79 if(uuid == null){
80 return null;
81 }
82 return service.findByUuid(uuid);
83 }
84
85 /* TODO implement
86 *
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
93 } else {
94 // requires merging detached object ?gilead?
95 service.update(object);
96 }
97 }
98
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
105 } else {
106 service.save(object);
107 }
108 }
109
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
116 }
117 }
118 */
119
120
121 }