KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > scheduler > CmsEditScheduledJobInfoDialog


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/scheduler/CmsEditScheduledJobInfoDialog.java,v $
3  * Date : $Date: 2006/03/27 14:52:59 $
4  * Version: $Revision: 1.26 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.tools.scheduler;
33
34 import org.opencms.configuration.CmsSystemConfiguration;
35 import org.opencms.jsp.CmsJspActionElement;
36 import org.opencms.main.CmsContextInfo;
37 import org.opencms.main.OpenCms;
38 import org.opencms.monitor.CmsMemoryMonitor;
39 import org.opencms.notification.CmsContentNotificationJob;
40 import org.opencms.scheduler.CmsScheduledJobInfo;
41 import org.opencms.scheduler.jobs.CmsCreateImageSizeJob;
42 import org.opencms.scheduler.jobs.CmsImageCacheCleanupJob;
43 import org.opencms.scheduler.jobs.CmsPublishJob;
44 import org.opencms.scheduler.jobs.CmsStaticExportJob;
45 import org.opencms.search.CmsSearchManager;
46 import org.opencms.util.CmsStringUtil;
47 import org.opencms.validation.CmsPointerLinkValidator;
48 import org.opencms.widgets.CmsCheckboxWidget;
49 import org.opencms.widgets.CmsComboWidget;
50 import org.opencms.widgets.CmsInputWidget;
51 import org.opencms.widgets.CmsSelectWidgetOption;
52 import org.opencms.widgets.CmsUserWidget;
53 import org.opencms.widgets.CmsVfsFileWidget;
54 import org.opencms.workplace.CmsDialog;
55 import org.opencms.workplace.CmsWidgetDialog;
56 import org.opencms.workplace.CmsWidgetDialogParameter;
57 import org.opencms.workplace.CmsWorkplaceSettings;
58
59 import java.util.ArrayList JavaDoc;
60 import java.util.Iterator JavaDoc;
61 import java.util.List JavaDoc;
62 import java.util.Map JavaDoc;
63
64 import javax.servlet.http.HttpServletRequest JavaDoc;
65 import javax.servlet.http.HttpServletResponse JavaDoc;
66 import javax.servlet.jsp.PageContext JavaDoc;
67
68 /**
69  * Dialog to edit new and existing scheduled jobs in the administration view.<p>
70  *
71  * @author Andreas Zahner
72  *
73  * @version $Revision: 1.26 $
74  *
75  * @since 6.0.0
76  */

