raimund@441: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz raimund@441: * Software engineering by Intevation GmbH raimund@441: * raimund@441: * This file is Free Software under the GNU GPL (v>=3) raimund@441: * and comes with ABSOLUTELY NO WARRANTY! Check out raimund@441: * the documentation coming with IMIS-Labordaten-Application for details. raimund@441: */ raimund@441: package de.intevation.lada.rest; raimund@441: raimund@455: import java.util.ArrayList; raimund@450: import java.util.List; raimund@450: raimund@441: import javax.enterprise.context.RequestScoped; raimund@441: import javax.inject.Inject; raimund@455: import javax.json.JsonArray; raimund@455: import javax.json.JsonException; raimund@455: import javax.json.JsonObject; raimund@455: import javax.persistence.Query; raimund@450: import javax.ws.rs.DELETE; raimund@441: import javax.ws.rs.GET; raimund@450: import javax.ws.rs.POST; raimund@450: import javax.ws.rs.PUT; raimund@441: import javax.ws.rs.Path; raimund@441: import javax.ws.rs.PathParam; raimund@441: import javax.ws.rs.Produces; raimund@441: import javax.ws.rs.core.Context; raimund@441: import javax.ws.rs.core.HttpHeaders; raimund@441: import javax.ws.rs.core.MediaType; raimund@455: import javax.ws.rs.core.MultivaluedMap; raimund@455: import javax.ws.rs.core.UriInfo; raimund@441: raimund@441: import org.apache.log4j.Logger; raimund@441: raimund@441: import de.intevation.lada.model.land.LProbe; raimund@450: import de.intevation.lada.model.land.ProbeTranslation; raimund@455: import de.intevation.lada.query.QueryTools; raimund@441: import de.intevation.lada.util.annotation.AuthenticationConfig; raimund@441: import de.intevation.lada.util.annotation.AuthorizationConfig; raimund@441: import de.intevation.lada.util.annotation.RepositoryConfig; raimund@441: import de.intevation.lada.util.auth.Authentication; raimund@441: import de.intevation.lada.util.auth.AuthenticationType; raimund@441: import de.intevation.lada.util.auth.Authorization; raimund@441: import de.intevation.lada.util.auth.AuthorizationType; raimund@450: import de.intevation.lada.util.data.QueryBuilder; raimund@441: import de.intevation.lada.util.data.Repository; raimund@441: import de.intevation.lada.util.data.RepositoryType; raimund@441: import de.intevation.lada.util.rest.Response; raimund@543: import de.intevation.lada.validation.Validator; raimund@543: import de.intevation.lada.validation.Violation; raimund@543: import de.intevation.lada.validation.annotation.ValidationConfig; raimund@441: raimund@451: raimund@451: /** raimund@451: * This class produces a RESTful service to interact with probe objects. raimund@451: * raimund@451: * @author Raimund Renkert raimund@451: */ raimund@441: @Path("probe") raimund@441: @RequestScoped raimund@441: public class ProbeService { raimund@441: raimund@451: /* The logger used in this class.*/ raimund@441: @Inject raimund@441: private Logger logger; raimund@441: raimund@451: /* The data repository granting read/write access.*/ raimund@441: @Inject raimund@441: @RepositoryConfig(type=RepositoryType.RW) raimund@441: private Repository defaultRepo; raimund@441: raimund@451: /* The authentication module.*/ raimund@441: @Inject raimund@441: @AuthenticationConfig(type=AuthenticationType.NONE) raimund@441: private Authentication authentication; raimund@441: raimund@451: /* The authorization module.*/ raimund@441: @Inject raimund@441: @AuthorizationConfig(type=AuthorizationType.NONE) raimund@441: private Authorization authorization; raimund@441: raimund@543: @Inject raimund@543: @ValidationConfig(type="Probe") raimund@543: private Validator validator; raimund@543: raimund@451: /** raimund@451: * Get all probe objects. raimund@451: * raimund@451: * @return Response object containing all probe objects. raimund@451: */ raimund@455: @SuppressWarnings("unchecked") raimund@441: @GET raimund@441: @Path("/") raimund@441: @Produces("application/json") raimund@455: public Response get( raimund@455: @Context HttpHeaders headers, raimund@455: @Context UriInfo info raimund@455: ) { raimund@441: if (!authentication.isAuthenticated(headers)) { raimund@441: logger.debug("User is not authenticated!"); raimund@441: return new Response(false, 699, null); raimund@441: } raimund@455: MultivaluedMap params = info.getQueryParameters(); raimund@455: if (params.isEmpty() || !params.containsKey("qid")) { raimund@455: return defaultRepo.getAll(LProbe.class, "land"); raimund@455: } raimund@455: String qid = params.getFirst("qid"); raimund@455: JsonObject jsonQuery = QueryTools.getQueryById(qid); raimund@455: String sql = ""; raimund@455: List filters = new ArrayList(); raimund@455: List results = new ArrayList(); raimund@455: try { raimund@455: sql = jsonQuery.getString("sql"); raimund@455: JsonArray jsonFilters = jsonQuery.getJsonArray("filters"); raimund@455: JsonArray jsonResults = jsonQuery.getJsonArray("result"); raimund@455: for (int i = 0; i < jsonFilters.size(); i++) { raimund@455: filters.add( raimund@455: jsonFilters.getJsonObject(i).getString("dataIndex")); raimund@455: } raimund@455: for (int i = 0; i < jsonResults.size(); i++) { raimund@455: results.add( raimund@455: jsonResults.getJsonObject(i).getString("dataIndex")); raimund@455: } raimund@455: } raimund@455: catch (JsonException je) { raimund@455: return new Response(false, 603, new ArrayList()); raimund@455: } raimund@455: Query query = QueryTools.prepareQuery( raimund@455: sql, raimund@455: filters, raimund@455: params, raimund@455: defaultRepo.entityManager("land")); raimund@455: return new Response(true, 200, QueryTools.prepareResult(query.getResultList(), results)); raimund@441: } raimund@441: raimund@451: /** raimund@451: * Get a probe object by id. raimund@451: * raimund@451: * @return Response object containing a single probe. raimund@451: */ raimund@441: @GET raimund@441: @Path("/{id}") raimund@441: @Produces(MediaType.APPLICATION_JSON) raimund@441: public Response getById( raimund@441: @Context HttpHeaders headers, raimund@441: @PathParam("id") String id raimund@441: ) { raimund@441: if (!authentication.isAuthenticated(headers)) { raimund@441: logger.debug("User is not authenticated!"); raimund@441: return new Response(false, 699, null); raimund@441: } raimund@543: Response response = raimund@543: defaultRepo.getById(LProbe.class, Integer.valueOf(id), "land"); raimund@543: Violation violation = validator.validate(response.getData()); raimund@543: if (violation.hasWarnings()) { raimund@543: response.setWarnings(violation.getWarnings()); raimund@543: } raimund@543: return response; raimund@441: } raimund@441: raimund@451: /** raimund@451: * Create a new probe object. raimund@451: * raimund@451: * @return Response object containing the new probe object. raimund@451: */ raimund@450: @POST raimund@450: @Path("/") raimund@450: @Produces(MediaType.APPLICATION_JSON) raimund@450: public Response create(@Context HttpHeaders headers, LProbe probe) { raimund@450: if (!authentication.isAuthenticated(headers)) { raimund@450: return new Response(false, 699, null); raimund@450: } raimund@543: Violation violation = validator.validate(probe); raimund@543: if (violation.hasErrors()) { raimund@543: Response response = new Response(false, 604, probe); raimund@543: response.setErrors(violation.getErrors()); raimund@543: response.setWarnings(violation.getWarnings()); raimund@543: return response; raimund@543: } raimund@450: /* Persist the new probe object*/ raimund@543: Response newProbe = defaultRepo.create(probe, "land"); raimund@543: LProbe ret = (LProbe)newProbe.getData(); raimund@450: /* Create and persist a new probe translation object*/ raimund@450: ProbeTranslation trans = new ProbeTranslation(); raimund@450: trans.setProbeId(ret); raimund@450: defaultRepo.create(trans, "land"); raimund@450: /* Get and return the new probe object*/ raimund@543: Response response = raimund@450: defaultRepo.getById(LProbe.class, ret.getId(), "land"); raimund@543: if(violation.hasWarnings()) { raimund@543: response.setWarnings(violation.getWarnings()); raimund@543: } raimund@543: return response; raimund@450: } raimund@450: raimund@450: /** raimund@450: * Update an existing probe object. raimund@450: * raimund@450: * @return Response object containing the updated probe object. raimund@450: */ raimund@450: @PUT raimund@450: @Path("/") raimund@450: @Produces(MediaType.APPLICATION_JSON) raimund@450: public Response update(@Context HttpHeaders headers, LProbe probe) { raimund@450: if (!authentication.isAuthenticated(headers)) { raimund@450: logger.debug("User is not authenticated!"); raimund@450: return new Response(false, 699, null); raimund@450: } raimund@543: Violation violation = validator.validate(probe); raimund@543: if (violation.hasErrors()) { raimund@543: Response response = new Response(false, 604, probe); raimund@543: response.setErrors(violation.getErrors()); raimund@543: response.setWarnings(violation.getWarnings()); raimund@543: return response; raimund@543: } raimund@450: Response response = defaultRepo.update(probe, "land"); raimund@450: Response updated = defaultRepo.getById( raimund@450: LProbe.class, raimund@450: ((LProbe)response.getData()).getId(), "land"); raimund@543: if (violation.hasWarnings()) { raimund@543: updated.setWarnings(violation.getWarnings()); raimund@543: } raimund@450: return updated; raimund@450: } raimund@450: raimund@450: /** raimund@450: * Delete an existing probe object by id. raimund@450: * raimund@450: * @return Response object. raimund@450: */ raimund@450: @DELETE raimund@450: @Path("/{id}") raimund@450: @Produces(MediaType.APPLICATION_JSON) raimund@450: public Response delete( raimund@450: @Context HttpHeaders headers, raimund@450: @PathParam("id") String id raimund@450: ) { raimund@450: if (!authentication.isAuthenticated(headers)) { raimund@450: logger.debug("User is not authenticated!"); raimund@450: return new Response(false, 699, null); raimund@450: } raimund@450: /* Get the probe object by id*/ raimund@450: Response probe = raimund@450: defaultRepo.getById(LProbe.class, Integer.valueOf(id), "land"); raimund@450: LProbe probeObj = (LProbe)probe.getData(); raimund@450: /* Create a query and request the probetranslation object for the raimund@450: * probe*/ raimund@450: QueryBuilder builder = raimund@450: new QueryBuilder( raimund@450: defaultRepo.entityManager("land"), ProbeTranslation.class); raimund@450: builder.and("probe", probeObj.getId()); raimund@450: Response probeTrans = defaultRepo.filter(builder.getQuery(), "land"); raimund@450: @SuppressWarnings("unchecked") raimund@450: ProbeTranslation probeTransObj = ((List)probeTrans.getData()).get(0); raimund@450: /* Delete the probe translation object*/ raimund@450: defaultRepo.delete(probeTransObj, "land"); raimund@450: /* Delete the probe object*/ raimund@450: Response response = defaultRepo.delete(probeObj, "land"); raimund@450: return response; raimund@441: } raimund@441: }