comparison src/main/java/de/intevation/lada/rest/StatusService.java @ 787:7033810468d9

Validate status objects.
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 18 Nov 2015 15:37:32 +0100
parents 574391f1d88f
children dea5ed4b7fa4
comparison
equal deleted inserted replaced
786:9a2dcb355d89 787:7033810468d9
4 * This file is Free Software under the GNU GPL (v>=3) 4 * This file is Free Software under the GNU GPL (v>=3)
5 * and comes with ABSOLUTELY NO WARRANTY! Check out 5 * and comes with ABSOLUTELY NO WARRANTY! Check out
6 * the documentation coming with IMIS-Labordaten-Application for details. 6 * the documentation coming with IMIS-Labordaten-Application for details.
7 */ 7 */
8 package de.intevation.lada.rest; 8 package de.intevation.lada.rest;
9
10 import java.util.List;
9 11
10 import javax.enterprise.context.RequestScoped; 12 import javax.enterprise.context.RequestScoped;
11 import javax.inject.Inject; 13 import javax.inject.Inject;
12 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletRequest;
13 import javax.ws.rs.DELETE; 15 import javax.ws.rs.DELETE;
21 import javax.ws.rs.core.HttpHeaders; 23 import javax.ws.rs.core.HttpHeaders;
22 import javax.ws.rs.core.MediaType; 24 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.MultivaluedMap; 25 import javax.ws.rs.core.MultivaluedMap;
24 import javax.ws.rs.core.UriInfo; 26 import javax.ws.rs.core.UriInfo;
25 27
28 import org.apache.log4j.Logger;
29
26 import de.intevation.lada.lock.LockConfig; 30 import de.intevation.lada.lock.LockConfig;
27 import de.intevation.lada.lock.LockType; 31 import de.intevation.lada.lock.LockType;
28 import de.intevation.lada.lock.ObjectLocker; 32 import de.intevation.lada.lock.ObjectLocker;
29 import de.intevation.lada.model.land.LMessung; 33 import de.intevation.lada.model.land.LMessung;
30 import de.intevation.lada.model.land.LStatusProtokoll; 34 import de.intevation.lada.model.land.LStatusProtokoll;
36 import de.intevation.lada.util.data.QueryBuilder; 40 import de.intevation.lada.util.data.QueryBuilder;
37 import de.intevation.lada.util.data.Repository; 41 import de.intevation.lada.util.data.Repository;
38 import de.intevation.lada.util.data.RepositoryType; 42 import de.intevation.lada.util.data.RepositoryType;
39 import de.intevation.lada.util.rest.RequestMethod; 43 import de.intevation.lada.util.rest.RequestMethod;
40 import de.intevation.lada.util.rest.Response; 44 import de.intevation.lada.util.rest.Response;
45 import de.intevation.lada.validation.Validator;
46 import de.intevation.lada.validation.Violation;
47 import de.intevation.lada.validation.annotation.ValidationConfig;
41 48
42 /** 49 /**
43 * REST service for Status objects. 50 * REST service for Status objects.
44 * <p> 51 * <p>
45 * The services produce data in the application/json media type. 52 * The services produce data in the application/json media type.
75 */ 82 */
76 @Path("status") 83 @Path("status")
77 @RequestScoped 84 @RequestScoped
78 public class StatusService { 85 public class StatusService {
79 86
87 @Inject
88 private Logger logger = Logger.getLogger(StatusService.class);
89
80 /** 90 /**
81 * The data repository granting read/write access. 91 * The data repository granting read/write access.
82 */ 92 */
83 @Inject 93 @Inject
84 @RepositoryConfig(type=RepositoryType.RW) 94 @RepositoryConfig(type=RepositoryType.RW)
95 * The authorization module. 105 * The authorization module.
96 */ 106 */
97 @Inject 107 @Inject
98 @AuthorizationConfig(type=AuthorizationType.HEADER) 108 @AuthorizationConfig(type=AuthorizationType.HEADER)
99 private Authorization authorization; 109 private Authorization authorization;
110
111 @Inject
112 @ValidationConfig(type="Status")
113 private Validator validator;
100 114
101 /** 115 /**
102 * Get all Status objects. 116 * Get all Status objects.
103 * <p> 117 * <p>
104 * The requested objects can be filtered using a URL parameter named 118 * The requested objects can be filtered using a URL parameter named
147 public Response getById( 161 public Response getById(
148 @Context HttpHeaders headers, 162 @Context HttpHeaders headers,
149 @Context HttpServletRequest request, 163 @Context HttpServletRequest request,
150 @PathParam("id") String id 164 @PathParam("id") String id
151 ) { 165 ) {
166 Response response = defaultRepo.getById(
167 LStatusProtokoll.class,
168 Integer.valueOf(id),
169 "land");
170 LStatusProtokoll status = (LStatusProtokoll)response.getData();
171 Violation violation = validator.validate(status);
172 if (violation.hasErrors() || violation.hasWarnings()) {
173 response.setErrors(violation.getErrors());
174 response.setWarnings(violation.getWarnings());
175 }
176
152 return authorization.filter( 177 return authorization.filter(
153 request, 178 request,
154 defaultRepo.getById(LStatusProtokoll.class, Integer.valueOf(id), "land"), 179 response,
155 LStatusProtokoll.class); 180 LStatusProtokoll.class);
156 } 181 }
157 182
158 /** 183 /**
159 * Create a Status object. 184 * Create a Status object.
224 status.setStatusStufe(currentStatus.getStatusStufe() + 1); 249 status.setStatusStufe(currentStatus.getStatusStufe() + 1);
225 } 250 }
226 else { 251 else {
227 return new Response(false, 699, null); 252 return new Response(false, 699, null);
228 } 253 }
254 }
255 Violation violation = validator.validate(status);
256 if (violation.hasErrors()) {
257 Response response = new Response(false, 604, status);
258 response.setErrors(violation.getErrors());
259 response.setWarnings(violation.getWarnings());
260 return response;
229 } 261 }
230 Response response = defaultRepo.create(status, "land"); 262 Response response = defaultRepo.create(status, "land");
231 LStatusProtokoll created = (LStatusProtokoll)response.getData(); 263 LStatusProtokoll created = (LStatusProtokoll)response.getData();
232 messung.setStatus(created.getId()); 264 messung.setStatus(created.getId());
233 defaultRepo.update(messung, "land"); 265 defaultRepo.update(messung, "land");
283 statusNew.setErzeuger(status.getErzeuger()); 315 statusNew.setErzeuger(status.getErzeuger());
284 statusNew.setMessungsId(status.getMessungsId()); 316 statusNew.setMessungsId(status.getMessungsId());
285 statusNew.setStatusStufe(status.getStatusStufe()); 317 statusNew.setStatusStufe(status.getStatusStufe());
286 statusNew.setStatusWert(status.getStatusWert()); 318 statusNew.setStatusWert(status.getStatusWert());
287 statusNew.setText(status.getText()); 319 statusNew.setText(status.getText());
320 Violation violation = validator.validate(statusNew);
321 if (violation.hasErrors()) {
322 Response response = new Response(false, 604, statusNew);
323 response.setErrors(violation.getErrors());
324 response.setWarnings(violation.getWarnings());
325 return response;
326 }
327
288 Response response = defaultRepo.create(statusNew, "land"); 328 Response response = defaultRepo.create(statusNew, "land");
289 LStatusProtokoll created = (LStatusProtokoll)response.getData(); 329 LStatusProtokoll created = (LStatusProtokoll)response.getData();
290 messung.setStatus(created.getId()); 330 messung.setStatus(created.getId());
291 defaultRepo.update(messung, "land"); 331 defaultRepo.update(messung, "land");
292 332
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)