KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > CmsMultiDialog


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/CmsMultiDialog.java,v $
3  * Date : $Date: 2006/03/27 14:52:43 $
4  * Version: $Revision: 1.2 $
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;
33
34 import org.opencms.file.CmsPropertyDefinition;
35 import org.opencms.file.CmsResource;
36 import org.opencms.file.CmsResourceFilter;
37 import org.opencms.i18n.CmsMessageContainer;
38 import org.opencms.i18n.I_CmsMessageBundle;
39 import org.opencms.jsp.CmsJspActionElement;
40 import org.opencms.main.CmsException;
41 import org.opencms.main.CmsLog;
42 import org.opencms.main.CmsMultiException;
43 import org.opencms.security.CmsPermissionSet;
44 import org.opencms.util.CmsStringUtil;
45
46 import java.util.ArrayList JavaDoc;
47 import java.util.Collections 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.PageContext JavaDoc;
54
55 import org.apache.commons.logging.Log;
56
57 /**
58  * The base class to build a dialog capable of multiple file operations.<p>
59  *
60  * Extend this class for workplace dialogs that can perform operations on more than one
61  * VFS resource like copy, move, touch etc.<p>
62  *
63  * Provides methods to determine if a multi-resource operation has to be done and helper methods,
64  * e.g. to get the list of resources to work with.<p>
65  *
66  * @author Andreas Zahner
67  *
68  * @version $Revision: 1.2 $
69  *
70  * @since 6.2.0
71  */

72 public abstract class CmsMultiDialog extends CmsDialog {
73
74     /** The delimiter that is used in the resource list request parameter. */
75     public static final String JavaDoc DELIMITER_RESOURCES = "|";
76
77     /** Request parameter name for the resource list. */
78     public static final String JavaDoc PARAM_RESOURCELIST = "resourcelist";
79
80     /** Collects all eventually thrown exceptions during a multi operation. */
81     private CmsMultiException m_multiOperationException;
82
83     /** The resourcelist parameter value. */
84     private String JavaDoc m_paramResourcelist;
85
86     /** The list of resource names for the multi operation. */
87     private List JavaDoc m_resourceList;
88     
89     /** The log object for this class. */
90     private static final Log LOG = CmsLog.getLog(CmsMultiDialog.class);
91
92     /**
93      * Public constructor.<p>
94      *
95      * @param jsp an initialized JSP action element
96      */

97     public CmsMultiDialog(CmsJspActionElement jsp) {
98
99         super(jsp);
100         m_multiOperationException = new CmsMultiException();
101     }
102
103     /**
104      * Public constructor with JSP variables.<p>
105      *
106      * @param context the JSP page context
107      * @param req the JSP request
108      * @param res the JSP response
109      */

110     public CmsMultiDialog(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
111
112         this(new CmsJspActionElement(context, req, res));
113     }
114
115     /**
116      * Adds an exception thrown during a multi resource operation to the multi exception.<p>
117      *
118      * After iterating the dialog resources, use {@link #checkMultiOperationException(I_CmsMessageBundle, String)} to
119      * display the multi exception depending on collected exceptions.<p>
120      *
121      * @param exc the exception that was thrown
122      */

123     public void addMultiOperationException(CmsException exc) {
124
125         m_multiOperationException.addException(exc);
126     }
127
128     /**
129      * Builds the HTML for the resource list that is affected by the multi operation.<p>
130      *
131      * @return the HTML for the resource list that is affected by the multi operation
132      */

133     public String JavaDoc buildResourceList() {
134
135         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
136         result.append("<table border=\"0\">\n");
137         Iterator JavaDoc i = getResourceList().iterator();
138         while (i.hasNext()) {
139             String JavaDoc resName = (String JavaDoc)i.next();
140             result.append("\t<tr>\n");
141             result.append("\t\t<td style=\"vertical-align:top;\">./");
142             result.append(CmsResource.getName(resName));
143             result.append("&nbsp;</td>\n\t\t<td style=\"vertical-align:top;\">");
144             String JavaDoc title = null;
145             try {
146                 // get the title property value
147
title = getCms().readPropertyObject(resName, CmsPropertyDefinition.PROPERTY_TITLE, false).getValue(null);
148             } catch (CmsException e) {
149                 // ignore this exception, title not found
150
}
151             if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(title)) {
152                 // show the title information
153
result.append(title);
154             }
155             result.append("</td>\n\t</tr>\n");
156         }
157         result.append("</table>");
158         return result.toString();
159     }
160
161     /**
162      * Checks if an exception occured during a multi resource operation, and throws a new exception if necessary.<p>
163      *
164      * @param messages the message bundle to use for the exception message generation
165      * @param key the key for the exception to throw with one parameter
166      * @throws CmsException the exception that is thrown when the multi operation was not successful
167      */

168     public void checkMultiOperationException(I_CmsMessageBundle messages, String JavaDoc key) throws CmsException {
169
170         if (m_multiOperationException.hasExceptions()) {
171             m_multiOperationException.setMessage(new CmsMessageContainer(
172                 messages,
173                 key,
174                 new Object JavaDoc[] {m_multiOperationException}));
175             throw m_multiOperationException;
176         }
177     }
178
179     /**
180      * Returns the value of the resourcelist parameter, or null if the parameter is not provided.<p>
181      *
182      * This parameter selects the resources to perform operations on.<p>
183      *
184      * @return the value of the resourcelist parameter or null, if the parameter is not provided
185      */

