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();
}
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);
}
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();
}
Aggregations