KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsLock.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.CmsObject;
35 import org.opencms.file.CmsResource;
36 import org.opencms.file.CmsResourceFilter;
37 import org.opencms.jsp.CmsJspActionElement;
38 import org.opencms.main.CmsException;
39 import org.opencms.security.CmsPermissionSet;
40 import org.opencms.workplace.CmsDialogSelector;
41 import org.opencms.workplace.CmsMultiDialog;
42 import org.opencms.workplace.CmsWorkplaceSettings;
43 import org.opencms.workplace.I_CmsDialogHandler;
44
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  * Creates the dialogs for locking, unlocking or steal lock operations on a resource.<p>
54  *
55  * The following files use this class:
56  * <ul>
57  * <li>/commons/lock_standard.jsp
58  * <li>/commons/lockchange_standard.jsp
59  * <li>/commons/unlock_standard.jsp
60  * </ul>
61  * <p>
62  *
63  * @author Andreas Zahner
64  *
65  * @version $Revision: 1.16 $
66  *
67  * @since 6.0.0
68  */

69 public class CmsLock extends CmsMultiDialog implements I_CmsDialogHandler {
70
71     /** Value for the action: confirmed. */
72     public static final int ACTION_SUBMIT_NOCONFIRMATION = 200;
73
74     /** Request parameter value for the action: submit form without user interaction. */
75     public static final String JavaDoc DIALOG_SUBMIT_NOCONFIRMATION = "submitnoconfirmation";
76
77     /** The dialog type: lock a resource. */
78     public static final String JavaDoc DIALOG_TYPE_LOCK = "lock";
79     /** The dialog type: Steal a lock. */
80     public static final String JavaDoc DIALOG_TYPE_LOCKCHANGE = "lockchange";
81     /** The dialog type: unlock a resource. */
82     public static final String JavaDoc DIALOG_TYPE_UNLOCK = "unlock";
83
84     /** Type of the operation which is performed: lock resource. */
85     public static final int TYPE_LOCK = 1;
86     /** Type of the operation which is performed: steal a lock. */
87     public static final int TYPE_LOCKCHANGE = 2;
88     /** Type of the operation which is performed: unlock resource. */
89     public static final int TYPE_UNLOCK = 3;
90
91     /** The lock dialog URI. */
92     public static final String JavaDoc URI_LOCK_DIALOG = PATH_DIALOGS + "lock_standard.jsp";
93     /** The steal lock dialog URI. */
94     public static final String JavaDoc URI_LOCKCHANGE_DIALOG = PATH_DIALOGS + "lockchange_standard.jsp";
95     /** The unlock dialog URI. */
96     public static final String JavaDoc URI_UNLOCK_DIALOG = PATH_DIALOGS + "unlock_standard.jsp";
97
98     /** Flag indicating if the selected resources to lockes contain locked subresources.*/
99     private boolean m_hasLockedSubResources;
100
101     /**
102      * Default constructor needed for dialog handler implementation.<p>
103      */

104     public CmsLock() {
105
106         super(null);
107     }
108
109     /**
110      * Public constructor.<p>
111      *
112      * @param jsp an initialized JSP action element
113      */

114     public CmsLock(CmsJspActionElement jsp) {
115
116         super(jsp);
117     }
118
119     /**
120      * Public constructor with JSP variables.<p>
121      *
122      * @param context the JSP page context
123      * @param req the JSP request
124      * @param res the JSP response
125      */

126     public CmsLock(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
127
128         this(new CmsJspActionElement(context, req, res));
129     }
130
131     /**
132      * Determines if the resource should be locked, unlocked or if the lock should be stolen.<p>
133      *
134      * @param cms the CmsObject
135      * @return the dialog action: lock, change lock (steal) or unlock
136      */

137     public static int getDialogAction(CmsObject cms) {
138
139         String JavaDoc fileName = CmsResource.getName(cms.getRequestContext().getUri());
140         if (fileName == null) {
141             // file name could not be determined, return "unlock" action
142
return TYPE_UNLOCK;
143         } else if (fileName.equalsIgnoreCase("lock.jsp")) {
144             // a "lock" action is requested
145
return TYPE_LOCK;
146         } else if (fileName.indexOf("change") != -1) {
147             // a "steal lock" action is requested
148
return TYPE_LOCKCHANGE;
149         } else {
150             // an "unlock" action is requested
151
return TYPE_UNLOCK;
152         }
153     }
154
155     /**
156      * Performs the lock/unlock operation, will be called by the JSP page.<p>
157      *
158      * @throws JspException if problems including sub-elements occur
159      */

160     public void actionToggleLock() throws JspException JavaDoc {
161
162         // save initialized instance of this class in request attribute for included sub-elements
163
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
164
165         try {
166
167             if (performDialogOperation()) {
168                 // if no exception is caused and "true" is returned the lock/unlock operation was successful
169
actionCloseDialog();
170             } else {
171                 // "false" returned, display "please wait" screen
172
getJsp().include(FILE_DIALOG_SCREEN_WAIT);
173             }
174         } catch (Throwable JavaDoc e) {
175             // exception occured, show error dialog
176
includeErrorpage(this, e);
177         }
178     }
179
180     /**
181      * Builds the HTML for the text that is shown on the confirmation dialog depending on the selected resource(s).<p>
182      *
183      * @return the HTML for the text that is shown on the confirmation dialog depending on the selected resource(s)
184      */

185     public String JavaDoc buildDialogText() {
186
187         switch (getDialogAction(getCms())) {
188             case TYPE_LOCK:
189                 if (hasLockedSubResources()) {
190                     if (isMultiOperation()) {
191                         return key(Messages.GUI_LOCK_MULTI_INFO_LOCKEDSUBRESOURCES_0);
192                     } else {
193                         return key(Messages.GUI_LOCK_INFO_LOCKEDSUBRESOURCES_0);
194                     }
195                 } else {
196                     if (isMultiOperation()) {
197                         return key(Messages.GUI_LOCK_MULTI_LOCK_CONFIRMATION_0);
198                     } else {
199                         return key(Messages.GUI_LOCK_CONFIRMATION_0);
200                     }
201                 }
202             case TYPE_LOCKCHANGE:
203                 return key(Messages.GUI_LOCK_CHANGE_CONFIRMATION_0);
204             case TYPE_UNLOCK:
205             default:
206                 if (isMultiOperation()) {
207                     return key(Messages.GUI_LOCK_MULTI_UNLOCK_CONFIRMATION_0);
208                 } else {
209                     return key(Messages.GUI_LOCK_UNLOCK_CONFIRMATION_0);
210                 }
211         }
212     }
213
214     /**
215      * @see org.opencms.workplace.I_CmsDialogHandler#getDialogHandler()
216      */

217     public String JavaDoc getDialogHandler() {
218
219         return CmsDialogSelector.DIALOG_LOCK;
220     }
221
222     /**
223      * @see org.opencms.workplace.I_CmsDialogHandler#getDialogUri(java.lang.String, CmsJspActionElement)
224      */

225     public String JavaDoc getDialogUri(String JavaDoc resource, CmsJspActionElement jsp) {
226
227         switch (getDialogAction(jsp.getCmsObject())) {
228             case TYPE_LOCK:
229                 return URI_LOCK_DIALOG;
230             case TYPE_LOCKCHANGE:
231                 return URI_LOCKCHANGE_DIALOG;
232             case TYPE_UNLOCK:
233             default:
234                 return URI_UNLOCK_DIALOG;
235         }
236     }
237
238     /**
239      * Returns true if the resources to lock have locked subresources.<p>
240      *
241      * @return true if the resources to lock have locked subresources
242      */

243     public boolean hasLockedSubResources() {
244
245         return m_hasLockedSubResources;
246     }
247
248     /**
249      * Sets if the resources to lock have locked subresources.<p>
250      *
251      * @param hasLockedSubResources if the resources to lock have locked subresources
252      */

253     public void setHasLockedSubResources(boolean hasLockedSubResources) {
254
255         m_hasLockedSubResources = hasLockedSubResources;
256     }
257
258     /**
259      * Determines whether to show the lock dialog depending on the users settings and the dilaog type.<p>
260      *
261      * In case of locking a folder, a confirmation dialog is needed if any sub resources are already locked.<p>
262      *
263      * @return true if dialogs should be shown, otherwise false
264      */

265     public boolean showConfirmation() {
266
267         boolean showConfirmation = getSettings().getUserSettings().getDialogShowLock();
268         if (DIALOG_TYPE_LOCK.equals(getParamDialogtype())) {
269             // in case of locking resources, check if there are locked sub resources in the selected folder(s)
270
Iterator JavaDoc i = getResourceList().iterator();
271             while (i.hasNext()) {
272                 String JavaDoc resName = (String JavaDoc)i.next();
273                 try {
274                     CmsResource res = getCms().readResource(resName);
275                     if (res.isFolder() && getCms().countLockedResources(resName) > 0) {
276                         // found folder with locked subresources, set flag to show confirmation dialog
277
setHasLockedSubResources(true);
278                         return true;
279                     }
280                 } catch (CmsException e) {
281                     // error reading a resource, should usually never happen
282
}
283             }
284         }
285         return showConfirmation;
286     }
287
288     /**
289      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
290      */

291     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
292
293         // fill the parameter values in the get/set methods
294
fillParamValues(request);
295
296         // set the action for the JSP switch
297
if (DIALOG_CONFIRMED.equals(getParamAction())) {
298             setAction(ACTION_CONFIRMED);
299         } else if (DIALOG_CANCEL.equals(getParamAction())) {
300             setAction(ACTION_CANCEL);
301         } else if (DIALOG_WAIT.equals(getParamAction())) {
302             setAction(ACTION_WAIT);
303         } else {
304             switch (getDialogAction(getCms())) {
305                 case TYPE_LOCK:
306                     setDialogTitle(Messages.GUI_LOCK_RESOURCE_1, Messages.GUI_LOCK_MULTI_LOCK_2);
307                     setParamDialogtype(DIALOG_TYPE_LOCK);
308                     // check the required permissions to lock/unlock a single resource
309
if (!isMultiOperation() && !checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
310                         // no write permissions for the resource, set cancel action to close dialog
311
setAction(ACTION_CANCEL);
312                         return;
313                     }
314                     break;
315                 case TYPE_LOCKCHANGE:
316                     setDialogTitle(Messages.GUI_LOCK_STEAL_1, Messages.GUI_LOCK_MULTI_STEAL_2);
317                     setParamDialogtype(DIALOG_TYPE_UNLOCK);
318                     break;
319                 case TYPE_UNLOCK:
320                 default:
321                     setDialogTitle(Messages.GUI_LOCK_UNLOCK_1, Messages.GUI_LOCK_MULTI_UNLOCK_2);
322                     setParamDialogtype(DIALOG_TYPE_UNLOCK);
323             }
324             // set action depending on user settings
325
if (showConfirmation()) {
326                 // show confirmation dialog
327
setAction(ACTION_DEFAULT);
328             } else {
329                 // lock/unlock resource without confirmation
330
setAction(ACTION_SUBMIT_NOCONFIRMATION);
331             }
332         }
333     }
334
335     /**
336      * Performs the lock/unlock/steal lock operation.<p>
337      *
338      * @return true, if the operation was performed, otherwise false
339      * @throws CmsException if operation is not successful
340      */

