From 201b3bedd707416559be1256f08bda73f7cbd6c4 Mon Sep 17 00:00:00 2001 From: Tor-Einar Skog <tor-einar.skog@nibio.no> Date: Wed, 18 Mar 2020 14:22:30 +0100 Subject: [PATCH] Supporting private forecasts in KML --- .../controller/session/ForecastBean.java | 26 ++++++++++--- .../vips/logic/service/LogicService.java | 37 ++++++++++++++++--- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/main/java/no/nibio/vips/logic/controller/session/ForecastBean.java b/src/main/java/no/nibio/vips/logic/controller/session/ForecastBean.java index 26e104b7..0d9baaa5 100755 --- a/src/main/java/no/nibio/vips/logic/controller/session/ForecastBean.java +++ b/src/main/java/no/nibio/vips/logic/controller/session/ForecastBean.java @@ -710,7 +710,7 @@ public class ForecastBean { return resource; } - public Kml getForecastsAggregateKml(List<Integer> organizationIds, List<Integer> cropOrganismIds, Date theDate, String serverName) + public Kml getForecastsAggregateKml(List<Integer> organizationIds, List<Integer> cropOrganismIds, Date theDate, String serverName, VipsLogicUser user) { //String iconPath = Globals.PROTOCOL + "://" + serverName + "/public/images/"; String iconPath = "//" + serverName + "/public/images/"; @@ -751,13 +751,13 @@ public class ForecastBean { if(organizationIds.size() == 1 && organizationIds.get(0).equals(-1)) { em.createNamedQuery("Organization.findAll",Organization.class).getResultStream().forEach( - org-> poisWithAggregate.addAll(getPointOfInterestForecastsAggregate(org.getOrganizationId(), cropOrganismIds, theDate)) + org-> poisWithAggregate.addAll(getPointOfInterestForecastsAggregate(org.getOrganizationId(), cropOrganismIds, theDate, user)) ); } else { organizationIds.stream().forEach( - orgId-> poisWithAggregate.addAll(getPointOfInterestForecastsAggregate(orgId, cropOrganismIds, theDate)) + orgId-> poisWithAggregate.addAll(getPointOfInterestForecastsAggregate(orgId, cropOrganismIds, theDate, user)) ); } @@ -1015,9 +1015,15 @@ public class ForecastBean { * @param organizationId Filter for organization * @param cropOrganismIds Filter for crops * @param theDate Filter for date. If theDate=systemDate, data is fetched from the caching table forecast_result_cache + * @param user if not null: Include private forecasts for this user * @return */ - private List<PointOfInterest> getPointOfInterestForecastsAggregate(Integer organizationId, List<Integer> cropOrganismIds, Date theDate) { + private List<PointOfInterest> getPointOfInterestForecastsAggregate( + Integer organizationId, + List<Integer> cropOrganismIds, + Date theDate, + VipsLogicUser user + ) { // TODO: More precise gathering of POIs... List<PointOfInterest> pois; if(organizationId != null && organizationId > 0) @@ -1050,14 +1056,22 @@ public class ForecastBean { "WHERE forecast_configuration_id IN( \n" + " SELECT forecast_configuration_id \n" + " FROM forecast_configuration \n" + - " WHERE is_private IS FALSE \n" + - " AND forecast_configuration_id > 0 \n" + + " WHERE forecast_configuration_id > 0 \n" + + + (user == null ? + " AND is_private IS FALSE \n" + :" AND (is_private IS FALSE OR (is_private IS TRUE AND vips_logic_user_id=:vipsLogicUserId))" + ) + " AND location_point_of_interest_id=:locationPointOfInterestId \n" + (cropOrganismIds != null && ! cropOrganismIds.isEmpty() ? " AND crop_organism_id IN (" + StringUtils.join(cropOrganismIds, ",") + ") " : "") + ")\n" + "AND valid_time_start between :midnight AND :nextMidnight"; //System.out.println(poi.getName() + " SQL=" + sql); Query q = em.createNativeQuery(sql); + if(user != null) + { + q.setParameter("vipsLogicUserId", user); + } q.setParameter("locationPointOfInterestId", poi.getPointOfInterestId()); q.setParameter("midnight", midnight); q.setParameter("nextMidnight", nextMidnight); diff --git a/src/main/java/no/nibio/vips/logic/service/LogicService.java b/src/main/java/no/nibio/vips/logic/service/LogicService.java index 3b57ecf2..13742fcd 100755 --- a/src/main/java/no/nibio/vips/logic/service/LogicService.java +++ b/src/main/java/no/nibio/vips/logic/service/LogicService.java @@ -534,7 +534,6 @@ public class LogicService { } /** - * * @param organizationId * @param cropCategoryIds * @return @@ -546,8 +545,22 @@ public class LogicService { @Facet("restricted") public Response getForecastResultsAggregate( @PathParam("organizationId") Integer organizationId, - @QueryParam("cropCategoryId") List<Integer> cropCategoryIds) + @QueryParam("cropCategoryId") List<Integer> cropCategoryIds, + @QueryParam("userUUID") String userUUID) { + VipsLogicUser viewUser = null; + if(userUUID != null) + { + try + { + UUID uUUID = UUID.fromString(userUUID); + viewUser = SessionControllerGetter.getUserBean().findVipsLogicUser(uUUID); + } + catch(IllegalArgumentException ex) + { + // Skip this + } + } List<Integer> organizationIds = new ArrayList<>(); organizationIds.add(organizationId); if(cropCategoryIds == null || cropCategoryIds.isEmpty()) @@ -557,7 +570,7 @@ public class LogicService { else { List<Integer> cropOrganismIds = SessionControllerGetter.getOrganismBean().getCropCategoryOrganismIds(cropCategoryIds); - Kml retVal = SessionControllerGetter.getForecastBean().getForecastsAggregateKml(organizationIds, cropOrganismIds, SystemTime.getSystemTime(), ServletUtil.getServerName(httpServletRequest)); + Kml retVal = SessionControllerGetter.getForecastBean().getForecastsAggregateKml(organizationIds, cropOrganismIds, SystemTime.getSystemTime(), ServletUtil.getServerName(httpServletRequest), viewUser); return Response.ok().entity(retVal).build(); } } @@ -574,7 +587,8 @@ public class LogicService { @Facet("restricted") public Response getForecastResultsAggregate( @QueryParam("organizationId") List<Integer> organizationIds, - @QueryParam("cropCategoryId") List<Integer> cropCategoryIds) + @QueryParam("cropCategoryId") List<Integer> cropCategoryIds, + @QueryParam("userUUID") String userUUID) { if(cropCategoryIds == null || cropCategoryIds.isEmpty()) { @@ -582,8 +596,21 @@ public class LogicService { } else { + VipsLogicUser viewUser = null; + if(userUUID != null) + { + try + { + UUID uUUID = UUID.fromString(userUUID); + viewUser = SessionControllerGetter.getUserBean().findVipsLogicUser(uUUID); + } + catch(IllegalArgumentException ex) + { + // Skip this + } + } List<Integer> cropOrganismIds = SessionControllerGetter.getOrganismBean().getCropCategoryOrganismIds(cropCategoryIds); - Kml retVal = SessionControllerGetter.getForecastBean().getForecastsAggregateKml(organizationIds, cropOrganismIds, SystemTime.getSystemTime(), ServletUtil.getServerName(httpServletRequest)); + Kml retVal = SessionControllerGetter.getForecastBean().getForecastsAggregateKml(organizationIds, cropOrganismIds, SystemTime.getSystemTime(), ServletUtil.getServerName(httpServletRequest), viewUser); return Response.ok().entity(retVal).build(); } } -- GitLab