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

Adding support for CSV output of forecast results

parent f6d26676
No related branches found
No related tags found
No related merge requests found
...@@ -170,6 +170,12 @@ ...@@ -170,6 +170,12 @@
<version>9.4.1211</version> <version>9.4.1211</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.6.2</version>
<type>jar</type>
</dependency>
<dependency> <dependency>
<groupId>javax</groupId> <groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId> <artifactId>javaee-web-api</artifactId>
......
...@@ -113,6 +113,10 @@ public class ForecastBean { ...@@ -113,6 +113,10 @@ public class ForecastBean {
{ {
// Authentication // Authentication
ForecastConfiguration fc = em.find(ForecastConfiguration.class, forecastConfigurationId); ForecastConfiguration fc = em.find(ForecastConfiguration.class, forecastConfigurationId);
if(fc == null)
{
return true;
}
if(fc.getIsPrivate()) if(fc.getIsPrivate())
{ {
if(userUUID == null) if(userUUID == null)
......
...@@ -115,6 +115,60 @@ public class LogicService { ...@@ -115,6 +115,60 @@ public class LogicService {
} }
} }
/**
* Get all results for one pest prediction
* @param forecastConfigurationId
* @param userUUID if the forecast is private, the correct userUUID must be supplied.
* @return
*/
@GET
@Path("forecastresults/{forecastConfigurationId}/csv")
@GZIP
@Produces("text/plain;charset=UTF-8")
public Response getForecastResultsCSV(
@PathParam("forecastConfigurationId") Long forecastConfigurationId,
@QueryParam("userUUID") String userUUID
)
{
if(SessionControllerGetter.getForecastBean().isUserAuthorizedForForecastConfiguration(forecastConfigurationId, userUUID))
{
String CSVOutput = "";
List<ForecastResult> results = SessionControllerGetter.getForecastBean().getForecastResults(forecastConfigurationId);
if(results != null && ! results.isEmpty())
{
List<String> parameters = new ArrayList<>();//new String[results.get(0).getKeys().size() + 3];
parameters.add("Valid time start");
parameters.add("Valid time end");
parameters.add("Warning status");
results.get(0).getKeys().stream().forEach(key->{
parameters.add(key);
});
CSVOutput += String.join(",", parameters) + "\n";
CSVOutput += results.stream().map(result->{
String line = result.getValidTimeStart() + "," + result.getValidTimeEnd() + "," + result.getWarningStatus();
Map<String, String> valueMap = result.getAllValuesAsMap();
for(int i=3;i<parameters.size();i++)
{
line += "," + valueMap.get(parameters.get(i));
}
line += "\n";
return line;
}).collect(Collectors.joining("\n"));
return Response.ok().entity(CSVOutput).build();
}
return Response.ok().entity("").build();
}
else
{
return Response.status(Response.Status.UNAUTHORIZED).build();
}
}
/** /**
* Get the latestDays results for one pest prediction * Get the latestDays results for one pest prediction
* @param forecastConfigurationId * @param forecastConfigurationId
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment