Search in sources :

Example 6 with Featureofinterest

use of com.webstart.model.Featureofinterest 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 7 with Featureofinterest

use of com.webstart.model.Featureofinterest 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 8 with Featureofinterest

use of com.webstart.model.Featureofinterest 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 9 with Featureofinterest

use of com.webstart.model.Featureofinterest 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)

Example 10 with Featureofinterest

use of com.webstart.model.Featureofinterest 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

Featureofinterest (com.webstart.model.Featureofinterest)14 HelperCls (com.webstart.Helpers.HelperCls)12 DateTime (org.joda.time.DateTime)12 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)12 Users (com.webstart.model.Users)9 LocalDateTime (org.joda.time.LocalDateTime)9 ResponseEntity (org.springframework.http.ResponseEntity)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Timestamp (java.sql.Timestamp)4 SimpleDateFormat (java.text.SimpleDateFormat)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ObservableMeasure (com.webstart.DTO.ObservableMeasure)3 WateringMeasure (com.webstart.DTO.WateringMeasure)3 ParserException (jdk.nashorn.internal.runtime.ParserException)3 Instant (org.joda.time.Instant)3 com.itextpdf.text (com.itextpdf.text)2 WateringValueTime (com.webstart.DTO.WateringValueTime)2 NumericValue (com.webstart.model.NumericValue)2 Observation (com.webstart.model.Observation)2