Mercurial > lada > lada-server
changeset 195:ad8e9cc164a8
New service and repository for 'Ort'.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Mon, 01 Jul 2013 14:45:27 +0200 |
parents | 682a3acdae70 |
children | e39bc0132c3a |
files | src/main/java/de/intevation/lada/data/OrtRepository.java src/main/java/de/intevation/lada/rest/OrtService.java |
diffstat | 2 files changed, 190 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/data/OrtRepository.java Mon Jul 01 14:45:27 2013 +0200 @@ -0,0 +1,93 @@ +package de.intevation.lada.data; + +import javax.ejb.EJBTransactionRolledbackException; +import javax.inject.Inject; +import javax.inject.Named; +import javax.persistence.EntityExistsException; +import javax.persistence.TransactionRequiredException; + +import de.intevation.lada.manage.Manager; +import de.intevation.lada.model.Ort; +import de.intevation.lada.rest.Response; + +@Named("ortrepository") +public class OrtRepository +extends Repository +{ + @Inject + @Named("datamanager") + private Manager manager; + + @Override + public Response create(Object object) { + if (!(object instanceof Ort)) { + return new Response(false, 602, object); + } + Ort ort = (Ort)object; + try { + manager.create(ort); + return new Response(true, 200, ort); + } + catch(EntityExistsException eee) { + return new Response(false, 601, ort); + } + catch(IllegalArgumentException iae) { + return new Response(false, 602, ort); + } + catch(TransactionRequiredException tre) { + return new Response(false, 603, ort); + } + } + + @Override + public Response update(Object object) { + if (!(object instanceof Ort)) { + return new Response(false, 602, object); + } + Ort ort = (Ort)object; + Response response = new Response(true, 200, ort); + try { + manager.update(ort); + return response; + } + catch (EntityExistsException eee) { + response.setSuccess(false); + response.setMessage(601); + } + catch (IllegalArgumentException iae) { + response.setSuccess(false); + response.setMessage(602); + } + catch (TransactionRequiredException tre) { + response.setSuccess(false); + response.setMessage(603); + } + catch (EJBTransactionRolledbackException te) { + response.setSuccess(false); + response.setMessage(604); + } + return response; + } + + @Override + public Response delete(Object object) { + if (!(object instanceof Ort)) { + return new Response(false, 602, null); + } + Ort ort = (Ort)object; + Response response = new Response(true, 200, null); + try { + manager.delete(ort); + } + catch (IllegalArgumentException iae) { + response.setSuccess(false); + response.setMessage(602); + } + catch (TransactionRequiredException tre) { + response.setSuccess(false); + response.setMessage(603); + } + return response; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/rest/OrtService.java Mon Jul 01 14:45:27 2013 +0200 @@ -0,0 +1,97 @@ +package de.intevation.lada.rest; + +import java.util.ArrayList; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.inject.Named; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.UriInfo; + +import de.intevation.lada.data.QueryBuilder; +import de.intevation.lada.data.Repository; +import de.intevation.lada.model.Ort; + +@Path("/ortinfo") +@RequestScoped +public class OrtService +{ + /** + * The Repository for SUmwelt. + */ + @Inject + @Named("ortrepository") + private Repository repository; + + /** + * Request a LZusatzWert via its id. + * + * @param id The LProbe id + * @return JSON Object via REST service. + */ + @GET + @Path("/{id}") + @Produces("text/json") + public Response findById(@PathParam("id") String id) { + return repository.findById(Ort.class, id); + } + + /** + * Request LMessert via a filter. + * + * Query parameters are used for the filter in form of key-value pairs. + * + * @param info The URL query parameters. + * @return JSON Object via Rest service. + */ + @GET + @Produces("text/json") + public Response filter(@Context UriInfo info) { + MultivaluedMap<String, String> params = info.getQueryParameters(); + if (params.isEmpty() || !params.containsKey("probeId")) { + return new Response(false, 609, new ArrayList<Ort>()); + } + String paramValue = params.getFirst("probeId"); + QueryBuilder<Ort> builder = + new QueryBuilder<Ort>( + repository.getEntityManager(), Ort.class); + builder.and("probeId", paramValue); + return repository.filter(builder.getQuery()); + } + + @PUT + @Produces("text/json") + @Path("/{ortId}") + @Consumes("application/json") + public Response update(Ort ort) { + return repository.update(ort); + } + + @POST + @Produces("text/json") + @Consumes("application/json") + public Response create(Ort ort) { + return repository.create(ort); + } + + @DELETE + @Path("/{ortId}") + public Response delete(@PathParam("ortId") String ortId ) { + Response response = repository.findById(Ort.class, ortId); + Ort ort = (Ort)response.getData(); + if (ort != null) { + repository.delete(ort); + return new Response(true, 200, null); + } + return new Response(false, 600, null); + } +}