Skip to content
Snippets Groups Projects
Commit 3c07ab03 authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

Minor modifications

parent 040708a0
Branches
Tags
No related merge requests found
...@@ -48,6 +48,7 @@ public class VIPSLogicApplication extends Application ...@@ -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.LogicService.class);
resources.add(no.nibio.vips.logic.service.VIPSMobileService.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.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.JacksonConfig.class);
//resources.add(no.nibio.vips.coremanager.service.ManagerResourceImpl.class); //resources.add(no.nibio.vips.coremanager.service.ManagerResourceImpl.class);
} }
...@@ -60,6 +61,7 @@ public class VIPSLogicApplication extends Application ...@@ -60,6 +61,7 @@ public class VIPSLogicApplication extends Application
private void addRestResourceClasses(Set<Class<?>> resources) { private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(no.nibio.vips.logic.modules.barleynetblotch.BarleyNetBlotchModelService.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.logic.service.JacksonConfig.class);
resources.add(no.nibio.vips.logic.service.LogicService.class); resources.add(no.nibio.vips.logic.service.LogicService.class);
resources.add(no.nibio.vips.logic.service.VIPSMobileService.class); resources.add(no.nibio.vips.logic.service.VIPSMobileService.class);
......
...@@ -24,22 +24,15 @@ import java.util.Calendar; ...@@ -24,22 +24,15 @@ import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; 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 javax.ws.rs.core.Response;
import no.nibio.vips.coremanager.service.ManagerResource;
import no.nibio.vips.entity.ModelConfiguration; import no.nibio.vips.entity.ModelConfiguration;
import no.nibio.vips.entity.Result; import no.nibio.vips.entity.Result;
import no.nibio.vips.entity.WeatherObservation; import no.nibio.vips.entity.WeatherObservation;
...@@ -56,7 +49,6 @@ import no.nibio.vips.util.ParseRESTParamUtil; ...@@ -56,7 +49,6 @@ import no.nibio.vips.util.ParseRESTParamUtil;
import no.nibio.vips.util.WeatherElements; import no.nibio.vips.util.WeatherElements;
import no.nibio.vips.util.weather.WeatherDataSourceException; import no.nibio.vips.util.weather.WeatherDataSourceException;
import no.nibio.vips.util.weather.WeatherDataSourceUtil; import no.nibio.vips.util.weather.WeatherDataSourceUtil;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
/** /**
* @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a>
......
/*
* 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();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment