changeset 857:c481688150e8

Create a new user object if not exists and use user authorization for queries.
author Raimund Renkert <raimund.renkert@intevation.de>
date Tue, 02 Feb 2016 15:32:04 +0100
parents c3a5e1ee63b8
children 49c88a71909b
files src/main/java/de/intevation/lada/rest/stamm/QueryService.java src/main/java/de/intevation/lada/util/auth/HeaderAuthorization.java src/main/java/de/intevation/lada/util/auth/UserInfo.java
diffstat 3 files changed, 117 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/rest/stamm/QueryService.java	Tue Feb 02 15:29:23 2016 +0100
+++ b/src/main/java/de/intevation/lada/rest/stamm/QueryService.java	Tue Feb 02 15:32:04 2016 +0100
@@ -7,14 +7,25 @@
  */
 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.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
 
+import de.intevation.lada.model.stamm.Favorite;
+import de.intevation.lada.model.stamm.Filter;
+import de.intevation.lada.model.stamm.FilterValue;
 import de.intevation.lada.model.stamm.Query;
+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;
@@ -59,20 +70,33 @@
     @RepositoryConfig(type=RepositoryType.RO)
     private Repository repository;
 
+    @Inject
+    @AuthorizationConfig(type=AuthorizationType.HEADER)
+    private Authorization authorization;
+
     /**
      * Request all configured probe queries.
      */
     @GET
     @Path("/probe")
     @Produces("application/json")
