KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsDelete.java,v $
3  * Date : $Date: 2006/03/27 14:52:18 $
4  * Version: $Revision: 1.17 $
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.lock.CmsLock;
38 import org.opencms.main.CmsException;
39 import org.opencms.main.CmsLog;
40 import org.opencms.main.OpenCms;
41 import org.opencms.security.CmsPermissionSet;
42 import org.opencms.workplace.CmsDialogSelector;
43 import org.opencms.workplace.CmsMultiDialog;
44 import org.opencms.workplace.CmsWorkplaceSettings;
45 import org.opencms.workplace.I_CmsDialogHandler;
46
47 import java.util.ArrayList JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.List JavaDoc;
50
51 import javax.servlet.http.HttpServletRequest JavaDoc;
52 import javax.servlet.http.HttpServletResponse JavaDoc;
53 import javax.servlet.jsp.JspException JavaDoc;
54 import javax.servlet.jsp.PageContext JavaDoc;
55
56 import org.apache.commons.logging.Log;
57
58 /**
59  * Provides methods for the delete resources dialog.<p>
60  *
61  * The following files use this class:
62  * <ul>
63  * <li>/commons/delete_standard.jsp
64  * </ul>
65  * <p>
66  *
67  * @author Andreas Zahner
68  *
69  * @version $Revision: 1.17 $
70  *
71  * @since 6.0.0
72  */