77 public class CmsEditScheduledJobInfoDialog extends CmsWidgetDialog {
78
79     /** The action to copy a job to edit. */
80     public static final String JavaDoc DIALOG_COPYJOB = "copyjob";
81
82     /** Defines which pages are valid for this dialog. */
83     public static final String JavaDoc[] PAGES = {"page1", "page2"};
84
85     /** Request parameter name for the job id. */
86     public static final String JavaDoc PARAM_JOBID = "jobid";
87
88     /** The job info object that is edited on this dialog. */
89     private CmsScheduledJobInfo m_jobInfo;
90
91     /** Stores the value of the request parameter for the job id. */
92     private String JavaDoc m_paramJobid;
93
94     /**
95      * Public constructor with JSP action element.<p>
96      *
97      * @param jsp an initialized JSP action element
98      */

99     public CmsEditScheduledJobInfoDialog(CmsJspActionElement jsp) {
100
101         super(jsp);
102     }
103
104     /**
105      * Public constructor with JSP variables.<p>
106      *
107      * @param context the JSP page context
108      * @param req the JSP request
109      * @param res the JSP response
110      */

111     public CmsEditScheduledJobInfoDialog(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
112
113         this(new CmsJspActionElement(context, req, res));
114     }
115
116     /**
117      * Commits the edited scheduled job to the scheduler.<p>
118      */

119     public void actionCommit() {
120
121         List JavaDoc errors = new ArrayList JavaDoc();
122
123         try {
124             // schedule the edited job
125
OpenCms.getScheduleManager().scheduleJob(getCms(), m_jobInfo);
126             // update the XML configuration
127
OpenCms.writeConfiguration(CmsSystemConfiguration.class);
128             // refresh the list
129
Map JavaDoc objects = (Map JavaDoc)getSettings().getListObject();
130             if (objects != null) {
131                 objects.remove(CmsSchedulerList.class.getName());
132             }
133         } catch (Throwable JavaDoc t) {
134             errors.add(t);
135         }
136         // set the list of errors to display when saving failed
137
setCommitErrors(errors);
138     }
139
140     /**
141      * Returns the job id parameter value.<p>
142      *
143      * @return the job id parameter value
144      */

145     public String JavaDoc getParamJobid() {
146
147         return m_paramJobid;
148     }
149
150     /**
151      * Sets the job id parameter value.<p>
152      *
153      * @param jobid the job id parameter value
154      */

155     public void setParamJobid(String JavaDoc jobid) {
156
157         m_paramJobid = jobid;
158     }
159
160     /**
161      * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
162      *
163      * This overwrites the method from the super class to create a layout variation for the widgets.<p>
164      *
165      * @param dialog the dialog (page) to get the HTML for
166      * @return the dialog HTML for all defined widgets of the named dialog (page)
167      */

168     protected String JavaDoc createDialogHtml(String JavaDoc dialog) {
169
170         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
171
172         // create widget table
173
result.append(createWidgetTableStart());
174
175         // show error header once if there were validation errors
176
result.append(createWidgetErrorHeader());
177
178         if (dialog.equals(PAGES[0])) {
179             // create the widgets for the first dialog page
180
result.append(createWidgetBlockStart(key(Messages.GUI_EDITOR_LABEL_JOBSETTINGS_BLOCK_0)));
181             result.append(createDialogRowsHtml(0, 4));
182             result.append(createWidgetBlockEnd());
183             result.append(createWidgetBlockStart(key(Messages.GUI_EDITOR_LABEL_CONTEXTINFO_BLOCK_0)));
184             result.append(createDialogRowsHtml(5, 11));
185             result.append(createWidgetBlockEnd());
186         } else if (dialog.equals(PAGES[1])) {
187             // create the widget for the second dialog page
188
result.append(createWidgetBlockStart(key(Messages.GUI_EDITOR_LABEL_PARAMETERS_BLOCK_0)));
189             result.append(createDialogRowsHtml(12, 12));
190             result.append(createWidgetBlockEnd());
191         }
192
193         // close widget table
194
result.append(createWidgetTableEnd());
195
196         return result.toString();
197     }
198
199     /**
200      * Creates the list of widgets for this dialog.<p>
201      */

202     protected void defineWidgets() {
203
204         // initialize the scheduled job object to use for the dialog
205
initScheduledJobObject();
206
207         // required to read the default values for the optional context parameters for the widgets
208
CmsContextInfo dC = new CmsContextInfo();
209
210         // widgets to display on the first dialog page
211
addWidget(new CmsWidgetDialogParameter(m_jobInfo, "jobName", PAGES[0], new CmsInputWidget()));
212         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "className", PAGES[0], new CmsComboWidget(getComboClasses())));
213         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "cronExpression", PAGES[0], new CmsComboWidget(
214             getComboCronExpressions())));
215         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "reuseInstance", PAGES[0], new CmsCheckboxWidget()));
216         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "active", PAGES[0], new CmsCheckboxWidget()));
217         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "contextInfo.userName", PAGES[0], new CmsUserWidget(
218             null,
219             null,
220             null)));
221         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "contextInfo.projectName", PAGES[0], new CmsInputWidget()));
222         addWidget(new CmsWidgetDialogParameter(
223             m_jobInfo,
224             "contextInfo.siteRoot",
225             dC.getSiteRoot(),
226             PAGES[0],
227             new CmsVfsFileWidget(),
228             0,
229             1));
230         addWidget(new CmsWidgetDialogParameter(
231             m_jobInfo,
232             "contextInfo.requestedUri",
233             dC.getRequestedUri(),
234             PAGES[0],
235             new CmsVfsFileWidget(),
236             0,
237             1));
238         addWidget(new CmsWidgetDialogParameter(
239             m_jobInfo,
240             "contextInfo.localeName",
241             dC.getLocaleName(),
242             PAGES[0],
243             new CmsInputWidget(),
244             0,
245             1));
246         addWidget(new CmsWidgetDialogParameter(
247             m_jobInfo,
248             "contextInfo.encoding",
249             dC.getEncoding(),
250             PAGES[0],
251             new CmsInputWidget(),
252             0,
253             1));
254         addWidget(new CmsWidgetDialogParameter(
255             m_jobInfo,
256             "contextInfo.remoteAddr",
257             dC.getRemoteAddr(),
258             PAGES[0],
259             new CmsInputWidget(),
260             0,
261             1));
262
263         // widget to display on the second dialog page
264
addWidget(new CmsWidgetDialogParameter(m_jobInfo, "parameters", PAGES[1], new CmsInputWidget()));
265     }
266
267     /**
268      * Returns the example cron class names to show in the combo box.<p>
269      *
270      * The result list elements are of type <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code>.<p>
271      *
272      * @return the example cron class names to show in the combo box
273      */

