KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/modules/CmsDependenciesEdit.java,v $
3  * Date : $Date: 2005/06/23 11:11:38 $
4  * Version: $Revision: 1.10 $
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.configuration.CmsConfigurationException;
35 import org.opencms.jsp.CmsJspActionElement;
36 import org.opencms.main.OpenCms;
37 import org.opencms.module.CmsModule;
38 import org.opencms.module.CmsModuleDependency;
39 import org.opencms.security.CmsSecurityException;
40 import org.opencms.util.CmsStringUtil;
41 import org.opencms.widgets.CmsInputWidget;
42 import org.opencms.widgets.CmsSelectWidget;
43 import org.opencms.widgets.CmsSelectWidgetOption;
44 import org.opencms.workplace.CmsDialog;
45 import org.opencms.workplace.CmsWidgetDialog;
46 import org.opencms.workplace.CmsWidgetDialogParameter;
47 import org.opencms.workplace.CmsWorkplaceSettings;
48
49 import java.util.ArrayList JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Map JavaDoc;
53
54 import javax.servlet.http.HttpServletRequest JavaDoc;
55 import javax.servlet.http.HttpServletResponse JavaDoc;
56 import javax.servlet.jsp.PageContext JavaDoc;
57
58 /**
59  * Class to edit a module dependencies.<p>
60  *
61  * @author Michael Emmerich
62  *
63  * @version $Revision: 1.10 $
64  *
65  * @since 6.0.0
66  */

67 public class CmsDependenciesEdit extends CmsWidgetDialog {
68
69     /** The dialog type. */
70     public static final String JavaDoc DIALOG_TYPE = "DependenciesEdit";
71
72     /** Defines which pages are valid for this dialog. */
73     public static final String JavaDoc[] PAGES = {"page1"};
74
75     /** The module dependency object that is shown on this dialog. */
76     private CmsModuleDependency m_dependency;
77
78     /** Dependency name. */
79     private String JavaDoc m_paramDependency;
80
81     /** Modulename. */
82     private String JavaDoc m_paramModule;
83
84     /**
85      * Public constructor with JSP action element.<p>
86      *
87      * @param jsp an initialized JSP action element
88      */

89     public CmsDependenciesEdit(CmsJspActionElement jsp) {
90
91         super(jsp);
92     }
93
94     /**
95      * Public constructor with JSP variables.<p>
96      *
97      * @param context the JSP page context
98      * @param req the JSP request
99      * @param res the JSP response
100      */

101     public CmsDependenciesEdit(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
102
103         this(new CmsJspActionElement(context, req, res));
104     }
105
106     /**
107      * Commits the edited module.<p>
108      */

109     public void actionCommit() {
110
111         List JavaDoc errors = new ArrayList JavaDoc();
112
113         try {
114             // get the correct module
115
String JavaDoc moduleName = getParamModule();
116             CmsModule module = (CmsModule)OpenCms.getModuleManager().getModule(moduleName).clone();
117             // get the current dependencies from the module
118
List JavaDoc oldDependencies = module.getDependencies();
119             // now loop through the dependencies and create the new list of dependencies
120
List JavaDoc newDependencies = new ArrayList JavaDoc();
121             Iterator JavaDoc i = oldDependencies.iterator();
122             while (i.hasNext()) {
123                 CmsModuleDependency dep = (CmsModuleDependency)i.next();
124                 if (!dep.getName().equals(m_dependency.getName())) {
125                     newDependencies.add(dep);
126                 }
127             }
128             // update the dependencies
129
newDependencies.add(m_dependency);
130             module.setDependencies(newDependencies);
131             // update the module
132
OpenCms.getModuleManager().updateModule(getCms(), module);
133             // refresh the list
134
Map JavaDoc objects = (Map JavaDoc)getSettings().getListObject();
135             if (objects != null) {
136                 objects.remove(CmsModulesList.class.getName());
137                 objects.remove(CmsModulesDependenciesList.class.getName());
138             }
139         } catch (CmsConfigurationException ce) {
140             errors.add(ce);
141         } catch (CmsSecurityException se) {
142             errors.add(se);
143         }
144
145         // set the list of errors to display when saving failed
146
setCommitErrors(errors);
147     }
148
149     /**
150      * Builds the HTML for the dialog form.<p>
151      *
152      * @return the HTML for the dialog form
153      */

154     public String JavaDoc buildDialogForm() {
155
156         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
157
158         try {
159
160             // create the dialog HTML
161
result.append(createDialogHtml(getParamPage()));
162
163         } catch (Throwable JavaDoc t) {
164             // TODO: Error handling
165
}
166         return result.toString();
167     }
168
169     /**
170      * @see org.opencms.workplace.CmsDialog#getCancelAction()
171      */

172     public String JavaDoc getCancelAction() {
173
174         // set the default action
175
setParamPage((String JavaDoc)getPages().get(0));
176
177         return DIALOG_SET;
178     }
179
180     /**
181      * Gets the module dependency parameter.<p>
182      *
183      * @return the module dependency parameter
184      */

185     public String JavaDoc getParamDependency() {
186
187         return m_paramDependency;
188     }
189
190     /**
191      * Gets the module parameter.<p>
192      *
193      * @return the module parameter
194      */

195     public String JavaDoc getParamModule() {
196
197         return m_paramModule;
198     }
199
200     /**
201      * Sets the module dependency parameter.<p>
202      * @param paramDependency the module dependency parameter
203      */

204     public void setParamDependency(String JavaDoc paramDependency) {
205
206         m_paramDependency = paramDependency;
207     }
208
209     /**
210      * Sets the module parameter.<p>
211      * @param paramModule the module parameter
212      */

213     public void setParamModule(String JavaDoc paramModule) {
214
215         m_paramModule = paramModule;
216     }
217
218     /**
219      * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
220      *
221      * @param dialog the dialog (page) to get the HTML for
222      * @return the dialog HTML for all defined widgets of the named dialog (page)
223      */

224     protected String JavaDoc createDialogHtml(String JavaDoc dialog) {
225
226         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
227
228         // create table
229
result.append(createWidgetTableStart());
230
231         // show error header once if there were validation errors
232
result.append(createWidgetErrorHeader());
233
234         if (dialog.equals(PAGES[0])) {
235             result.append(dialogBlockStart(key("label.dependencyinformation")));
236             result.append(createWidgetTableStart());
237             result.append(createDialogRowsHtml(0, 1));
238             result.append(createWidgetTableEnd());
239             result.append(dialogBlockEnd());
240         }
241
242         // close table
243
result.append(createWidgetTableEnd());
244
245         return result.toString();
246     }
247
248     /**
249      * Creates the list of widgets for this dialog.<p>
250      */

251     protected void defineWidgets() {
252
253         initModule();
254
255         addWidget(new CmsWidgetDialogParameter(m_dependency, "name", PAGES[0], new CmsSelectWidget(getModules())));
256         addWidget(new CmsWidgetDialogParameter(m_dependency, "version.version", PAGES[0], new CmsInputWidget()));
257
258     }
259
260     /**
261      * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
262      */

263     protected String JavaDoc[] getPageArray() {
264
265         return PAGES;
266     }
267
268     /**
269      * @see org.opencms.workplace.CmsWorkplace#initMessages()
270      */

271     protected void initMessages() {
272
273         // add specific dialog resource bundle
274
addMessages(Messages.get().getBundleName());
275         // add default resource bundles
276
super.initMessages();
277     }
278
279     /**
280      * Initializes the module to work with depending on the dialog state and request parameters.<p>
281      */

282     protected void initModule() {
283
284         Object JavaDoc o;
285         CmsModule module;
286
287         // first get the correct module
288
if (CmsStringUtil.isNotEmpty(m_paramModule)) {
289             module = (CmsModule)OpenCms.getModuleManager().getModule(m_paramModule).clone();
290         } else {
291             // create a new module
292
module = new CmsModule();
293         }
294
295         // now try to get the dependency
296
if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
297             o = null;
298         } else {
299             // this is not the initial call, get module dependency from session
300
o = getDialogObject();
301         }
302
303         if (!(o instanceof CmsModuleDependency)) {
304             if (m_paramDependency == null) {
305                 // there was no parameter given, so create a new, empty dependency
306
m_dependency = new CmsModuleDependency();
307             } else {
308                 // create a new module dependency by reading it from the module
309
List JavaDoc dependencies = module.getDependencies();
310                 m_dependency = new CmsModuleDependency();
311                 if (dependencies != null && dependencies.size() > 0) {
312                     Iterator JavaDoc i = dependencies.iterator();
313                     while (i.hasNext()) {
314                         CmsModuleDependency dependency = (CmsModuleDependency)i.next();
315                         if (dependency.getName().equals(m_paramDependency)) {
316                             m_dependency = dependency;
317                         }
318                     }
319                 }
320             }
321
322         } else {
323             // reuse module dependency stored in session
324
m_dependency = (CmsModuleDependency)o;
325         }
326
327     }
328
329     /**
330      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
331      */

