changeset 859:0e895f13f0b6

merged.
author Raimund Renkert <raimund.renkert@intevation.de>
date Tue, 02 Feb 2016 15:33:24 +0100
parents 49c88a71909b (diff) e797391714a0 (current diff)
children 7c6b7c08d31d
files
diffstat 12 files changed, 446 insertions(+), 120 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/model/stamm/Favorite.java	Tue Feb 02 15:03:29 2016 +0100
+++ b/src/main/java/de/intevation/lada/model/stamm/Favorite.java	Tue Feb 02 15:33:24 2016 +0100
@@ -9,7 +9,10 @@
 
 import java.io.Serializable;
 
+import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.ManyToOne;
@@ -26,16 +29,16 @@
     private static final long serialVersionUID = 1L;
 
     @Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    @Column(name="id", unique=true, nullable=false)
     private Integer id;
 
     //bi-directional many-to-one association to LadaUser
-    @ManyToOne
-    @JoinColumn(name="user_id")
-    private LadaUser ladaUser;
+    @Column(name="user_id")
+    private Integer userId;
 
-    //bi-directional many-to-one association to Query
-    @ManyToOne
-    private Query query;
+    @Column(name="query_id")
+    private Integer queryId;
 
     public Favorite() {
     }
@@ -48,20 +51,20 @@
         this.id = id;
     }
 