274     protected List JavaDoc getComboClasses() {
275
276         List JavaDoc result = new ArrayList JavaDoc();
277         result.add(new CmsSelectWidgetOption(
278             CmsPublishJob.class.getName(),
279             false,
280             null,
281             key(Messages.GUI_EDITOR_CRONCLASS_PUBLISH_0)));
282         result.add(new CmsSelectWidgetOption(
283             CmsStaticExportJob.class.getName(),
284             false,
285             null,
286             key(Messages.GUI_EDITOR_CRONCLASS_STATICEXPORT_0)));
287         result.add(new CmsSelectWidgetOption(
288             CmsPointerLinkValidator.class.getName(),
289             false,
290             null,
291             key(Messages.GUI_EDITOR_CRONCLASS_POINTERVALIDATION_0)));
292         result.add(new CmsSelectWidgetOption(
293             CmsMemoryMonitor.class.getName(),
294             false,
295             null,
296             key(Messages.GUI_EDITOR_CRONCLASS_MEMORYMONITOR_0)));
297         result.add(new CmsSelectWidgetOption(
298             CmsSearchManager.class.getName(),
299             false,
300             null,
301             key(Messages.GUI_EDITOR_CRONCLASS_SEARCHINDEX_0)));
302         result.add(new CmsSelectWidgetOption(
303             CmsContentNotificationJob.class.getName(),
304             false,
305             null,
306             key(Messages.GUI_EDITOR_CRONCLASS_CONTENTNOTIFICATION_0)));
307         result.add(new CmsSelectWidgetOption(
308             CmsCreateImageSizeJob.class.getName(),
309             false,
310             null,
311             key(Messages.GUI_EDITOR_CRONCLASS_IMAGESIZE_0)));
312         result.add(new CmsSelectWidgetOption(
313             CmsImageCacheCleanupJob.class.getName(),
314             false,
315             null,
316             key(Messages.GUI_EDITOR_CRONCLASS_IMAGE_CACHECLEAN_0)));
317         return result;
318     }
319
320     /**
321      * Returns the example cron expressions to show in the combo box.<p>
322      *
323      * The result list elements are of type <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code>.<p>
324      *
325      * @return the example cron expressions to show in the combo box
326      */

327     protected List JavaDoc getComboCronExpressions() {
328
329         List JavaDoc result = new ArrayList JavaDoc();
330
331         // 0 0 3 * * ? (daily at 3 am)
332
result.add(new CmsSelectWidgetOption("0 0 3 * * ?", false, null, key(Messages.GUI_EDITOR_CRONJOB_EXAMPLE1_0)));
333         // 0 0/30 * * * ? (daily every thirty minutes)
334
result.add(new CmsSelectWidgetOption("0 0/30 * * * ?", false, null, key(Messages.GUI_EDITOR_CRONJOB_EXAMPLE2_0)));
335         // 0 30 8 ? * 4 (every Wednesday at 8:30 am)
336
result.add(new CmsSelectWidgetOption("0 30 8 ? * 4", false, null, key(Messages.GUI_EDITOR_CRONJOB_EXAMPLE3_0)));
337         // 0 15 18 15 * ? (on the 20th day of the month at 6:15 pm)
338
result.add(new CmsSelectWidgetOption("0 15 18 20 * ?", false, null, key(Messages.GUI_EDITOR_CRONJOB_EXAMPLE4_0)));
339         // 0 45 15 ? * 1 2005-2006 (every Sunday from the year 2005 to 2006 at 3:45 pm)
340
result.add(new CmsSelectWidgetOption(
341             "0 45 15 ? * 1 2005-2006",
342             false,
343             null,
344             key(Messages.GUI_EDITOR_CRONJOB_EXAMPLE5_0)));
345         return result;
346     }
347
348     /**
349      * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
350      */

