Search in sources :

Example 1 with ObservableMeasure

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

the class ExtractController method getPDF.

@RequestMapping(value = "{mydevice}/{observablepropertyid}/pdf", params = { "dtstart", "dtend" }, method = RequestMethod.POST)
public ResponseEntity<byte[]> getPDF(@PathVariable("observablepropertyid") Long observablePropertyId, @PathVariable("mydevice") String mydevice, @RequestParam("dtstart") String datetimestart, @RequestParam("dtend") String datetimeend, HttpServletRequest request) {
    ObservableMeasure observableMeasure = 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);
        // 
        observableMeasure = observationProperyService.getObservationData(observablePropertyId, 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(observableMeasure, 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) ObservableMeasure(com.webstart.DTO.ObservableMeasure) com.itextpdf.text(com.itextpdf.text) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 2 with ObservableMeasure

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

the class ExtractController method getXlsx.

@RequestMapping(value = "{mydevice}/{observablepropertyid}/xls", params = { "dtstart", "dtend" }, method = RequestMethod.POST)
public ResponseEntity<byte[]> getXlsx(@PathVariable("observablepropertyid") Long observablepropertyid, @PathVariable("mydevice") String mydevice, @RequestParam("dtstart") String datetimestart, @RequestParam("dtend") String datetimeend, HttpServletRequest request) {
    ObservableMeasure observableMeasure = 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);
        // 
        observableMeasure = observationProperyService.getObservationData(observablepropertyid, user.getUser_id(), mydevice, from, to);
        // Create excel document
        HSSFWorkbook wbook = createXlsx(observableMeasure);
        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) ObservableMeasure(com.webstart.DTO.ObservableMeasure) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 3 with ObservableMeasure

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

the class ExtractController method getCsv.

@RequestMapping(value = "{mydevice}/{observablepropertyid}/csv", params = { "dtstart", "dtend" }, method = RequestMethod.POST)
public void getCsv(@PathVariable("observablepropertyid") Long observablepropertyid, @PathVariable("mydevice") String mydevice, @RequestParam("dtstart") String datetimestart, @RequestParam("dtend") String datetimeend, HttpServletRequest request, HttpServletResponse response) {
    response.setContentType("text/plain");
    DateTime.now().toString("yyyyMMddHHmmss");
    // String reportName = String.format("Measures-%1$s.csv", DateTime.now().toString("yyyyMMddHHmmss"));
    String reportName = "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);
        // 
        // ObservableMeasure observableMeasure = observationProperyService.getObservationData(observablepropertyid, user.getUser_id(), mydevice, from, to);
        ObservableMeasure observableMeasure = observationProperyService.getObservationData(observablepropertyid, 1, mydevice, from, to);
        ArrayList<String> rows = new ArrayList<String>();
        rows.add("Measure date & time");
        rows.add(";");
        rows.add("Measure value");
        rows.add(";");
        rows.add("Measure unit");
        rows.add("\n");
        // write table row data
        for (ValueTime valueTime : observableMeasure.getMeasuredata()) {
            rows.add(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(valueTime.getPhenomenonDateTime()));
            rows.add(";");
            rows.add(valueTime.getValue().toString());
            rows.add(";");
            rows.add(observableMeasure.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) ValueTime(com.webstart.DTO.ValueTime) WateringValueTime(com.webstart.DTO.WateringValueTime) ObservableMeasure(com.webstart.DTO.ObservableMeasure) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

ObservableMeasure (com.webstart.DTO.ObservableMeasure)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 ValueTime (com.webstart.DTO.ValueTime)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