-    public LadaUser getLadaUser() {
-        return this.ladaUser;
+    public Integer getUserId() {
+        return this.userId;
     }
 
-    public void setLadaUser(LadaUser ladaUser) {
-        this.ladaUser = ladaUser;
+    public void setUserId(Integer userId) {
+        this.userId = userId;
     }
 
-    public Query getQuery() {
-        return this.query;
+    public Integer getQueryId() {
+        return this.queryId;
     }
 
-    public void setQuery(Query query) {
-        this.query = query;
+    public void setQueryId(Integer queryId) {
+        this.queryId = queryId;
     }
 
 }
--- a/src/main/java/de/intevation/lada/model/stamm/Filter.java	Tue Feb 02 15:03:29 2016 +0100
+++ b/src/main/java/de/intevation/lada/model/stamm/Filter.java	Tue Feb 02 15:33:24 2016 +0100
@@ -11,8 +11,11 @@
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.ManyToOne;
+import javax.persistence.Transient;
 
 import com.fasterxml.jackson.annotation.JsonIgnore;
 
@@ -26,6 +29,8 @@
     private static final long serialVersionUID = 1L;
 
     @Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    @Column(name="id", unique=true, nullable=false)
     private Integer id;
 
     @Column(name="data_index")
@@ -41,6 +46,9 @@
     @ManyToOne
     private Query query;
 
+    @Transient
+    private String value;
+
     public Filter() {
     }
 
@@ -93,4 +101,18 @@
     public void setQuery(Query query) {
         this.query = query;
     }
+
+    /**
+     * @return the value
+     */
+    public String getValue() {
+        return value;
+    }
+
+    /**
+     * @param value the value to set
+     */
+    public void setValue(String value) {
+        this.value = value;
+    }
 }
--- a/src/main/java/de/intevation/lada/model/stamm/FilterValue.java	Tue Feb 02 15:03:29 2016 +0100
+++ b/src/main/java/de/intevation/lada/model/stamm/FilterValue.java	Tue Feb 02 15:33:24 2016 +0100
@@ -11,6 +11,8 @@
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.ManyToOne;
@@ -27,21 +29,22 @@
     private static final long serialVersionUID = 1L;
 
     @Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    @Column(name="id", unique=true, nullable=false)
     private Integer id;
 
     private String value;
 
     //bi-directional many-to-one association to Filter
-    @ManyToOne
-    private Filter filter;
+    @Column(name="filter_id")
+    private Integer filterId;
 
     //bi-directional many-to-one association to LadaUser
-    @ManyToOne
-    @JoinColumn(name="user_id")
-    private LadaUser ladaUser;
+    @Column(name="user_id")
+    private Integer userId;
 
     @Column(name="query_id")
-    private Integer query;
+    private Integer queryId;
 
     public FilterValue() {
     }
@@ -62,28 +65,27 @@
         this.value = value;
     }
 
-    public Filter getFilter() {
-        return this.filter;
-    }
-
-    public void setFilter(Filter filter) {
-        this.filter = filter;
-    }
-
-    public LadaUser getLadaUser() {
-        return this.ladaUser;
+    public Integer getFilterId() {
+        return this.filterId;
     }
 
-    public void setLadaUser(LadaUser ladaUser) {
-        this.ladaUser = ladaUser;
+    public void setFilterId(Integer filterId) {
+        this.filterId = filterId;
     }
 
-    public Integer getQuery() {
-        return this.query;
+    public Integer getUserId() {
+        return this.userId;
     }
 
-    public void setQuery(Integer query) {
-        this.query = query;
+    public void setUserId(Integer userId) {
+        this.userId = userId;
     }
 
+    public Integer getQueryId() {
+        return this.queryId;
+    }
+
+    public void setQueryId(Integer queryId) {
+        this.queryId = queryId;
+    }
 }
--- a/src/main/java/de/intevation/lada/model/stamm/LadaUser.java	Tue Feb 02 15:03:29 2016 +0100
+++ b/src/main/java/de/intevation/lada/model/stamm/LadaUser.java	Tue Feb 02 15:33:24 2016 +0100
@@ -10,7 +10,11 @@
 import java.io.Serializable;
 import java.util.List;
 
+import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.OneToMany;
 import javax.persistence.Table;
@@ -26,18 +30,12 @@
     private static final long serialVersionUID = 1L;
 
     @Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    @Column(name="id", unique=true, nullable=false)
     private Integer id;
 
     private String name;
 
-    //bi-directional many-to-one association to Favorite
-    @OneToMany(mappedBy="ladaUser")
-    private List<Favorite> favorites;
-
-    //bi-directional many-to-one association to FilterValue
-    @OneToMany(mappedBy="ladaUser")
-    private List<FilterValue> filterValues;
-
     public LadaUser() {
     }
 
@@ -56,49 +54,4 @@
     public void setName(String name) {
         this.name = name;
     }
-
-    public List<Favorite> getFavorites() {
-        return this.favorites;
-    }
-
-    public void setFavorites(List<Favorite> favorites) {
-        this.favorites = favorites;
-    }
-
-    public Favorite addFavorite(Favorite favorite) {
-        getFavorites().add(favorite);
-        favorite.setLadaUser(this);
-
-        return favorite;
-    }
-
-    public Favorite removeFavorite(Favorite favorite) {
-        getFavorites().remove(favorite);
-        favorite.setLadaUser(null);
-
-        return favorite;
-    }
-
-    public List<FilterValue> getFilterValues() {
-        return this.filterValues;
-    }
-
-    public void setFilterValues(List<FilterValue> filterValues) {
-        this.filterValues = filterValues;
-    }
-
-    public FilterValue addFilterValue(FilterValue filterValue) {
-        getFilterValues().add(filterValue);
-        filterValue.setLadaUser(this);
-
-        return filterValue;
-    }
-
-    public FilterValue removeFilterValue(FilterValue filterValue) {
-        getFilterValues().remove(filterValue);
-        filterValue.setLadaUser(null);
-
-        return filterValue;
-    }
-
 }
--- a/src/main/java/de/intevation/lada/model/stamm/Query.java	Tue Feb 02 15:03:29 2016 +0100
+++ b/src/main/java/de/intevation/lada/model/stamm/Query.java	Tue Feb 02 15:33:24 2016 +0100
@@ -10,10 +10,14 @@
 import java.io.Serializable;
 import java.util.List;
 
+import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.OneToMany;
+import javax.persistence.Transient;
 
 import com.fasterxml.jackson.annotation.JsonIgnore;
 
@@ -27,6 +31,8 @@
     private static final long serialVersionUID = 1L;
 
     @Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    @Column(name="id", unique=true, nullable=false)
     private Integer id;
 
     private String description;
@@ -37,9 +43,8 @@
 
     private String type;
 
-    //bi-directional many-to-one association to Favorite
-    @OneToMany(fetch=FetchType.EAGER, mappedBy="query")
-    private List<Favorite> favorites;
+    @Transient
+    private Boolean favorite;
 
     //bi-directional many-to-one association to Filter
     @OneToMany(fetch=FetchType.EAGER, mappedBy="query")
@@ -92,28 +97,15 @@
         this.type = type;
     }
 
-    @JsonIgnore
-    public List<Favorite> getFavorites() {
-        return this.favorites;
-    }
-
-    @JsonIgnore
-    public void setFavorites(List<Favorite> favorites) {
-        this.favorites = favorites;
-    }
-
-    public Favorite addFavorite(Favorite favorite) {
-        getFavorites().add(favorite);
-        favorite.setQuery(this);
-
+    public Boolean isFavorite() {
+        if (favorite == null) {
+            return false;
+        }
         return favorite;
     }
 
-    public Favorite removeFavorite(Favorite favorite) {
-        getFavorites().remove(favorite);
-        favorite.setQuery(null);
-
-        return favorite;
+    public void setFavorite(Boolean favorite) {
+        this.favorite = favorite;
     }
 
     public List<Filter> getFilters() {
--- a/src/main/java/de/intevation/lada/model/stamm/Result.java	Tue Feb 02 15:03:29 2016 +0100
+++ b/src/main/java/de/intevation/lada/model/stamm/Result.java	Tue Feb 02 15:33:24 2016 +0100
@@ -11,6 +11,8 @@
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.ManyToOne;
 import javax.persistence.Table;
@@ -28,6 +30,8 @@
     private static final long serialVersionUID = 1L;
 
     @Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    @Column(name="id", unique=true, nullable=false)
     private Integer id;
 
     @Column(name="data_index")
--- a/src/main/java/de/intevation/lada/query/QueryTools.java	Tue Feb 02 15:03:29 2016 +0100
+++ b/src/main/java/de/intevation/lada/query/QueryTools.java	Tue Feb 02 15:33:24 2016 +0100
@@ -211,6 +211,9 @@
             filters,
             params,
             repository.entityManager("land"));
+        if (q == null) {
+            return new ArrayList();
+        }
         return prepareResult(q.getResultList(), results);
     }
 
@@ -223,6 +226,9 @@
         javax.persistence.Query query = manager.createNativeQuery(sql);
         for (Filter filter: filters) {
             List<String> param = params.get(filter.getDataIndex());
+            if (param == null) {
+                return null;
+            }
             List<String> clean = new ArrayList<String>();
             for(String p : param) {
                 clean.add(p.replace(",", "|"));
--- /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:33: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:33: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);
+    }
+}
--- a/src/main/java/de/intevation/lada/rest/stamm/QueryService.java	Tue Feb 02 15:03:29 2016 +0100
+++ b/src/main/java/de/intevation/lada/rest/stamm/QueryService.java	Tue Feb 02 15:33:24 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:03:29 2016 +0100
+++ b/src/main/java/de/intevation/lada/util/auth/HeaderAuthorization.java	Tue Feb 02 15:33:24 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:03:29 2016 +0100
+++ b/src/main/java/de/intevation/lada/util/auth/UserInfo.java	Tue Feb 02 15:33:24 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)