73 public class CmsDelete extends CmsMultiDialog implements I_CmsDialogHandler {
74
75     /** Value for the action: delete the resource. */
76     public static final int ACTION_DELETE = 100;
77
78     /** The dialog type. */
79     public static final String JavaDoc DIALOG_TYPE = "delete";
80     
81     /** Request parameter name for the deletevfslinks parameter. */
82     public static final String JavaDoc PARAM_DELETEVFSLINKS = "deletevfslinks";
83
84     /** The delete dialog URI. */
85     public static final String JavaDoc URI_DELETE_DIALOG = PATH_DIALOGS + "delete_standard.jsp";
86
87     /** The log object for this class. */
88     private static final Log LOG = CmsLog.getLog(CmsDelete.class);
89
90     private String JavaDoc m_deleteVfsLinks;
91
92     /**
93      * Default constructor needed for dialog handler implementation.<p>
94      */

95     public CmsDelete() {
96
97         super(null);
98     }
99
100     /**
101      * Public constructor with JSP action element.<p>
102      *
103      * @param jsp an initialized JSP action element
104      */

105     public CmsDelete(CmsJspActionElement jsp) {
106
107         super(jsp);
108     }
109
110     /**
111      * Public constructor with JSP variables.<p>
112      *
113      * @param context the JSP page context
114      * @param req the JSP request
115      * @param res the JSP response
116      */

117     public CmsDelete(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
118
119         this(new CmsJspActionElement(context, req, res));
120     }
121
122     /**
123      * Performs the delete action, will be called by the JSP page.<p>
124      *
125      * @throws JspException if problems including sub-elements occur
126      */

127     public void actionDelete() throws JspException JavaDoc {
128
129         // save initialized instance of this class in request attribute for included sub-elements
130
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
131         try {
132             boolean isFolder = false;
133             if (!isMultiOperation()) {
134                 CmsResource resource = getCms().readResource(getParamResource(), CmsResourceFilter.ALL);
135                 resource = getCms().readResource(getParamResource(), CmsResourceFilter.ALL);
136                 isFolder = resource.isFolder();
137             }
138             if (performDialogOperation()) {
139                 // if no exception is caused and "true" is returned delete operation was successful
140
if (isMultiOperation() || isFolder) {
141                     // set request attribute to reload the explorer tree view
142
List JavaDoc folderList = new ArrayList JavaDoc(1);
143                     folderList.add(CmsResource.getParentFolder((String JavaDoc)getResourceList().get(0)));
144                     getJsp().getRequest().setAttribute(REQUEST_ATTRIBUTE_RELOADTREE, folderList);
145                 }
146                 actionCloseDialog();
147             } else {
148                 // "false" returned, display "please wait" screen
149
getJsp().include(FILE_DIALOG_SCREEN_WAIT);
150             }
151         } catch (Throwable JavaDoc e) {
152             // prepare common message part
153
includeErrorpage(this, e);
154         }
155     }
156
157     /**
158      * Returns the html for the "delete siblings" options when deleting a a resource with siblings.<p>
159      *
160      * @return the html for the "delete siblings" options
161      */

162     public String JavaDoc buildDeleteSiblings() {
163
164         StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
165         if (isMultiOperation() || (hasVfsLinks() && hasCorrectLockstate())) {
166             // show only for multi resource operation or if resource has siblings and correct lock state
167
int defaultMode = getSettings().getUserSettings().getDialogDeleteFileMode();
168             if (! isMultiOperation()) {
169                 result.append(key(Messages.GUI_DELETE_WARNING_SIBLINGS_0));
170                 result.append("<p>");
171             }
172             result.append("<input type=\"radio\" name=\"");
173             result.append(PARAM_DELETEVFSLINKS);
174             result.append("\" value=\"false\"");
175             if (defaultMode == CmsResource.DELETE_PRESERVE_SIBLINGS) {
176                 result.append(" checked=\"checked\"");
177             }
178             result.append(">&nbsp;");
179             result.append(key(Messages.GUI_DELETE_PRESERVE_SIBLINGS_0));
180             result.append("<br>");
181             result.append("<input type=\"radio\" name=\"");
182             result.append(PARAM_DELETEVFSLINKS);
183             result.append("\" value=\"true\"");
184             if (defaultMode == CmsResource.DELETE_REMOVE_SIBLINGS) {
185                 result.append(" checked=\"checked\"");
186             }
187             result.append(">&nbsp;");
188             result.append(key(Messages.GUI_DELETE_ALL_SIBLINGS_0));
189             result.append("<p>");
190         }
191         if (isMultiOperation()) {
192             result.append(key(Messages.GUI_DELETE_MULTI_CONFIRMATION_0));
193         } else {
194             result.append(key(Messages.GUI_DELETE_CONFIRMATION_0));
195         }
196         return result.toString();
197     }
198
199     /**
200      * @see org.opencms.workplace.I_CmsDialogHandler#getDialogHandler()
201      */

202     public String JavaDoc getDialogHandler() {
203
204         return CmsDialogSelector.DIALOG_DELETE;
205     }
206
207     /**
208      * @see org.opencms.workplace.I_CmsDialogHandler#getDialogUri(java.lang.String, CmsJspActionElement)
209      */

210     public String JavaDoc getDialogUri(String JavaDoc resource, CmsJspActionElement jsp) {
211
212         return URI_DELETE_DIALOG;
213     }
214
215     /**
216      * Returns the value of the boolean option to delete VFS links.<p>
217      *
218      * @return the value of the boolean option to delete VFS links as a lower case string
219      */

220     public String JavaDoc getParamDeleteVfsLinks() {
221
222         return m_deleteVfsLinks;
223     }
224
225     /**
226      * Checks if the current resource has lock state exclusive or inherited.<p>
227      *
228      * This is used to determine whether the dialog shows the option to delete all
229      * siblings of the resource or not.
230      *
231      * @return true if lock state is exclusive or inherited, otherwise false
232      */

233     public boolean hasCorrectLockstate() {
234
235         CmsLock lock = null;
236         try {
237             // get the lock state for the current resource
238
lock = getCms().getLock(getParamResource());
239         } catch (CmsException e) {
240             // error getting lock state, log the error and return false
241
LOG.error(e.getLocalizedMessage(), e);
242             return false;
243         }
244         int type = lock.getType();
245         // check if autolock feature is enabled
246
boolean autoLockFeature = lock.isNullLock() && OpenCms.getWorkplaceManager().autoLockResources();
247         return (autoLockFeature || type == CmsLock.TYPE_EXCLUSIVE || type == CmsLock.TYPE_INHERITED);
248     }
249
250     /**
251      * Checks if VFS links are pointing to this resource.
252      *
253      * @return true if one or more VFS links are pointing to this resource
254      */

255     public boolean hasVfsLinks() {
256
257         try {
258             return getCms().readSiblings(getParamResource(), CmsResourceFilter.ALL).size() > 1;
259         } catch (CmsException e) {
260             LOG.error(e.getLocalizedMessage(), e);
261             return false;
262         }
263
264     }
265
266     /**
267      * Sets the value of the boolean option to delete VFS links.<p>
268      *
269      * @param value the value of the boolean option to delete VFS links
270      */

271     public void setParamDeleteVfsLinks(String JavaDoc value) {
272
273         m_deleteVfsLinks = value;
274     }
275
276     /**
277      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
278      */

279     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
280
281         // fill the parameter values in the get/set methods
282
fillParamValues(request);
283
284         // check the required permissions to delete the resource
285
if (!checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
286             // no write permissions for the resource, set cancel action to close dialog
287
setParamAction(DIALOG_CANCEL);
288         }
289
290         // set the dialog type
291
setParamDialogtype(DIALOG_TYPE);
292         // set the action for the JSP switch
293
if (DIALOG_TYPE.equals(getParamAction())) {
294             setAction(ACTION_DELETE);
295         } else if (DIALOG_WAIT.equals(getParamAction())) {
296             setAction(ACTION_WAIT);
297         } else if (DIALOG_CANCEL.equals(getParamAction())) {
298             setAction(ACTION_CANCEL);
299         } else {
300             setAction(ACTION_DEFAULT);
301             // build title for delete dialog
302
setDialogTitle(Messages.GUI_DELETE_RESOURCE_1, Messages.GUI_DELETE_MULTI_2);
303         }
304     }
305
306     /**
307      * Performs the resource deletion.<p>
308      *
309      * @return true, if the resource(s) was/were deleted, otherwise false
310      * @throws CmsException if deletion is not successful
311      */

