KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > workplace > CmsReplace


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/workplace/CmsReplace.java,v $
3  * Date : $Date: 2005/06/27 23:22:07 $
4  * Version: $Revision: 1.5 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2001 The OpenCms Group
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 OpenCms, please see the
22  * OpenCms Website: http://www.opencms.org
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  */

28
29 package com.opencms.workplace;
30
31 import org.opencms.file.CmsObject;
32 import org.opencms.main.CmsException;
33 import org.opencms.main.OpenCms;
34
35 import com.opencms.core.I_CmsSession;
36 import com.opencms.legacy.CmsLegacyException;
37 import com.opencms.legacy.CmsXmlTemplateLoader;
38
39 import java.util.Enumeration JavaDoc;
40 import java.util.Hashtable JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.Vector JavaDoc;
43
44 /**
45  * This class is invoked for the workplace "replace" function in the context menu.
46  *
47  * @author Thomas Weckert
48  * @version $Revision: 1.5 $
49  *
50  * @deprecated Will not be supported past the OpenCms 6 release.
51  */

52 public final class CmsReplace extends CmsWorkplaceDefault {
53
54     private static final String JavaDoc C_PARA_NEXT_STEP = "STEP";
55
56     /** Internal debugging flag */
57     private static final int DEBUG = 0;
58
59     /** The name of the old resource which will be replaced */
60     private String JavaDoc m_OldResourceName;
61
62     /** The type of the old resource which will be replaced */
63     private String JavaDoc m_OldResourceType;
64
65     /** The next screen/step to process */
66     private String JavaDoc m_NextStep;
67
68     /** The name of the new resource which will be uploaded */
69     private String JavaDoc m_UploadResourceName;
70
71     /** The type of the new resource which will be uploaded */
72     private String JavaDoc m_UploadResourceType;
73
74     /** The content of the new resource which will be uploaded */
75     private byte[] m_UploadResourceContent;
76
77     /** The XML template document */
78     private CmsXmlWpTemplateFile m_XmlTemplateDocument;
79
80     /** The name of the template section that is put out as HTML */
81     private String JavaDoc m_TemplateSection;
82
83     public byte[] getContent(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) throws CmsException {
84         // initialize the XML template
85
this.initTemplate(cms, parameters, templateFile);
86
87         // read the submitted input parameters
88
this.readInput(cms, parameters);
89
90         if ("1".equals(m_NextStep)) {
91             // the resource was uploaded, switch to the next screen to select its type
92
m_TemplateSection = "step1";
93         } else if ("2".equals(m_NextStep)) {
94             // check the autolock resource setting and lock the resource if necessary
95
if (OpenCms.getWorkplaceManager().autoLockResources()) {
96                 if (cms.getLock(m_OldResourceName).isNullLock()) {
97                     // resource is not locked, lock it automatically
98
cms.lockResource(m_OldResourceName);
99                 }
100             }
101             
102             // the type of the new resource was selected, so replace the old with the new resource
103
int type = OpenCms.getResourceManager().getResourceType(m_UploadResourceType).getTypeId();
104             cms.replaceResource(m_OldResourceName, type, m_UploadResourceContent, null);
105             //cms.unlockResource( m_OldResourceName );
106

107             // leave the session clean
108
this.clearSessionValues(CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true));
109
110             try {
111                 // send the user back to the file listing finally
112
CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendCmsRedirect(getConfigFile(cms).getWorkplaceActionPath() + CmsWorkplaceAction.getExplorerFileUri(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()));
113             } catch (Exception JavaDoc ex) {
114                 throw new CmsLegacyException("Redirect failed: " + getConfigFile(cms).getWorkplaceActionPath() + CmsWorkplaceAction.getExplorerFileUri(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()), ex);
115             }
116         }
117
118         // pump the template with the input form out on the first submission
119
return startProcessing(cms, m_XmlTemplateDocument, "", parameters, m_TemplateSection);
120     }
121
122     private void readInput(CmsObject cms, Hashtable JavaDoc theParameters) throws CmsException {
123         I_CmsSession session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true);
124
125         // reset all member values first
126
m_OldResourceName = null;
127         m_OldResourceType = null;
128         m_UploadResourceName = null;
129         m_UploadResourceContent = null;
130         m_UploadResourceType = null;
131         m_NextStep = null;
132
133         //////////////////////////////////////////////////////////////////////
134

