KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > database > CmsDatabaseImportFromServer


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/database/CmsDatabaseImportFromServer.java,v $
3  * Date : $Date: 2006/03/28 13:10:02 $
4  * Version: $Revision: 1.14 $
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.database;
33
34 import org.opencms.jsp.CmsJspActionElement;
35 import org.opencms.main.OpenCms;
36 import org.opencms.widgets.CmsDisplayWidget;
37 import org.opencms.widgets.CmsSelectWidget;
38 import org.opencms.widgets.CmsSelectWidgetOption;
39 import org.opencms.workplace.CmsWidgetDialog;
40 import org.opencms.workplace.CmsWidgetDialogParameter;
41 import org.opencms.workplace.CmsWorkplaceSettings;
42 import org.opencms.workplace.tools.CmsToolDialog;
43 import org.opencms.workplace.tools.CmsToolManager;
44
45 import java.io.File JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Map JavaDoc;
52
53 import javax.servlet.ServletException JavaDoc;
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 upload a zip file containing VFS resources from the server.<p>
60  *
61  * @author Andreas Zahner
62  *
63  * @version $Revision: 1.14 $
64  *
65  * @since 6.0.0
66  */

67 public class CmsDatabaseImportFromServer extends CmsWidgetDialog {
68
69     /** The dialog type. */
70     public static final String JavaDoc DIALOG_TYPE = "DatabaseImportServer";
71
72     /** Defines which pages are valid for this dialog. */
73     public static final String JavaDoc[] PAGES = {"page1"};
74
75     /** Import file request parameter. */
76     public static final String JavaDoc PARAM_IMPORTFILE = "importFile";
77
78     /** The import JSP report workplace URI. */
79     protected static final String JavaDoc IMPORT_ACTION_REPORT = PATH_WORKPLACE + "admin/database/reports/import.jsp";
80
81     /** Name of the manifest file used in upload files. */
82     private static final String JavaDoc FILE_MANIFEST = "manifest.xml";
83
84     /** Name of the subfolder containing the OpenCms module packages. */
85     private static final String JavaDoc FOLDER_MODULES = "modules";
86
87     /** The import file name stored by the selectbox widget. */
88     private String JavaDoc m_importFile;
89
90     /**
91      * Public constructor with JSP action element.<p>
92      *
93      * @param jsp an initialized JSP action element
94      */

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

107     public CmsDatabaseImportFromServer(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
108
109         this(new CmsJspActionElement(context, req, res));
110     }
111
112     /**
113      * Returns the list of all uploadable zip files and uploadable folders available on the server.<p>
114      *
115      * @param includeFolders if true, the uploadable folders are included in the list
116      * @return the list of all uploadable zip files and uploadable folders available on the server
117      */

118     protected static List JavaDoc getFileListFromServer(boolean includeFolders) {
119
120         List JavaDoc result = new ArrayList JavaDoc();
121
122         // get the RFS package export path
123
String JavaDoc exportpath = OpenCms.getSystemInfo().getPackagesRfsPath();
124         File JavaDoc folder = new File JavaDoc(exportpath);
125
126         // get a list of all files of the packages folder
127
String JavaDoc[] files = folder.list();
128         for (int i = 0; i < files.length; i++) {
129             File JavaDoc diskFile = new File JavaDoc(exportpath, files[i]);
130             // check this is a file and ends with zip -> this is a database upload file
131
if (diskFile.isFile() && diskFile.getName().endsWith(".zip")) {
132                 result.add(diskFile.getName());
133             } else if (diskFile.isDirectory()
134                 && includeFolders
135                 && (!diskFile.getName().equalsIgnoreCase(FOLDER_MODULES))
136                 && ((new File JavaDoc(diskFile + File.separator + FILE_MANIFEST)).exists())) {
137                 // this is an unpacked package, add it to uploadable files
138
result.add(diskFile.getName());
139             }
140         }
141
142         return result;
143     }
144
145     /**
146      * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
147      */

148     public void actionCommit() throws IOException JavaDoc, ServletException JavaDoc {
149
150         List JavaDoc errors = new ArrayList JavaDoc();
151
152         Map JavaDoc params = new HashMap JavaDoc();
153         params.put(PARAM_FILE, getImportFile());
154         // set style to display report in correct layout
155
params.put(PARAM_STYLE, CmsToolDialog.STYLE_NEW);
156         // set close link to get back to overview after finishing the import
157
params.put(PARAM_CLOSELINK, CmsToolManager.linkForToolPath(getJsp(), "/database"));
158         // redirect to the report output JSP
159
getToolManager().jspForwardPage(this, IMPORT_ACTION_REPORT, params);
160         // set the list of errors to display when saving failed
161
setCommitErrors(errors);
162     }
163
164     /**
165      * Returns the importFile parameter.<p>
166      *
167      * @return the importFile parameter
168      */

169     public String JavaDoc getImportFile() {
170
171         return m_importFile;
172     }
173
174     /**
175      * Sets the importFile parameter.<p>
176      *
177      * @param importFile the importFile parameter
178      */

179     public void setImportFile(String JavaDoc importFile) {
180
181         m_importFile = importFile;
182     }
183
184     /**
185      * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
186      *
187      * @param dialog the dialog (page) to get the HTML for
188      * @return the dialog HTML for all defined widgets of the named dialog (page)
189      */

190     protected String JavaDoc createDialogHtml(String JavaDoc dialog) {
191
192         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
193
194         // create table
195
result.append(createWidgetTableStart());
196
197         // show error header once if there were validation errors
198
result.append(createWidgetErrorHeader());
199
200         if (dialog.equals(PAGES[0])) {
201             result.append(dialogBlockStart(key("label.block.importFileFromServer")));
202             result.append(createWidgetTableStart());
203             result.append(createDialogRowsHtml(0, 0));
204             result.append(createWidgetTableEnd());
205             result.append(dialogBlockEnd());
206         }
207
208         // close table
209
result.append(createWidgetTableEnd());
210
211         return result.toString();
212     }
213
214     /**
215      * Creates the list of widgets for this dialog.<p>
216      */

217     protected void defineWidgets() {
218
219         // get available files from server
220
List JavaDoc files = getFilesFromServer();
221         
222         if (files.isEmpty()) {
223             // no import files available, display message
224
addWidget(new CmsWidgetDialogParameter(this, PARAM_IMPORTFILE, PAGES[0], new CmsDisplayWidget(key(Messages.GUI_IMPORTSERVER_NO_DB_EXPORTS_0))));
225         } else {
226             // add the file select box widget
227
addWidget(new CmsWidgetDialogParameter(this, PARAM_IMPORTFILE, PAGES[0], new CmsSelectWidget(files)));
228         }
229     }
230
231     /**
232      * Returns the list of all uploadable zip files and uploadable folders available on the server.<p>
233      *
234      * The list is returned as a String separated by "|" to use as configuration parameter for selectbox widgets.<p>
235      *
236      * @return pipe separated list of file names
237      */

238     protected List JavaDoc getFilesFromServer() {
239
240         List JavaDoc retVal = new ArrayList JavaDoc();
241         Iterator JavaDoc i = getFileListFromServer(true).iterator();
242         while (i.hasNext()) {
243             String JavaDoc fileName = (String JavaDoc)i.next();
244             retVal.add(new CmsSelectWidgetOption(fileName));
245         }
246         return retVal;
247     }
248
249     /**
250      * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
251      */

252     protected String JavaDoc[] getPageArray() {
253
254         return PAGES;
255     }
256
257     /**
258      * @see org.opencms.workplace.CmsWorkplace#initMessages()
259      */

260     protected void initMessages() {
261
262         // add specific dialog resource bundle
263
addMessages(Messages.get().getBundleName());
264         // add default resource bundles
265
super.initMessages();
266     }
267
268     /**
269      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
270      */

271     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
272
273         // set the dialog type
274
setParamDialogtype(DIALOG_TYPE);
275
276         super.initWorkplaceRequestValues(settings, request);
277     }
278 }
Popular Tags