312     protected boolean performDialogOperation() throws CmsException {
313
314         int deleteOption = -1;
315
316         // check if the current resource is a folder for single operation
317
boolean isFolder = isOperationOnFolder();
318         // on folder deletion or multi operation display "please wait" screen, not for simple file deletion
319
if ((isMultiOperation() || isFolder) && !DIALOG_WAIT.equals(getParamAction())) {
320             // return false, this will trigger the "please wait" screen
321
return false;
322         }
323
324         // determine the correct delete option
325
deleteOption = Boolean.valueOf(getParamDeleteVfsLinks()).booleanValue() ? CmsResource.DELETE_REMOVE_SIBLINGS
326         : CmsResource.DELETE_PRESERVE_SIBLINGS;
327
328         Iterator JavaDoc i = getResourceList().iterator();
329         // iterate the resources to delete
330
while (i.hasNext()) {
331             String JavaDoc resName = (String JavaDoc)i.next();
332             try {
333                 performSingleDeleteOperation(resName, deleteOption);
334             } catch (CmsException e) {
335                 if (isMultiOperation()) {
336                     // collect exceptions to create a detailed output
337
addMultiOperationException(e);
338                 } else {
339                     // for single operation, throw the exception immediately
340
throw e;
341                 }
342             }
343         }
344         // check if exceptions occured
345
checkMultiOperationException(Messages.get(), Messages.ERR_DELETE_MULTI_0);
346
347         return true;
348     }
349
350     /**
351      * Performs the delete operation for a single VFS resource.<p>
352      *
353      * @param resource the resource VFS path
354      * @param deleteOption the delete option for sibling deletion
355      * @throws CmsException if deleting the resource fails
356      */

357     protected void performSingleDeleteOperation(String JavaDoc resource, int deleteOption) throws CmsException {
358
359         // lock resource if autolock is enabled
360
checkLock(resource);
361         // delete the resource
362
getCms().deleteResource(resource, deleteOption);
363     }
364
365 }
Popular Tags