use of com.serotonin.m2m2.reports.vo.ReportPointVO in project ma-modules-public by infiniteautomation.
the class ReportVO method validate.
@Override
public void validate(ProcessResult response) {
super.validate(response);
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");
UserDao dao = UserDao.instance;
User user = dao.getUser(userId);
if (user == null) {
response.addContextualMessage("userId", "reports.validate.userDNE");
}
File t = ReportCommon.instance.getTemplateFile(template);
if (!t.isFile())
response.addContextualMessage("template", "reports.validate.template");
DataPointDao dataPointDao = DataPointDao.instance;
for (ReportPointVO point : points) {
DataPointVO vo = dataPointDao.getDataPoint(point.getPointId(), false);
String pointXid = "unknown";
if (vo != null) {
pointXid = vo.getXid();
try {
Permissions.ensureDataPointReadPermission(user, dataPointDao.getDataPoint(point.getPointId(), false));
} catch (PermissionException e) {
response.addContextualMessage("points", "reports.vaildate.pointDNE");
}
} else {
response.addContextualMessage("points", "reports.validate.pointPermissions", user.getUsername(), pointXid);
}
try {
if (!StringUtils.isBlank(point.getColour()))
ColorUtils.toColor(point.getColour());
} catch (InvalidArgumentException e) {
response.addContextualMessage("points", "reports.validate.colour", point.getColour(), pointXid);
}
if (point.getWeight() <= 0)
response.addContextualMessage("points", "reports.validate.weight");
}
// Validate the schedule
if (schedule) {
if (schedulePeriod == SCHEDULE_CRON) {
try {
new CronTimerTrigger(scheduleCron);
} catch (ParseException e) {
response.addContextualMessage("scheduleCron", "validate.invalidValue");
}
}
}
}
use of com.serotonin.m2m2.reports.vo.ReportPointVO in project ma-modules-public by infiniteautomation.
the class ReportsDwr method saveReport.
@DwrPermission(custom = ReportPermissionDefinition.PERMISSION)
public ProcessResult saveReport(int id, String name, String xid, 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 schedule, int schedulePeriod, int runDelayMinutes, String scheduleCron, boolean email, boolean includeData, boolean zipData, List<RecipientListEntryBean> recipients) {
ProcessResult response = new ProcessResult();
// Basic validation
validateData(response, name, points, dateRangeType, relativeDateType, previousPeriodCount, pastPeriodCount);
// Validate XID
if (StringUtils.isBlank(xid))
response.addContextualMessage("xid", "validate.required");
else if (StringValidation.isLengthGreaterThan(xid, 50))
response.addMessage("xid", new TranslatableMessage("validate.notLongerThan", 50));
else if (!ReportDao.instance.isXidUnique(xid, id))
response.addContextualMessage("xid", "validate.xidUsed");
if (schedule) {
if (schedulePeriod == ReportVO.SCHEDULE_CRON) {
// Check the cron pattern.
try {
new CronTimerTrigger(scheduleCron);
} catch (Exception e) {
response.addContextualMessage("scheduleCron", "reports.validate.cron", e.getMessage());
}
} else {
if (runDelayMinutes < 0)
response.addContextualMessage("runDelayMinutes", "reports.validate.lessThan0");
else if (runDelayMinutes > 59)
response.addContextualMessage("runDelayMinutes", "reports.validate.greaterThan59");
}
}
if (email && recipients.isEmpty())
response.addContextualMessage("recipients", "reports.validate.needRecip");
if (response.getHasMessages())
return response;
User user = Common.getUser();
ReportDao reportDao = ReportDao.instance;
ReportVO report;
if (id == Common.NEW_ID) {
report = new ReportVO();
report.setUserId(user.getId());
} else
report = reportDao.getReport(id);
ReportCommon.ensureReportPermission(user, report);
// Update the new values.
report.setXid(xid);
report.setName(name);
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);
report.setSchedule(schedule);
report.setSchedulePeriod(schedulePeriod);
report.setRunDelayMinutes(runDelayMinutes);
report.setScheduleCron(scheduleCron);
report.setEmail(email);
report.setIncludeData(includeData);
report.setZipData(zipData);
report.setRecipients(recipients);
// Save the report
reportDao.saveReport(report);
// Conditionally schedule the report.
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);
}
ReportJob.scheduleReportJob(host, port, report);
// Send back the report id in case this was new.
response.addData("reportId", report.getId());
response.addData("report", report);
return response;
}
use of com.serotonin.m2m2.reports.vo.ReportPointVO in project ma-modules-public by infiniteautomation.
the class M2MReportVO method convert.
/**
* @return
*/
public ReportVO convert(M2MReportDao legacyDao) {
ReportVO vo = new ReportVO();
List<ReportPointVO> newPoints = new ArrayList<ReportPointVO>();
for (M2MReportPointVO legacyPoint : this.points) newPoints.add(legacyPoint.convert(legacyDao));
vo.setPoints(newPoints);
// Find the user from M2M
String username = legacyDao.getUsername(userId);
User user = UserDao.instance.getUser(username);
if (user != null)
vo.setUserId(user.getId());
else
throw new ShouldNeverHappenException("Unable to find User " + username + " in Mango.");
// Use default vo.setTemplate(template);
vo.setIncludeEvents(includeEvents);
vo.setIncludeUserComments(includeUserComments);
vo.setDateRangeType(dateRangeType);
vo.setRelativeDateType(relativeDateType);
vo.setPreviousPeriodCount(previousPeriodCount);
vo.setPreviousPeriodType(previousPeriodType);
vo.setPastPeriodCount(pastPeriodCount);
vo.setPastPeriodType(pastPeriodType);
vo.setFromNone(fromNone);
vo.setFromYear(fromYear);
vo.setFromMonth(fromMonth);
vo.setFromDay(fromDay);
vo.setFromHour(fromHour);
vo.setFromMinute(fromMinute);
vo.setToNone(toNone);
vo.setToYear(toYear);
vo.setToMonth(toMonth);
vo.setToDay(toDay);
vo.setToHour(toHour);
vo.setToMinute(toMinute);
vo.setSchedule(schedule);
vo.setSchedulePeriod(schedulePeriod);
vo.setScheduleCron(scheduleCron);
vo.setRunDelayMinutes(runDelayMinutes);
vo.setEmail(email);
// Convert the Recipient list
List<RecipientListEntryBean> newRecipients = new ArrayList<RecipientListEntryBean>();
for (M2MRecipientListEntryBean legacyRecipient : this.recipients) newRecipients.add(legacyRecipient.convert(legacyDao));
vo.setIncludeData(includeData);
vo.setZipData(zipData);
return vo;
}
use of com.serotonin.m2m2.reports.vo.ReportPointVO in project ma-modules-public by infiniteautomation.
the class M2MReportPointVO method convert.
/**
* @return
*/
public ReportPointVO convert(M2MReportDao legacyDao) {
ReportPointVO point = new ReportPointVO();
// Lookup point from M2M
String legacyXid = legacyDao.getDataPointXid(this.pointId);
// Lookup the new point by XID
DataPointVO dp = DataPointDao.instance.getByXid(legacyXid);
if (dp != null)
point.setPointId(dp.getId());
else
throw new ShouldNeverHappenException("No point found in Mango that corresponds to M2M XID: " + legacyXid);
point.setColour(colour);
point.setConsolidatedChart(consolidatedChart);
point.setPlotType(DataPointVO.PlotTypes.STEP);
point.setPointKey("");
point.setWeight(1);
return point;
}
use of com.serotonin.m2m2.reports.vo.ReportPointVO in project ma-modules-public by infiniteautomation.
the class ReportVO method jsonRead.
/* (non-Javadoc)
* @see com.serotonin.json.spi.JsonSerializable#jsonRead(com.serotonin.json.JsonReader, com.serotonin.json.type.JsonObject)
*/
@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
super.jsonRead(reader, jsonObject);
if (jsonObject.containsKey("userId")) {
userId = jsonObject.getInt("userId");
} else if (jsonObject.containsKey("user")) {
String username = jsonObject.getString("user");
if (org.apache.commons.lang3.StringUtils.isBlank(username))
throw new TranslatableJsonException("emport.error.missingValue", "user");
User user = UserDao.instance.getUser(username);
if (user == null)
throw new TranslatableJsonException("emport.error.missingUser", username);
userId = user.getId();
}
String text = jsonObject.getString("includeEvents");
if (text != null) {
includeEvents = EVENT_CODES.getId(text);
if (includeEvents == -1)
throw new TranslatableJsonException("emport.error.invalid", "includeEvents", text, EVENT_CODES.getCodeList());
}
text = jsonObject.getString("dateRangeType");
if (text != null) {
dateRangeType = DATE_RANGE_TYPES.getId(text);
if (dateRangeType == -1)
throw new TranslatableJsonException("emport.error.invalid", "dateRangeType", text, DATE_RANGE_TYPES.getCodeList());
}
if (dateRangeType == DATE_RANGE_TYPE_RELATIVE) {
text = jsonObject.getString("relativeDateType");
if (text != null) {
relativeDateType = DATE_RELATIVE_TYPES.getId(text);
if (relativeDateType == -1)
throw new TranslatableJsonException("emport.error.invalid", "relativeDateType", text, DATE_RELATIVE_TYPES.getCodeList());
}
if (relativeDateType == RELATIVE_DATE_TYPE_PREVIOUS) {
text = jsonObject.getString("previousPeriodType");
if (text != null) {
previousPeriodType = Common.TIME_PERIOD_CODES.getId(text);
if (previousPeriodType == -1)
throw new TranslatableJsonException("emport.error.invalid", "previousPeriodType", text, Common.TIME_PERIOD_CODES.getCodeList());
previousPeriodCount = jsonObject.getInt("previousPeriods");
} else {
// FOR legacy bug where previousPeriodType was misspelled
text = jsonObject.getString("perviousPeriodType");
if (text != null) {
previousPeriodType = Common.TIME_PERIOD_CODES.getId(text);
if (previousPeriodType == -1)
throw new TranslatableJsonException("emport.error.invalid", "previousPeriodType", text, Common.TIME_PERIOD_CODES.getCodeList());
previousPeriodCount = jsonObject.getInt("previousPeriods");
}
}
} else if (relativeDateType == RELATIVE_DATE_TYPE_PREVIOUS) {
text = jsonObject.getString("pastPeriodType");
if (text != null) {
pastPeriodType = Common.TIME_PERIOD_CODES.getId(text);
if (pastPeriodType == -1)
throw new TranslatableJsonException("emport.error.invalid", "pastPeriodType", text, Common.TIME_PERIOD_CODES.getCodeList());
pastPeriodCount = jsonObject.getInt("pastPeriods");
}
}
} else if (dateRangeType == DATE_RANGE_TYPE_SPECIFIC) {
fromNone = jsonObject.getBoolean("fromInception");
if (!fromNone) {
fromYear = jsonObject.getInt("fromYear");
fromMonth = jsonObject.getInt("fromMonth");
fromDay = jsonObject.getInt("fromDay");
fromHour = jsonObject.getInt("fromHour");
fromMinute = jsonObject.getInt("fromMinute");
}
toNone = jsonObject.getBoolean("toLatest");
if (!toNone) {
toYear = jsonObject.getInt("toYear");
toMonth = jsonObject.getInt("toMonth");
toDay = jsonObject.getInt("toDay");
toHour = jsonObject.getInt("toHour");
toMinute = jsonObject.getInt("toMinute");
}
}
schedule = jsonObject.getBoolean("schedule");
if (schedule) {
text = jsonObject.getString("schedulePeriod");
if (text != null) {
schedulePeriod = SCHEDULE_CODES.getId(text);
if (schedulePeriod == -1)
throw new TranslatableJsonException("emport.error.invalid", "schedulePeriod", text, SCHEDULE_CODES.getCodeList());
if (schedulePeriod == SCHEDULE_CRON) {
scheduleCron = jsonObject.getString("scheduleCron");
try {
new CronTimerTrigger(scheduleCron);
} catch (ParseException e) {
throw new TranslatableJsonException("emport.error.invalid", "scheduleCron", scheduleCron, "cron expressions");
}
}
} else {
throw new TranslatableJsonException("emport.error.invalid", "schedulePeriod", "null", SCHEDULE_CODES.getCodeList());
}
}
email = jsonObject.getBoolean("email");
if (email) {
JsonArray recipientsArray = jsonObject.getJsonArray("recipients");
boolean add = true;
if (recipientsArray != null) {
for (JsonValue jv : recipientsArray) {
RecipientListEntryBean recipient = new RecipientListEntryBean();
reader.readInto(recipient, jv);
for (RecipientListEntryBean existing : recipients) {
if (existing.equals(recipient)) {
reader.readInto(existing, jv);
add = false;
break;
}
}
if (add) {
recipients.add(recipient);
} else {
add = true;
}
}
} else {
throw new TranslatableJsonException("emport.error.invalid", "recipients", "null", "valid users, email addresses or mailing lists");
}
includeData = jsonObject.getBoolean("includeData");
if (includeData)
zipData = jsonObject.getBoolean("zipData");
}
JsonArray pointsArray = jsonObject.getJsonArray("points");
if (pointsArray != null) {
points = new ArrayList<ReportPointVO>();
for (JsonValue jv : pointsArray) {
ReportPointVO reportPoint = new ReportPointVO();
reader.readInto(reportPoint, jv);
// TODO prevent adding the same point twice?
points.add(reportPoint);
}
}
}
Aggregations