135         // clear session values on the first load
136
if (theParameters.get(CmsWorkplaceDefault.C_PARA_INITIAL) != null) {
137             this.clearSessionValues(session);
138         }
139
140         //////////////////////////////////////////////////////////////////////
141

142         // save the name of the old resource in the session
143
m_OldResourceName = (String JavaDoc)theParameters.get(CmsWorkplaceDefault.C_PARA_RESOURCE);
144         if (m_OldResourceName != null) {
145             session.putValue(CmsWorkplaceDefault.C_PARA_RESOURCE, m_OldResourceName);
146
147             // preserve the type of the old resource depending on the file extension as well
148
Map JavaDoc fileExtensions = OpenCms.getResourceManager().getExtensionMapping();
149             String JavaDoc uploadFilenameExtension = m_OldResourceName.substring(m_OldResourceName.lastIndexOf('.') + 1).toLowerCase();
150
151             if (fileExtensions != null) {
152                 m_OldResourceType = (String JavaDoc)fileExtensions.get(uploadFilenameExtension);
153             }
154
155             if (m_OldResourceType == null) {
156                 // the resource type should be at least the empty string
157
m_OldResourceType = "";
158             }
159
160             session.putValue("OLD_TYPE", m_OldResourceType);
161         } else {
162             m_OldResourceName = (String JavaDoc)session.getValue(CmsWorkplaceDefault.C_PARA_RESOURCE);
163             m_OldResourceType = (String JavaDoc)session.getValue("OLD_TYPE");
164         }
165
166         //////////////////////////////////////////////////////////////////////
167

168         // get the next step parameter that tells this class what to do next...
169
m_NextStep = (String JavaDoc)theParameters.get(CmsReplace.C_PARA_NEXT_STEP);
170
171         //////////////////////////////////////////////////////////////////////
172

173         // get both the name and content of the new resource and save it in the session
174
Enumeration JavaDoc allUploadedFilenames = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getFileNames();
175         while (allUploadedFilenames.hasMoreElements()) {
176             m_UploadResourceName = (String JavaDoc)allUploadedFilenames.nextElement();
177         }
178
179         if (m_UploadResourceName != null) {
180             session.putValue("NEW_RESOURCE", m_UploadResourceName);
181
182             m_UploadResourceContent = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getFile(m_UploadResourceName);
183             session.putValue(CmsWorkplaceDefault.C_PARA_FILECONTENT, m_UploadResourceContent);
184         } else {
185             m_UploadResourceName = (String JavaDoc)session.getValue("NEW_RESOURCE");
186             m_UploadResourceContent = (byte[])session.getValue(CmsWorkplaceDefault.C_PARA_FILECONTENT);
187         }
188         m_XmlTemplateDocument.setData("NEW_RESOURCE", m_UploadResourceName);
189
190         //////////////////////////////////////////////////////////////////////
191

192         // get the type of the new resource
193
m_UploadResourceType = (String JavaDoc)theParameters.get(CmsWorkplaceDefault.C_PARA_NEWTYPE);
194         if (m_UploadResourceType != null) {
195             session.putValue(CmsWorkplaceDefault.C_PARA_NEWTYPE, m_UploadResourceType);
196         } else {
197             m_UploadResourceType = (String JavaDoc)session.getValue(CmsWorkplaceDefault.C_PARA_NEWTYPE);
198         }
199
200         //////////////////////////////////////////////////////////////////////
201

202         if (DEBUG > 0) {
203             System.out.println("\nnext step: " + m_NextStep);
204             System.out.println("old resource: " + m_OldResourceName);
205             System.out.println("old type: " + m_OldResourceType);
206             System.out.println("new resource: " + m_UploadResourceName);
207             System.out.println("new type: " + m_UploadResourceType);
208         }
209     }
210
211     private void initTemplate(CmsObject cms, Hashtable JavaDoc theParameters, String JavaDoc theTemplateFile) throws CmsException {
212         // reset all values first
213
m_XmlTemplateDocument = null;
214         m_TemplateSection = null;
215
216         // create the XML output template file
217
m_XmlTemplateDocument = new CmsXmlWpTemplateFile(cms, theTemplateFile);
218     }
219
220     /**
221      * Removes the values cached in the session.
222      */

