use of com.serotonin.m2m2.reports.vo.ReportPointVO in project ma-modules-public by infiniteautomation.
the class ReportVO method getXidMapping.
public Map<String, String> getXidMapping() {
DataPointDao dpd = DataPointDao.instance;
Map<String, String> ans = new HashMap<String, String>();
for (ReportPointVO vo : points) {
// Check to see if point exists
DataPointVO dp = dpd.get(vo.getPointId());
if (dp != null)
ans.put(dp.getXid(), vo.getPointKey());
}
return ans;
}
use of com.serotonin.m2m2.reports.vo.ReportPointVO in project ma-modules-public by infiniteautomation.
the class ReportWorkItem method execute.
@Override
public void execute() {
try {
ReportLicenseChecker.checkLicense();
} catch (LicenseViolatedException e) {
LOG.error("Your core license doesn't permit you to use the reports module.");
reportInstance.setReportStartTime(Common.timer.currentTimeMillis());
reportInstance.setReportEndTime(Common.timer.currentTimeMillis());
reportInstance.setRecordCount(-1);
reportDao.saveReportInstance(reportInstance);
return;
}
LOG.debug("Running report with id " + reportConfig.getId() + ", instance id " + reportInstance.getId());
reportInstance.setRunStartTime(System.currentTimeMillis());
reportDao.saveReportInstance(reportInstance);
Translations translations = Common.getTranslations();
// Create a list of DataPointVOs to which the user has permission.
DataPointDao dataPointDao = DataPointDao.instance;
List<ReportDao.PointInfo> points = new ArrayList<ReportDao.PointInfo>(reportConfig.getPoints().size());
for (ReportPointVO reportPoint : reportConfig.getPoints()) {
DataPointVO point = dataPointDao.getDataPoint(reportPoint.getPointId());
if (point != null && Permissions.hasDataPointReadPermission(user, point)) {
String colour = null;
try {
if (!StringUtils.isBlank(reportPoint.getColour()))
colour = ColorUtils.toHexString(reportPoint.getColour()).substring(1);
} catch (InvalidArgumentException e) {
// Should never happen since the colour would have been validated on save, so just let it go
// as null.
}
points.add(new ReportDao.PointInfo(point, colour, reportPoint.getWeight(), reportPoint.isConsolidatedChart(), reportPoint.getPlotType()));
}
}
int recordCount = 0;
try {
if (!points.isEmpty()) {
if (Common.databaseProxy.getNoSQLProxy() == null)
recordCount = reportDao.runReportSQL(reportInstance, points);
else
recordCount = reportDao.runReportNoSQL(reportInstance, points);
}
} catch (RuntimeException e) {
recordCount = -1;
throw e;
} catch (Throwable e) {
recordCount = -1;
throw new RuntimeException("Report instance failed", e);
} finally {
reportInstance.setRunEndTime(System.currentTimeMillis());
reportInstance.setRecordCount(recordCount);
reportDao.saveReportInstance(reportInstance);
}
if (reportConfig.isEmail()) {
String inlinePrefix = "R" + System.currentTimeMillis() + "-" + reportInstance.getId() + "-";
// TODO should we create different instances of the email based upon language and timezone?
// We are creating an email from the result. Create the content.
final ReportChartCreator creator = new ReportChartCreator(translations, TimeZone.getDefault());
creator.createContent(host, port, reportInstance, reportDao, inlinePrefix, reportConfig.isIncludeData());
// Create the to list
Set<String> addresses = MailingListDao.instance.getRecipientAddresses(reportConfig.getRecipients(), new DateTime(reportInstance.getReportStartTime()));
String[] toAddrs = addresses.toArray(new String[0]);
// Create the email content object.
EmailContent emailContent = new EmailContent(null, creator.getHtml(), Common.UTF8);
// Add the consolidated chart
if (creator.getImageData() != null)
emailContent.addInline(new EmailInline.ByteArrayInline(inlinePrefix + ReportChartCreator.IMAGE_CONTENT_ID, creator.getImageData(), ImageChartUtils.getContentType()));
// Add the point charts
for (PointStatistics pointStatistics : creator.getPointStatistics()) {
if (pointStatistics.getImageData() != null)
emailContent.addInline(new EmailInline.ByteArrayInline(inlinePrefix + pointStatistics.getChartName(), pointStatistics.getImageData(), ImageChartUtils.getContentType()));
}
// Add optional images used by the template.
for (String s : creator.getInlineImageList()) addImage(emailContent, s);
// Check if we need to attach the data.
if (reportConfig.isIncludeData()) {
addFileAttachment(emailContent, reportInstance.getName() + ".csv", creator.getExportFile());
addFileAttachment(emailContent, reportInstance.getName() + "Events.csv", creator.getEventFile());
addFileAttachment(emailContent, reportInstance.getName() + "Comments.csv", creator.getCommentFile());
}
PostEmailRunnable[] postEmail = null;
if (reportConfig.isIncludeData()) {
// See that the temp file(s) gets deleted after the email is sent.
PostEmailRunnable deleteTempFile = new PostEmailRunnable() {
@Override
public void run() {
for (File file : filesToDelete) {
if (!file.delete())
LOG.warn("Temp file " + file.getPath() + " not deleted");
}
}
};
postEmail = new PostEmailRunnable[] { deleteTempFile };
}
try {
TranslatableMessage lm = new TranslatableMessage("ftl.scheduledReport", reportConfig.getName());
String subject = creator.getSubject();
if (subject == null)
subject = lm.translate(translations);
EmailWorkItem.queueEmail(toAddrs, subject, emailContent, postEmail);
} catch (AddressException e) {
LOG.error(e);
}
if (reportConfig.isSchedule()) {
// Delete the report instance.
reportDao.deleteReportInstance(reportInstance.getId(), user.getId());
}
}
LOG.debug("Finished running report with id " + reportConfig.getId() + ", instance id " + reportInstance.getId());
}
use of com.serotonin.m2m2.reports.vo.ReportPointVO in project ma-modules-public by infiniteautomation.
the class ReportsDwr method createReportFromWatchlist.
@DwrPermission(custom = ReportPermissionDefinition.PERMISSION)
public ReportVO createReportFromWatchlist(String name, int[] dataPointIds) {
ReportVO report = new ReportVO();
User user = Common.getUser();
report.setName(new TranslatableMessage("common.copyPrefix", name).translate(getTranslations()));
report.setXid(Common.generateXid("REP_"));
DataPointDao dataPointDao = DataPointDao.instance;
for (int id : dataPointIds) {
DataPointVO dp = dataPointDao.getDataPoint(id, false);
if (dp == null || !Permissions.hasDataPointReadPermission(user, dp))
continue;
ReportPointVO rp = new ReportPointVO();
rp.setPointId(dp.getId());
rp.setPointKey("p" + dp.getId());
rp.setColour(dp.getChartColour());
rp.setConsolidatedChart(true);
rp.setPlotType(dp.getPlotType());
report.getPoints().add(rp);
}
return report;
}
use of com.serotonin.m2m2.reports.vo.ReportPointVO in project ma-modules-public by infiniteautomation.
the class ReportsDwr method runReport.
@DwrPermission(custom = ReportPermissionDefinition.PERMISSION)
public ProcessResult runReport(String xid, String name, List<ReportPointVO> points, String template, int includeEvents, boolean includeUserComments, int dateRangeType, int relativeDateType, int previousPeriodCount, int previousPeriodType, int pastPeriodCount, int pastPeriodType, boolean fromNone, int fromYear, int fromMonth, int fromDay, int fromHour, int fromMinute, boolean toNone, int toYear, int toMonth, int toDay, int toHour, int toMinute, boolean email, boolean includeData, boolean zipData, List<RecipientListEntryBean> recipients) {
ProcessResult response = new ProcessResult();
// Basic validation
// TODO Replace with vo.validate()
validateData(response, name, points, dateRangeType, relativeDateType, previousPeriodCount, pastPeriodCount);
if (!response.getHasMessages()) {
ReportVO report = new ReportVO();
report.setXid(xid);
report.setName(name);
report.setUserId(Common.getUser().getId());
report.setPoints(points);
report.setTemplate(template);
report.setIncludeEvents(includeEvents);
report.setIncludeUserComments(includeUserComments);
report.setDateRangeType(dateRangeType);
report.setRelativeDateType(relativeDateType);
report.setPreviousPeriodCount(previousPeriodCount);
report.setPreviousPeriodType(previousPeriodType);
report.setPastPeriodCount(pastPeriodCount);
report.setPastPeriodType(pastPeriodType);
report.setFromNone(fromNone);
report.setFromYear(fromYear);
report.setFromMonth(fromMonth);
report.setFromDay(fromDay);
report.setFromHour(fromHour);
report.setFromMinute(fromMinute);
report.setToNone(toNone);
report.setToYear(toYear);
report.setToMonth(toMonth);
report.setToDay(toDay);
report.setToHour(toHour);
report.setToMinute(toMinute);
// Never allow schedule when running from UI
report.setSchedule(false);
report.setEmail(email);
report.setIncludeData(includeData);
report.setZipData(zipData);
report.setRecipients(recipients);
String host = "";
WebContext webContext = WebContextFactory.get();
int port;
if (webContext != null) {
HttpServletRequest req = webContext.getHttpServletRequest();
host = req.getServerName();
port = req.getLocalPort();
} else {
port = Common.envProps.getInt("web.port", 8080);
}
ReportWorkItem.queueReport(host, port, report);
}
return response;
}
use of com.serotonin.m2m2.reports.vo.ReportPointVO in project ma-modules-public by infiniteautomation.
the class ReportsDwr method validateData.
private void validateData(ProcessResult response, String name, List<ReportPointVO> points, int dateRangeType, int relativeDateType, int previousPeriodCount, int pastPeriodCount) {
if (StringUtils.isBlank(name))
response.addContextualMessage("name", "reports.validate.required");
if (StringValidation.isLengthGreaterThan(name, 100))
response.addContextualMessage("name", "reports.validate.longerThan100");
if (points.isEmpty())
response.addContextualMessage("points", "reports.validate.needPoint");
if (dateRangeType != ReportVO.DATE_RANGE_TYPE_RELATIVE && dateRangeType != ReportVO.DATE_RANGE_TYPE_SPECIFIC)
response.addGenericMessage("reports.validate.invalidDateRangeType");
if (relativeDateType != ReportVO.RELATIVE_DATE_TYPE_PAST && relativeDateType != ReportVO.RELATIVE_DATE_TYPE_PREVIOUS)
response.addGenericMessage("reports.validate.invalidRelativeDateType");
if (previousPeriodCount < 1)
response.addContextualMessage("previousPeriodCount", "reports.validate.periodCountLessThan1");
if (pastPeriodCount < 1)
response.addContextualMessage("pastPeriodCount", "reports.validate.periodCountLessThan1");
User user = Common.getUser();
DataPointDao dataPointDao = DataPointDao.instance;
for (ReportPointVO point : points) {
Permissions.ensureDataPointReadPermission(user, dataPointDao.getDataPoint(point.getPointId(), false));
try {
if (!StringUtils.isBlank(point.getColour()))
ColorUtils.toColor(point.getColour());
} catch (InvalidArgumentException e) {
response.addContextualMessage("points", "reports.validate.colour", point.getColour());
}
if (point.getWeight() <= 0)
response.addContextualMessage("points", "reports.validate.weight");
}
}
Aggregations