KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/workplace/CmsAdminModuleCreate.java,v $
3 * Date : $Date: 2005/06/27 23:22:07 $
4 * Version: $Revision: 1.7 $
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.CmsExportPoint;
32 import org.opencms.file.CmsObject;
33 import org.opencms.file.CmsPropertyDefinition;
34 import org.opencms.file.CmsVfsResourceAlreadyExistsException;
35 import org.opencms.file.types.CmsResourceTypeFolder;
36 import org.opencms.main.CmsException;
37 import org.opencms.main.CmsLog;
38 import org.opencms.main.OpenCms;
39 import org.opencms.module.CmsModule;
40 import org.opencms.module.CmsModuleVersion;
41 import org.opencms.util.CmsStringUtil;
42 import org.opencms.workplace.CmsWorkplace;
43
44 import com.opencms.core.I_CmsSession;
45 import com.opencms.legacy.CmsLegacyException;
46 import com.opencms.legacy.CmsXmlTemplateLoader;
47 import com.opencms.template.CmsXmlTemplateFile;
48
49 import java.text.SimpleDateFormat JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.util.Date JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.Hashtable JavaDoc;
54 import java.util.List JavaDoc;
55 import java.util.Map JavaDoc;
56 import java.util.Vector JavaDoc;
57
58 /**
59  * Template class for displaying OpenCms workplace administration module create.
60  *
61  * Creation date: (27.10.00 10:28:08)
62  * @author Hanjo Riege
63  *
64  * @deprecated Will not be supported past the OpenCms 6 release.
65  */

