KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/workplace/CmsAdminDownGalleries.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.db.CmsImportFolder;
32 import org.opencms.file.CmsFolder;
33 import org.opencms.file.CmsObject;
34 import org.opencms.file.CmsPropertyDefinition;
35 import org.opencms.file.CmsResource;
36 import org.opencms.file.types.CmsResourceTypeFolder;
37 import org.opencms.file.types.CmsResourceTypeImage;
38 import org.opencms.main.CmsException;
39 import org.opencms.main.OpenCms;
40 import org.opencms.workplace.CmsWorkplace;
41
42 import com.opencms.core.I_CmsSession;
43 import com.opencms.legacy.CmsLegacyException;
44 import com.opencms.legacy.CmsXmlTemplateLoader;
45
46 import java.util.ArrayList JavaDoc;
47 import java.util.Collections JavaDoc;
48 import java.util.Enumeration JavaDoc;
49 import java.util.Hashtable JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Map JavaDoc;
52 import java.util.Vector JavaDoc;
53
54 /**
55  * Template Class for administration of download galleries
56  * <p>
57  *
58  * @author Mario Stanke
59  * @version $Revision: 1.5 $ $Date: 2005/06/27 23:22:07 $
60  * @see com.opencms.workplace.CmsXmlWpTemplateFile
61  *
62  * @deprecated Will not be supported past the OpenCms 6 release.
63  */

