View Javadoc

1   // 
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //   this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //   this list of conditions and the following disclaimer in the documentation  
12  //   and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //   nor the names of its contributors may be used to endorse or promote products  
15  //   derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  // 
28  package org.objectledge.modules.actions.scheduler;
29  
30  import java.text.ParseException;
31  import java.util.Date;
32  
33  import org.objectledge.context.Context;
34  import org.objectledge.parameters.Parameters;
35  import org.objectledge.parameters.RequestParameters;
36  import org.objectledge.pipeline.ProcessingException;
37  import org.objectledge.scheduler.AbstractJobDescriptor;
38  import org.objectledge.scheduler.AbstractScheduler;
39  import org.objectledge.scheduler.Schedule;
40  import org.objectledge.templating.TemplatingContext;
41  import org.objectledge.utils.StackTrace;
42  import org.objectledge.web.mvc.MVCContext;
43  import org.objectledge.web.mvc.builders.PolicyProtectedAction;
44  import org.objectledge.web.mvc.security.PolicySystem;
45  
46  /***
47   * Update job.
48   * 
49   * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a>
50   * @version $Id: UpdateJob.java,v 1.2 2005/07/07 08:29:26 zwierzem Exp $
51   */
52  public class UpdateJob 
53      extends PolicyProtectedAction
54  {
55      private AbstractScheduler scheduler;
56      
57      /***
58       * Action constructor.
59       */
60      public UpdateJob(PolicySystem policySystemArg, AbstractScheduler scheduler)
61      {
62          super(policySystemArg);
63  		this.scheduler = scheduler;
64      }
65  
66      /***
67       * Run the valve.
68       * 
69       * @param context the context.
70       * @throws ProcessingException if action processing fails.
71       */
72      public void process(Context context) throws ProcessingException
73      {
74          TemplatingContext templatingContext = TemplatingContext.getTemplatingContext(context);
75  		Parameters parameters = RequestParameters.getRequestParameters(context);
76  		String name = parameters.get("name", "");
77  	    String scheduleType = parameters.get("scheduleType","");
78          String scheduleConfig = parameters.get("scheduleConfig","");
79          String jobClassName = parameters.get("jobClassName","");
80          if(name.equals(""))
81          {
82  			templatingContext.put("result", "scheduler.invalid_name");
83  			return;
84          }
85          if(scheduleType.equals(""))
86          {
87  			templatingContext.put("result", "scheduler.schedule_type_empty");
88  			return;
89  		}
90  		if(scheduleConfig.equals(""))
91          {
92  			templatingContext.put("result", "scheduler.schedule_config_empty");
93  			return;
94          }
95          if(jobClassName.equals(""))
96          {
97  			templatingContext.put("result", "scheduler.job_class_empty");
98  			return;
99          }
100         int runCountLimit = parameters.getInt("runCountLimit",-1);
101         String runTimeLimitStartStr = parameters.get("runTimeLimitStart",null);
102         String runTimeLimitEndStr = parameters.get("runTimeLimitEnd",null);
103         boolean reentrant = parameters.getBoolean("reentrant",false);
104         boolean enabled = parameters.getBoolean("enabled",false);
105 
106         Date runTimeLimitStart = null;
107         Date runTimeLimitEnd = null;
108         try
109         {
110             if(runTimeLimitStartStr != null && !runTimeLimitStartStr.equals(""))
111             {
112                 runTimeLimitStart = scheduler.getDateFormat().parse(runTimeLimitStartStr);
113             }
114         }
115         catch(ParseException e)
116         {
117 			templatingContext.put("trace", new StackTrace(e));
118             templatingContext.put("result", "scheduler.invalid_run_time_limit_start");
119         }
120         try
121         {
122             if(runTimeLimitEndStr != null && !runTimeLimitEndStr.equals(""))
123             {
124                 runTimeLimitEnd = scheduler.getDateFormat().parse(runTimeLimitEndStr);
125             }
126         }
127         catch(ParseException e)
128         {
129 			templatingContext.put("trace", new StackTrace(e));
130             templatingContext.put("result", "scheduler.invalid_run_time_limit_end");
131         }
132 		AbstractJobDescriptor job = scheduler.getJobDescriptor(name);
133 		try
134 		{
135 			scheduler.disable(job);
136 			Schedule schedule = scheduler.createSchedule(scheduleType, scheduleConfig);
137             job.setSchedule(schedule);
138             job.setJobClassName(jobClassName);
139             job.setRunCountLimit(runCountLimit);
140             job.setTimeLimit(runTimeLimitStart, runTimeLimitEnd);
141             job.setReentrant(reentrant);
142 			if(enabled)
143 			{
144 				scheduler.enable(job);
145 			}
146 			templatingContext.put("result", "scheduler.updated_successfully");
147 			MVCContext mvcContext = MVCContext.getMVCContext(context);
148 			mvcContext.setView("scheduler.Jobs");
149 		}
150 		catch(Exception e)
151 		{
152 			throw new ProcessingException("failed to delete job");
153 		}
154     }
155 }
156