66 public class CmsAdminModuleCreate extends CmsWorkplaceDefault {
67     private final static String JavaDoc C_PACKETNAME = "packetname";
68     private final static String JavaDoc C_STEP = "step";
69     private final static String JavaDoc C_VERSION = "version";
70     private final static String JavaDoc C_GROUP = "group";
71     private final static String JavaDoc C_MODULENAME = "modulename";
72     private final static String JavaDoc C_DESCRIPTION = "description";
73     private final static String JavaDoc C_VIEW = "view";
74     private final static String JavaDoc C_ADMINPOINT = "adminpoint";
75     private final static String JavaDoc C_MAINTENANCE = "maintenance";
76     private final static String JavaDoc C_PUBLISHCLASS = "publishclass";
77     private final static String JavaDoc C_AUTHOR = "author";
78     private final static String JavaDoc C_EMAIL = "email";
79     private final static String JavaDoc C_DATE = "date";
80     private final static String JavaDoc C_MODULE_SESSION_DATA = "module_create_data";
81     private final static String JavaDoc C_MODULE_TYPE = "moduletype";
82     private final static String JavaDoc C_EXPORTCLASSES = "exportclasses";
83     private final static String JavaDoc C_EXPORTLIB = "exportlib";
84
85     /**
86      * Gets the content of a defined section in a given template file and its subtemplates
87      * with the given parameters.
88      *
89      * @see #getContent(CmsObject, String, String, Hashtable, String)
90      * @param cms CmsObject Object for accessing system resources.
91      * @param templateFile Filename of the template file.
92      * @param elementName Element name of this template in our parent template.
93      * @param parameters Hashtable with all template class parameters.
94      * @param templateSelector template section that should be processed.
95      */

96     public byte[] getContent(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) throws CmsException {
97         if(CmsLog.getLog(this).isDebugEnabled() && C_DEBUG) {
98             CmsLog.getLog(this).debug("Getting content of element " + ((elementName==null)?"<root>":elementName));
99             CmsLog.getLog(this).debug("Template file is: " + templateFile);
100             CmsLog.getLog(this).debug("Selected template section is: " + ((templateSelector==null)?"<default>":templateSelector));
101         }
102         CmsXmlTemplateFile templateDocument = getOwnTemplateFile(cms, templateFile, elementName, parameters, templateSelector);
103         I_CmsSession session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true);
104         String JavaDoc step = (String JavaDoc)parameters.get(C_STEP);
105         SimpleDateFormat JavaDoc dateFormat = new java.text.SimpleDateFormat JavaDoc("dd.MM.yyyy");
106         if((step == null) || "".equals(step)) {
107             templateDocument.setData(C_PACKETNAME, "");
108             templateDocument.setData(C_VERSION, "1");
109             templateDocument.setData(C_MODULENAME, "");
110             templateDocument.setData(C_DESCRIPTION, "");
111             templateDocument.setData(C_VIEW, "");
112             templateDocument.setData(C_ADMINPOINT, "");
113             templateDocument.setData(C_MAINTENANCE, "");
114             templateDocument.setData(C_PUBLISHCLASS, "");
115             templateDocument.setData(C_AUTHOR, "");
116             templateDocument.setData(C_EMAIL, "");
117             templateDocument.setData(C_MODULE_TYPE, "checked");
118             templateDocument.setData(C_EXPORTCLASSES, "checked");
119             templateDocument.setData(C_EXPORTLIB, "checked");
120             // set the current date:
121
templateDocument.setData(C_DATE, dateFormat.format(new Date JavaDoc()));
122         }else {
123             if("OK".equals(step) || "Ok".equals(step)) {
124                 String JavaDoc modulename = (String JavaDoc)parameters.get(C_PACKETNAME);
125                 String JavaDoc nicename = (String JavaDoc)parameters.get(C_MODULENAME);
126                 String JavaDoc version = (String JavaDoc)parameters.get(C_VERSION);
127                 String JavaDoc description = (String JavaDoc)parameters.get(C_DESCRIPTION);
128                 String JavaDoc maintenance = (String JavaDoc)parameters.get(C_MAINTENANCE);
129                 String JavaDoc publishclass = (String JavaDoc)parameters.get(C_PUBLISHCLASS);
130                 String JavaDoc author = (String JavaDoc)parameters.get(C_AUTHOR);
131                 String JavaDoc email = (String JavaDoc)parameters.get(C_EMAIL);
132                 String JavaDoc createDate = (String JavaDoc)parameters.get(C_DATE);
133                 String JavaDoc moduleType = (String JavaDoc)parameters.get(C_MODULE_TYPE);
134                 String JavaDoc exportClasses = (String JavaDoc)parameters.get(C_EXPORTCLASSES);
135                 String JavaDoc exportLib = (String JavaDoc)parameters.get(C_EXPORTLIB);
136                 String JavaDoc group = "";
137
138                 boolean mustExportClasses = ((exportClasses != null) && exportClasses.equals("checked"));
139                 boolean mustExportLib = ((exportLib != null) && exportLib.equals("checked"));
140                 
141                 boolean moduleExists = OpenCms.getModuleManager().hasModule(modulename);
142                 
143                 CmsModuleVersion moduleVersion;
144                 try {
145                     moduleVersion = new CmsModuleVersion(version);
146                 } catch (IllegalArgumentException JavaDoc e) {
147                     moduleVersion = null;
148                 }
149                 
150                 if((!CmsStringUtil.isValidJavaClassName(modulename)) || moduleExists || (moduleVersion == null)) {
151                     Hashtable JavaDoc sessionData = new Hashtable JavaDoc();
152                     sessionData.put(C_MODULENAME, getStringValue(nicename));
153                     sessionData.put(C_VERSION, getStringValue(version));
154                     sessionData.put(C_DESCRIPTION, getStringValue(description));
155                     sessionData.put(C_VIEW, "");
156                     sessionData.put(C_ADMINPOINT, "");
157                     sessionData.put(C_MAINTENANCE, getStringValue(maintenance));
158                     sessionData.put(C_PUBLISHCLASS, getStringValue(publishclass));
159                     sessionData.put(C_AUTHOR, getStringValue(author));
160                     sessionData.put(C_EMAIL, getStringValue(email));
161                     sessionData.put(C_DATE, getStringValue(createDate));
162                     sessionData.put(C_MODULE_TYPE, getStringValue(moduleType));
163                     session.putValue(C_MODULE_SESSION_DATA, sessionData);
164                     if(moduleExists) {
165                         templateSelector = "errorexists";
166                     }else {
167                         templateSelector = "errornoname";
168                     }
169                 }else {
170                     tryToCreateFolder(cms, CmsWorkplace.VFS_PATH_SYSTEM, "modules");
171                     // create the module (first test if we are in a project including /system/
172
try {
173                         cms.createResource(CmsWorkplace.VFS_PATH_MODULES + modulename, CmsResourceTypeFolder.RESOURCE_TYPE_ID);
174                     }catch(CmsException e) {
175                         if(!(e instanceof CmsVfsResourceAlreadyExistsException)) {
176                             // couldn't create Module
177
templateDocument.setData("details", CmsException.getStackTraceAsString(e));
178                             return startProcessing(cms, templateDocument, elementName, parameters, "errorProject");
179                         }else {
180                             try {
181                                 cms.readFolder(CmsWorkplace.VFS_PATH_MODULES + modulename + "/");
182                             }catch(CmsException ex) {
183                                 // folder exist but is deleted
184
templateDocument.setData("details", "Sorry, you have to publish this Project and create a new one.\n" + CmsException.getStackTraceAsString(e));
185                                 return startProcessing(cms, templateDocument, elementName, parameters, "errorProject");
186                             }
187                         }
188                     }
189                     long createDateLong = 0;
190                     try {
191                         createDateLong = dateFormat.parse(createDate).getTime();
192                     }catch(Exception JavaDoc exc) {
193                         createDateLong = (new Date JavaDoc()).getTime();
194                     }
195
196                     String JavaDoc modulePath = CmsWorkplace.VFS_PATH_MODULES + modulename + "/";
197                     List JavaDoc moduleResources = new ArrayList JavaDoc();
198                     moduleResources.add(modulePath);
199                     
200                     List JavaDoc moduleExportPoints = new ArrayList JavaDoc();
201                     if (mustExportClasses) {
202                         
203                         CmsExportPoint exportPoint =
204                             new CmsExportPoint(
205                                 CmsWorkplace.VFS_PATH_MODULES + modulename + "/classes/",
206                                 "WEB-INF/classes/");
207
208                         moduleExportPoints.add(exportPoint);
209                         
210                         // create the class folder, will get exportet to the "real" file system
211
tryToCreateFolder(cms, modulePath, "classes");
212                         // create all package folders beneth class folder
213
Vector JavaDoc cFolders = new Vector JavaDoc();
214                         String JavaDoc workString = modulename;
215                         while(workString.lastIndexOf('.') > -1) {
216                             cFolders.addElement(workString.substring(workString.lastIndexOf('.') + 1));
217                             workString = workString.substring(0, workString.lastIndexOf('.'));
218                         }
219                         tryToCreateFolder(cms, modulePath+"classes/", workString);
220                         workString = modulePath + "classes/" + workString + "/";
221                         for(int i = cFolders.size() - 1;i >= 0;i--) {
222                             tryToCreateFolder(cms, workString, (String JavaDoc)cFolders.elementAt(i));
223                             workString = workString + (String JavaDoc)cFolders.elementAt(i) + "/";
224                         }
225                     }
226                     
227                     if (mustExportLib) {
228
229                         CmsExportPoint exportPoint =
230                             new CmsExportPoint(
231                                 CmsWorkplace.VFS_PATH_MODULES + modulename + "/lib/",
232                                 "WEB-INF/lib/");
233
234                         moduleExportPoints.add(exportPoint);
235
236                         // create the lib folder, will get exportet to the "real" file system
237
tryToCreateFolder(cms, modulePath, "lib");
238                     }
239                     
240                     List JavaDoc moduleDepedencies = new ArrayList JavaDoc();
241                     Map JavaDoc moduleParameters = new HashMap JavaDoc();
242
243                     // as default don't export any module data
244
cms.writeProperty(modulePath, CmsPropertyDefinition.PROPERTY_EXPORT, "false");
245                     
246                     // create the templates folder
247
tryToCreateFolder(cms, modulePath, CmsWorkplace.VFS_DIR_TEMPLATES);
248                     // create the "default_bodies" folder
249
tryToCreateFolder(cms, modulePath, CmsWorkplace.VFS_DIR_DEFAULTBODIES);
250                         
251                     CmsModule updatedModule =
252                         new CmsModule(
253                             modulename,
254                             group,
255                             nicename,
256                             publishclass,
257                             description,
258                             moduleVersion,
259                             author,
260                             email,
261                             createDateLong,
262                             null,
263                             0L,
264                             moduleDepedencies,
265                             moduleExportPoints,
266                             moduleResources,
267                             moduleParameters);
268
269                     OpenCms.getModuleManager().addModule(cms, updatedModule);
270                     
271                     try {
272                         CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendCmsRedirect(getConfigFile(cms).getWorkplaceAdministrationPath() + "module/index.html");
273                     }catch(Exception JavaDoc e) {
274                         throw new CmsLegacyException("Redirect fails :system/workplace/administration/module/index.html", e);
275                     }
276                     return null;
277                 }
278             }else {
279                 if("fromerrorpage".equals(step)) {
280                     Hashtable JavaDoc sessionData = (Hashtable JavaDoc)session.getValue(C_MODULE_SESSION_DATA);
281                     session.removeValue(C_MODULE_SESSION_DATA);
282                     templateDocument.setData(C_PACKETNAME, "");
283                     templateDocument.setData(C_VERSION, (String JavaDoc)sessionData.get(C_VERSION));
284                     templateDocument.setData(C_MODULENAME, (String JavaDoc)sessionData.get(C_MODULENAME));
285                     templateDocument.setData(C_DESCRIPTION, (String JavaDoc)sessionData.get(C_DESCRIPTION));
286                     templateDocument.setData(C_VIEW, "");
287                     templateDocument.setData(C_ADMINPOINT, "");
288                     templateDocument.setData(C_MAINTENANCE, (String JavaDoc)sessionData.get(C_MAINTENANCE));
289                     templateDocument.setData(C_PUBLISHCLASS, (String JavaDoc)sessionData.get(C_PUBLISHCLASS));
290                     templateDocument.setData(C_AUTHOR, (String JavaDoc)sessionData.get(C_AUTHOR));
291                     templateDocument.setData(C_EMAIL, (String JavaDoc)sessionData.get(C_EMAIL));
292                     templateDocument.setData(C_DATE, (String JavaDoc)sessionData.get(C_DATE));
293                     templateDocument.setData(C_MODULE_TYPE, (String JavaDoc)sessionData.get(C_MODULE_TYPE));
294                     templateSelector = "";
295                 }
296             }
297         }
298
299         // Now load the template file and start the processing
300
return startProcessing(cms, templateDocument, elementName, parameters, templateSelector);
301     }
302
303     /**
304      * returns the String or "" if it is null.
305      * Creation date: (29.10.00 16:05:38)
306      * @return java.lang.String
307      * @param param java.lang.String
308      */

309     private String JavaDoc getStringValue(String JavaDoc param) {
310         if(param == null) {
311             return "";
312         }
313         return param;
314     }
315
316     /**
317      * Indicates if the results of this class are cacheable.
318      *
319      * @param cms CmsObject Object for accessing system resources
320      * @param templateFile Filename of the template file
321      * @param elementName Element name of this template in our parent template.
322      * @param parameters Hashtable with all template class parameters.
323      * @param templateSelector template section that should be processed.
324      * @return <EM>true</EM> if cacheable, <EM>false</EM> otherwise.
325      */

326     public boolean isCacheable(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
327         return false;
328     }
329
330     /**
331      * Insert the method's description here.
332      * Creation date: (03.11.00 08:23:13)
333      * @param param org.opencms.file.CmsObject
334      * @param folder java.lang.String
335      * @param newFolder java.lang.String
336      */

337     private void tryToCreateFolder(CmsObject cms, String JavaDoc folder, String JavaDoc newFolder) {
338         try {
339             cms.createResource(folder + newFolder, CmsResourceTypeFolder.RESOURCE_TYPE_ID);
340         }catch(Exception JavaDoc e) {
341         }
342     }
343 }
344
Popular Tags