64
65 public class CmsAdminDownGalleries extends CmsAdminGallery {
66
67     /** Vector containing all names of the radiobuttons */
68     private Vector JavaDoc m_names = null;
69
70     /** Vector containing all links attached to the radiobuttons */
71     private Vector JavaDoc m_values = null;
72     
73     protected static final String JavaDoc C_PARAM_CANCEL = "cancel";
74     
75     /**
76      * This method must return the path to the gallery root folder.<p>
77      *
78      * The root folder names are usually defined as constants in
79      * the I_CmsWpConstants interface.
80      *
81      * @return The path to the gallery root folder
82      */

83     public String JavaDoc getGalleryPath() {
84         return CmsWorkplaceDefault.C_VFS_GALLERY_DOWNLOAD;
85     }
86     
87     /**
88      * This method must return the path to the gallery icon.<p>
89      *
90      * The gallery image is displayed in the list of available galleries.
91      *
92      * @param cms The current CmsObject
93      * @return The path to the gallery icon
94      * @throws CmsException In case of problem accessing system resources
95      */

96     public String JavaDoc getGalleryIconPath(CmsObject cms) throws CmsException {
97         CmsXmlWpConfigFile config = this.getConfigFile(cms);
98         return config.getWpPicturePath() + "ic_file_download.gif";
99     }
100         
101     /**
102      * Default XMLTemplate method called to build the output,
103      *
104      * @param cms CmsObject for accessing system resources
105      * @param templateFile Filename of the template file
106      * @param elementName Element name of this template in our parent template
107      * @param parameters Hashtable with all template class parameters
108      * @param templateSelector template section that should be processed
109      *
110      * @return A HTML String converted to bytes that contains the generated output
111      */

112     public byte[] getContent(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName,
113             Hashtable JavaDoc parameters, String JavaDoc templateSelector) throws CmsException {
114                 
115         I_CmsSession session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true);
116         CmsXmlWpTemplateFile xmlTemplateDocument = (CmsXmlWpTemplateFile)getOwnTemplateFile(cms,
117                 templateFile, elementName, parameters, templateSelector);
118
119         // get the URL to which we need to return when we're done
120
String JavaDoc lasturl = getLastUrl(cms, parameters);
121         
122         // check if this is the inital call to the page
123
getInitial(session, parameters) ;
124                 
125         // Get the folder for the gallery
126
String JavaDoc foldername = getGalleryPath(cms, session, parameters);
127         CmsFolder thefolder = cms.readFolder(foldername);
128                         
129         // Check if we must redirect to head_1
130
if(foldername.equals(CmsWorkplaceDefault.C_VFS_GALLERY_DOWNLOAD) && templateFile.endsWith("administration_head_downgalleries2")) {
131             // we are in the wrong head - use the first one
132
xmlTemplateDocument = (CmsXmlWpTemplateFile)getOwnTemplateFile(cms, CmsWorkplace.VFS_PATH_WORKPLACE + "administration/downloadgallery/administration_head_downgalleries1", elementName, parameters, templateSelector);
133         }
134
135         // Check if we must redirect to head_2
136
try {
137             String JavaDoc parent = CmsResource.getParentFolder(cms.getSitePath(thefolder));
138             if(foldername.startsWith(CmsWorkplaceDefault.C_VFS_GALLERY_DOWNLOAD) && (parent.equals(CmsWorkplaceDefault.C_VFS_GALLERY_PICS)) && templateFile.endsWith("administration_head_downgalleries1")) {
139                 // we are in the wrong head - use the second one
140
xmlTemplateDocument = (CmsXmlWpTemplateFile)getOwnTemplateFile(cms, CmsWorkplace.VFS_PATH_WORKPLACE + "administration/htmlgallery/administration_head_downgalleries2", elementName, parameters, templateSelector);
141             }
142         }
143         catch(Exception JavaDoc e) {}
144                 
145         // Now read further parameters
146
String JavaDoc action = (String JavaDoc)parameters.get("action");
147         String JavaDoc unzip = (String JavaDoc) parameters.get("unzip");
148         String JavaDoc nofolder = (String JavaDoc) parameters.get("NOFOLDER");
149         //get the filetype
150
String JavaDoc newtype = (String JavaDoc)parameters.get(CmsWorkplaceDefault.C_PARA_NEWTYPE);
151         if(newtype != null) {
152             session.putValue(CmsWorkplaceDefault.C_PARA_NEWTYPE, newtype);
153         } else {
154             newtype = (String JavaDoc)session.getValue(CmsWorkplaceDefault.C_PARA_NEWTYPE);
155         }
156         String JavaDoc newname = (String JavaDoc)parameters.get(CmsWorkplaceDefault.C_PARA_NAME);
157         String JavaDoc title = (String JavaDoc)parameters.get("TITLE"); // both for gallery and upload file
158
String JavaDoc step = (String JavaDoc)parameters.get("step");
159         
160         String JavaDoc filename = null;
161         if(foldername == null) {
162             foldername = "";
163         }
164         if("new".equals(action)) {
165             String JavaDoc galleryname = (String JavaDoc)parameters.get("NAME");
166 // String group = (String)parameters.get("GROUP");
167
// boolean read = parameters.get("READ") != null;
168
// boolean write = parameters.get("WRITE") != null;
169
try {
170
171                 // create the folder
172

173                 // get the path from the workplace.ini
174
String JavaDoc superfolder = getConfigFile(cms).getDownGalleryPath();
175                 CmsResource folder = cms.createResource(superfolder + galleryname, CmsResourceTypeFolder.RESOURCE_TYPE_ID);
176                 cms.writeProperty(cms.getSitePath(folder), CmsPropertyDefinition.PROPERTY_TITLE, title);
177                 // TODO: check how to set the appropriate access using acl
178
/*
179                 cms.chgrp(cms.readAbsolutePath(folder), group);
180                 int flag = folder.getAccessFlags();
181
182                 // set the access rights for 'other' users
183                 if(read != ((flag & C_ACCESS_PUBLIC_READ) != 0)) {
184                     flag ^= C_ACCESS_PUBLIC_READ;
185                 }
186                 if(write != ((flag & C_ACCESS_PUBLIC_WRITE) != 0)) {
187                     flag ^= C_ACCESS_PUBLIC_WRITE;
188                 }
189                 if((flag & C_ACCESS_GROUP_READ) == 0){
190                     flag ^= C_ACCESS_GROUP_READ;
191                 }
192                 if((flag & C_ACCESS_GROUP_WRITE) == 0){
193                     flag ^= C_ACCESS_GROUP_WRITE;
194                 }
195                 if((flag & C_ACCESS_GROUP_VISIBLE) == 0){
196                     flag ^= C_ACCESS_GROUP_VISIBLE;
197                 }
198                 if((flag & C_PERMISSION_READ ) == 0){
199                     flag ^= C_PERMISSION_READ;
200                 }
201                 if((flag & C_PERMISSION_WRITE) == 0){
202                     flag ^= C_PERMISSION_WRITE;
203                 }
204                 if((flag & C_PERMISSION_VIEW) == 0){
205                     flag ^= C_PERMISSION_VIEW;
206                 }
207                 if((flag & C_ACCESS_PUBLIC_VISIBLE) == 0){
208                     flag ^= C_ACCESS_PUBLIC_VISIBLE;
209                 }
210                cms.chmod(cms.readAbsolutePath(folder), flag);
211                */

212                cms.unlockResource(cms.getSitePath(folder));
213             }
214             catch(CmsException ex) {
215                 xmlTemplateDocument.setData("ERRORDETAILS", CmsException.getStackTraceAsString(ex));
216                 templateSelector = "error";
217             }
218         }
219         else {
220             if("upload".equals(action)) {
221
222                 // get filename and file content if available
223
byte[] filecontent = new byte[0];
224
225                 // get the filename
226
Enumeration JavaDoc files = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getFileNames();
227                 while(files.hasMoreElements()) {
228                     filename = (String JavaDoc)files.nextElement();
229                 }
230                 if(filename != null) {
231                     session.putValue(CmsWorkplaceDefault.C_PARA_RESOURCE, filename);
232                 }
233                 filename = (String JavaDoc)session.getValue(CmsWorkplaceDefault.C_PARA_RESOURCE);
234
235                 // get the filecontent
236
if(filename != null) {
237                     filecontent = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getFile(filename);
238                 }
239                 if(filecontent != null) {
240                     session.putValue(CmsWorkplaceDefault.C_PARA_FILECONTENT, filecontent);
241                 }
242                 filecontent = (byte[])session.getValue(CmsWorkplaceDefault.C_PARA_FILECONTENT);
243                 if("0".equals(step)) {
244                     templateSelector = "";
245                 }
246                 else {
247                     if("1".equals(step)) {
248
249                         // display the select filetype screen
250
if(filename != null) {
251
252                             // check if the file size is 0
253
if(filecontent.length == 0) {
254                                 templateSelector = "error";
255                                 xmlTemplateDocument.setData("details", filename);
256                             }
257                             else {
258                                 if(unzip != null) {
259                                     // try to unzip the file here ...
260
boolean noSubFolder = (nofolder != null ? true : false);
261                                     CmsImportFolder zip = new CmsImportFolder(
262                                         filecontent, foldername, cms, noSubFolder);
263                                     if( zip.isValidZipFile() ) {
264
265                                         // remove the values form the session
266
session.removeValue(CmsWorkplaceDefault.C_PARA_RESOURCE);
267                                         session.removeValue(CmsWorkplaceDefault.C_PARA_FILECONTENT);
268                                         session.removeValue(CmsWorkplaceDefault.C_PARA_NEWTYPE);
269                                         // return to the filelist
270
try {
271                                             //CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendCmsRedirect( getConfigFile(cms).getWorkplaceActionPath()+C_WP_EXPLORER_FILELIST);
272
if((lasturl != null) && (lasturl != "")) {
273                                                 CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendRedirect(lasturl);
274                                             }
275                                             else {
276                                                 CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendCmsRedirect(
277                                                     getConfigFile(cms).getWorkplaceActionPath()
278                                                     + CmsWorkplaceAction.getExplorerFileUri(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()));
279                                             }
280                                         } catch(Exception JavaDoc ex) {
281                                             throw new CmsLegacyException(
282                                                 "Redirect fails :" + getConfigFile(cms).getWorkplaceActionPath()
283                                                 + CmsWorkplaceAction.getExplorerFileUri(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()), ex);
284                                         }
285                                         return null;
286                                     }
287                                 } // else, zip was not valid, so continue ...
288
templateSelector = "step1";
289                             }
290                         }
291                     }
292                     else {
293                         if("2".equals(step)) {
294
295                             // get the selected resource and check if it is an image
296
int type = OpenCms.getResourceManager().getResourceType(newtype).getTypeId();
297                             if(newtype.equals(CmsResourceTypeImage.getStaticTypeName())) {
298
299                                 // the file type is an image
300
templateSelector = "image";
301                                 xmlTemplateDocument.setData("MIME", filename);
302                                 xmlTemplateDocument.setData("SIZE", "Not yet available");
303                                 xmlTemplateDocument.setData("FILESIZE", new Integer JavaDoc(filecontent.length).toString() + " Bytes");
304                             }
305                             else {
306
307                                 // create the new file.
308
// todo: error handling if file already exits
309

310                                 try{
311                                     cms.createResource(foldername + filename, type, filecontent, Collections.EMPTY_LIST);
312                                 }catch(CmsException e){
313                                     // remove the values form the session
314
session.removeValue(CmsWorkplaceDefault.C_PARA_RESOURCE);
315                                     session.removeValue(CmsWorkplaceDefault.C_PARA_FILECONTENT);
316                                     session.removeValue(CmsWorkplaceDefault.C_PARA_NEWTYPE);
317                                     xmlTemplateDocument.setData("details", CmsException.getStackTraceAsString(e));
318                                     return startProcessing(cms, xmlTemplateDocument, "", parameters, "error2");
319
320                                 }
321                                 // remove the values form the session
322
session.removeValue(CmsWorkplaceDefault.C_PARA_RESOURCE);
323                                 session.removeValue(CmsWorkplaceDefault.C_PARA_FILECONTENT);
324                                 session.removeValue(CmsWorkplaceDefault.C_PARA_NEWTYPE);
325
326                                 // return to the filelist
327
try {
328
329                                     //CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendCmsRedirect( getConfigFile(cms).getWorkplaceActionPath()+C_WP_EXPLORER_FILELIST);
330
if((lasturl != null) && (lasturl != "")) {
331                                         CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendRedirect(lasturl);
332                                     }
333                                     else {
334                                         CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendCmsRedirect(getConfigFile(cms).getWorkplaceActionPath() + CmsWorkplaceAction.getExplorerFileUri(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()));
335                                     }
336                                 }
337                                 catch(Exception JavaDoc ex) {
338                                     throw new CmsLegacyException("Redirect fails :" + getConfigFile(cms).getWorkplaceActionPath() + CmsWorkplaceAction.getExplorerFileUri(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()), ex);
339                                 }
340                                 return null;
341                             }
342                         }
343                         else {
344                             if("3".equals(step)) {
345
346                                 // get the data from the special image upload dialog
347

348                                 // check if a new filename is given
349
if(newname != null) {
350                                     filename = newname;
351                                 }
352
353                                 // create the new file.
354

355                                 // todo: error handling if file already exits
356
int type = OpenCms.getResourceManager().getResourceType(newtype).getTypeId();
357
358                                 List JavaDoc properties = null;
359                                 if (title != null) {
360                                     properties = new ArrayList JavaDoc();
361                                     properties.add(new org.opencms.file.CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, title, null));
362                                 } else {
363                                     properties = Collections.EMPTY_LIST;
364                                 }
365                                 
366                                 cms.createResource(foldername + filename, type, filecontent, properties);
367
368                                 // remove the values form the session
369
session.removeValue(CmsWorkplaceDefault.C_PARA_RESOURCE);
370                                 session.removeValue(CmsWorkplaceDefault.C_PARA_FILECONTENT);
371                                 session.removeValue(CmsWorkplaceDefault.C_PARA_NEWTYPE);
372                                 session.removeValue("lasturl");
373
374                                 // return to the filelist
375
try {
376                                     if((lasturl != null) && (lasturl != "")) {
377                                         CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendRedirect(lasturl);
378                                     }
379                                     else {
380                                         CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendCmsRedirect(getConfigFile(cms).getWorkplaceActionPath() + CmsWorkplaceAction.getExplorerFileUri(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()));
381                                     }
382                                 }
383                                 catch(Exception JavaDoc ex) {
384                                     throw new CmsLegacyException("Redirect fails :" + getConfigFile(cms).getWorkplaceActionPath() + CmsWorkplaceAction.getExplorerFileUri(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()), ex);
385                                 }
386                                 return null;
387                             }
388                         }
389                     }
390                 }
391             }
392         }
393  
394         xmlTemplateDocument.setData("link_value", foldername);
395         xmlTemplateDocument.setData("lasturl", lasturl);
396         xmlTemplateDocument.setData("galleryRootFolder", CmsWorkplaceDefault.C_VFS_GALLERY_DOWNLOAD);
397         
398         if(filename != null) {
399             xmlTemplateDocument.setData("FILENAME", filename);
400         }
401
402         // Finally start the processing
403
return startProcessing(cms, xmlTemplateDocument, elementName, parameters, templateSelector);
404     }
405
406     /**
407      * Gets the resources displayed in the Radiobutton group on the chtype dialog.
408      *
409      * @param cms The CmsObject.
410      * @param lang The langauge definitions.
411      * @param names The names of the new rescources.
412      * @param values The links that are connected with each resource.
413      * @param parameters Hashtable of parameters (not used yet).
414      * @param descriptions Description that will be displayed for the new resource.
415      * @return The vectors names and values are filled with the information found in the workplace.ini.
416      * @return the number of the preselected item, -1 if none preselected
417      * @throws Throws CmsException if something goes wrong.
418      */

