Search in sources :

Example 1 with MappedRowCallback

use of com.serotonin.db.MappedRowCallback 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 MappedRowCallback

use of com.serotonin.db.MappedRowCallback in project ma-core-public by infiniteautomation.

the class Upgrade14 method upgrade.

/* (non-Javadoc)
	 * @see com.serotonin.m2m2.db.upgrade.DBUpgrade#upgrade()
	 */
@Override
protected void upgrade() throws Exception {
    OutputStream os = createUpdateLogOutputStream();
    // Run the scripts to add the ID Column
    Map<String, String[]> scripts = new HashMap<>();
    scripts.put(DatabaseProxy.DatabaseType.DERBY.name(), derbyIdColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.MYSQL.name(), mysqlIdColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.MSSQL.name(), mssqlIdColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.H2.name(), h2IdColumnScript);
    runScript(scripts, os);
    // Run the scripts to create the XID column but not restrict it to NOT NULL
    scripts = new HashMap<>();
    scripts.put(DatabaseProxy.DatabaseType.DERBY.name(), derbyXidColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.MYSQL.name(), mysqlXidColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.MSSQL.name(), mssqlXidColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.H2.name(), h2XidColumnScript);
    runScript(scripts, os);
    // Set XIDs on all the user comments, ugh
    AtomicInteger count = new AtomicInteger();
    final String prefix = "UC_";
    final RowMapper<Integer> mapper = new RowMapper<Integer>() {

        @Override
        public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
            return rs.getInt(1);
        }
    };
    final MappedRowCallback<Integer> callback = new MappedRowCallback<Integer>() {

        @Override
        public void row(Integer item, int index) {
            ejt.update("UPDATE userComments SET xid=? WHERE id=?", new Object[] { prefix + UUID.randomUUID(), item });
            count.incrementAndGet();
        }
    };
    this.ejt.query("SELECT id FROM userComments", new Object[] {}, new RowCallbackHandler() {

        private int rowNum = 0;

        public void processRow(ResultSet rs) throws SQLException {
            callback.row(mapper.mapRow(rs, rowNum), rowNum);
            rowNum++;
        }
    });
    String upgradedString = new String("Updated " + count.get() + " user comments with XIDs.\n");
    os.write(upgradedString.getBytes(Common.UTF8_CS));
    // Run the scripts to restrict the XID to NOT NULL
    scripts.put(DatabaseProxy.DatabaseType.DERBY.name(), derbyXidNotNullColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.MYSQL.name(), mysqlXidNotNullColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.MSSQL.name(), mssqlXidNotNullColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.H2.name(), h2XidNotNullColumnScript);
    runScript(scripts, os);
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) OutputStream(java.io.OutputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MappedRowCallback(com.serotonin.db.MappedRowCallback) ResultSet(java.sql.ResultSet) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler) RowMapper(org.springframework.jdbc.core.RowMapper)

Example 3 with MappedRowCallback

use of com.serotonin.db.MappedRowCallback in project ma-core-public by infiniteautomation.

the class ChartExportServlet method exportCsv.

/**
 * Do the export as a CSV File
 * @param response
 * @param from
 * @param to
 * @param def
 * @param user
 * @throws IOException
 */
private void exportCsv(HttpServletRequest request, 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("text/csv");
    final Translations translations = Common.getTranslations();
    final ExportCsvStreamer exportCreator = new ExportCsvStreamer(request.getServerName(), request.getLocalPort(), response.getWriter(), translations);
    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);
            exportCreator.pointData(edv);
        }
    };
    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());
            pointInfo.setDataPointId(pointId);
            exportCreator.startPoint(pointInfo);
            pointValueDao.getPointValuesBetween(pointId, from, to, callback);
        }
    }
    exportCreator.done();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) MappedRowCallback(com.serotonin.db.MappedRowCallback) ExportDataValue(com.serotonin.m2m2.vo.export.ExportDataValue) ExportCsvStreamer(com.serotonin.m2m2.vo.export.ExportCsvStreamer) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) ExportPointInfo(com.serotonin.m2m2.vo.export.ExportPointInfo) Translations(com.serotonin.m2m2.i18n.Translations)

Aggregations

MappedRowCallback (com.serotonin.db.MappedRowCallback)3 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)2 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)2 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)2 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)2 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)2 ExportDataValue (com.serotonin.m2m2.vo.export.ExportDataValue)2 ExportPointInfo (com.serotonin.m2m2.vo.export.ExportPointInfo)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Translations (com.serotonin.m2m2.i18n.Translations)1 PointValueEmporter (com.serotonin.m2m2.rt.dataImage.PointValueEmporter)1 SpreadsheetEmporter (com.serotonin.m2m2.vo.emport.SpreadsheetEmporter)1 ExportCsvStreamer (com.serotonin.m2m2.vo.export.ExportCsvStreamer)1 BufferedOutputStream (java.io.BufferedOutputStream)1 OutputStream (java.io.OutputStream)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 RowCallbackHandler (org.springframework.jdbc.core.RowCallbackHandler)1