KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > commons > CmsTouch


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsTouch.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.commons;
33
34 import org.opencms.file.CmsResource;
35 import org.opencms.file.CmsResourceFilter;
36 import org.opencms.jsp.CmsJspActionElement;
37 import org.opencms.main.CmsException;
38 import org.opencms.security.CmsPermissionSet;
39 import org.opencms.util.CmsStringUtil;
40 import org.opencms.widgets.CmsCalendarWidget;
41 import org.opencms.workplace.CmsMultiDialog;
42 import org.opencms.workplace.CmsWorkplaceSettings;
43
44 import java.text.ParseException JavaDoc;
45 import java.util.Iterator JavaDoc;
46
47 import javax.servlet.http.HttpServletRequest JavaDoc;
48 import javax.servlet.http.HttpServletResponse JavaDoc;
49 import javax.servlet.jsp.JspException JavaDoc;
50 import javax.servlet.jsp.PageContext JavaDoc;
51
52 /**
53  * Provides methods for the touch resource(s) dialog.<p>
54  *
55  * The following files use this class:
56  * <ul>
57  * <li>/commons/touch.jsp
58  * </ul>
59  * <p>
60  *
61  * @author Andreas Zahner
62  *
63  * @version $Revision: 1.18 $
64  *
65  * @since 6.0.0
66  */

