Search in sources :

Example 1 with WateringMeasure

use of com.webstart.DTO.WateringMeasure in project FarmCloud by vratsasg.

the class ExtractController method getPDF.

@RequestMapping(value = "{mydevice}/watering/pdf", params = { "dtstart", "dtend" }, method = RequestMethod.POST)
public ResponseEntity<byte[]> getPDF(@PathVariable("mydevice") String mydevice, @RequestParam("dtstart") String datetimestart, @RequestParam("dtend") String datetimeend, HttpServletRequest request) {
    WateringMeasure wateringMeasure = null;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] contents;
    try {
        Users user = (Users) request.getSession().getAttribute("current_user");
        // DateTime Convertable
        Featureofinterest featureofinterest = featureofInterestService.getFeatureofinterestByIdentifier(mydevice);
        DateTimeFormatter dtfInput = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        HelperCls.ConvertToDateTime convertable = new HelperCls.ConvertToDateTime();
        DateTime from = convertable.GetUTCDateTime(datetimestart, dtfInput, featureofinterest.getTimezone(), StatusTimeConverterEnum.TO_UTC);
        DateTime to = convertable.GetUTCDateTime(datetimeend, dtfInput, featureofinterest.getTimezone(), StatusTimeConverterEnum.TO_UTC);
        // 
        wateringMeasure = observationProperyService.getWateringData(user.getUser_id(), mydevice, from, to);
        // 
        com.itextpdf.text.Document doc = new com.itextpdf.text.Document();
        // Do this BEFORE document.open()
        PdfWriter.getInstance(doc, byteArrayOutputStream);
        doc.open();
        // Whatever function that you use to create your PDF
        doc = createPDF(wateringMeasure, datetimestart, datetimeend, doc);
        doc.close();
        contents = byteArrayOutputStream.toByteArray();
    // generate the file
    } catch (Exception exc) {
        exc.printStackTrace();
        return null;
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    String filename = "measures.pdf";
    headers.setContentDispositionFormData(filename, filename);
    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
    ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
    return response;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Featureofinterest(com.webstart.model.Featureofinterest) HelperCls(com.webstart.Helpers.HelperCls) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Users(com.webstart.model.Users) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) ResponseEntity(org.springframework.http.ResponseEntity) com.itextpdf.text(com.itextpdf.text) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) WateringMeasure(com.webstart.DTO.WateringMeasure)

Example 2 with WateringMeasure

use of com.webstart.DTO.WateringMeasure in project FarmCloud by vratsasg.

the class ExtractController method getCsv.

@RequestMapping(value = "{mydevice}/watering/csv", params = { "dtstart", "dtend" }, method = RequestMethod.POST)
public void getCsv(@PathVariable("mydevice") String mydevice, @RequestParam("dtstart") String datetimestart, @RequestParam("dtend") String datetimeend, HttpServletRequest request, HttpServletResponse response) {
    response.setContentType("text/plain");
    String reportName = "Watering_Measures.csv";
    response.setHeader("Content-disposition", "attachment; filename=" + reportName);
    try {
        Users user = (Users) request.getSession().getAttribute("current_user");
        // DateTime Convertable
        Featureofinterest featureofinterest = featureofInterestService.getFeatureofinterestByIdentifier(mydevice);
        DateTimeFormatter dtfInput = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        HelperCls.ConvertToDateTime convertable = new HelperCls.ConvertToDateTime();
        DateTime from = convertable.GetUTCDateTime(datetimestart, dtfInput, featureofinterest.getTimezone(), StatusTimeConverterEnum.TO_UTC);
        DateTime to = convertable.GetUTCDateTime(datetimeend, dtfInput, featureofinterest.getTimezone(), StatusTimeConverterEnum.TO_UTC);
        // 
        WateringMeasure wateringMeasure = observationProperyService.getWateringData(user.getUser_id(), mydevice, from, to);
        // 
        ArrayList<String> rows = new ArrayList<String>();
        rows.add("Watering Time from");
        rows.add(";");
        rows.add("Watering Time until");
        rows.add(";");
        rows.add("Total Duration");
        rows.add(";");
        rows.add("Watering consumption");
        rows.add(";");
        rows.add("Unit");
        rows.add("\n");
        // write table row data
        for (WateringValueTime valueTime : wateringMeasure.getMeasuredata()) {
            rows.add(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(valueTime.getPhenomenonDateTimeFrom()));
            rows.add(";");
            rows.add(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(valueTime.getPhenomenonDateTimeTo()));
            rows.add(";");
            rows.add(valueTime.getDateTimeDiff());
            rows.add(";");
            rows.add(valueTime.getValue().toString());
            rows.add(";");
            rows.add(wateringMeasure.getUnit());
            rows.add("\n");
        }
        Iterator<String> iter = rows.iterator();
        while (iter.hasNext()) {
            String outputString = (String) iter.next();
            response.getOutputStream().print(outputString);
        }
        response.getOutputStream().flush();
    } catch (Exception exc) {
        exc.printStackTrace();
    }
}
Also used : Featureofinterest(com.webstart.model.Featureofinterest) HelperCls(com.webstart.Helpers.HelperCls) ArrayList(java.util.ArrayList) Users(com.webstart.model.Users) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) WateringValueTime(com.webstart.DTO.WateringValueTime) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) SimpleDateFormat(java.text.SimpleDateFormat) WateringMeasure(com.webstart.DTO.WateringMeasure)

