001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.mail;
018:
019: import org.kuali.bus.services.KSBServiceLocator;
020: import org.kuali.rice.core.Core;
021: import org.kuali.rice.lifecycle.Lifecycle;
022: import org.quartz.CronTrigger;
023: import org.quartz.JobDetail;
024: import org.quartz.ObjectAlreadyExistsException;
025: import org.quartz.Scheduler;
026: import org.quartz.SchedulerException;
027: import org.quartz.Trigger;
028:
029: import edu.iu.uis.eden.EdenConstants;
030:
031: /**
032: * A {@link Lifecycle} which is initialized on system startup that sets up
033: * the daily and weekly email reminders.
034: *
035: * @author rkirkend
036: */
037: public class EmailReminderLifecycle implements Lifecycle {
038:
039: private static final String DAILY_TRIGGER_NAME = "Daily Email Trigger";
040: private static final String DAILY_JOB_NAME = "Daily Email";
041: private static final String WEEKLY_TRIGGER_NAME = "Weekly Email Trigger";
042: private static final String WEEKLY_JOB_NAME = "Weekly Email";
043: private static final String EMAIL_BATCH_GROUP_NAME = "Email Batch";
044:
045: private boolean started;
046:
047: public boolean isStarted() {
048: return started;
049: }
050:
051: public void start() throws Exception {
052: String emailBatchGroup = "Email Batch";
053:
054: CronTrigger dailyTrigger = new CronTrigger(DAILY_TRIGGER_NAME,
055: emailBatchGroup,
056: Core.getCurrentContextConfig().getProperty(
057: EdenConstants.DAILY_EMAIL_CRON_EXPRESSION));
058: JobDetail dailyJobDetail = new JobDetail(DAILY_JOB_NAME,
059: emailBatchGroup, DailyEmailJob.class);
060: dailyTrigger.setJobName(dailyJobDetail.getName());
061: dailyTrigger.setJobGroup(dailyJobDetail.getGroup());
062: addJobToScheduler(dailyJobDetail);
063: addTriggerToScheduler(dailyTrigger);
064:
065: CronTrigger weeklyTrigger = new CronTrigger(
066: WEEKLY_TRIGGER_NAME, emailBatchGroup,
067: Core.getCurrentContextConfig().getProperty(
068: EdenConstants.WEEKLY_EMAIL_CRON_EXPRESSION));
069: JobDetail weeklyJobDetail = new JobDetail(WEEKLY_JOB_NAME,
070: emailBatchGroup, WeeklyEmailJob.class);
071: weeklyTrigger.setJobName(weeklyJobDetail.getName());
072: weeklyTrigger.setJobGroup(weeklyJobDetail.getGroup());
073: addJobToScheduler(weeklyJobDetail);
074: addTriggerToScheduler(weeklyTrigger);
075:
076: started = true;
077: }
078:
079: public void stop() throws Exception {
080: started = false;
081: }
082:
083: private void addJobToScheduler(JobDetail jobDetail)
084: throws SchedulerException {
085: getScheduler().addJob(jobDetail, true);
086: }
087:
088: private void addTriggerToScheduler(Trigger trigger)
089: throws SchedulerException {
090: boolean triggerExists = (getScheduler().getTrigger(
091: trigger.getName(), trigger.getGroup()) != null);
092: if (!triggerExists) {
093: try {
094: getScheduler().scheduleJob(trigger);
095: } catch (ObjectAlreadyExistsException ex) {
096: getScheduler().rescheduleJob(trigger.getName(),
097: trigger.getGroup(), trigger);
098: }
099: } else {
100: getScheduler().rescheduleJob(trigger.getName(),
101: trigger.getGroup(), trigger);
102: }
103: }
104:
105: private Scheduler getScheduler() {
106: return KSBServiceLocator.getScheduler();
107: }
108:
109: }
|