351     protected String JavaDoc[] getPageArray() {
352
353         return PAGES;
354     }
355
356     /**
357      * @see org.opencms.workplace.CmsWorkplace#initMessages()
358      */

359     protected void initMessages() {
360
361         // add specific dialog resource bundle
362
addMessages(Messages.get().getBundleName());
363         // add default resource bundles
364
super.initMessages();
365     }
366
367     /**
368      * Initializes the scheduled job object to work with depending on the dialog state and request parameters.<p>
369      *
370      * Three initializations of the scheduled job object on first dialog call are possible:
371      * <ul>
372      * <li>edit an existing scheduled job</li>
373      * <li>create a new scheduled job</li>
374      * <li>copy an existing scheduled job and edit it</li>
375      * </ul>
376      */

377     protected void initScheduledJobObject() {
378
379         Object JavaDoc o;
380
381         boolean setActive = false;
382
383         if (CmsStringUtil.isEmpty(getParamAction())
384             || CmsDialog.DIALOG_INITIAL.equals(getParamAction())
385             || DIALOG_COPYJOB.equals(getParamAction())) {
386             // this is the initial dialog call
387
if (CmsStringUtil.isNotEmpty(getParamJobid())) {
388                 // edit or copy an existing job, get the job object from manager
389
setActive = OpenCms.getScheduleManager().getJob(getParamJobid()).isActive();
390                 o = OpenCms.getScheduleManager().getJob(getParamJobid()).clone();
391             } else {
392                 // create a new job for the new job dialog
393
o = null;
394             }
395         } else {
396             // this is not the initial call, get the job object from session
397
o = getDialogObject();
398         }
399
400         if (!(o instanceof CmsScheduledJobInfo)) {
401             // create a new job info object
402
m_jobInfo = new CmsScheduledJobInfo();
403             m_jobInfo.setContextInfo(new CmsContextInfo());
404         } else {
405             // reuse job info object stored in session
406
m_jobInfo = (CmsScheduledJobInfo)o;
407         }
408
409         if (setActive) {
410             // initial call of edit an existing job, set active state of cloned job
411
m_jobInfo.setActive(true);
412         }
413
414         if (DIALOG_COPYJOB.equals(getParamAction())) {
415             // initial call of copy job action, clear the job id of the cloned job
416
m_jobInfo.clearId();
417         }
418     }
419
420     /**
421      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
422      */

423     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
424
425         // initialize parameters and dialog actions in super implementation
426
super.initWorkplaceRequestValues(settings, request);
427
428         // save the current state of the job (may be changed because of the widget values)
429
setDialogObject(m_jobInfo);
430     }
431
432     /**
433      * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
434      */

435     protected void validateParamaters() throws Exception JavaDoc {
436
437         if (!isNewJob()) {
438             // test if params are available
439
if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getParamJobid())) {
440                 // validate the params
441
Iterator JavaDoc it = OpenCms.getScheduleManager().getJobs().iterator();
442                 while (it.hasNext()) {
443                     CmsScheduledJobInfo job = (CmsScheduledJobInfo)it.next();
444                     if (job.getId().equals(getParamJobid())) {
445                         // params are ok!
446
return;
447                     }
448                 }
449             }
450             // params are no valid
451
throw new Exception JavaDoc();
452         }
453     }
454
455     /**
456      * Checks if the new job dialog has to be displayed.<p>
457      *
458      * @return <code>true</code> if the new job dialog has to be displayed
459      */

460     private boolean isNewJob() {
461
462         return getCurrentToolPath().equals("/scheduler/new");
463     }
464 }
Popular Tags