diff --git a/src/main/java/no/nibio/vips/logic/VIPSLogicApplication.java b/src/main/java/no/nibio/vips/logic/VIPSLogicApplication.java index a4f38659c67d49e5f11b04ee958b3ccfe5485e11..0100e091eba22d206ae3ddab7b164ec5fc0e74f5 100644 --- a/src/main/java/no/nibio/vips/logic/VIPSLogicApplication.java +++ b/src/main/java/no/nibio/vips/logic/VIPSLogicApplication.java @@ -48,6 +48,7 @@ public class VIPSLogicApplication extends Application resources.add(no.nibio.vips.logic.service.LogicService.class); resources.add(no.nibio.vips.logic.service.VIPSMobileService.class); resources.add(no.nibio.vips.logic.modules.barleynetblotch.BarleyNetBlotchModelService.class); + resources.add(no.nibio.vips.logic.modules.roughage.RoughageService.class); //resources.add(no.nibio.vips.logic.service.JacksonConfig.class); //resources.add(no.nibio.vips.coremanager.service.ManagerResourceImpl.class); } @@ -60,6 +61,7 @@ public class VIPSLogicApplication extends Application private void addRestResourceClasses(Set<Class<?>> resources) { resources.add(no.nibio.vips.logic.modules.barleynetblotch.BarleyNetBlotchModelService.class); + resources.add(no.nibio.vips.logic.modules.roughage.RoughageService.class); resources.add(no.nibio.vips.logic.service.JacksonConfig.class); resources.add(no.nibio.vips.logic.service.LogicService.class); resources.add(no.nibio.vips.logic.service.VIPSMobileService.class); diff --git a/src/main/java/no/nibio/vips/logic/modules/barleynetblotch/BarleyNetBlotchModelService.java b/src/main/java/no/nibio/vips/logic/modules/barleynetblotch/BarleyNetBlotchModelService.java index 3ffc0a8b19bdae5b3f83ea7d06b527b123830114..0e07d1f1f270e5ddbfc78ec9e0be6439627666f5 100644 --- a/src/main/java/no/nibio/vips/logic/modules/barleynetblotch/BarleyNetBlotchModelService.java +++ b/src/main/java/no/nibio/vips/logic/modules/barleynetblotch/BarleyNetBlotchModelService.java @@ -24,22 +24,15 @@ import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.TimeZone; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.persistence.EntityManager; import javax.persistence.NoResultException; import javax.persistence.PersistenceContext; -import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Response; -import no.nibio.vips.coremanager.service.ManagerResource; import no.nibio.vips.entity.ModelConfiguration; import no.nibio.vips.entity.Result; import no.nibio.vips.entity.WeatherObservation; @@ -56,7 +49,6 @@ import no.nibio.vips.util.ParseRESTParamUtil; import no.nibio.vips.util.WeatherElements; import no.nibio.vips.util.weather.WeatherDataSourceException; import no.nibio.vips.util.weather.WeatherDataSourceUtil; -import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; /** * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> diff --git a/src/main/java/no/nibio/vips/logic/modules/roughage/RoughageService.java b/src/main/java/no/nibio/vips/logic/modules/roughage/RoughageService.java new file mode 100644 index 0000000000000000000000000000000000000000..a07de1403cda25fdd4040d375db2df882d31b358 --- /dev/null +++ b/src/main/java/no/nibio/vips/logic/modules/roughage/RoughageService.java @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSLogic. + * VIPSLogic is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSLogic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.logic.modules.roughage; + +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.TimeZone; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Response; +import no.nibio.vips.entity.ModelConfiguration; +import no.nibio.vips.entity.Result; +import no.nibio.vips.entity.WeatherObservation; +import no.nibio.vips.logic.entity.Organization; +import no.nibio.vips.logic.entity.PointOfInterestWeatherStation; +import no.nibio.vips.logic.util.RunModelException; +import no.nibio.vips.logic.util.SessionControllerGetter; +import no.nibio.vips.logic.util.SystemTime; +import no.nibio.vips.util.ParseRESTParamUtil; +import no.nibio.vips.util.WeatherElements; +import no.nibio.vips.util.WeatherUtil; +import no.nibio.vips.util.weather.WeatherDataSourceException; +import no.nibio.vips.util.weather.WeatherDataSourceUtil; + +/** + * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +@Path("rest/roughage") +public class RoughageService { + private final static String VIPSCOREMANAGER_URL = System.getProperty("no.nibio.vips.logic.VIPSCOREMANAGER_URL"); + + @PersistenceContext(unitName="VIPSLogic-PU") + EntityManager em; + + @GET + @Path("nutrition/runmodel/{organizationId}") + @Produces("application/json;charset=UTF-8") + public Response runModel( + @PathParam("organizationId") Integer organizationId, + @QueryParam("timeZone") String timeZoneStr, + @QueryParam("weatherStationId") Integer weatherStationId, + @QueryParam("firstHarvest") String firstHarvestStr, + @QueryParam("secondHarvest") String secondHarvestStr, + @QueryParam("soilType") Integer soilType, + @QueryParam("cloverShare") Integer cloverShare + ) + { + ParseRESTParamUtil parseUtil = new ParseRESTParamUtil(); + TimeZone timeZone = TimeZone.getTimeZone(timeZoneStr); + Date firstHarvest = parseUtil.parseISODate(firstHarvestStr, timeZone); + Date secondHarvest = secondHarvestStr != null ? parseUtil.parseISODate(secondHarvestStr, timeZone) : null; + // Calculating start and end date of weather data retrieval + // Start is April 1st + Calendar cal = Calendar.getInstance(timeZone); + cal.setTime(firstHarvest); + cal.set(Calendar.MONTH, Calendar.APRIL); + cal.set(Calendar.DATE, 1); + Date aprilFirst = new WeatherUtil().normalizeToExactDate(cal.getTime(), timeZone); + // End date for weather data depends on season + // We try September 30th. If that's in the future, + // We add 10 days to today + Date dateOfLastWeatherData; + cal.setTime(aprilFirst); + cal.set(Calendar.MONTH, Calendar.SEPTEMBER); + cal.set(Calendar.DATE, 30); + Date endOfSeptember = cal.getTime(); + if(endOfSeptember.after(SystemTime.getSystemTime())) + { + cal.setTime(SystemTime.getSystemTime()); + cal.add(Calendar.DATE, 10); + dateOfLastWeatherData = cal.getTime(); + } + else + { + dateOfLastWeatherData = endOfSeptember; + } + + PointOfInterestWeatherStation weatherStation = em.find(PointOfInterestWeatherStation.class, weatherStationId); + WeatherDataSourceUtil wsdUtil = new WeatherDataSourceUtil(); + + // Getting the weather observations + List<WeatherObservation> observations; + try { + observations = wsdUtil.getWeatherObservations( + weatherStation, + WeatherObservation.LOG_INTERVAL_ID_1D, + new String[]{ + WeatherElements.TEMPERATURE_MEAN, + WeatherElements.PRECIPITATION, + WeatherElements.GLOBAL_RADIATION, + WeatherElements.SOIL_TEMPERATURE_10CM_MEAN, + WeatherElements.POTENTIAL_EVAPORATION + }, + aprilFirst, + dateOfLastWeatherData + ); + } catch (WeatherDataSourceException ex) { + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build(); + } + + ModelConfiguration config = new ModelConfiguration(); + config.setModelId("ROUGHAGENU"); + config.setConfigParameter("observations", observations); + config.setConfigParameter("timeZone", timeZone.getID()); + config.setConfigParameter("firstHarvest", firstHarvest); + config.setConfigParameter("soilType", soilType); + config.setConfigParameter("cloverShare", cloverShare); + + // Optional parameters + if(secondHarvest != null) + { + config.setConfigParameter("secondHarvest", secondHarvest); + } + // Must get the VIPSCore user id for this organization + Organization org = em.find(Organization.class, organizationId); + Integer VIPSCoreUserId = org.getDefaultVipsCoreUserId(); + + List<Result> results; + try + { + results = SessionControllerGetter.getForecastBean().runForecast(config, VIPSCoreUserId); + } + catch(RunModelException ex) + { + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build(); + } + return Response.ok().entity(results).build(); + } +}