KickJava   Java API By Example, From Geeks To Geeks.

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


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

60
61 public class CmsAdminPicGalleries extends CmsAdminGallery {
62     
63     /**
64      * This method must be implemented by all galleries.
65      * It must return the path to the gallery root folder.<p>
66      */

67     public String JavaDoc getGalleryPath() {
68         return CmsWorkplaceDefault.C_VFS_GALLERY_PICS;
69     }
70     
71     /**
72      * This method must return the path to the gallery icon.<p>
73      *
74      * The gallery image is displayed in the list of available galleries.
75      *
76      * @param cms The current CmsObject
77      * @return The path to the gallery icon
78      * @throws CmsException In case of problem accessing system resources
79      */

80     public String JavaDoc getGalleryIconPath(CmsObject cms) throws CmsException {
81         CmsXmlWpConfigFile config = this.getConfigFile(cms);
82         return config.getWpPicturePath() + "ic_file_picgallery.gif";
83     }
84     
85     /**
86      * Default XMLTemplate method called to build the output,
87      *
88      * @param cms CmsObject for accessing system resources
89      * @param templateFile Filename of the template file
90      * @param elementName Element name of this template in our parent template
91      * @param parameters Hashtable with all template class parameters
92      * @param templateSelector template section that should be processed
93      *
94      * @return A HTML String converted to bytes that contains the generated output
95      */

96     public byte[] getContent(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName,
97             Hashtable JavaDoc parameters, String JavaDoc templateSelector) throws CmsException {
98                 
99         I_CmsSession session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true);
100         CmsXmlWpTemplateFile xmlTemplateDocument = (CmsXmlWpTemplateFile)getOwnTemplateFile(cms,
101                 templateFile, elementName, parameters, templateSelector);
102                 
103         // Get the URL to which we need to return when we're done
104
String JavaDoc lasturl = getLastUrl(cms, parameters);
105         
106         // Check if this is the inital call to the page
107
getInitial(session, parameters) ;
108                 
109         // Get the folder for the gallery
110
String JavaDoc foldername = getGalleryPath(cms, session, parameters);
111         CmsFolder thefolder = cms.readFolder(foldername);
112         
113         // get the file size upload limitation value (value is in kB)
114
int maxFileSize = OpenCms.getWorkplaceManager().getFileMaxUploadSize();
115         // check if current user belongs to Admin group, if so no file upload limit
116
if ((maxFileSize <= 0) || cms.userInGroup(cms.getRequestContext().currentUser().getName(), OpenCms.getDefaultUsers().getGroupAdministrators())) {
117             maxFileSize = -1;
118             xmlTemplateDocument.setData("limitation", "");
119         }
120         else {
121             xmlTemplateDocument.setData("maxfilesize", "" + maxFileSize);
122             try {
123                 String JavaDoc limitation = xmlTemplateDocument.getProcessedDataValue("filesize_limited");
124                 xmlTemplateDocument.setData("limitation", limitation);
125             } catch (CmsException e) {
126                 xmlTemplateDocument.setData("limitation", "");
127             }
128         }
129                
130         // Check if we must redirect to head_1
131
if(foldername.equals(CmsWorkplaceDefault.C_VFS_GALLERY_PICS) && templateFile.endsWith("administration_head_picgalleries2")) {
132             // we are in the wrong head - use the first one
133
xmlTemplateDocument = (CmsXmlWpTemplateFile)getOwnTemplateFile(cms, CmsWorkplace.VFS_PATH_WORKPLACE + "administration/picgallery/administration_head_picgalleries1", elementName, parameters, templateSelector);
134         }
135
136         // Check if we must redirect to head_2
137
try {
138             String JavaDoc parent = CmsResource.getParentFolder(cms.getSitePath(thefolder));
139             if(foldername.startsWith(CmsWorkplaceDefault.C_VFS_GALLERY_PICS) && (parent.equals(CmsWorkplaceDefault.C_VFS_GALLERY_PICS)) && templateFile.endsWith("administration_head_picgalleries1")) {
140                 // we are in the wrong head - use the second one
141
xmlTemplateDocument = (CmsXmlWpTemplateFile)getOwnTemplateFile(cms, CmsWorkplace.VFS_PATH_WORKPLACE + "administration/htmlgallery/administration_head_picgalleries2", elementName, parameters, templateSelector);
142             }
143         }
144         catch(Exception JavaDoc e) {}
145         
146         // Now read further parameters
147
String JavaDoc action = (String JavaDoc)parameters.get("action");
148         String JavaDoc unzip = (String JavaDoc) parameters.get("unzip");
149         String JavaDoc newname = (String JavaDoc)parameters.get(CmsWorkplaceDefault.C_PARA_NAME);
150         String JavaDoc title = (String JavaDoc)parameters.get("TITLE"); // both for gallery and upload file
151
String JavaDoc step = (String JavaDoc)parameters.get("step");
152         String JavaDoc imagedescription = (String JavaDoc)parameters.get("DESCRIPTION");
153         
154         if(foldername == null) {
155             foldername = "";
156         }
157         if("new".equals(action)) {
158             String JavaDoc galleryname = (String JavaDoc)parameters.get("NAME");
159             String JavaDoc group = (String JavaDoc)parameters.get("GROUP");
160             if(galleryname != null && group != null && galleryname != "" && group != "") {
161 // boolean read = parameters.get("READ") != null;
162
// boolean write = parameters.get("WRITE") != null;
163
try {
164
165                     // create the folder
166

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

208                     cms.unlockResource(cms.getSitePath(folder));
209                 }
210                 catch(CmsException ex) {
211                     xmlTemplateDocument.setData("ERRORDETAILS", CmsException.getStackTraceAsString(ex));
212                     templateSelector = "error";
213                 }
214             }
215             else {
216                 templateSelector = "datamissing";
217             }
218         }
219         else {
220             
221             if("upload".equals(action)) {
222
223                 // get filename and file content if available
224
String JavaDoc filename = null;
225                 byte[] filecontent = new byte[0];
226
227                 // get the filename
228
Enumeration JavaDoc files = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getFileNames();
229                 while(files.hasMoreElements()) {
230                     filename = (String JavaDoc)files.nextElement();
231                 }
232                 if(filename != null) {
233                     session.putValue(CmsWorkplaceDefault.C_PARA_RESOURCE, filename);
234                 }
235                 filename = (String JavaDoc)session.getValue(CmsWorkplaceDefault.C_PARA_RESOURCE);
236
237                 // get the filecontent
238
if(filename != null) {
239                     filecontent = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getFile(filename);
240                 }
241                 if(filecontent != null) {
242                     session.putValue(CmsWorkplaceDefault.C_PARA_FILECONTENT, filecontent);
243                 }
244                 filecontent = (byte[])session.getValue(CmsWorkplaceDefault.C_PARA_FILECONTENT);
245                 if("0".equals(step)) {
246                     templateSelector = "";
247                 }
248                 else {
249                     if("1".equals(step)) {
250
251                         // display the select filetype screen
252
if(filename != null) {
253
254                             // check if the file size is 0
255
if(filecontent.length == 0) {
256                                 templateSelector = "error";
257                                 xmlTemplateDocument.setData("details", filename);
258                             }
259                             
260                             // check if the file size is larger than the maximum allowed upload size
261
else if ((maxFileSize > 0) && (filecontent.length > (maxFileSize * 1024))) {
262                                 templateSelector = "errorfilesize";
263                                 xmlTemplateDocument.setData("details", filename+": "+(filecontent.length/1024)+" kb, max. "+maxFileSize+" kb.");
264                             }
265                                              
266                             else {
267                                 if(unzip != null) {
268                                     // try to unzip the file here ...
269
CmsImportFolder zip = new CmsImportFolder(
270                                         filecontent, foldername, cms, true);
271                                     if( zip.isValidZipFile() ) {
272
273                                     // remove the values form the session
274
session.removeValue(CmsWorkplaceDefault.C_PARA_RESOURCE);
275                                     session.removeValue(CmsWorkplaceDefault.C_PARA_FILECONTENT);
276                                     session.removeValue(CmsWorkplaceDefault.C_PARA_NEWTYPE);
277                                     session.removeValue("unzip");
278                                     // return to the filelist
279
try {
280                                         if((lasturl != null) && (lasturl != "")) {
281                                             CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendRedirect(lasturl);
282                                         }
283                                         else {
284                                             CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendCmsRedirect(
285                                                 getConfigFile(cms).getWorkplaceActionPath() + CmsWorkplaceAction.getExplorerFileUri(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()));
286                                         }
287                                     } catch(Exception JavaDoc ex) {
288                                         throw new CmsLegacyException(
289                                             "Redirect fails :" + getConfigFile(cms).getWorkplaceActionPath()
290                                             + CmsWorkplaceAction.getExplorerFileUri(CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest()), ex);
291                                     }
292                                     return null;
293                                     }
294                                 } // else, zip was not valid, so continue ...
295
xmlTemplateDocument.setData("MIME", filename);
296                                 xmlTemplateDocument.setData("SIZE", "Not yet available");
297                                 xmlTemplateDocument.setData("FILESIZE",
298                                         new Integer JavaDoc(filecontent.length).toString() + " Bytes");
299                                 xmlTemplateDocument.setData("FILENAME", filename);
300                                 xmlTemplateDocument.setData("IMAGEDESCRIPTION",imagedescription);
301                                 templateSelector = "step1";
302                             }
303                         }
304                     }
305                     else {
306                         if("2".equals(step)) {
307
308                             // check if a new filename is given
309
if(newname != null) {
310                                 filename = newname;
311                             }
312                             try {
313                                 CmsFile file = (CmsFile)cms.createResource(foldername + filename, CmsResourceTypeImage.getStaticTypeId(), filecontent, null);
314                                 if(title != null) {
315                                     String JavaDoc filepath = cms.getSitePath(file);
316                                     cms.writeProperty(filepath, CmsPropertyDefinition.PROPERTY_TITLE, title);
317                                 }
318                                 if(imagedescription != null) {
319                                     String JavaDoc filepath = cms.getSitePath(file);
320                                     cms.writeProperty(filepath, CmsPropertyDefinition.PROPERTY_DESCRIPTION, imagedescription);
321                                 }
322                             }
323                             catch(CmsException ex) {
324                                 xmlTemplateDocument.setData("details", CmsException.getStackTraceAsString(ex));
325                                 templateSelector = "error";
326                                 xmlTemplateDocument.setData("link_value", foldername);
327                                 xmlTemplateDocument.setData("lasturl", lasturl);
328                                 return startProcessing(cms, xmlTemplateDocument, elementName,
329                                         parameters, templateSelector);
330                             }
331                             try {
332                                 CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendRedirect(lasturl);
333                             }
334                             catch(Exception JavaDoc ex) {
335                                 throw new CmsLegacyException("Redirect fails :" + getConfigFile(cms).getWorkplaceActionPath() +
336                                         lasturl, ex);
337                             }
338                             return null;
339                         }
340                     }
341                 }
342             }
343         }
344         
345         xmlTemplateDocument.setData("link_value", foldername);
346         xmlTemplateDocument.setData("lasturl", lasturl);
347         xmlTemplateDocument.setData("galleryRootFolder", CmsWorkplaceDefault.C_VFS_GALLERY_PICS);
348
349         // Finally start the processing
350
return startProcessing(cms, xmlTemplateDocument, elementName, parameters,
351                 templateSelector);
352     }
353 }
354
Popular Tags