Search in sources :

Example 1 with SpreadsheetEmporter

use of com.serotonin.m2m2.vo.emport.SpreadsheetEmporter in project ma-core-public by infiniteautomation.

the class ChartExportServlet method exportExcel.

/**
 * Do the export as Excel XLSX File
 * @param response
 * @param from
 * @param to
 * @param def
 * @param user
 * @throws IOException
 */
private void exportExcel(HttpServletResponse response, long from, long to, DataExportDefinition def, User user) throws IOException {
    DataPointDao dataPointDao = DataPointDao.instance;
    PointValueDao pointValueDao = Common.databaseProxy.newPointValueDao();
    // Stream the content.
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    final List<PointValueEmporter> sheetEmporters = new ArrayList<PointValueEmporter>();
    final AtomicInteger sheetIndex = new AtomicInteger();
    sheetEmporters.add(new PointValueEmporter(Common.translate("emport.pointValues") + " " + sheetIndex.get()));
    final SpreadsheetEmporter emporter = new SpreadsheetEmporter(FileType.XLSX);
    BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
    emporter.prepareExport(bos);
    emporter.prepareSheetExport(sheetEmporters.get(0));
    final ExportDataValue edv = new ExportDataValue();
    MappedRowCallback<PointValueTime> callback = new MappedRowCallback<PointValueTime>() {

        @Override
        public void row(PointValueTime pvt, int rowIndex) {
            edv.setValue(pvt.getValue());
            edv.setTime(pvt.getTime());
            if (pvt instanceof AnnotatedPointValueTime)
                edv.setAnnotation(((AnnotatedPointValueTime) pvt).getSourceMessage());
            else
                edv.setAnnotation(null);
            sheetEmporters.get(sheetIndex.get()).exportRow(edv);
            if (sheetEmporters.get(sheetIndex.get()).getRowsAdded() >= emporter.getMaxRowsPerSheet()) {
                ExportPointInfo info = sheetEmporters.get(sheetIndex.get()).getPointInfo();
                sheetIndex.incrementAndGet();
                PointValueEmporter sheetEmporter = new PointValueEmporter(Common.translate("emport.pointValues") + " " + sheetIndex.get());
                sheetEmporter.setPointInfo(info);
                sheetEmporters.add(sheetEmporter);
                emporter.prepareSheetExport(sheetEmporters.get(sheetIndex.get()));
            }
        }
    };
    for (int pointId : def.getPointIds()) {
        DataPointVO dp = dataPointDao.getDataPoint(pointId, false);
        if (Permissions.hasDataPointReadPermission(user, dp)) {
            ExportPointInfo pointInfo = new ExportPointInfo();
            pointInfo.setXid(dp.getXid());
            pointInfo.setPointName(dp.getName());
            pointInfo.setDeviceName(dp.getDeviceName());
            pointInfo.setTextRenderer(dp.getTextRenderer());
            sheetEmporters.get(sheetIndex.get()).setPointInfo(pointInfo);
            pointValueDao.getPointValuesBetween(pointId, from, to, callback);
        }
    }
    emporter.finishExport();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) ExportDataValue(com.serotonin.m2m2.vo.export.ExportDataValue) ArrayList(java.util.ArrayList) ExportPointInfo(com.serotonin.m2m2.vo.export.ExportPointInfo) SpreadsheetEmporter(com.serotonin.m2m2.vo.emport.SpreadsheetEmporter) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MappedRowCallback(com.serotonin.db.MappedRowCallback) PointValueEmporter(com.serotonin.m2m2.rt.dataImage.PointValueEmporter) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with SpreadsheetEmporter

use of com.serotonin.m2m2.vo.emport.SpreadsheetEmporter in project ma-core-public by infiniteautomation.

the class FileUploadController method parseFile.

/**
 * Parse the Import Files
 * @param input
 * @param model
 * @param translations
 */
