merging /branches/cdmlib/SPRINT-Chichorieae1/ to trunk
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / BaseController.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.cdm.remote.controller;
12
13 import java.io.IOException;
14 import java.util.List;
15 import java.util.UUID;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
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;
26
27 import eu.etaxonomy.cdm.api.service.IService;
28 import eu.etaxonomy.cdm.model.common.CdmBase;
29 import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor;
30
31 //$Id$
32 /**
33 * based on org.cateproject.controller.common
34 * @author b.clark
35 * @author a.kohlbecker
36 *
37 * @param <T>
38 * @param <SERVICE>
39 */
40
41 public abstract class BaseController<T extends CdmBase, SERVICE extends IService<T>> extends AbstractController {
42
43 public static final Logger logger = Logger.getLogger(BaseController.class);
44
45 protected static final Integer DEFAULT_PAGE_SIZE = 30;
46
47 protected SERVICE service;
48
49
50 protected List<String> initializationStrategy = DEFAULT_INIT_STRATEGY;
51
52 public abstract void setService(SERVICE service);
53
54 public void setInitializationStrategy(List<String> initializationStrategy) {
55 this.initializationStrategy = initializationStrategy;
56 }
57
58 @InitBinder
59 public void initBinder(WebDataBinder binder) {
60 binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
61 }
62
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);
68 return obj;
69 }
70
71 /**
72 * @param request
73 * @param response
74 * @param obj
75 * @return
76 * @throws IOException
77 */
78 protected <CDM_BASE> CDM_BASE getCdmBase(HttpServletRequest request, HttpServletResponse response,
79 List<String> initStrategy, Class<CDM_BASE> clazz) throws IOException {
80 T obj = null;
81 try {
82 UUID uuid = readValueUuid(request, null);
83 Assert.notNull(uuid, HttpStatusMessage.UUID_MISSING.toString());
84
85 if(initStrategy == null){
86 // may be null is set to null via the setter
87 obj = service.find(uuid);
88 } else {
89 obj = service.load(uuid, initStrategy);
90 }
91 Assert.notNull(obj, HttpStatusMessage.UUID_NOT_FOUND.toString());
92
93 } catch (IllegalArgumentException iae) {
94 HttpStatusMessage.fromString(iae.getMessage()).send(response);
95 }
96 CDM_BASE t;
97 try {
98 t = (CDM_BASE)obj;
99 return t;
100 } catch (Exception e) {
101 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
102 return null;
103 }
104 }
105
106 /* TODO implement
107
108 private Validator validator;
109
110 private javax.validation.Validator javaxValidator;
111
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()) {
117 throw new Error();
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
120 } else {
121 // requires merging detached object ?gilead?
122 service.save(object);
123 }
124
125 return object;
126 }
127
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());
132
133
134 Set<ConstraintViolation<T>> constraintViolations = javaxValidator.validate(object);
135 if (!constraintViolations.isEmpty()) {
136 throw new Error();
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
139 } else {
140 // requires merging detached object ?gilead?
141 service.save(object);
142 }
143
144 return object;
145 }
146
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
153 } else {
154 service.save(object);
155 }
156 }
157
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
164 }
165 }
166 */
167
168
169 }