Example 3 with WateringMeasure

use of com.webstart.DTO.WateringMeasure in project FarmCloud by vratsasg.

the class ExtractController method getXlsx.

@RequestMapping(value = "{mydevice}/watering/xls", params = { "dtstart", "dtend" }, method = RequestMethod.POST)
public ResponseEntity<byte[]> getXlsx(@PathVariable("mydevice") String mydevice, @RequestParam("dtstart") String datetimestart, @RequestParam("dtend") String datetimeend, HttpServletRequest request) {
    WateringMeasure wateringMeasure = null;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] contents;
    try {
        Users user = (Users) request.getSession().getAttribute("current_user");
        // DateTime Convertable
        Featureofinterest featureofinterest = featureofInterestService.getFeatureofinterestByIdentifier(mydevice);
        DateTimeFormatter dtfInput = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        HelperCls.ConvertToDateTime convertable = new HelperCls.ConvertToDateTime();
        DateTime from = convertable.GetUTCDateTime(datetimestart, dtfInput, featureofinterest.getTimezone(), StatusTimeConverterEnum.TO_UTC);
        DateTime to = convertable.GetUTCDateTime(datetimeend, dtfInput, featureofinterest.getTimezone(), StatusTimeConverterEnum.TO_UTC);
        // 
        wateringMeasure = observationProperyService.getWateringData(user.getUser_id(), mydevice, from, to);
        // Create excel document
        HSSFWorkbook wbook = createXlsx(wateringMeasure);
        wbook.write(byteArrayOutputStream);
        contents = byteArrayOutputStream.toByteArray();
    // generate the file
    } catch (Exception exc) {
        exc.printStackTrace();
        return null;
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
    String filename = "measures.xls";
    headers.setContentDispositionFormData(filename, filename);
    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
    ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
    return response;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Featureofinterest(com.webstart.model.Featureofinterest) HelperCls(com.webstart.Helpers.HelperCls) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Users(com.webstart.model.Users) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) ResponseEntity(org.springframework.http.ResponseEntity) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) WateringMeasure(com.webstart.DTO.WateringMeasure)

Aggregations

WateringMeasure (com.webstart.DTO.WateringMeasure)3 HelperCls (com.webstart.Helpers.HelperCls)3 Featureofinterest (com.webstart.model.Featureofinterest)3 Users (com.webstart.model.Users)3 DateTime (org.joda.time.DateTime)3 LocalDateTime (org.joda.time.LocalDateTime)3 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HttpHeaders (org.springframework.http.HttpHeaders)2 ResponseEntity (org.springframework.http.ResponseEntity)2 com.itextpdf.text (com.itextpdf.text)1 WateringValueTime (com.webstart.DTO.WateringValueTime)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1