Search in sources :

Example 6 with CronTimerTrigger

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");
    }
}
Also used : CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) IOException(java.io.IOException) JsonException(com.serotonin.json.JsonException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) DateTime(org.joda.time.DateTime)

Example 7 with CronTimerTrigger

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);
        }
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) User(com.serotonin.m2m2.vo.User) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) ParseException(java.text.ParseException) RecipientListEntryBean(com.serotonin.m2m2.web.dwr.beans.RecipientListEntryBean)

Example 8 with CronTimerTrigger

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);
        }
    }
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) ParseException(java.text.ParseException)

Example 9 with CronTimerTrigger

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;
}
Also used : OneTimeTrigger(com.serotonin.timer.OneTimeTrigger) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) ParseException(java.text.ParseException) DateTime(org.joda.time.DateTime) Date(java.util.Date)

Example 10 with CronTimerTrigger

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);
    }
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) ParseException(java.text.ParseException)

Aggregations

CronTimerTrigger (com.serotonin.timer.CronTimerTrigger)11 ParseException (java.text.ParseException)8 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)7 DateTime (org.joda.time.DateTime)4 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)3 User (com.serotonin.m2m2.vo.User)3 InvalidArgumentException (com.serotonin.InvalidArgumentException)2 JsonException (com.serotonin.json.JsonException)2 OneTimeTrigger (com.serotonin.timer.OneTimeTrigger)2 IOException (java.io.IOException)2 Date (java.util.Date)2 JsonArray (com.serotonin.json.type.JsonArray)1 JsonValue (com.serotonin.json.type.JsonValue)1 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)1 UserDao (com.serotonin.m2m2.db.dao.UserDao)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 ReportDao (com.serotonin.m2m2.reports.ReportDao)1 ReportVO (com.serotonin.m2m2.reports.vo.ReportVO)1 TimeoutTask (com.serotonin.m2m2.util.timeout.TimeoutTask)1