KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsUndoChanges.java,v $
3  * Date : $Date: 2006/03/27 14:52:18 $
4  * Version: $Revision: 1.16 $
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.main.CmsLog;
39 import org.opencms.security.CmsPermissionSet;
40 import org.opencms.util.CmsUUID;
41 import org.opencms.workplace.CmsMultiDialog;
42 import org.opencms.workplace.CmsWorkplaceSettings;
43
44 import java.util.Iterator 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 import org.apache.commons.logging.Log;
52
53 /**
54  * Provides methods for the undo changes on a resource dialog.<p>
55  *
56  * The following files use this class:
57  * <ul>
58  * <li>/commons/undochanges.jsp
59  * </ul>
60  * <p>
61  *
62  * @author Andreas Zahner
63  *
64  * @version $Revision: 1.16 $
65  *
66  * @since 6.0.0
67  */

68 public class CmsUndoChanges extends CmsMultiDialog {
69
70     /** Value for the action: undo changes. */
71     public static final int ACTION_UNDOCHANGES = 100;
72
73     /** The dialog type. */
74     public static final String JavaDoc DIALOG_TYPE = "undochanges";
75
76     /** Request parameter name for the recursive flag.<p> */
77     public static final String JavaDoc PARAM_RECURSIVE = "recursive";
78
79     /** The log object for this class. */
80     private static final Log LOG = CmsLog.getLog(CmsUndoChanges.class);
81
82     private CmsResource m_currentResource;
83     private String JavaDoc m_paramRecursive;
84
85     /**
86      * Public constructor with JSP action element.<p>
87      *
88      * @param jsp an initialized JSP action element
89      */

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

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

112     public void actionUndoChanges() throws JspException JavaDoc {
113
114         // save initialized instance of this class in request attribute for included sub-elements
115
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
116         try {
117             if (performDialogOperation()) {
118                 // if no exception is caused undo changes operation was successful
119
actionCloseDialog();
120             } else {
121                 // "false" returned, display "please wait" screen
122
getJsp().include(FILE_DIALOG_SCREEN_WAIT);
123             }
124         } catch (Throwable JavaDoc e) {
125             // error during deletion, show error dialog
126
includeErrorpage(this, e);
127         }
128     }
129
130     /**
131      * Returns the HTML for the undo changes options and detailed output for single resource operations.<p>
132      *
133      * @return the HTML for the undo changes options
134      */

135     public String JavaDoc buildDialogOptions() {
136
137         StringBuffer JavaDoc result = new StringBuffer JavaDoc(256);
138
139         if (!isMultiOperation()) {
140             result.append(dialogSpacer());
141             result.append(key(Messages.GUI_UNDO_LASTMODIFIED_INFO_3, new Object JavaDoc[] {
142                 getFileName(),
143                 getLastModifiedDate(),
144                 getLastModifiedUser()}));
145         }
146         result.append(dialogSpacer());
147         result.append(key(Messages.GUI_UNDO_CONFIRMATION_0));
148         if (isOperationOnFolder()) {
149             // show recursive option if folder(s) is/are selected
150
result.append(dialogSpacer());
151             result.append("<input type=\"checkbox\" name=\"");
152             result.append(PARAM_RECURSIVE);
153             result.append("\" value=\"true\">&nbsp;");
154             if (isMultiOperation()) {
155                 result.append(key(Messages.GUI_UNDO_CHANGES_MULTI_SUBRESOURCES_0));
156             } else {
157                 result.append(key(Messages.GUI_UNDO_CHANGES_SUBRESOURCES_0));
158             }
159         }
160
161         return result.toString();
162     }
163
164     /**
165      * Returns the value of the recursive parameter,
166      * or null if this parameter was not provided.<p>
167      *
168      * The recursive parameter on folders decides if all subresources
169      * of the folder should be unchanged, too.<p>
170      *
171      * @return the value of the recursive parameter
172      */

173     public String JavaDoc getParamRecursive() {
174
175         return m_paramRecursive;
176     }
177
178     /**
179      * Sets the value of the recursive parameter.<p>
180      *
181      * @param value the value to set
182      */

183     public void setParamRecursive(String JavaDoc value) {
184
185         m_paramRecursive = value;
186     }
187
188     /**
189      * Returns the current CmsResource.<p>
190      *
191      * @return the CmsResource
192      */

193     protected CmsResource getCurrentResource() {
194
195         return m_currentResource;
196     }
197
198     /**
199      * Returns the file name without path information of the current resource.<p>
200      *
201      * @return the name of the current resource
202      */

203     protected String JavaDoc getFileName() {
204
205         return CmsResource.getName(getParamResource());
206     }
207
208     /**
209      * Returns the last modified date of the current resource as localized String.<p>
210      *
211      * @return the date of last modification
212      */

213     protected String JavaDoc getLastModifiedDate() {
214
215         long dateLong = getCurrentResource().getDateLastModified();
216         return getMessages().getDateTime(dateLong);
217     }
218
219     /**
220      * Returns the user who made the last changes to the current resource.<p>
221      *
222      * @return the user who changed the resource
223      */

224     protected String JavaDoc getLastModifiedUser() {
225
226         CmsUUID userId = getCurrentResource().getUserLastModified();
227         try {
228             return getCms().readUser(userId).getName();
229         } catch (CmsException e) {
230             return "";
231         }
232     }
233
234     /**
235      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
236      */

237     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
238
239         // fill the parameter values in the get/set methods
240
fillParamValues(request);
241
242         // check the required permissions to undo changes of the resource
243
if (!checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
244             // no write permissions for the resource, set cancel action to close dialog
245
setParamAction(DIALOG_CANCEL);
246         }
247
248         // set the dialog type
249
setParamDialogtype(DIALOG_TYPE);
250         // set the action for the JSP switch
251
if (DIALOG_TYPE.equals(getParamAction())) {
252             setAction(ACTION_UNDOCHANGES);
253         } else if (DIALOG_WAIT.equals(getParamAction())) {
254             setAction(ACTION_WAIT);
255         } else if (DIALOG_CANCEL.equals(getParamAction())) {
256             setAction(ACTION_CANCEL);
257         } else {
258             setAction(ACTION_DEFAULT);
259             // build title for undo changes dialog
260
setDialogTitle(Messages.GUI_UNDO_CHANGES_1, Messages.GUI_UNDO_CHANGES_MULTI_2);
261         }
262
263         if (! isMultiOperation()) {
264             // collect resource to display information on single operation dialog
265
try {
266                 setCurrentResource(getCms().readResource(getParamResource(), CmsResourceFilter.ALL));
267             } catch (CmsException e) {
268                 // should usually never happen
269
if (LOG.isInfoEnabled()) {
270                     LOG.info(e.getLocalizedMessage());
271                 }
272             }
273         }
274
275     }
276
277     /**
278      * Performs the undo changes operation on a resource.<p>
279      *
280      * @return true, if the changes on a resource were undone, otherwise false
281      * @throws CmsException if undo changes is not successful
282      */

