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