-    public Response getProbe() {
+    public Response getProbe(
+        @Context HttpServletRequest request
+    ) {
+        UserInfo userInfo = authorization.getInfo(request);
         QueryBuilder<Query> builder = new QueryBuilder<Query>(
             repository.entityManager("stamm"),
             Query.class
         );
         builder.and("type", "probe");
-        return repository.filter(builder.getQuery(), "stamm");
-        //return new Response(true, 200, QueryTools.getProbeConfig());
+        List<Query> queries = repository.filterPlain(builder.getQuery(), "stamm");
+
+        markFavorites(queries, userInfo);
+
+        setFilterValues(queries, 0);
+        setFilterValues(queries, userInfo.getUserId());
+
+        return new Response(true, 200, queries);
     }
 
     /**
@@ -81,13 +105,20 @@
     @GET
     @Path("/messprogramm")
     @Produces("application/json")
-    public Response getMessprogramm() {
+    public Response getMessprogramm(
+        @Context HttpServletRequest request
+    ) {
+        UserInfo userInfo = authorization.getInfo(request);
         QueryBuilder<Query> builder = new QueryBuilder<Query>(
             repository.entityManager("stamm"),
             Query.class
         );
         builder.and("type", "messprogramm");
-        return repository.filter(builder.getQuery(), "stamm");
+        List<Query> queries = repository.filterPlain(builder.getQuery(), "stamm");
+
+        markFavorites(queries, userInfo);
+
+        return new Response(true, 200, queries);
     }
 
     /**
@@ -96,7 +127,10 @@
     @GET
     @Path("/stammdaten")
     @Produces("application/json")
-    public Response getStammdaten() {
+    public Response getStammdaten(
+        @Context HttpServletRequest request
+    ) {
+        UserInfo userInfo = authorization.getInfo(request);
         QueryBuilder<Query> builder = new QueryBuilder<Query>(
             repository.entityManager("stamm"),
             Query.class
@@ -105,6 +139,47 @@
         builder.or("type", "probenehmer");
         builder.or("type", "datensatzerzeuger");
         builder.or("type", "messprogrammkategorie");
-        return repository.filter(builder.getQuery(), "stamm");
+        List<Query> queries = repository.filterPlain(builder.getQuery(), "stamm");
+
+        markFavorites(queries, userInfo);
+
+        return new Response(true, 200, queries);
+    }
+
+    private void markFavorites(List<Query> queries, UserInfo userInfo) {
+        QueryBuilder<Favorite> fBuilder = new QueryBuilder<Favorite>(
+            repository.entityManager("stamm"),
+            Favorite.class
+        );
+        fBuilder.and("userId", userInfo.getUserId());
+        List<Favorite> favorites = repository.filterPlain(fBuilder.getQuery(), "stamm");
+        for (Favorite f : favorites) {
+            for (Query q : queries) {
+                if (q.getId().equals(f.getQueryId())) {
+                    q.setFavorite(true);
+                }
+            }
+        }
+    }
+
+    private void setFilterValues(List<Query> queries, Integer userId) {
+        QueryBuilder<FilterValue> builder = new QueryBuilder<FilterValue>(
+            repository.entityManager("stamm"),
+            FilterValue.class
+        );
+        builder.and("userId", userId);
+        for (Query q : queries) {
+            builder.and("queryId", q.getId());
+            List<FilterValue> values = repository.filterPlain(builder.getQuery(), "stamm");
+            for (Filter f : q.getFilters()) {
+                for (FilterValue fv : values) {
+                    if (fv.getFilterId().equals(f.getId())) {
+                        f.setValue(fv.getValue());
+                    }
+                }
+            }
+            builder = builder.getEmptyBuilder();
+            builder.and("userId", userId);
+        }
     }
 }
--- a/src/main/java/de/intevation/lada/util/auth/HeaderAuthorization.java	Tue Feb 02 15:29:23 2016 +0100
+++ b/src/main/java/de/intevation/lada/util/auth/HeaderAuthorization.java	Tue Feb 02 15:32:04 2016 +0100
@@ -7,7 +7,6 @@
  */
 package de.intevation.lada.util.auth;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
@@ -18,6 +17,8 @@
 import javax.persistence.EntityManager;
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.log4j.Logger;
+
 import de.intevation.lada.model.land.LKommentarM;
 import de.intevation.lada.model.land.LKommentarP;
 import de.intevation.lada.model.land.LMessung;
@@ -28,6 +29,7 @@
 import de.intevation.lada.model.land.LZusatzWert;
 import de.intevation.lada.model.stamm.Auth;
 import de.intevation.lada.model.stamm.DatensatzErzeuger;
+import de.intevation.lada.model.stamm.LadaUser;
 import de.intevation.lada.model.stamm.MessprogrammKategorie;
 import de.intevation.lada.model.stamm.Ort;
 import de.intevation.lada.model.stamm.Probenehmer;
@@ -47,11 +49,14 @@
 @AuthorizationConfig(type=AuthorizationType.HEADER)
 public class HeaderAuthorization implements Authorization {
 
+    @Inject
+    private Logger logger;
+
     /**
      * The Repository used to read from Database.
      */
     @Inject
-    @RepositoryConfig(type=RepositoryType.RO)
+    @RepositoryConfig(type=RepositoryType.RW)
     private Repository repository;
 
     @SuppressWarnings("rawtypes")
@@ -94,6 +99,19 @@
                 request.getAttribute("lada.user.roles").toString();
             UserInfo info = getGroupsFromDB(roleString);
             info.setName(request.getAttribute("lada.user.name").toString());
+            QueryBuilder<LadaUser> builder = new QueryBuilder<LadaUser>(
+                repository.entityManager("stamm"),
+                LadaUser.class
+            );
+            builder.and("name", info.getName());
+            List<LadaUser> user = repository.filterPlain(builder.getQuery(), "stamm");
+            if (user == null || user.isEmpty()) {
+                LadaUser newUser = new LadaUser();
+                newUser.setName(info.getName());
+                Response r = repository.create(newUser, "stamm");
+                user = repository.filterPlain(builder.getQuery(), "stamm");
+            }
+            info.setUserId(user.get(0).getId());
             return info;
         }
         return null;
--- a/src/main/java/de/intevation/lada/util/auth/UserInfo.java	Tue Feb 02 15:29:23 2016 +0100
+++ b/src/main/java/de/intevation/lada/util/auth/UserInfo.java	Tue Feb 02 15:32:04 2016 +0100
@@ -19,6 +19,7 @@
  */
 public class UserInfo {
     private String name;
+    private Integer userId;
     private List<String> messstellen;
     private List<String> netzbetreiber;
     private List<String> roles;
@@ -46,6 +47,20 @@
     }
 
     /**
+     * @return the userId
+     */
+    public Integer getUserId() {
+        return userId;
+    }
+
+    /**
+     * @param userId the userId to set
+     */
+    public void setUserId(Integer userId) {
+        this.userId = userId;
+    }
+
+    /**
      * @return the messstellen
      */
     public List<String> getMessstellen() {
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)