first implementation of DerivedUnitFacadeController
[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 protected SERVICE service;
44
45 public abstract void setService(SERVICE service);
46
47 @InitBinder
48 public void initBinder(WebDataBinder binder) {
49 binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
50 }
51
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);
57 return obj;
58 }
59
60 /**
61 * @param request
62 * @param response
63 * @param obj
64 * @return
65 * @throws IOException
66 */
67 protected <CDM_BASE> CDM_BASE getCdmBase(HttpServletRequest request, HttpServletResponse response,
68 List<String> initStrategy, Class<CDM_BASE> clazz) throws IOException {
69 T obj = null;
70 try {
71 UUID uuid = readValueUuid(request, null);
72 Assert.notNull(uuid, HttpStatusMessage.UUID_MISSING.toString());
73
74 if(initStrategy == null){
75 // may be null is set to null via the setter
76 obj = service.find(uuid);
77 } else {
78 obj = service.load(uuid, initStrategy);
79 }
80 Assert.notNull(obj, HttpStatusMessage.UUID_NOT_FOUND.toString());
81
82 } catch (IllegalArgumentException iae) {
83 HttpStatusMessage.fromString(iae.getMessage()).send(response);
84 }
85 CDM_BASE t;
86 try {
87 t = (CDM_BASE)obj;
88 return t;
89 } catch (Exception e) {
90 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
91 return null;
92 }
93 }
94
95 /* TODO implement
96
97 private Validator validator;
98
99 private javax.validation.Validator javaxValidator;
100
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()) {
106 throw new Error();
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
109 } else {
110 // requires merging detached object ?gilead?
111 service.save(object);
112 }
113
114 return object;
115 }
116
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());
121
122
123 Set<ConstraintViolation<T>> constraintViolations = javaxValidator.validate(object);
124 if (!constraintViolations.isEmpty()) {
125 throw new Error();
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
128 } else {
129 // requires merging detached object ?gilead?
130 service.save(object);
131 }
132
133 return object;
134 }
135
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
142 } else {
143 service.save(object);
144 }
145 }
146
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
153 }
154 }
155 */
156
157
158 }