67 public class CmsTouch extends CmsMultiDialog {
68
69     /** Value for the action: touch. */
70     public static final int ACTION_TOUCH = 100;
71
72     /** The dialog type. */
73     public static final String JavaDoc DIALOG_TYPE = "touch";
74
75     /** Request parameter name for timestamp. */
76     public static final String JavaDoc PARAM_NEWTIMESTAMP = "newtimestamp";
77     
78     /** Request parameter name for the recursive flag. */
79     public static final String JavaDoc PARAM_RECURSIVE = "recursive";
80     
81     private String JavaDoc m_paramNewtimestamp;
82     private String JavaDoc m_paramRecursive;
83
84     /** Default value for date last modified, the release and expire date. */
85     public static final String JavaDoc DEFAULT_DATE_STRING = "-";
86
87     /**
88      * Public constructor.<p>
89      *
90      * @param jsp an initialized JSP action element
91      */

92     public CmsTouch(CmsJspActionElement jsp) {
93
94         super(jsp);
95     }
96
97     /**
98      * Public constructor with JSP variables.<p>
99      *
100      * @param context the JSP page context
101      * @param req the JSP request
102      * @param res the JSP response
103      */

104     public CmsTouch(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
105
106         this(new CmsJspActionElement(context, req, res));
107     }
108
109     /**
110      * Performs the resource touching, will be called by the JSP page.<p>
111      *
112      * @throws JspException if problems including sub-elements occur
113      */

114     public void actionTouch() throws JspException JavaDoc {
115
116         // save initialized instance of this class in request attribute for included sub-elements
117
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
118         try {
119             if (performDialogOperation()) {
120                 // if no exception is caused and "true" is returned the touch operation was successful
121
actionCloseDialog();
122             } else {
123                 // "false" returned, display "please wait" screen
124
getJsp().include(FILE_DIALOG_SCREEN_WAIT);
125             }
126         } catch (Throwable JavaDoc e) {
127             includeErrorpage(this, e);
128         }
129     }
130
131     /**
132      * Creates the "recursive" checkbox for touching subresources of folders.<p>
133      *
134      * @return the String with the checkbox input field or an empty String for folders.
135      */

136     public String JavaDoc buildCheckRecursive() {
137
138         StringBuffer JavaDoc retValue = new StringBuffer JavaDoc(256);
139
140         // show the checkbox only for folders
141
if (isOperationOnFolder()) {
142             retValue.append("<tr>\n\t<td colspan=\"3\" style=\"white-space: nowrap;\" unselectable=\"on\">");
143             retValue.append("<input type=\"checkbox\" name=\"");
144             retValue.append(PARAM_RECURSIVE);
145             retValue.append("\" value=\"true\">&nbsp;");
146             retValue.append(key(Messages.GUI_TOUCH_MODIFY_SUBRESOURCES_0));
147             retValue.append("</td>\n</tr>\n");
148         }
149         return retValue.toString();
150     }
151
152     /**
153      * Creates the HTML JavaScript and stylesheet includes required by the calendar for the head of the page.<p>
154      *
155      * @return the necessary HTML code for the js and stylesheet includes
156      *
157      * @deprecated use {@link CmsCalendarWidget#calendarIncludes(java.util.Locale)}, this is just here so that old JSP still work
158      */

159     public String JavaDoc calendarIncludes() {
160
161         return CmsCalendarWidget.calendarIncludes(getLocale());
162     }
163
164     /**
165      * Generates the HTML to initialize the JavaScript calendar element on the end of a page.<p>
166      *
167      * @param inputFieldId the ID of the input field where the date is pasted to
168      * @param triggerButtonId the ID of the button which triggers the calendar
169      * @param align initial position of the calendar popup element
170      * @param singleClick if true, a single click selects a date and closes the calendar, otherwise calendar is closed by doubleclick
171      * @param weekNumbers show the week numbers in the calendar or not
172      * @param mondayFirst show monday as first day of week
173      * @param dateStatusFunc name of the function which determines if/how a date should be disabled
174      * @param showTime true if the time selector should be shown, otherwise false
175      * @return the HTML code to initialize a calendar poup element
176      *
177      * @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
178      */

179     public String JavaDoc calendarInit(
180         String JavaDoc inputFieldId,
181         String JavaDoc triggerButtonId,
182         String JavaDoc align,
183         boolean singleClick,
184         boolean weekNumbers,
185         boolean mondayFirst,
186         String JavaDoc dateStatusFunc,
187         boolean showTime) {
188
189         return CmsCalendarWidget.calendarInit(
190             getMessages(),
191             inputFieldId,
192             triggerButtonId,
193             align,
194             singleClick,
195             weekNumbers,
196             mondayFirst,
197             dateStatusFunc,
198             showTime);
199     }
200
201     /**
202      * Returns the current date and time as String formatted in localized pattern.<p>
203      *
204      * @return the current date and time as String formatted in localized pattern
205      */

206     public String JavaDoc getCurrentDateTime() {
207
208         // get the current date & time
209
return CmsCalendarWidget.getCalendarLocalizedTime(getLocale(), getMessages(), System.currentTimeMillis());
210     }
211
212     /**
213      * Returns the value of the new timestamp parameter,
214      * or null if this parameter was not provided.<p>
215      *
216      * The timestamp parameter stores the new timestamp as String.<p>
217      *
218      * @return the value of the new timestamp parameter
219      */

220     public String JavaDoc getParamNewtimestamp() {
221
222         return m_paramNewtimestamp;
223     }
224
225     /**
226      * Returns the value of the recursive parameter,
227      * or null if this parameter was not provided.<p>
228      *
229      * The recursive parameter on folders decides if all subresources
230      * of the folder should be touched, too.<p>
231      *
232      * @return the value of the recursive parameter
233      */

234     public String JavaDoc getParamRecursive() {
235
236         return m_paramRecursive;
237     }
238
239     /**
240      * Sets the value of the new timestamp parameter.<p>
241      *
242      * @param value the value to set
243      */

244     public void setParamNewtimestamp(String JavaDoc value) {
245
246         m_paramNewtimestamp = value;
247     }
248
249     /**
250      * Sets the value of the recursive parameter.<p>
251      *
252      * @param value the value to set
253      */

254     public void setParamRecursive(String JavaDoc value) {
255
256         m_paramRecursive = value;
257     }
258
259     /**
260      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
261      */

262     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
263
264         // fill the parameter values in the get/set methods
265
fillParamValues(request);
266         
267         // check the required permissions to touch the resource
268
if (! checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
269             // no write permissions for the resource, set cancel action to close dialog
270
setParamAction(DIALOG_CANCEL);
271         }
272         
273         // set the dialog type
274
setParamDialogtype(DIALOG_TYPE);
275
276         // set the action for the JSP switch
277
if (DIALOG_TYPE.equals(getParamAction())) {
278             setAction(ACTION_TOUCH);
279         } else if (DIALOG_WAIT.equals(getParamAction())) {
280             setAction(ACTION_WAIT);
281         } else if (DIALOG_CANCEL.equals(getParamAction())) {
282             setAction(ACTION_CANCEL);
283         } else {
284             setAction(ACTION_DEFAULT);
285             // build title for touch dialog
286
setDialogTitle(Messages.GUI_TOUCH_RESOURCE_1, Messages.GUI_TOUCH_MULTI_2);
287         }
288     }
289
290     /**
291      * Performs the resource touching.<p>
292      *
293      * @return true, if the resource was touched, otherwise false
294      * @throws CmsException if touching is not successful
295      */

