KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > history > CmsAdminHistoryClear


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/history/CmsAdminHistoryClear.java,v $
3  * Date : $Date: 2006/09/22 15:17:06 $
4  * Version: $Revision: 1.18 $
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.history;
33
34 import org.opencms.i18n.CmsMessages;
35 import org.opencms.jsp.CmsJspActionElement;
36 import org.opencms.main.CmsIllegalArgumentException;
37 import org.opencms.main.OpenCms;
38 import org.opencms.widgets.CmsCalendarWidget;
39 import org.opencms.workplace.CmsReport;
40 import org.opencms.workplace.CmsWorkplaceSettings;
41
42 import java.text.ParseException JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Map JavaDoc;
45
46 import javax.servlet.http.HttpServletRequest JavaDoc;
47 import javax.servlet.http.HttpServletResponse JavaDoc;
48 import javax.servlet.jsp.JspException JavaDoc;
49 import javax.servlet.jsp.PageContext JavaDoc;
50
51 /**
52  * Provides methods for the history clear dialog.<p>
53  *
54  * The following files use this class:
55  * <ul>
56  * <li>/system/workplace/administration/history/clearhistory/index.jsp
57  * </ul>
58  * <p>
59  *
60  * @author Andreas Zahner
61  *
62  * @version $Revision: 1.18 $
63  *
64  * @since 6.0.0
65  */