186     public String JavaDoc getParamResourcelist() {
187
188         if (CmsStringUtil.isNotEmpty(m_paramResourcelist) && !"null".equals(m_paramResourcelist)) {
189             return m_paramResourcelist;
190         } else {
191             return null;
192         }
193     }
194
195     /**
196      * Returns the resources that are defined for the dialog operation.<p>
197      *
198      * For single resource operations, the list contains one item: the resource name found
199      * in the request parameter value of the "resource" parameter.<p>
200      *
201      * @return the resources that are defined for the dialog operation
202      */

203     public List JavaDoc getResourceList() {
204
205         if (m_resourceList == null) {
206             // use lazy initializing
207
if (getParamResourcelist() != null) {
208                 // found the resourcelist parameter
209
m_resourceList = CmsStringUtil.splitAsList(getParamResourcelist(), DELIMITER_RESOURCES, true);
210                 Collections.sort(m_resourceList);
211             } else {
212                 // this is a single resource operation, create list containing the resource name
213
m_resourceList = new ArrayList JavaDoc(1);
214                 m_resourceList.add(getParamResource());
215             }
216         }
217         return m_resourceList;
218     }
219
220     /**
221      * Returns true if the dialog operation has to be performed on multiple resources.<p>
222      *
223      * @return true if the dialog operation has to be performed on multiple resources, otherwise false
224      */

225     public boolean isMultiOperation() {
226
227         return (getResourceList().size() > 1);
228     }
229
230     /**
231      * Sets the title of the dialog depending on the operation type, multiple or single operation.<p>
232      *
233      * @param singleKey the key for the single operation
234      * @param multiKey the key for the multiple operation
235      */

236     public void setDialogTitle(String JavaDoc singleKey, String JavaDoc multiKey) {
237
238         if (isMultiOperation()) {
239             // generate title with number of selected resources and parent folder parameters
240
String JavaDoc resCount = String.valueOf(getResourceList().size());
241             String JavaDoc currentFolder = CmsResource.getFolderPath(getSettings().getExplorerResource());
242             currentFolder = CmsStringUtil.formatResourceName(currentFolder, 40);
243             Object JavaDoc[] params = new Object JavaDoc[] {resCount, currentFolder};
244             setParamTitle(key(multiKey, params));
245         } else {
246             // generate title using the resource name as parameter for the key
247
String JavaDoc resourceName = CmsStringUtil.formatResourceName(getParamResource(), 50);
248             setParamTitle(key(singleKey, new Object JavaDoc[] {resourceName}));
249         }
250     }
251
252     /**
253      * Sets the value of the resourcelist parameter.<p>
254      *
255      * @param paramResourcelist the value of the resourcelist parameter
256      */

257     public void setParamResourcelist(String JavaDoc paramResourcelist) {
258
259         m_paramResourcelist = paramResourcelist;
260     }
261     
262     /**
263      * Checks if the permissions of the current user on the single resource to use in the dialog are sufficient.<p>
264      *
265      * For a multi resource operation, this returns always true, checks only for single resource operations.<p>
266      *
267      * @see CmsDialog#checkResourcePermissions(CmsPermissionSet, boolean)
268      *
269      * @param required the required permissions for the dialog
270      * @param neededForFolder if true, the permissions are required for the parent folder of the resource (e.g. for editors)
271      * @return true if the permissions are sufficient, otherwise false
272      */

273     protected boolean checkResourcePermissions(CmsPermissionSet required, boolean neededForFolder) {
274
275         if (isMultiOperation()) {
276             // for multi resource operation, return always true
277
return true;
278         } else {
279             // check for single resource operation
280
return super.checkResourcePermissions(required, neededForFolder);
281         }
282     }
283     
284     /**
285      * Checks if the resource operation is an operation on at least one folder.<p>
286      *
287      * @return true if the operation an operation on at least one folder, otherwise false
288      */

289     protected boolean isOperationOnFolder() {
290
291         Iterator JavaDoc i = getResourceList().iterator();
292         while (i.hasNext()) {
293             String JavaDoc resName = (String JavaDoc)i.next();
294             try {
295                 CmsResource curRes = getCms().readResource(resName, CmsResourceFilter.ALL);
296                 if (curRes.isFolder()) {
297                     // found a folder
298
return true;
299                 }
300             } catch (CmsException e) {
301                 // can usually be ignored
302
if (LOG.isInfoEnabled()) {
303                     LOG.info(e.getLocalizedMessage());
304                 }
305             }
306         }
307         return false;
308     }
309
310     /**
311      * Performs the dialog operation for the selected resources.<p>
312      *
313      * @return true, if the operation was successful, otherwise false
314      * @throws CmsException if operation was not successful
315      */

316     protected abstract boolean performDialogOperation() throws CmsException;
317
318 }
Popular Tags