283     protected boolean performDialogOperation() throws CmsException {
284
285         // check if the current resource is a folder for single operation
286
boolean isFolder = isOperationOnFolder();
287         // on folder undo changes or multi operation display "please wait" screen, not for simple file undo changes
288
if ((isMultiOperation() || isFolder) && !DIALOG_WAIT.equals(getParamAction())) {
289             // return false, this will trigger the "please wait" screen
290
return false;
291         }
292         
293         // get the flag if the undo changes is recursive from request parameter
294
boolean undoRecursive = Boolean.valueOf(getParamRecursive()).booleanValue();
295         
296         Iterator JavaDoc i = getResourceList().iterator();
297         // iterate the resources to delete
298
while (i.hasNext()) {
299             String JavaDoc resName = (String JavaDoc)i.next();
300             try {
301                 // lock resource if autolock is enabled
302
checkLock(resName);
303                 // undo changes on the resource
304
getCms().undoChanges(resName, undoRecursive);
305             } catch (CmsException e) {
306                 if (isMultiOperation()) {
307                     // collect exceptions to create a detailed output
308
addMultiOperationException(e);
309                 } else {
310                     // for single operation, throw the exception immediately
311
throw e;
312                 }
313             }
314         }
315         // check if exceptions occured
316
checkMultiOperationException(Messages.get(), Messages.ERR_UNDO_CHANGES_MULTI_0);
317
318         return true;
319     }
320
321     /**
322      * Sets the current CmsResource.<p>
323      *
324      * @param res the CmsResource
325      */

326     protected void setCurrentResource(CmsResource res) {
327
328         m_currentResource = res;
329     }
330
331 }
332
Popular Tags