66 public class CmsAdminHistoryClear extends CmsReport {
67
68     /** Value for the action: clear history. */
69     public static final int ACTION_SAVE_EDIT = 300;
70
71     /** Request parameter value for the action: clear history. */
72     public static final String JavaDoc DIALOG_SAVE_EDIT = "saveedit";
73
74     /** The dialog type. */
75     public static final String JavaDoc DIALOG_TYPE = "historyclear";
76
77     /**
78      * Public constructor with JSP action element.<p>
79      *
80      * @param jsp an initialized JSP action element
81      */

82     public CmsAdminHistoryClear(CmsJspActionElement jsp) {
83
84         super(jsp);
85     }
86
87     /**
88      * Public constructor with JSP variables.<p>
89      *
90      * @param context the JSP page context
91      * @param req the JSP request
92      * @param res the JSP response
93      */

94     public CmsAdminHistoryClear(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
95
96         this(new CmsJspActionElement(context, req, res));
97     }
98
99     /**
100      * Performs the change of the history settings, this method is called by the JSP.<p>
101      *
102      * @throws JspException if something goes wrong
103      */

104     public void actionEdit() throws JspException JavaDoc {
105
106         // save initialized instance of this class in request attribute for included sub-elements
107
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
108
109         switch (getAction()) {
110             case ACTION_REPORT_UPDATE:
111                 setParamAction(REPORT_UPDATE);
112                 getJsp().include(FILE_REPORT_OUTPUT);
113                 break;
114             case ACTION_REPORT_END:
115                 actionCloseDialog();
116                 break;
117             case ACTION_REPORT_BEGIN:
118             case ACTION_SAVE_EDIT:
119             default:
120                 Map JavaDoc params = new HashMap JavaDoc();
121                 try {
122                     params = getBackupParams();
123                     CmsAdminHistoryClearThread thread = new CmsAdminHistoryClearThread(getCms(), params);
124                     setParamAction(REPORT_BEGIN);
125                     setParamThread(thread.getUUID().toString());
126                     getJsp().include(FILE_REPORT_OUTPUT);
127                 } catch (Throwable JavaDoc e) {
128                     // error setting history values, show error dialog
129
includeErrorpage(this, e);
130                 }
131                 break;
132         }
133     }
134
135     /**
136      * Builds the HTML for the history settings input form.<p>
137      *
138      * @return the HTML code for the history settings input form
139      */

140     public String JavaDoc buildClearForm() {
141
142         CmsMessages messages = Messages.get().getBundle(getLocale());
143         StringBuffer JavaDoc retValue = new StringBuffer JavaDoc(512);
144         int maxVersions = OpenCms.getSystemInfo().getVersionHistoryMaxCount();
145
146         // append settings info or disabled message if history is disabled
147
retValue.append(dialogBlockStart(messages.key(Messages.GUI_LABEL_ADMNIN_HISTORY_SETTINGS_0)));
148         if (isHistoryEnabled()) {
149             retValue.append(messages.key(Messages.GUI_INPUT_HISTORY_CLEAR_VERSIONINFO_1, new Object JavaDoc[] {new Integer JavaDoc(
150                 maxVersions)}));
151             retValue.append("<br>");
152             retValue.append(messages.key(Messages.GUI_INPUT_HISTORY_CLEAR_SELECTINFO_0));
153         } else {
154             retValue.append(messages.key(Messages.GUI_INPUT_HISTORY_CLEAR_DISABLEDINFO_0));
155         }
156         retValue.append(dialogBlockEnd());
157         retValue.append(dialogSpacer());
158
159         // append input fields if history is enabled
160
if (isHistoryEnabled()) {
161             retValue.append("<table border=\"0\">\n");
162             retValue.append("<tr>\n");
163             retValue.append("<td>" + messages.key(Messages.GUI_INPUT_HISTORY_CLEAR_NUMBER_0) + "</td>\n");
164             retValue.append("<td colspan=\"2\">" + buildSelectVersions(null) + "</td>\n");
165             retValue.append("</tr>\n");
166             retValue.append("<tr><td colspan=\"3\">&nbsp;</td></tr>\n");
167             retValue.append("<tr>\n");
168             retValue.append("<td>" + messages.key(Messages.GUI_INPUT_HISTORY_CLEAR_DATE_0) + "</td>\n");
169             retValue.append("<td>");
170             retValue.append("<input type=\"text\" name=\"date\" id=\"date\">");
171             retValue.append("</td>\n<td>");
172             retValue.append("<img SRC=\"" + getSkinUri() + "buttons/calendar.png\" id=\"triggercalendar\" ");
173             retValue.append("alt=\""
174                 + messages.key(Messages.GUI_CALENDAR_INPUT_CHOOSEDATE_0)
175                 + "\" title=\""
176                 + messages.key(Messages.GUI_CALENDAR_INPUT_CHOOSEDATE_0)
177                 + "\">");
178             retValue.append("</td>\n");
179             retValue.append("</tr>\n");
180             retValue.append("</table>\n");
181         }
182
183         return retValue.toString();
184     }
185
186     /**
187      * Build the HTML code for a select box of versions to keep.<p>
188      *
189      * @param attributes optional additional attributes of the select tag
190      * @return the HTML code for a select box of versions
191      */

192     public String JavaDoc buildSelectVersions(String JavaDoc attributes) {
193
194         return buildSelectNumbers("versions", attributes, 0, OpenCms.getSystemInfo().getVersionHistoryMaxCount());
195     }
196
197     /**
198      * Creates the HTML JavaScript and stylesheet includes required by the calendar for the head of the page.<p>
199      *
200      * @return the necessary HTML code for the js and stylesheet includes
201      *
202      * @deprecated use {@link CmsCalendarWidget#calendarIncludes(java.util.Locale)}, this is just here so that old JSP still work
203      */

204     public String JavaDoc calendarIncludes() {
205
206         return CmsCalendarWidget.calendarIncludes(getLocale());
207     }
208
209     /**
210      * Generates the HTML to initialize the JavaScript calendar element on the end of a page.<p>
211      *
212      * @param inputFieldId the ID of the input field where the date is pasted to
213      * @param triggerButtonId the ID of the button which triggers the calendar
214      * @param align initial position of the calendar popup element
215      * @param singleClick if true, a single click selects a date and closes the calendar, otherwise calendar is closed by doubleclick
216      * @param weekNumbers show the week numbers in the calendar or not
217      * @param mondayFirst show monday as first day of week
218      * @param dateStatusFunc name of the function which determines if/how a date should be disabled
219      * @return the HTML code to initialize a calendar poup element
220      *
221      * @deprecated use {@link CmsCalendarWidget#calendarInit(org.opencms.i18n.CmsMessages, String, String, String, boolean, boolean, boolean, String, boolean)}, this is just here so that old JSP still work
222      */

223     public String JavaDoc calendarInit(
224         String JavaDoc inputFieldId,
225         String JavaDoc triggerButtonId,
226         String JavaDoc align,
227         boolean singleClick,
228         boolean weekNumbers,
229         boolean mondayFirst,
230         String JavaDoc dateStatusFunc) {
231
232         return CmsCalendarWidget.calendarInit(
233             getMessages(),
234             inputFieldId,
235             triggerButtonId,
236             align,
237             singleClick,
238             weekNumbers,
239             mondayFirst,
240             dateStatusFunc,
241             false);
242     }
243
244     /**
245      * Returns true if the version history is enabled, otherwise false.<p>
246      *
247      * @return true if the version history is enabled, otherwise false
248      */

249     public boolean isHistoryEnabled() {
250
251         return OpenCms.getSystemInfo().isVersionHistoryEnabled();
252     }
253
254     /**
255      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
256      */

257     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
258
259         // fill the parameter values in the get/set methods
260
fillParamValues(request);
261
262         // set the dialog type
263
setParamDialogtype(DIALOG_TYPE);
264         // set the action for the JSP switch
265
if (DIALOG_SAVE_EDIT.equals(getParamAction())) {
266             if (isHistoryEnabled()) {
267                 // history is enabled, start clearing
268
setAction(ACTION_SAVE_EDIT);
269             } else {
270                 // history is disabled, return to admin view
271
setAction(ACTION_CANCEL);
272             }
273         } else if (REPORT_UPDATE.equals(getParamAction())) {
274             setAction(ACTION_REPORT_UPDATE);
275         } else if (REPORT_BEGIN.equals(getParamAction())) {
276             setAction(ACTION_REPORT_BEGIN);
277         } else if (REPORT_END.equals(getParamAction())) {
278             setAction(ACTION_REPORT_END);
279         } else if (DIALOG_CANCEL.equals(getParamAction())) {
280             setAction(ACTION_CANCEL);
281         } else {
282             // set the default action
283
setAction(ACTION_DEFAULT);
284             setParamTitle(Messages.get().getBundle(getLocale()).key(Messages.GUI_LABEL_ADMIN_HISTORY_CLEAR_0));
285         }
286     }
287
288     /**
289      * Creates the HTML code for a select box with integer values.<p>
290      *
291      * @param fieldName the name of the select box
292      * @param attributes the optional tag attributes
293      * @param startValue the start integer value for the options
294      * @param endValue the end integer value for the options
295      * @return the HTML code for the select box
296      */

