# HG changeset patch # User Raimund Renkert # Date 1454423524 -3600 # Node ID c481688150e8d81f3f268abfe1e3df8b015f6bdd # Parent c3a5e1ee63b8701e580a191cf6a306471f8ff419 Create a new user object if not exists and use user authorization for queries. diff -r c3a5e1ee63b8 -r c481688150e8 src/main/java/de/intevation/lada/rest/stamm/QueryService.java --- 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 builder = new QueryBuilder( repository.entityManager("stamm"), Query.class ); builder.and("type", "probe"); - return repository.filter(builder.getQuery(), "stamm"); - //return new Response(true, 200, QueryTools.getProbeConfig()); + List 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 builder = new QueryBuilder( repository.entityManager("stamm"), Query.class ); builder.and("type", "messprogramm"); - return repository.filter(builder.getQuery(), "stamm"); + List 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 builder = new QueryBuilder( 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 queries = repository.filterPlain(builder.getQuery(), "stamm"); + + markFavorites(queries, userInfo); + + return new Response(true, 200, queries); + } + + private void markFavorites(List queries, UserInfo userInfo) { + QueryBuilder fBuilder = new QueryBuilder( + repository.entityManager("stamm"), + Favorite.class + ); + fBuilder.and("userId", userInfo.getUserId()); + List 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 queries, Integer userId) { + QueryBuilder builder = new QueryBuilder( + repository.entityManager("stamm"), + FilterValue.class + ); + builder.and("userId", userId); + for (Query q : queries) { + builder.and("queryId", q.getId()); + List 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); + } } } diff -r c3a5e1ee63b8 -r c481688150e8 src/main/java/de/intevation/lada/util/auth/HeaderAuthorization.java --- 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 builder = new QueryBuilder( + repository.entityManager("stamm"), + LadaUser.class + ); + builder.and("name", info.getName()); + List 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; diff -r c3a5e1ee63b8 -r c481688150e8 src/main/java/de/intevation/lada/util/auth/UserInfo.java --- 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 messstellen; private List netzbetreiber; private List 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 getMessstellen() {