KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/modules/CmsModulesUploadFromServer.java,v $
3  * Date : $Date: 2006/03/28 13:54:48 $
4  * Version: $Revision: 1.21 $
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.CmsRuntimeException;
37 import org.opencms.main.OpenCms;
38 import org.opencms.module.CmsModule;
39 import org.opencms.module.CmsModuleDependency;
40 import org.opencms.module.CmsModuleImportExportHandler;
41 import org.opencms.module.CmsModuleManager;
42 import org.opencms.widgets.CmsDisplayWidget;
43 import org.opencms.widgets.CmsSelectWidget;
44 import org.opencms.widgets.CmsSelectWidgetOption;
45 import org.opencms.workplace.CmsWidgetDialog;
46 import org.opencms.workplace.CmsWidgetDialogParameter;
47 import org.opencms.workplace.CmsWorkplaceSettings;
48 import org.opencms.workplace.tools.CmsToolDialog;
49 import org.opencms.workplace.tools.CmsToolManager;
50
51 import java.io.File JavaDoc;
52 import java.io.IOException JavaDoc;
53 import java.util.ArrayList JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.util.List JavaDoc;
56 import java.util.Map JavaDoc;
57
58 import javax.servlet.ServletException JavaDoc;
59 import javax.servlet.http.HttpServletRequest JavaDoc;
60 import javax.servlet.http.HttpServletResponse JavaDoc;
61 import javax.servlet.jsp.PageContext JavaDoc;
62
63 /**
64  * Class to upload a module from the server.<p>
65  *
66  * @author Michael Emmerich
67  *
68  * @version $Revision: 1.21 $
69  *
70  * @since 6.0.0
71  */

72 public class CmsModulesUploadFromServer extends CmsWidgetDialog {
73
74     /** The dialog type. */
75     public static final String JavaDoc DIALOG_TYPE = "ModulesUploadServer";
76
77     /** Defines which pages are valid for this dialog. */
78     public static final String JavaDoc[] PAGES = {"page1"};
79
80     /** Modulename parameter. */
81     public static final String JavaDoc PARAM_MODULENAME = "modulename";
82
83     /** The import action. */
84     protected static final String JavaDoc IMPORT_ACTION_REPORT = "/system/workplace/admin/modules/reports/import.jsp";
85
86     /** The replace action. */
87     protected static final String JavaDoc REPLACE_ACTION_REPORT = "/system/workplace/admin/modules/reports/replace.jsp";
88
89     /** Modulename. */
90     private String JavaDoc m_moduleupload;
91
92     /**
93      * Public constructor with JSP action element.<p>
94      *
95      * @param jsp an initialized JSP action element
96      */

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

109     public CmsModulesUploadFromServer(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
110
111         this(new CmsJspActionElement(context, req, res));
112     }
113
114     /**
115      * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
116      */

117     public void actionCommit() throws IOException JavaDoc, ServletException JavaDoc {
118
119         List JavaDoc errors = new ArrayList JavaDoc();
120         CmsModule module = null;
121         try {
122             String JavaDoc importpath = OpenCms.getSystemInfo().getPackagesRfsPath();
123             importpath = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
124                 importpath + "modules/" + m_moduleupload);
125             module = CmsModuleImportExportHandler.readModuleFromImport(importpath);
126
127             // check if all dependencies are fulfilled
128
List JavaDoc dependencies = OpenCms.getModuleManager().checkDependencies(
129                 module,
130                 CmsModuleManager.DEPENDENCY_MODE_IMPORT);
131             if (!dependencies.isEmpty()) {
132                 StringBuffer JavaDoc dep = new StringBuffer JavaDoc(32);
133                 for (int i = 0; i < dependencies.size(); i++) {
134                     CmsModuleDependency dependency = (CmsModuleDependency)dependencies.get(i);
135                     dep.append("\n - ");
136                     dep.append(dependency.getName());
137                     dep.append(" (Version: ");
138                     dep.append(dependency.getVersion());
139                     dep.append(")");
140                 }
141                 errors.add(new CmsRuntimeException(Messages.get().container(
142                     Messages.ERR_ACTION_MODULE_DEPENDENCY_2,
143                     m_moduleupload,
144                     new String JavaDoc(dep))));
145             }
146
147         } catch (CmsConfigurationException e) {
148             errors.add(new CmsRuntimeException(Messages.get().container(
149                 Messages.ERR_ACTION_MODULE_UPLOAD_1,
150                 m_moduleupload), e));
151         }
152
153         if (errors.isEmpty()) {
154
155             // refresh the list
156
Map JavaDoc objects = (Map JavaDoc)getSettings().getListObject();
157             if (objects != null) {
158                 objects.remove(CmsModulesList.class.getName());
159             }
160
161             // redirect
162
Map JavaDoc param = new HashMap JavaDoc();
163             param.put(CmsModulesList.PARAM_MODULE, m_moduleupload);
164             param.put(PARAM_STYLE, CmsToolDialog.STYLE_NEW);
165             param.put(PARAM_CLOSELINK, CmsToolManager.linkForToolPath(getJsp(), "/modules"));
166             if (OpenCms.getModuleManager().hasModule(module.getName())) {
167                 param.put(PARAM_MODULENAME, module.getName());
168                 getToolManager().jspForwardPage(this, REPLACE_ACTION_REPORT, param);
169             } else {
170                 getToolManager().jspForwardPage(this, IMPORT_ACTION_REPORT, param);
171             }
172         }
173
174         // set the list of errors to display when saving failed
175
setCommitErrors(errors);
176     }
177
178     /**
179      * Gets the module parameter.<p>
180      *
181      * @return the module parameter
182      */

