KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > modules > CmsModulesEditBase


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/modules/CmsModulesEditBase.java,v $
3  * Date : $Date: 2006/03/27 14:52:53 $
4  * Version: $Revision: 1.12 $
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.tools.modules;
33
34 import org.opencms.db.CmsExportPoint;
35 import org.opencms.file.types.CmsResourceTypeFolder;
36 import org.opencms.jsp.CmsJspActionElement;
37 import org.opencms.main.CmsException;
38 import org.opencms.main.OpenCms;
39 import org.opencms.module.CmsModule;
40 import org.opencms.util.CmsStringUtil;
41 import org.opencms.workplace.CmsDialog;
42 import org.opencms.workplace.CmsWidgetDialog;
43 import org.opencms.workplace.CmsWorkplace;
44 import org.opencms.workplace.CmsWorkplaceSettings;
45
46 import java.util.List JavaDoc;
47 import java.util.Map JavaDoc;
48 import java.util.Set JavaDoc;
49 import java.util.StringTokenizer JavaDoc;
50
51 import javax.servlet.http.HttpServletRequest JavaDoc;
52 import javax.servlet.http.HttpServletResponse JavaDoc;
53 import javax.servlet.jsp.PageContext JavaDoc;
54
55 /**
56  * Base class to edit an exiting module.<p>
57  *
58  * @author Michael Emmerich
59  *
60  * @version $Revision: 1.12 $
61  *
62  * @since 6.0.0
63  */

64 public class CmsModulesEditBase extends CmsWidgetDialog {
65
66     /** The dialog type. */
67     public static final String JavaDoc DIALOG_TYPE = "ModulesEdit";
68
69     /** Defines which pages are valid for this dialog. */
70     public static final String JavaDoc[] PAGES = {"page1"};
71
72     /** Classes folder within the module. */
73     public static final String JavaDoc PATH_CLASSES = "classes/";
74
75     /** Elements folder within the module. */
76     public static final String JavaDoc PATH_ELEMENTS = "elements/";
77
78     /** Lib folder within the module. */
79     public static final String JavaDoc PATH_LIB = "lib/";
80
81     /** Resources folder within the module. */
82     public static final String JavaDoc PATH_RESOURCES = "resources/";
83
84     /** Template folder within the module. */
85     public static final String JavaDoc PATH_TEMPLATES = "templates/";
86
87     /** The module object that is edited on this dialog. */
88     protected CmsModule m_module;
89
90     /** Modulename. */
91     protected String JavaDoc m_paramModule;
92
93     /**
94      * Public constructor with JSP action element.<p>
95      *
96      * @param jsp an initialized JSP action element
97      */

98     public CmsModulesEditBase(CmsJspActionElement jsp) {
99
100         super(jsp);
101     }
102
103     /**
104      * Public constructor with JSP variables.<p>
105      *
106      * @param context the JSP page context
107      * @param req the JSP request
108      * @param res the JSP response
109      */

110     public CmsModulesEditBase(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
111
112         this(new CmsJspActionElement(context, req, res));
113     }
114
115     /**
116      * Commits the edited module.<p>
117      */

118     public void actionCommit() {
119
120         if (!hasCommitErrors()) {
121             //check if we have to update an existing module or to create a new one
122
Set JavaDoc moduleNames = OpenCms.getModuleManager().getModuleNames();
123             if (moduleNames.contains(m_module.getName())) {
124                 // update the module information
125
try {
126                     OpenCms.getModuleManager().updateModule(getCms(), m_module);
127                 } catch (CmsException e) {
128                     addCommitError(e);
129                 }
130             } else {
131                 try {
132                     m_module = createModuleFolders((CmsModule)m_module.clone());
133                     OpenCms.getModuleManager().addModule(getCms(), m_module);
134                 } catch (CmsException e) {
135                     addCommitError(e);
136                 }
137             }
138         }
139
140         if (!hasCommitErrors()) {
141             // refresh the list
142
Map JavaDoc objects = (Map JavaDoc)getSettings().getListObject();
143             if (objects != null) {
144                 objects.remove(CmsModulesList.class.getName());
145             }
146         }
147     }
148
149     /**
150      * @see org.opencms.workplace.CmsDialog#getCancelAction()
151      */

152     public String JavaDoc getCancelAction() {
153
154         // set the default action
155
setParamPage((String JavaDoc)getPages().get(0));
156
157         return DIALOG_SET;
158     }
159
160     /**
161      * Gets the module parameter.<p>
162      *
163      * @return the module parameter
164      */

165     public String JavaDoc getParamModule() {
166
167         return m_paramModule;
168     }
169
170     /**
171      * Sets the module parameter.<p>
172      * @param paramModule the module parameter
173      */

174     public void setParamModule(String JavaDoc paramModule) {
175
176         m_paramModule = paramModule;
177     }
178
179     /**
180      * Creates the list of widgets for this dialog.<p>
181      */

182     protected void defineWidgets() {
183
184         // nothing to add here, see the different edit modules
185
}
186
187     /**
188      * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
189      */

190     protected String JavaDoc[] getPageArray() {
191
192         return PAGES;
193     }
194
195     /**
196      * @see org.opencms.workplace.CmsWorkplace#initMessages()
197      */

198     protected void initMessages() {
199
200         // add specific dialog resource bundle
201
addMessages(Messages.get().getBundleName());
202         // add default resource bundles
203
super.initMessages();
204     }
205
206     /**
207      * Initializes the module to work with depending on the dialog state and request parameters.<p>
208      */

209     protected void initModule() {
210
211         Object JavaDoc o;
212
213         if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
214             // this is the initial dialog call
215
if (CmsStringUtil.isNotEmpty(m_paramModule)) {
216                 // edit an existing module, get it from manager
217
o = OpenCms.getModuleManager().getModule(m_paramModule);
218             } else {
219                 // create a new module
220
o = null;
221             }
222         } else {
223             // this is not the initial call, get module from session
224
o = getDialogObject();
225         }
226
227         if (!(o instanceof CmsModule)) {
228             // create a new module
229
m_module = new CmsModule();
230
231         } else {
232             // reuse module stored in session
233
m_module = (CmsModule)((CmsModule)o).clone();
234         }
235     }
236
237     /**
238      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
239      */