protected void parseFile(InputStream input, Map<String, Object> model, Translations translations, HttpServletRequest request) {
    // Get the filename
    String filename = (String) model.get("filename");
    SpreadsheetEmporter emporter;
    if (filename == null)
        return;
    else {
        if (filename.toLowerCase().endsWith(".xls"))
            emporter = new SpreadsheetEmporter(FileType.XLS);
        else if (filename.toLowerCase().endsWith(".xlsx"))
            emporter = new SpreadsheetEmporter(FileType.XLSX);
        else
            return;
    }
    // Switch on the type
    String dataType = (String) model.get("dataType");
    if (dataType != null) {
        if (dataType.equals("pointValue")) {
            // List the sheets and create sheet emporters for each
            for (Sheet sheet : emporter.listSheets(input)) emporter.doImport(input, new PointValueEmporter(sheet.getSheetName()));
        } else
            throw new ShouldNeverHappenException("Unsupported data.");
    }
    model.put("hasImportErrors", emporter.hasErrors());
    // Get the messages
    if (emporter.hasErrors()) {
        List<String> errorMessages = new ArrayList<String>();
        for (TranslatableMessage msg : emporter.getErrorMessages()) {
            errorMessages.add(msg.translate(translations));
        }
        model.put("errorMessages", errorMessages);
    }
    model.put("rowsImported", emporter.getRowsAdded());
    model.put("rowsDeleted", emporter.getRowsDeleted());
    model.put("rowsWithErrors", emporter.getRowErrors());
}
Also used : PointValueEmporter(com.serotonin.m2m2.rt.dataImage.PointValueEmporter) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) ArrayList(java.util.ArrayList) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) SpreadsheetEmporter(com.serotonin.m2m2.vo.emport.SpreadsheetEmporter) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 3 with SpreadsheetEmporter

use of com.serotonin.m2m2.vo.emport.SpreadsheetEmporter in project ma-core-public by infiniteautomation.

the class EventExportServlet method exportExcel.

/**
 * @param response
 * @param def
 * @param user
 * @throws IOException
 */
private void exportExcel(HttpServletResponse response, User user) throws IOException {
    // Stream the content.
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    final EventInstanceEmporter sheetEmporter = new EventInstanceEmporter();
    final SpreadsheetEmporter emporter = new SpreadsheetEmporter(FileType.XLSX);
    BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
    emporter.prepareExport(bos);
    emporter.prepareSheetExport(sheetEmporter);
    QueryDefinition queryData = (QueryDefinition) user.getAttribute("eventInstanceExportDefinition");
    DojoQueryCallback<EventInstanceVO> callback = new DojoQueryCallback<EventInstanceVO>(false) {

        @Override
        public void row(EventInstanceVO vo, int rowIndex) {
            sheetEmporter.exportRow(vo);
        }
    };
    EventInstanceDao.instance.exportQuery(queryData.getQuery(), queryData.getSort(), null, null, queryData.isOr(), callback);
    emporter.finishExport();
}
Also used : QueryDefinition(com.serotonin.m2m2.web.dwr.QueryDefinition) EventInstanceEmporter(com.serotonin.m2m2.vo.event.EventInstanceEmporter) EventInstanceVO(com.serotonin.m2m2.vo.event.EventInstanceVO) DojoQueryCallback(com.serotonin.m2m2.db.dao.DojoQueryCallback) SpreadsheetEmporter(com.serotonin.m2m2.vo.emport.SpreadsheetEmporter) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

SpreadsheetEmporter (com.serotonin.m2m2.vo.emport.SpreadsheetEmporter)3 PointValueEmporter (com.serotonin.m2m2.rt.dataImage.PointValueEmporter)2 BufferedOutputStream (java.io.BufferedOutputStream)2 ArrayList (java.util.ArrayList)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 MappedRowCallback (com.serotonin.db.MappedRowCallback)1 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)1 DojoQueryCallback (com.serotonin.m2m2.db.dao.DojoQueryCallback)1 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)1 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 EventInstanceEmporter (com.serotonin.m2m2.vo.event.EventInstanceEmporter)1 EventInstanceVO (com.serotonin.m2m2.vo.event.EventInstanceVO)1 ExportDataValue (com.serotonin.m2m2.vo.export.ExportDataValue)1 ExportPointInfo (com.serotonin.m2m2.vo.export.ExportPointInfo)1 QueryDefinition (com.serotonin.m2m2.web.dwr.QueryDefinition)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1