183     public String JavaDoc getModuleupload() {
184
185         return m_moduleupload;
186     }
187
188     /**
189      * Sets the module parameter.<p>
190      * @param module the module parameter
191      */

192     public void setModuleupload(String JavaDoc module) {
193
194         m_moduleupload = module;
195     }
196
197     /**
198      * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
199      *
200      * @param dialog the dialog (page) to get the HTML for
201      * @return the dialog HTML for all defined widgets of the named dialog (page)
202      */

203     protected String JavaDoc createDialogHtml(String JavaDoc dialog) {
204
205         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
206
207         // create table
208
result.append(createWidgetTableStart());
209
210         // show error header once if there were validation errors
211
result.append(createWidgetErrorHeader());
212
213         if (dialog.equals(PAGES[0])) {
214             result.append(dialogBlockStart(key("label.uploadfromserver")));
215             result.append(createWidgetTableStart());
216             result.append(createDialogRowsHtml(0, 0));
217             result.append(createWidgetTableEnd());
218             result.append(dialogBlockEnd());
219         }
220
221         // close table
222
result.append(createWidgetTableEnd());
223
224         return result.toString();
225     }
226
227     /**
228      * Creates the list of widgets for this dialog.<p>
229      */

230     protected void defineWidgets() {
231
232         List JavaDoc selectOptions = getModulesFromServer();
233
234         if (selectOptions.isEmpty()) {
235             // no import modules available, display message
236
addWidget(new CmsWidgetDialogParameter(this, "moduleupload", PAGES[0], new CmsDisplayWidget(key(Messages.GUI_MODULES_IMPORT_NOT_AVAILABLE_0))));
237         } else {
238             // add the file select box widget
239
addWidget(new CmsWidgetDialogParameter(this, "moduleupload", PAGES[0], new CmsSelectWidget(selectOptions)));
240         }
241     }
242
243     /**
244      * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
245      */

246     protected String JavaDoc[] getPageArray() {
247
248         return PAGES;
249     }
250
251     /**
252      * @see org.opencms.workplace.CmsWorkplace#initMessages()
253      */

254     protected void initMessages() {
255
256         // add specific dialog resource bundle
257
addMessages(Messages.get().getBundleName());
258         // add default resource bundles
259
super.initMessages();
260     }
261
262     /**
263      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
264      */

265     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
266
267         // set the dialog type
268
setParamDialogtype(DIALOG_TYPE);
269
270         super.initWorkplaceRequestValues(settings, request);
271
272         // save the current state of the job (may be changed because of the widget values)
273
setDialogObject(m_moduleupload);
274     }
275
276     /**
277      * Returns the list of all modules available on the server in prepared CmsSelectWidgetOption objects.<p>
278      *
279      * @return List of module names in CmsSelectWidgetOption objects
280      */

281     private List JavaDoc getModulesFromServer() {
282
283         List JavaDoc result = new ArrayList JavaDoc();
284
285         // get the systems-exportpath
286
String JavaDoc exportpath = OpenCms.getSystemInfo().getPackagesRfsPath();
287         exportpath = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(exportpath + "modules");
288         File JavaDoc folder = new File JavaDoc(exportpath);
289
290         // get a list of all files
291
String JavaDoc[] list = folder.list();
292         for (int i = 0; i < list.length; i++) {
293             try {
294                 File JavaDoc diskFile = new File JavaDoc(exportpath, list[i]);
295                 // check if it is a file and ends with zip -> this is a module
296
if (diskFile.isFile() && diskFile.getName().endsWith(".zip")) {
297                     result.add(new CmsSelectWidgetOption(diskFile.getName()));
298                 } else if (diskFile.isDirectory() && ((new File JavaDoc(diskFile + File.separator + "manifest.xml")).exists())) {
299                     // this is a folder with manifest file -> this a module
300
result.add(new CmsSelectWidgetOption(diskFile.getName()));
301                 }
302             } catch (Throwable JavaDoc t) {
303                 // ignore and continue
304
}
305         }
306
307         return result;
308     }
309
310 }
Popular Tags