297     private String JavaDoc buildSelectNumbers(String JavaDoc fieldName, String JavaDoc attributes, int startValue, int endValue) {
298
299         StringBuffer JavaDoc retValue = new StringBuffer JavaDoc(512);
300
301         retValue.append("<select name=\"" + fieldName + "\"");
302         if (attributes != null) {
303             retValue.append(" " + attributes);
304         }
305         retValue.append(">\n");
306         retValue.append("\t<option value=\"\" selected=\"selected\">"
307             + Messages.get().getBundle(getLocale()).key(Messages.GUI_INPUT_HISTORY_CLEAR_SELECT_0)
308             + "</option>\n");
309         for (int i = startValue; i <= endValue; i++) {
310             retValue.append("\t<option value=\"" + i + "\">" + i + "</option>\n");
311         }
312         retValue.append("</select>\n");
313
314         return retValue.toString();
315     }
316
317     /**
318      * Returns the necessary parameters to perform the backup deletion.<p>
319      *
320      * @return a map with necessary parameters for the deleteBackups method
321      * @throws CmsIllegalArgumentException if something goes wrong
322      */

323     private Map JavaDoc getBackupParams() throws CmsIllegalArgumentException {
324
325         HttpServletRequest JavaDoc request = getJsp().getRequest();
326         Map JavaDoc parameterMap = new HashMap JavaDoc();
327
328         // get the delete information from the request parameters
329
String JavaDoc paramVersions = request.getParameter("versions");
330         String JavaDoc paramDate = request.getParameter("date");
331
332         // check the submitted values
333
int versions = 0;
334         long timeStamp = 0;
335         boolean useVersions = false;
336         try {
337             versions = Integer.parseInt(paramVersions);
338             useVersions = true;
339         } catch (NumberFormatException JavaDoc e) {
340             // no int value submitted, check date fields
341
try {
342                 timeStamp = CmsCalendarWidget.getCalendarDate(getMessages(), paramDate, false);
343             } catch (ParseException JavaDoc ex) {
344                 // no date values submitted, throw exception
345

346                 throw new CmsIllegalArgumentException(
347                     Messages.get().container(Messages.ERR_INVALID_DATE_1, paramDate),
348                     ex);
349             }
350         }
351
352         // set the timeStamp one day to the future to delete versions
353
if (useVersions) {
354             timeStamp = System.currentTimeMillis() + 86400000;
355         }
356         if (DEBUG) {
357             System.err.println("Versions: " + versions + "\nDate: " + timeStamp);
358         }
359         // add the correct values to the parameter map
360
parameterMap.put("timeStamp", String.valueOf(timeStamp));
361         parameterMap.put("versions", String.valueOf(versions));
362
363         if (DEBUG) {
364             System.err.println("Done");
365         }
366         return parameterMap;
367     }
368
369 }
370
Popular Tags