223     private void clearSessionValues(I_CmsSession theSession) {
224         // remove all session values
225
theSession.removeValue(CmsWorkplaceDefault.C_PARA_RESOURCE); // name of the old resource
226
theSession.removeValue("OLD_TYPE"); // type of the old resource
227
theSession.removeValue("NEW_RESOURCE"); // name of the new resource
228
theSession.removeValue(CmsWorkplaceDefault.C_PARA_FILECONTENT); // content of the new resource
229
}
230
231     /**
232      * The element cache, programmer's best friend.
233      */

234     public boolean isCacheable(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
235         return false;
236     }
237
238     /**
239      * Prepares for a group of radio buttons the field names, field values and
240      * icon names. Additionally, the index of the checked radio button is set
241      * depending on the file type that we upload.
242      *
243      * @param cms The CmsObject
244      * @param language The langauge definitions
245      * @param names The names of the resource types
246      * @param values The values of the resource types
247      * @param parameters Hashtable of parameters (not used yet)
248      * @param descriptions Description that will be displayed for the new resource
249      * @return the index of the checked radio button, -1 if none is checked
250      * @throws Throws CmsException if something goes wrong.
251      */

252     public int getResources(CmsObject cms, CmsXmlLanguageFile language, Vector JavaDoc resourceTypeIconNames, Vector JavaDoc resourceTypeFieldValues, Vector JavaDoc resourceTypeFieldNames, Hashtable JavaDoc parameters) throws CmsException {
253         CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true);
254         Vector JavaDoc resourceTypeNames = new Vector JavaDoc();
255         Vector JavaDoc resourceTypeValues = new Vector JavaDoc();
256         int checkedRadioIndex = 0;
257
258         CmsXmlWpConfigFile configFile = new CmsXmlWpConfigFile(cms);
259         configFile.getWorkplaceIniData(resourceTypeNames, resourceTypeValues, "RESOURCETYPES", "RESOURCE");
260
261         // robustness, robustness, robustness...
262
if (resourceTypeIconNames == null) {
263             resourceTypeIconNames = new Vector JavaDoc();
264         }
265
266         if (resourceTypeFieldValues == null) {
267             resourceTypeFieldValues = new Vector JavaDoc();
268         }
269
270         if (resourceTypeFieldNames == null) {
271             resourceTypeFieldNames = new Vector JavaDoc();
272         }
273
274         int count = resourceTypeNames.size();
275         for (int i = 0; i < count; i++) {
276             String JavaDoc currentResourceTypeFieldValue = (String JavaDoc)resourceTypeValues.elementAt(i);
277             String JavaDoc currentResourceTypeName = (String JavaDoc)resourceTypeNames.elementAt(i);
278
279             // submit value of the radio button for the current resource type
280
resourceTypeFieldValues.addElement(currentResourceTypeFieldValue);
281
282             // dito, name of the icon
283
resourceTypeIconNames.addElement("file_" + currentResourceTypeName);
284
285             // dito, clear text name of the current resoure type
286
String JavaDoc currentResourceTypeFieldName = null;
287
288             if (language != null) {
289                 currentResourceTypeFieldName = language.getLanguageValue("fileicon." + currentResourceTypeName);
290             } else {
291                 currentResourceTypeFieldName = currentResourceTypeName;
292             }
293
294             resourceTypeFieldNames.addElement(currentResourceTypeFieldName);
295
296             // prove if the current resource type matches the resource type of
297
// the old resource to make a preselection by checking the
298
// right radio button...
299
if (m_OldResourceType.equals(currentResourceTypeName)) {
300                 checkedRadioIndex = i;
301             }
302         }
303
304         return checkedRadioIndex;
305     }
306
307 }
308
Popular Tags