332     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
333
334         // set the dialog type
335
setParamDialogtype(DIALOG_TYPE);
336
337         super.initWorkplaceRequestValues(settings, request);
338
339         // save the current state of the module (may be changed because of the widget values)
340
setDialogObject(m_dependency);
341     }
342
343     /**
344      * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
345      */

346     protected void validateParamaters() throws Exception JavaDoc {
347
348         String JavaDoc moduleName = getParamModule();
349         // check module
350
CmsModule module = OpenCms.getModuleManager().getModule(moduleName);
351         if (module == null) {
352             throw new Exception JavaDoc();
353         }
354         // check dependency
355
if (!isNewDependency()) {
356             Iterator JavaDoc it = module.getDependencies().iterator();
357             while (it.hasNext()) {
358                 CmsModuleDependency dep = (CmsModuleDependency)it.next();
359                 if (dep.getName().equals(getParamDependency())) {
360                     // dependency found
361
return;
362                 }
363             }
364             throw new Exception JavaDoc();
365         }
366     }
367
368     /**
369      * Get the list of all modules available.<p>
370      *
371      * @return list of module names
372      */

373     private List JavaDoc getModules() {
374
375         List JavaDoc retVal = new ArrayList JavaDoc();
376         // get all modules
377
Iterator JavaDoc i = OpenCms.getModuleManager().getModuleNames().iterator();
378         // add them to the list of modules
379
while (i.hasNext()) {
380             String JavaDoc moduleName = (String JavaDoc)i.next();
381             if (moduleName.equals(getParamDependency())) {
382                 // check for the preselection
383
retVal.add(new CmsSelectWidgetOption(moduleName, true));
384             } else {
385                 retVal.add(new CmsSelectWidgetOption(moduleName, false));
386             }
387         }
388         return retVal;
389     }
390
391     /**
392      * Checks if the new dependency dialog has to be displayed.<p>
393      *
394      * @return <code>true</code> if the new dependency dialog has to be displayed
395      */

396     private boolean isNewDependency() {
397
398         return getCurrentToolPath().equals("/modules/edit/dependencies/new");
399     }
400 }
401
Popular Tags