419     public int getResources(CmsObject cms, CmsXmlLanguageFile lang, Vector JavaDoc names, Vector JavaDoc values, Vector JavaDoc descriptions, Hashtable JavaDoc parameters) throws CmsException {
420         I_CmsSession session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true);
421         String JavaDoc filename = (String JavaDoc)session.getValue(CmsWorkplaceDefault.C_PARA_RESOURCE);
422         String JavaDoc suffix = filename.substring(filename.lastIndexOf('.') + 1);
423         suffix = suffix.toLowerCase(); // file extension of filename
424

425         // read the known file extensions from the database
426
Map JavaDoc extensions = OpenCms.getResourceManager().getExtensionMapping();
427         String JavaDoc resType = new String JavaDoc();
428         if(extensions != null) {
429             resType = (String JavaDoc)extensions.get(suffix);
430         }
431         if(resType == null) {
432             resType = "";
433         }
434         int ret = 0;
435
436         // Check if the list of available resources is not yet loaded from the workplace.ini
437
if(m_names == null || m_values == null) {
438             m_names = new Vector JavaDoc();
439             m_values = new Vector JavaDoc();
440             CmsXmlWpConfigFile configFile = new CmsXmlWpConfigFile(cms);
441             configFile.getWorkplaceIniData(m_names, m_values, "RESOURCETYPES", "RESOURCE");
442         }
443
444         // Check if the temportary name and value vectors are not initialized, create
445
// them if nescessary.
446
if(names == null) {
447             names = new Vector JavaDoc();
448         }
449         if(values == null) {
450             values = new Vector JavaDoc();
451         }
452         if(descriptions == null) {
453             descriptions = new Vector JavaDoc();
454         }
455
456         // OK. Now m_names and m_values contain all available
457
// resource information.
458
// Loop through the vectors and fill the result vectors.
459
int numViews = m_names.size();
460         for(int i = 0;i < numViews;i++) {
461             String JavaDoc loopValue = (String JavaDoc)m_values.elementAt(i);
462             String JavaDoc loopName = (String JavaDoc)m_names.elementAt(i);
463             values.addElement(loopValue);
464             names.addElement("file_" + loopName);
465             String JavaDoc descr;
466             if(lang != null) {
467                 descr = lang.getLanguageValue("fileicon." + loopName);
468             }
469             else {
470                 descr = loopName;
471             }
472             descriptions.addElement(descr);
473             if(resType.equals(loopName)) {
474
475                 // known file extension
476
ret = i;
477             }
478         }
479         return ret;
480     }
481 }
482
Popular Tags