comparison src/main/java/de/intevation/lada/rest/ProbeService.java @ 553:2b7c7f3e51b7 openid

Merge current default into openid
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 13 Mar 2015 14:26:52 +0100
parents 8e3f57e2f4af 7a0e3d49ae33
children b0d674240c29
comparison
equal deleted inserted replaced
552:28fd6616e0f8 553:2b7c7f3e51b7
43 import de.intevation.lada.util.auth.AuthorizationType; 43 import de.intevation.lada.util.auth.AuthorizationType;
44 import de.intevation.lada.util.data.QueryBuilder; 44 import de.intevation.lada.util.data.QueryBuilder;
45 import de.intevation.lada.util.data.Repository; 45 import de.intevation.lada.util.data.Repository;
46 import de.intevation.lada.util.data.RepositoryType; 46 import de.intevation.lada.util.data.RepositoryType;
47 import de.intevation.lada.util.rest.Response; 47 import de.intevation.lada.util.rest.Response;
48 import de.intevation.lada.validation.Validator;
49 import de.intevation.lada.validation.Violation;
50 import de.intevation.lada.validation.annotation.ValidationConfig;
48 51
49 52
50 /** 53 /**
51 * This class produces a RESTful service to interact with probe objects. 54 * This class produces a RESTful service to interact with probe objects.
52 * 55 *
72 75
73 /* The authorization module.*/ 76 /* The authorization module.*/
74 @Inject 77 @Inject
75 @AuthorizationConfig(type=AuthorizationType.NONE) 78 @AuthorizationConfig(type=AuthorizationType.NONE)
76 private Authorization authorization; 79 private Authorization authorization;
80
81 @Inject
82 @ValidationConfig(type="Probe")
83 private Validator validator;
77 84
78 /** 85 /**
79 * Get all probe objects. 86 * Get all probe objects.
80 * 87 *
81 * @return Response object containing all probe objects. 88 * @return Response object containing all probe objects.
139 ) { 146 ) {
140 if (!authentication.isAuthenticated(headers)) { 147 if (!authentication.isAuthenticated(headers)) {
141 logger.debug("User is not authenticated!"); 148 logger.debug("User is not authenticated!");
142 return new Response(false, 699, null); 149 return new Response(false, 699, null);
143 } 150 }
144 return defaultRepo.getById(LProbe.class, Integer.valueOf(id), "land"); 151 Response response =
152 defaultRepo.getById(LProbe.class, Integer.valueOf(id), "land");
153 Violation violation = validator.validate(response.getData());
154 if (violation.hasWarnings()) {
155 response.setWarnings(violation.getWarnings());
156 }
157 return response;
145 } 158 }
146 159
147 /** 160 /**
148 * Create a new probe object. 161 * Create a new probe object.
149 * 162 *
154 @Produces(MediaType.APPLICATION_JSON) 167 @Produces(MediaType.APPLICATION_JSON)
155 public Response create(@Context HttpHeaders headers, LProbe probe) { 168 public Response create(@Context HttpHeaders headers, LProbe probe) {
156 if (!authentication.isAuthenticated(headers)) { 169 if (!authentication.isAuthenticated(headers)) {
157 return new Response(false, 699, null); 170 return new Response(false, 699, null);
158 } 171 }
172 Violation violation = validator.validate(probe);
173 if (violation.hasErrors()) {
174 Response response = new Response(false, 604, probe);
175 response.setErrors(violation.getErrors());
176 response.setWarnings(violation.getWarnings());
177 return response;
178 }
159 /* Persist the new probe object*/ 179 /* Persist the new probe object*/
160 Response response = defaultRepo.create(probe, "land"); 180 Response newProbe = defaultRepo.create(probe, "land");
161 LProbe ret = (LProbe)response.getData(); 181 LProbe ret = (LProbe)newProbe.getData();
162 /* Create and persist a new probe translation object*/ 182 /* Create and persist a new probe translation object*/
163 ProbeTranslation trans = new ProbeTranslation(); 183 ProbeTranslation trans = new ProbeTranslation();
164 trans.setProbeId(ret); 184 trans.setProbeId(ret);
165 defaultRepo.create(trans, "land"); 185 defaultRepo.create(trans, "land");
166 /* Get and return the new probe object*/ 186 /* Get and return the new probe object*/
167 Response created = 187 Response response =
168 defaultRepo.getById(LProbe.class, ret.getId(), "land"); 188 defaultRepo.getById(LProbe.class, ret.getId(), "land");
169 return new Response(true, 200, created.getData()); 189 if(violation.hasWarnings()) {
190 response.setWarnings(violation.getWarnings());
191 }
192 return response;
170 } 193 }
171 194
172 /** 195 /**
173 * Update an existing probe object. 196 * Update an existing probe object.
174 * 197 *
179 @Produces(MediaType.APPLICATION_JSON) 202 @Produces(MediaType.APPLICATION_JSON)
180 public Response update(@Context HttpHeaders headers, LProbe probe) { 203 public Response update(@Context HttpHeaders headers, LProbe probe) {
181 if (!authentication.isAuthenticated(headers)) { 204 if (!authentication.isAuthenticated(headers)) {
182 logger.debug("User is not authenticated!"); 205 logger.debug("User is not authenticated!");
183 return new Response(false, 699, null); 206 return new Response(false, 699, null);
207 }
208 Violation violation = validator.validate(probe);
209 if (violation.hasErrors()) {
210 Response response = new Response(false, 604, probe);
211 response.setErrors(violation.getErrors());
212 response.setWarnings(violation.getWarnings());
213 return response;
184 } 214 }
185 Response response = defaultRepo.update(probe, "land"); 215 Response response = defaultRepo.update(probe, "land");
186 Response updated = defaultRepo.getById( 216 Response updated = defaultRepo.getById(
187 LProbe.class, 217 LProbe.class,
188 ((LProbe)response.getData()).getId(), "land"); 218 ((LProbe)response.getData()).getId(), "land");
219 if (violation.hasWarnings()) {
220 updated.setWarnings(violation.getWarnings());
221 }
189 return updated; 222 return updated;
190 } 223 }
191 224
192 /** 225 /**
193 * Delete an existing probe object by id. 226 * Delete an existing probe object by id.
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)