240     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
241
242         // set the dialog type
243
setParamDialogtype(DIALOG_TYPE);
244
245         super.initWorkplaceRequestValues(settings, request);
246
247         // save the current state of the module (may be changed because of the widget values)
248
setDialogObject(m_module);
249     }
250
251     /**
252      * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
253      */

254     protected void validateParamaters() throws Exception JavaDoc {
255
256         String JavaDoc moduleName = getParamModule();
257         // check module
258
if (!isNewModule()) {
259             CmsModule module = OpenCms.getModuleManager().getModule(moduleName);
260             if (module == null) {
261                 throw new Exception JavaDoc();
262             }
263         }
264     }
265
266     /**
267      * Creates all module folders that are selected in the input form.<p>
268      * @param module the module
269      */

270     private CmsModule createModuleFolders(CmsModule module) throws CmsException {
271
272         String JavaDoc modulePath = CmsWorkplace.VFS_PATH_MODULES + module.getName() + "/";
273         List JavaDoc exportPoints = module.getExportPoints();
274         List JavaDoc resources = module.getResources();
275
276         // set the createModuleFolder flag if any other flag is set
277
if (module.isCreateClassesFolder()
278             || module.isCreateElementsFolder()
279             || module.isCreateLibFolder()
280             || module.isCreateResourcesFolder()
281             || module.isCreateTemplateFolder()) {
282             module.setCreateModuleFolder(true);
283         }
284
285         // check if we have to create the module folder
286
if (module.isCreateModuleFolder()) {
287             getCms().createResource(modulePath, CmsResourceTypeFolder.getStaticTypeId());
288             // add the module folder to the resource list
289
resources.add(modulePath);
290             module.setResources(resources);
291         }
292
293         // check if we have to create the template folder
294
if (module.isCreateTemplateFolder()) {
295             String JavaDoc path = modulePath + PATH_TEMPLATES;
296             getCms().createResource(path, CmsResourceTypeFolder.getStaticTypeId());
297         }
298
299         // check if we have to create the elements folder
300
if (module.isCreateElementsFolder()) {
301             String JavaDoc path = modulePath + PATH_ELEMENTS;
302             getCms().createResource(path, CmsResourceTypeFolder.getStaticTypeId());
303         }
304
305         // check if we have to create the resources folder
306
if (module.isCreateTemplateFolder()) {
307             String JavaDoc path = modulePath + PATH_RESOURCES;
308             getCms().createResource(path, CmsResourceTypeFolder.getStaticTypeId());
309         }
310
311         // check if we have to create the lib folder
312
if (module.isCreateLibFolder()) {
313             String JavaDoc path = modulePath + PATH_LIB;
314             getCms().createResource(path, CmsResourceTypeFolder.getStaticTypeId());
315             CmsExportPoint exp = new CmsExportPoint(path, "WEB-INF/lib/");
316             exportPoints.add(exp);
317             module.setExportPoints(exportPoints);
318         }
319
320         // check if we have to create the classes folder
321
if (module.isCreateClassesFolder()) {
322             String JavaDoc path = modulePath + PATH_CLASSES;
323             getCms().createResource(path, CmsResourceTypeFolder.getStaticTypeId());
324             CmsExportPoint exp = new CmsExportPoint(path, "WEB-INF/classes/");
325             exportPoints.add(exp);
326             module.setExportPoints(exportPoints);
327
328             // now create all subfolders for the package structure
329
StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(m_module.getName(), ".");
330             while (tok.hasMoreTokens()) {
331                 String JavaDoc folder = tok.nextToken();
332                 path += folder + "/";
333                 getCms().createResource(path, CmsResourceTypeFolder.getStaticTypeId());
334             }
335         }
336         return module;
337     }
338
339     /**
340      * Checks if the new module dialog has to be displayed.<p>
341      *
342      * @return <code>true</code> if the new module dialog has to be displayed
343      */

344     private boolean isNewModule() {
345
346         return getCurrentToolPath().equals("/modules/modules_new");
347     }
348 }
Popular Tags