package controllers;

import helpers.fileUtils.StorageUtil;
import play.mvc.Controller;
import play.mvc.Result;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import static play.libs.Json.toJson;

/**
 * Created by 1199 on 2/10/2016.
 */
public class Report extends Controller {
    private static final String FILEDIR = "/home/ajie/field-research/report/";

    public static Result generateFarmerReport (int seasonId) {
        Map<String, Object> result = new HashMap<>(1);
        try {
            String scriptFilePath = FILEDIR + "report.py";
            String reportFilePath = FILEDIR + "farmer_report.xlsx";

            String command = "python " + scriptFilePath + " " + reportFilePath + " " + seasonId;

            Process p = Runtime.getRuntime().exec(command);
            p.waitFor();

            Path path = Paths.get(reportFilePath);
            File file = path.toFile();
            file.renameTo(new File("AnalyticReport.xlsx"));
            if (!file.exists()) {
                throw new IOException("file does not exist");
            }

            response().setContentType(Files.probeContentType(path));
            byte [] fileBinaries = StorageUtil.readFileBinaries(file);
            return ok(fileBinaries);
        } catch (Exception e) {
            result.put("message", e.getMessage());
            return internalServerError(toJson(result));
        }
    }
}

@@ -53,16 +53,36 @@
 geUtil;%0A
+import play.Logger;%0A
 import p
@@ -97,24 +97,24 @@
 Controller;%0A
-
 import play.
@@ -475,17 +475,15 @@
 = %22/
-home/ajie
+var/www
 /fie
@@ -761,24 +761,24 @@
 ort.xlsx%22;%0A%0A
-
             
@@ -863,16 +863,52 @@
 sonId;%0A%0A
+            Logger.debug(command);%0A%0A
         

fix bug