KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsChtype.java,v $
3  * Date : $Date: 2006/04/04 14:39:16 $
4  * Version: $Revision: 1.21 $
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.file.types.CmsResourceTypePlain;
38 import org.opencms.jsp.CmsJspActionElement;
39 import org.opencms.loader.CmsLoaderException;
40 import org.opencms.main.CmsException;
41 import org.opencms.main.CmsLog;
42 import org.opencms.main.OpenCms;
43 import org.opencms.security.CmsPermissionSet;
44 import org.opencms.workplace.CmsDialog;
45 import org.opencms.workplace.CmsWorkplaceSettings;
46 import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
47
48 import java.util.List JavaDoc;
49
50 import javax.servlet.http.HttpServletRequest JavaDoc;
51 import javax.servlet.http.HttpServletResponse JavaDoc;
52 import javax.servlet.jsp.JspException JavaDoc;
53 import javax.servlet.jsp.PageContext JavaDoc;
54
55 import org.apache.commons.logging.Log;
56
57 /**
58  * The change resource type dialog handles the change of a resource type of a single VFS file.<p>
59  *
60  * The following files use this class:
61  * <ul>
62  * <li>/commons/chtype.jsp
63  * </ul>
64  * <p>
65  *
66  * @author Andreas Zahner
67  *
68  * @version $Revision: 1.21 $
69  *
70  * @since 6.0.0
71  */

