changeset 858:49c88a71909b

Added services for favorites and filter.
author Raimund Renkert <raimund.renkert@intevation.de>
date Tue, 02 Feb 2016 15:32:24 +0100
parents c481688150e8
children 0e895f13f0b6
files src/main/java/de/intevation/lada/rest/stamm/FavoriteService.java src/main/java/de/intevation/lada/rest/stamm/FilterService.java
diffstat 2 files changed, 236 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/rest/stamm/FavoriteService.java	Tue Feb 02 15:32:24 2016 +0100
@@ -0,0 +1,118 @@
+/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU GPL (v>=3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out
+ * the documentation coming with IMIS-Labordaten-Application for details.
+ */
+package de.intevation.lada.rest.stamm;
+
+import java.util.List;
+
+import javax.ejb.EJBTransactionRolledbackException;
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
+import javax.persistence.TransactionRequiredException;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.UriInfo;
+
+import de.intevation.lada.model.stamm.Favorite;
+import de.intevation.lada.util.annotation.AuthorizationConfig;
+import de.intevation.lada.util.annotation.RepositoryConfig;
+import de.intevation.lada.util.auth.Authorization;
+import de.intevation.lada.util.auth.AuthorizationType;
+import de.intevation.lada.util.auth.UserInfo;
+import de.intevation.lada.util.data.QueryBuilder;
+import de.intevation.lada.util.data.Repository;
+import de.intevation.lada.util.data.RepositoryType;
+import de.intevation.lada.util.rest.Response;
+
+/**
+ * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
+ */
+@Path("rest/favorite")
+@RequestScoped
+public class FavoriteService {
+
+    /**
+     * The data repository granting read/write access.
+     */
+    @Inject
+    @RepositoryConfig(type=RepositoryType.RW)
+    private Repository repository;
+
+    /**
+     * The authorization module.
+     */
+    @Inject
+    @AuthorizationConfig(type=AuthorizationType.HEADER)
+    private Authorization authorization;
+
+    @POST
+    @Path("/")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response create(
+        @Context HttpHeaders headers,
+        @Context HttpServletRequest request,
+        Favorite favorite
+    ) {
+        UserInfo userInfo = authorization.getInfo(request);
+        favorite.setUserId(userInfo.getUserId());
+        QueryBuilder<Favorite> builder = new QueryBuilder<Favorite>(
+            repository.entityManager("stamm"),
+            Favorite.class
+        );
+        builder.and("userId", userInfo.getUserId());
+        builder.and("queryId", favorite.getQueryId());
+        List<Favorite> favorites = repository.filterPlain(builder.getQuery(), "stamm");
+        if (favorites.isEmpty()) {
+            return repository.create(favorite, "stamm");
+        }
+        return new Response(false, 617, "exists");
+    }
+
+    @DELETE
+    @Path("/")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response delete(
+        @Context HttpHeaders headers,
+        @Context UriInfo info,
+        @Context HttpServletRequest request
+    ) {
+        UserInfo userInfo = authorization.getInfo(request);
+
+        MultivaluedMap<String, String> params = info.getQueryParameters();
+        if (params.isEmpty() || !params.containsKey("queryId")) {
+            return new Response(false, 618, "missing queryId parameter");
+        }
+        QueryBuilder<Favorite> builder = new QueryBuilder<Favorite>(
+            repository.entityManager("stamm"),
+            Favorite.class
+        );
+        builder.and("userId", userInfo.getUserId());
+        builder.and("queryId", params.getFirst("queryId"));
+
+        List<Favorite> fs = repository.filterPlain(builder.getQuery(), "stamm");
+        if (fs == null || fs.isEmpty()) {
+            return new Response(false, 600, "not existing");
+        }
+        /* Delete the object*/
+        try {
+            Response response = repository.delete(fs.get(0), "stamm");
+            return response;
+        }
+        catch(IllegalArgumentException | EJBTransactionRolledbackException |
+            TransactionRequiredException e) {
+            return new Response(false, 600, "");
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/rest/stamm/FilterService.java	Tue Feb 02 15:32:24 2016 +0100
@@ -0,0 +1,118 @@
+/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU GPL (v>=3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out
+ * the documentation coming with IMIS-Labordaten-Application for details.
+ */
+package de.intevation.lada.rest.stamm;
+
+import java.util.List;
+
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.DELETE;
+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.MediaType;
+
+import de.intevation.lada.model.stamm.Filter;
+import de.intevation.lada.model.stamm.FilterValue;
+import de.intevation.lada.util.annotation.AuthorizationConfig;
+import de.intevation.lada.util.annotation.RepositoryConfig;
+import de.intevation.lada.util.auth.Authorization;
+import de.intevation.lada.util.auth.AuthorizationType;
+import de.intevation.lada.util.auth.UserInfo;
+import de.intevation.lada.util.data.QueryBuilder;
+import de.intevation.lada.util.data.Repository;
+import de.intevation.lada.util.data.RepositoryType;
+import de.intevation.lada.util.rest.Response;
+
+@Path("rest/filter")
+@RequestScoped
+public class FilterService {
+
+    @Inject
+    @AuthorizationConfig(type=AuthorizationType.HEADER)
+    private Authorization authorization;
+
+    @Inject
+    @RepositoryConfig(type=RepositoryType.RW)
+    private Repository repository;
+
+    @PUT
+    @Path("/")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response update(
+        @Context HttpServletRequest request,
+        Filter filter
+    ) {
+        UserInfo userInfo = authorization.getInfo(request);
+        String value = filter.getValue();
+        Filter f = repository.getByIdPlain(Filter.class, filter.getId(), "stamm");
+        QueryBuilder<FilterValue> builder = new QueryBuilder<FilterValue>(
+            repository.entityManager("stamm"),
+            FilterValue.class
+        );
+        builder.and("userId", userInfo.getUserId());
+        builder.and("filterId", f.getId());
+        builder.and("queryId", f.getQuery().getId());
+        List<FilterValue> values = repository.filterPlain(builder.getQuery(), "stamm");
+        if (values == null || values.isEmpty()) {
+            FilterValue newValue = new FilterValue();
+            newValue.setFilterId(f.getId());
+            newValue.setQueryId(f.getQuery().getId());
+            newValue.setUserId(userInfo.getUserId());
+            newValue.setValue(value);
+            repository.create(newValue, "stamm");
+            f.setValue(value);
+            return new Response(true, 200, f);
+        }
+        else {
+            FilterValue fv = values.get(0);
+            fv.setValue(value);
+            repository.update(fv, "stamm");
+            f.setValue(value);
+            return new Response(true, 200, f);
+        }
+    }
+
+    @DELETE
+    @Path("/{id}")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response delete(
+        @Context HttpServletRequest request,
+        @PathParam("id") String id
+    ) {
+        UserInfo userInfo = authorization.getInfo(request);
+        Integer fId = Integer.valueOf(id);
+        Filter f = repository.getByIdPlain(Filter.class, fId, "stamm");
+        QueryBuilder<FilterValue> builder = new QueryBuilder<FilterValue>(
+            repository.entityManager("stamm"),
+            FilterValue.class
+        );
+        builder.and("userId", userInfo.getUserId());
+        builder.and("filterId", f.getId());
+        builder.and("queryId", f.getQuery().getId());
+        List<FilterValue> values = repository.filterPlain(builder.getQuery(), "stamm");
+        if (values == null || values.isEmpty()) {
+            return new Response(false, 618, "not existing");
+        }
+        repository.delete(values.get(0), "stamm");
+        QueryBuilder<FilterValue> fvBuilder = builder.getEmptyBuilder();
+        fvBuilder.and("userId", 0);
+        fvBuilder.and("filterId", f.getId());
+        fvBuilder.and("queryId", f.getQuery().getId());
+        List<FilterValue> basicValues = repository.filterPlain(fvBuilder.getQuery(), "stamm");
+        if (basicValues == null || basicValues.isEmpty()) {
+            f.setValue(null);
+            return new Response(true, 200, f);
+        }
+        f.setValue(basicValues.get(0).getValue());
+        return new Response(true, 200, f);
+    }
+}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)