296     protected boolean performDialogOperation() throws CmsException {
297
298         // on folder touch or multi resource operation display "please wait" screen, not for simple file copy
299
if (!DIALOG_WAIT.equals(getParamAction())) {
300             // check if the "please wait" screen has to be shown
301
if (isMultiOperation()) {
302                 // show please wait for every multi resource operation
303
return false;
304             } else {
305                 // check if the single resource is a folder
306
CmsResource sourceRes = getCms().readResource(getParamResource(), CmsResourceFilter.ALL);
307                 if (sourceRes.isFolder()) {
308                     return false;
309                 }
310             }
311         }
312
313         // get the new timestamp for the resource(s) from request parameter
314
long timeStamp = 0;
315         boolean correctDate = false;
316         try {
317             if (CmsStringUtil.isNotEmpty(getParamNewtimestamp())) {
318                 timeStamp = CmsCalendarWidget.getCalendarDate(getMessages(), getParamNewtimestamp(), true);
319                 correctDate = true;
320             }
321         } catch (ParseException JavaDoc e) {
322             throw new CmsException(Messages.get().container(Messages.ERR_PARSE_TIMESTAMP_1, getParamNewtimestamp()), e);
323         }
324
325         // get the flag if the touch is recursive from request parameter
326
boolean touchRecursive = Boolean.valueOf(getParamRecursive()).booleanValue();
327
328         // now touch the resource(s)
329
Iterator JavaDoc i = getResourceList().iterator();
330         while (i.hasNext()) {
331             String JavaDoc resName = (String JavaDoc)i.next();
332             try {
333                 touchSingleResource(resName, timeStamp, touchRecursive, correctDate);
334             } catch (CmsException e) {
335                 // collect exceptions to create a detailed output
336
addMultiOperationException(e);
337             }
338         }
339         checkMultiOperationException(Messages.get(), Messages.ERR_TOUCH_MULTI_0);
340         
341         return true;
342     }
343     
344     /**
345      * Performs a touch operation for a single resource.<p>
346      *
347      * @param resourceName the resource name of the resource to touch
348      * @param timeStamp the new time stamp
349      * @param recursive the flag if the touch operation is recursive
350      * @param correctDate the flag if the new time stamp is a correct date
351      * @throws CmsException if touching the resource fails
352      */

353     protected void touchSingleResource(String JavaDoc resourceName, long timeStamp, boolean recursive, boolean correctDate) throws CmsException {
354         
355         // lock resource if autolock is enabled
356
checkLock(resourceName);
357         CmsResource sourceRes = getCms().readResource(resourceName, CmsResourceFilter.ALL);
358         if (! correctDate) {
359             // no date value entered, use current resource modification date
360
timeStamp = sourceRes.getDateLastModified();
361         }
362         getCms().setDateLastModified(resourceName, timeStamp, recursive);
363     }
364 }
365
Popular Tags