341     protected boolean performDialogOperation() throws CmsException {
342
343         //on multi resource operation display "please wait" screen
344
if (isMultiOperation() && !DIALOG_WAIT.equals(getParamAction())) {
345             return false;
346         }
347         // determine action to perform (lock, unlock, change lock)
348
int dialogAction = getDialogAction(getCms());
349
350         // now perform the operation on the resource(s)
351
Iterator JavaDoc i = getResourceList().iterator();
352         while (i.hasNext()) {
353             String JavaDoc resName = (String JavaDoc)i.next();
354             try {
355                 performSingleResourceOperation(resName, dialogAction);
356             } catch (CmsException e) {
357                 // collect exceptions to create a detailed output
358
addMultiOperationException(e);
359             }
360         }
361         // generate the error message for exception
362
String JavaDoc message;
363         if (dialogAction == TYPE_LOCK) {
364             message = Messages.ERR_LOCK_MULTI_0;
365         } else {
366             message = Messages.ERR_UNLOCK_MULTI_0;
367         }
368         checkMultiOperationException(Messages.get(), message);
369
370         return true;
371     }
372
373     /**
374      * Performs the lock state operation on a single resource.<p>
375      *
376      * @param resourceName the resource name to perform the operation on
377      * @param dialogAction the lock action: lock, unlock or change lock
378      * @throws CmsException if the operation fails
379      */

380     protected void performSingleResourceOperation(String JavaDoc resourceName, int dialogAction) throws CmsException {
381
382         // store original name to use for lock action
383
String JavaDoc originalResourceName = resourceName;
384         CmsResource res = getCms().readResource(resourceName, CmsResourceFilter.ALL);
385         if (res.isFolder() && !resourceName.endsWith("/")) {
386             resourceName += "/";
387         }
388         // perform action depending on dialog uri
389
switch (dialogAction) {
390             case TYPE_LOCK:
391                 getCms().lockResource(originalResourceName);
392                 break;
393             case TYPE_LOCKCHANGE:
394                 getCms().changeLock(resourceName);
395                 break;
396             case TYPE_UNLOCK:
397             default:
398                 getCms().unlockResource(resourceName);
399         }
400     }
401
402 }
Popular Tags