use of com.serotonin.timer.CronTimerTrigger in project ma-modules-public by infiniteautomation.
the class MaintenanceEventVO method validate.
public void validate(ProcessResult response) {
if (StringValidation.isLengthGreaterThan(alias, 50))
response.addContextualMessage("alias", "maintenanceEvents.validate.aliasTooLong");
if (dataSourceId <= 0)
response.addContextualMessage("dataSourceId", "validate.invalidValue");
// Check that cron patterns are ok.
if (scheduleType == TYPE_CRON) {
try {
new CronTimerTrigger(activeCron);
} catch (Exception e) {
response.addContextualMessage("activeCron", "maintenanceEvents.validate.activeCron", e.getMessage());
}
try {
new CronTimerTrigger(inactiveCron);
} catch (Exception e) {
response.addContextualMessage("inactiveCron", "maintenanceEvents.validate.inactiveCron", e.getMessage());
}
}
// Test that the triggers can be created.
MaintenanceEventRT rt = new MaintenanceEventRT(this);
try {
rt.createTrigger(true);
} catch (RuntimeException e) {
response.addContextualMessage("activeCron", "maintenanceEvents.validate.activeTrigger", e.getMessage());
}
try {
rt.createTrigger(false);
} catch (RuntimeException e) {
response.addContextualMessage("inactiveCron", "maintenanceEvents.validate.inactiveTrigger", e.getMessage());
}
// If the event is once, make sure the active time is earlier than the inactive time.
if (scheduleType == TYPE_ONCE) {
DateTime adt = new DateTime(activeYear, activeMonth, activeDay, activeHour, activeMinute, activeSecond, 0);
DateTime idt = new DateTime(inactiveYear, inactiveMonth, inactiveDay, inactiveHour, inactiveMinute, inactiveSecond, 0);
if (idt.getMillis() <= adt.getMillis())
response.addContextualMessage("scheduleType", "maintenanceEvents.validate.invalidRtn");
}
}
use of com.serotonin.timer.CronTimerTrigger 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);
}
}
}
use of com.serotonin.timer.CronTimerTrigger in project ma-modules-public by infiniteautomation.
the class ReportJob method scheduleReportJob.
public static void scheduleReportJob(String host, int port, ReportVO report) {
synchronized (JOB_REGISTRY) {
// Ensure that there is no existing job.
unscheduleReportJob(report);
if (report.isSchedule()) {
CronTimerTrigger trigger;
if (report.getSchedulePeriod() == ReportVO.SCHEDULE_CRON) {
try {
trigger = new CronTimerTrigger(report.getScheduleCron());
} catch (ParseException e) {
throw new ShouldNeverHappenException(e);
}
} else
trigger = Common.getCronTrigger(report.getSchedulePeriod(), report.getRunDelayMinutes() * 60);
ReportJob reportJob = new ReportJob(trigger, report, host, port);
JOB_REGISTRY.put(report.getId(), reportJob);
Common.timer.schedule(reportJob);
}
}
}
use of com.serotonin.timer.CronTimerTrigger in project ma-modules-public by infiniteautomation.
the class ScheduledEventRT method createTrigger.
public TimerTrigger createTrigger(boolean activeTrigger) {
if (!activeTrigger && !vo.isReturnToNormal())
return null;
if (vo.getScheduleType() == ScheduledEventVO.TYPE_CRON) {
try {
if (activeTrigger)
return new CronTimerTrigger(vo.getActiveCron());
return new CronTimerTrigger(vo.getInactiveCron());
} catch (ParseException e) {
// Should never happen, so wrap and rethrow
throw new ShouldNeverHappenException(e);
}
}
if (vo.getScheduleType() == ScheduledEventVO.TYPE_ONCE) {
DateTime dt;
if (activeTrigger)
dt = new DateTime(vo.getActiveYear(), vo.getActiveMonth(), vo.getActiveDay(), vo.getActiveHour(), vo.getActiveMinute(), vo.getActiveSecond(), 0);
else
dt = new DateTime(vo.getInactiveYear(), vo.getInactiveMonth(), vo.getInactiveDay(), vo.getInactiveHour(), vo.getInactiveMinute(), vo.getInactiveSecond(), 0);
return new OneTimeTrigger(new Date(dt.getMillis()));
}
int month = vo.getActiveMonth();
int day = vo.getActiveDay();
int hour = vo.getActiveHour();
int minute = vo.getActiveMinute();
int second = vo.getActiveSecond();
if (!activeTrigger) {
month = vo.getInactiveMonth();
day = vo.getInactiveDay();
hour = vo.getInactiveHour();
minute = vo.getInactiveMinute();
second = vo.getInactiveSecond();
}
StringBuilder expression = new StringBuilder();
expression.append(second).append(' ');
expression.append(minute).append(' ');
if (vo.getScheduleType() == ScheduledEventVO.TYPE_HOURLY)
expression.append("* * * ?");
else {
expression.append(hour).append(' ');
if (vo.getScheduleType() == ScheduledEventVO.TYPE_DAILY)
expression.append("* * ?");
else if (vo.getScheduleType() == ScheduledEventVO.TYPE_WEEKLY)
expression.append("? * ").append(weekdays[day]);
else {
if (day > 0)
expression.append(day);
else if (day == -1)
expression.append('L');
else
expression.append(-day).append('L');
if (vo.getScheduleType() == ScheduledEventVO.TYPE_MONTHLY)
expression.append(" * ?");
else
expression.append(' ').append(month).append(" ?");
}
}
CronTimerTrigger cronTrigger;
try {
cronTrigger = new CronTimerTrigger(expression.toString());
} catch (ParseException e) {
// Should never happen, so wrap and rethrow
throw new ShouldNeverHappenException(e);
}
return cronTrigger;
}
use of com.serotonin.timer.CronTimerTrigger in project ma-core-public by infiniteautomation.
the class Common method getCronTrigger.
public static CronTimerTrigger getCronTrigger(int periodType, int delaySeconds) {
int delayMinutes = 0;
if (delaySeconds >= 60) {
delayMinutes = delaySeconds / 60;
delaySeconds %= 60;
if (delayMinutes >= 60)
delayMinutes = 59;
}
try {
switch(periodType) {
case TimePeriods.MILLISECONDS:
throw new ShouldNeverHappenException("Can't create a cron trigger for milliseconds");
case TimePeriods.SECONDS:
return new CronTimerTrigger("* * * * * ?");
case TimePeriods.MINUTES:
return new CronTimerTrigger(delaySeconds + " * * * * ?");
case TimePeriods.HOURS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " * * * ?");
case TimePeriods.DAYS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 * * ?");
case TimePeriods.WEEKS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 ? * MON");
case TimePeriods.MONTHS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 1 * ?");
case TimePeriods.YEARS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 1 JAN ?");
default:
throw new ShouldNeverHappenException("Invalid cron period type: " + periodType);
}
} catch (ParseException e) {
throw new ShouldNeverHappenException(e);
}
}
Aggregations