72 public class CmsChtype extends CmsDialog {
73
74     /** The dialog type.<p> */
75     public static final String JavaDoc DIALOG_TYPE = "chtype";
76
77     /** Request parameter name for the new resource type.<p> */
78     public static final String JavaDoc PARAM_NEWRESOURCETYPE = "newresourcetype";
79
80     /** The log object for this class. */
81     private static final Log LOG = CmsLog.getLog(CmsChtype.class);
82
83     private String JavaDoc m_paramNewResourceType;
84
85     /**
86      * Public constructor with JSP action element.<p>
87      *
88      * @param jsp an initialized JSP action element
89      */

90     public CmsChtype(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 CmsChtype(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
103
104         this(new CmsJspActionElement(context, req, res));
105     }
106
107     /**
108      * Builds the html for the list of possible types for the uploaded file.<p>
109      *
110      * This method can be used by all workplace dialog classes to build
111      * radio input buttons to select a resource type.<p>
112      *
113      * @param dialog the dialog class instance which creates the type list
114      * @param useTypeId if true, the resource type ID will be used for value attributes, otherwise the resource type names
115      * @return the list of possible files for the uploaded resource
116      */

117     public static String JavaDoc buildTypeList(CmsDialog dialog, boolean useTypeId) {
118
119         StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
120         try {
121             // get current Cms object
122
CmsObject cms = dialog.getCms();
123             // determine resource type id of resource to change
124
CmsResource res = cms.readResource(dialog.getParamResource(), CmsResourceFilter.ALL);
125             int currentResTypeId = res.getTypeId();
126             // get all available explorer type settings
127
List JavaDoc resTypes = OpenCms.getWorkplaceManager().getExplorerTypeSettings();
128             boolean isFolder = res.isFolder();
129             // loop through all visible resource types
130
for (int i = 0; i < resTypes.size(); i++) {
131                 boolean changeable = false;
132                 // get explorer type settings for current resource type
133
CmsExplorerTypeSettings settings = (CmsExplorerTypeSettings)resTypes.get(i);
134
135                 // only if settings is a real resourcetype
136
boolean isResourceType;
137                 try {
138                     OpenCms.getResourceManager().getResourceType(settings.getName());
139                     isResourceType = true;
140                 } catch (CmsLoaderException e) {
141                     isResourceType = false;
142                 }
143
144                 if (isResourceType) {
145                     if (settings.getName().equals("XMLTemplate")) {
146                         // skip legacy resource type XMLTemplate
147
continue;
148                     }
149                     
150                     int resTypeId = OpenCms.getResourceManager().getResourceType(settings.getName()).getTypeId();
151                     // determine if this resTypeId is changeable by currentResTypeId
152

153                     // changeable is true if current resource is a folder and this resource type also
154
if (isFolder && OpenCms.getResourceManager().getResourceType(resTypeId).isFolder()) {
155                         changeable = true;
156                     } else if (!isFolder && !OpenCms.getResourceManager().getResourceType(resTypeId).isFolder()) {
157                         // changeable is true if current resource is NOT a folder and this resource type also NOT
158
changeable = true;
159                     }
160
161                     if (changeable) {
162                         // determine if this resource type is editable for the current user
163
CmsPermissionSet permissions = settings.getAccess().getPermissions(cms);
164                         if (!permissions.requiresWritePermission() || !permissions.requiresControlPermission()) {
165                             // skip resource types without required write or create permissions
166
continue;
167                         }
168
169                         // create table row with input radio button
170
result.append("<tr><td>");
171                         result.append("<input type=\"radio\" name=\"");
172                         result.append(PARAM_NEWRESOURCETYPE);
173                         result.append("\" value=\"");
174                         if (useTypeId) {
175                             // use resource type id as value
176
result.append(resTypeId);
177                         } else {
178                             // use resource type name as value
179
result.append(settings.getName());
180                         }
181                         result.append("\"");
182                         if (resTypeId == currentResTypeId) {
183                             result.append(" checked=\"checked\"");
184                         }
185                         result.append("></td>");
186                         result.append("\t<td><img SRC=\"");
187                         result.append(getSkinUri());
188                         result.append("filetypes/");
189                         result.append(settings.getIcon());
190                         result.append("\" border=\"0\" title=\"");
191                         result.append(dialog.key(settings.getKey()));
192                         result.append("\"></td>\n");
193                         result.append("<td>");
194                         result.append(dialog.key(settings.getKey()));
195                         result.append("</td></tr>\n");
196                     }
197                 }
198             }
199         } catch (CmsException e) {
200             // error reading the VFS resource, log error
201
LOG.error(Messages.get().getBundle().key(Messages.ERR_BUILDING_RESTYPE_LIST_1, dialog.getParamResource()));
202         }
203         return result.toString();
204     }
205
206     /**
207      * Uploads the specified file and replaces the VFS file.<p>
208      *
209      * @throws JspException if inclusion of error dialog fails
210      */

211     public void actionChtype() throws JspException JavaDoc {
212
213         try {
214             int newType = CmsResourceTypePlain.getStaticTypeId();
215             try {
216                 // get new resource type id from request
217
newType = Integer.parseInt(getParamNewResourceType());
218             } catch (NumberFormatException JavaDoc nf) {
219                 throw new CmsException(Messages.get().container(Messages.ERR_GET_RESTYPE_1, getParamNewResourceType()));
220             }
221             // check the resource lock state
222
checkLock(getParamResource());
223             // change the resource type
224
getCms().chtype(getParamResource(), newType);
225             // close the dialog window
226
actionCloseDialog();
227         } catch (Throwable JavaDoc e) {
228             // error changing resource type, show error dialog
229
includeErrorpage(this, e);
230         }
231     }
232
233     /**
234      * Builds the html for the list of possible types for the uploaded file.<p>
235      *
236      * @return the list of possible files for the uploaded resource
237      */

238     public String JavaDoc buildTypeList() {
239
240         return buildTypeList(this, true);
241     }
242
243     /**
244      * Returns the new resource type parameter.<p>
245      *
246      * @return the new resource type parameter
247      */

248     public String JavaDoc getParamNewResourceType() {
249
250         return m_paramNewResourceType;
251     }
252
253     /**
254      * Sets the new resource type parameter.<p>
255      *
256      * @param newResourceType the new resource type parameter
257      */

258     public void setParamNewResourceType(String JavaDoc newResourceType) {
259
260         m_paramNewResourceType = newResourceType;
261     }
262
263     /**
264      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
265      */

266     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
267
268         // fill the parameter values in the get/set methods
269
fillParamValues(request);
270
271         // check the required permissions to change the resource type
272
if (!checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
273             // no write permissions for the resource, set cancel action to close dialog
274
setParamAction(DIALOG_CANCEL);
275         }
276
277         // set the dialog type
278
setParamDialogtype(DIALOG_TYPE);
279         // set the action for the JSP switch
280
if (DIALOG_OK.equals(getParamAction())) {
281             // ok button pressed, change file type
282
setAction(ACTION_OK);
283         } else if (DIALOG_CANCEL.equals(getParamAction())) {
284             // cancel button pressed
285
setAction(ACTION_CANCEL);
286         } else {
287             // first call of dialog
288
setAction(ACTION_DEFAULT);
289             // build title for change file type dialog
290
setParamTitle(key(Messages.GUI_CHTYPE_1, new Object JavaDoc[] {CmsResource.getName(getParamResource())}));
291         }
292     }
293 }
Popular Tags