001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.scheduler.def;
023:
024: import java.util.Date;
025:
026: import org.dom4j.Element;
027: import org.jbpm.calendar.BusinessCalendar;
028: import org.jbpm.calendar.Duration;
029: import org.jbpm.graph.def.Action;
030: import org.jbpm.graph.def.GraphElement;
031: import org.jbpm.graph.exe.ExecutionContext;
032: import org.jbpm.job.Timer;
033: import org.jbpm.jpdl.xml.JpdlXmlReader;
034: import org.jbpm.jpdl.xml.Problem;
035: import org.jbpm.scheduler.SchedulerService;
036: import org.jbpm.svc.Services;
037: import org.jbpm.util.Clock;
038:
039: public class CreateTimerAction extends Action {
040:
041: private static final long serialVersionUID = 1L;
042: static BusinessCalendar businessCalendar = new BusinessCalendar();
043:
044: String timerName = null;
045: String dueDate = null;
046: String repeat = null;
047: String transitionName = null;
048: Action timerAction = null;
049:
050: public void read(Element actionElement, JpdlXmlReader jpdlReader) {
051: timerName = actionElement.attributeValue("name");
052: timerAction = jpdlReader.readSingleAction(actionElement);
053:
054: dueDate = actionElement.attributeValue("duedate");
055: if (dueDate == null) {
056: jpdlReader
057: .addWarning("no duedate specified in create timer action '"
058: + actionElement + "'");
059: }
060: repeat = actionElement.attributeValue("repeat");
061: if ("true".equalsIgnoreCase(repeat)
062: || "yes".equalsIgnoreCase(repeat)) {
063: repeat = dueDate;
064: }
065: transitionName = actionElement.attributeValue("transition");
066:
067: if ((transitionName != null) && (repeat != null)) {
068: repeat = null;
069: jpdlReader.addProblem(new Problem(Problem.LEVEL_WARNING,
070: "ignoring repeat on timer with transition "
071: + actionElement.asXML()));
072: }
073: }
074:
075: public void execute(ExecutionContext executionContext)
076: throws Exception {
077: Timer timer = createTimer(executionContext);
078: SchedulerService schedulerService = (SchedulerService) Services
079: .getCurrentService(Services.SERVICENAME_SCHEDULER);
080: schedulerService.createTimer(timer);
081: }
082:
083: protected Timer createTimer(ExecutionContext executionContext) {
084: Timer timer = new Timer(executionContext.getToken());
085: timer.setName(timerName);
086: timer.setRepeat(repeat);
087: if (dueDate != null) {
088: Duration duration = new Duration(dueDate);
089: Date dueDateDate = businessCalendar.add(Clock
090: .getCurrentTime(), duration);
091: timer.setDueDate(dueDateDate);
092: }
093: timer.setAction(timerAction);
094: timer.setTransitionName(transitionName);
095: timer.setGraphElement(executionContext.getEventSource());
096: timer.setTaskInstance(executionContext.getTaskInstance());
097:
098: // if this action was executed for a graph element
099: if ((getEvent() != null)
100: && (getEvent().getGraphElement() != null)) {
101: GraphElement graphElement = getEvent().getGraphElement();
102: try {
103: executionContext.setTimer(timer);
104: // fire the create timer event on the same graph element
105: graphElement
106: .fireEvent("timer-create", executionContext);
107: } finally {
108: executionContext.setTimer(null);
109: }
110: }
111:
112: return timer;
113: }
114:
115: public String getDueDate() {
116: return dueDate;
117: }
118:
119: public void setDueDate(String dueDateDuration) {
120: this .dueDate = dueDateDuration;
121: }
122:
123: public String getRepeat() {
124: return repeat;
125: }
126:
127: public void setRepeat(String repeatDuration) {
128: this .repeat = repeatDuration;
129: }
130:
131: public String getTransitionName() {
132: return transitionName;
133: }
134:
135: public void setTransitionName(String transitionName) {
136: this .transitionName = transitionName;
137: }
138:
139: public String getTimerName() {
140: return timerName;
141: }
142:
143: public void setTimerName(String timerName) {
144: this .timerName = timerName;
145: }
146:
147: public Action getTimerAction() {
148: return timerAction;
149: }
150:
151: public void setTimerAction(Action timerAction) {
152: this.timerAction = timerAction;
153: }
154: }
|