return schedule;
}
private <T> CScheduledTask translateFrom(ScheduledTask<T> task) {
CScheduledTask storeableTask = new CScheduledTask();
storeableTask.setEnabled(task.isEnabled());
storeableTask.setId(task.getId());
storeableTask.setName(task.getName());
storeableTask.setType(task.getType());
storeableTask.setStatus(task.getTaskState().name());
if (task.getLastRun() != null) {
storeableTask.setLastRun(task.getLastRun().getTime());
}
if (task.getNextRun() != null) {
storeableTask.setNextRun(task.getNextRun().getTime());
}
for (String key : task.getTaskParams().keySet()) {
CProps props = new CProps();
props.setKey(key);
props.setValue(task.getTaskParams().get(key));
storeableTask.addProperty(props);
}
Schedule schedule = task.getSchedule();
CScheduleConfig storeableSchedule = new CScheduleConfig();
if (schedule != null) {
if (CronSchedule.class.isAssignableFrom(schedule.getClass())) {
storeableSchedule.setType(CScheduleConfig.TYPE_ADVANCED);
storeableSchedule.setCronCommand(((CronSchedule) schedule).getCronString());
}
else if (MonthlySchedule.class.isAssignableFrom(schedule.getClass())) {
storeableSchedule.setType(CScheduleConfig.TYPE_MONTHLY);
storeableSchedule.setStartDate(((MonthlySchedule) schedule).getStartDate().getTime());
Date endDate = ((MonthlySchedule) schedule).getEndDate();
if (endDate != null) {
storeableSchedule.setEndDate(endDate.getTime());
}
for (Iterator iter = ((MonthlySchedule) schedule).getDaysToRun().iterator(); iter.hasNext(); ) {
// TODO: String.valueOf is used because currently the days to run are integers in the monthly
// schedule
// needs to be string
storeableSchedule.addDaysOfMonth(String.valueOf(iter.next()));
}
}
else if (WeeklySchedule.class.isAssignableFrom(schedule.getClass())) {
storeableSchedule.setType(CScheduleConfig.TYPE_WEEKLY);
storeableSchedule.setStartDate(((WeeklySchedule) schedule).getStartDate().getTime());
Date endDate = ((WeeklySchedule) schedule).getEndDate();
if (endDate != null) {
storeableSchedule.setEndDate(endDate.getTime());
}
for (Iterator iter = ((WeeklySchedule) schedule).getDaysToRun().iterator(); iter.hasNext(); ) {
// TODO: String.valueOf is used because currently the days to run are integers in the weekly
// schedule
// needs to be string
storeableSchedule.addDaysOfWeek(String.valueOf(iter.next()));
}
}
else if (DailySchedule.class.isAssignableFrom(schedule.getClass())) {
storeableSchedule.setType(CScheduleConfig.TYPE_DAILY);
storeableSchedule.setStartDate(((DailySchedule) schedule).getStartDate().getTime());
Date endDate = ((DailySchedule) schedule).getEndDate();
if (endDate != null) {
storeableSchedule.setEndDate(endDate.getTime());
}
}
else if (HourlySchedule.class.isAssignableFrom(schedule.getClass())) {
storeableSchedule.setType(CScheduleConfig.TYPE_HOURLY);
storeableSchedule.setStartDate(((HourlySchedule) schedule).getStartDate().getTime());
Date endDate = ((HourlySchedule) schedule).getEndDate();
if (endDate != null) {
storeableSchedule.setEndDate(endDate.getTime());
}
}
else if (OnceSchedule.class.isAssignableFrom(schedule.getClass())) {
storeableSchedule.setType(CScheduleConfig.TYPE_ONCE);
storeableSchedule.setStartDate(((OnceSchedule) schedule).getStartDate().getTime());
}
else if (RunNowSchedule.class.isAssignableFrom(schedule.getClass())) {
storeableSchedule.setType(CScheduleConfig.TYPE_RUN_NOW);
}
else if (ManualRunSchedule.class.isAssignableFrom(schedule.getClass())) {
storeableSchedule.setType(CScheduleConfig.TYPE_MANUAL);
}
else {
throw new IllegalArgumentException("Unknown Schedule type: " + schedule.getClass().getName());
}
}
storeableTask.setSchedule(storeableSchedule);
return storeableTask;
}