001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.workplace.tools.database; 029 030import org.opencms.jsp.CmsJspActionElement; 031import org.opencms.main.OpenCms; 032import org.opencms.widgets.CmsCheckboxWidget; 033import org.opencms.widgets.CmsDisplayWidget; 034import org.opencms.widgets.CmsSelectWidget; 035import org.opencms.widgets.CmsSelectWidgetOption; 036import org.opencms.workplace.CmsWidgetDialog; 037import org.opencms.workplace.CmsWidgetDialogParameter; 038import org.opencms.workplace.CmsWorkplaceSettings; 039import org.opencms.workplace.tools.CmsToolDialog; 040import org.opencms.workplace.tools.CmsToolManager; 041 042import java.io.File; 043import java.io.IOException; 044import java.util.ArrayList; 045import java.util.HashMap; 046import java.util.Iterator; 047import java.util.List; 048import java.util.Map; 049 050import javax.servlet.ServletException; 051import javax.servlet.http.HttpServletRequest; 052import javax.servlet.http.HttpServletResponse; 053import javax.servlet.jsp.PageContext; 054 055/** 056 * Class to upload a zip file containing VFS resources from the server.<p> 057 * 058 * @since 6.0.0 059 */ 060public class CmsDatabaseImportFromServer extends CmsWidgetDialog { 061 062 /** The dialog type. */ 063 public static final String DIALOG_TYPE = "DatabaseImportServer"; 064 065 /** Defines which pages are valid for this dialog. */ 066 public static final String[] PAGES = {"page1"}; 067 068 /** Import file request parameter name. */ 069 public static final String PARAM_IMPORTFILE = "importFile"; 070 071 /** Keep permissions request parameter name. */ 072 public static final String PARAM_KEEPPERMISSIONS = "keepPermissions"; 073 074 /** The import JSP report workplace URI. */ 075 protected static final String IMPORT_ACTION_REPORT = PATH_WORKPLACE + "admin/database/reports/import.jsp"; 076 077 /** Name of the manifest file used in upload files. */ 078 private static final String FILE_MANIFEST = "manifest.xml"; 079 080 /** Name of the sub-folder containing the OpenCms module packages. */ 081 private static final String FOLDER_MODULES = "modules"; 082 083 /** The import file name stored by the select box widget. */ 084 private String m_importFile; 085 086 /** The keep permissions flag stored by the check box widget. */ 087 private String m_keepPermissions; 088 089 /** 090 * Public constructor with JSP action element.<p> 091 * 092 * @param jsp an initialized JSP action element 093 */ 094 public CmsDatabaseImportFromServer(CmsJspActionElement jsp) { 095 096 super(jsp); 097 } 098 099 /** 100 * Public constructor with JSP variables.<p> 101 * 102 * @param context the JSP page context 103 * @param req the JSP request 104 * @param res the JSP response 105 */ 106 public CmsDatabaseImportFromServer(PageContext context, HttpServletRequest req, HttpServletResponse res) { 107 108 this(new CmsJspActionElement(context, req, res)); 109 } 110 111 /** 112 * Returns the list of all uploadable zip files and uploadable folders available on the server.<p> 113 * 114 * @param includeFolders if true, the uploadable folders are included in the list 115 * @return the list of all uploadable zip files and uploadable folders available on the server 116 */ 117 protected static List getFileListFromServer(boolean includeFolders) { 118 119 List result = new ArrayList(); 120 121 // get the RFS package export path 122 String exportpath = OpenCms.getSystemInfo().getPackagesRfsPath(); 123 File folder = new File(exportpath); 124 125 // get a list of all files of the packages folder 126 String[] files = folder.list(); 127 for (int i = 0; i < files.length; i++) { 128 File diskFile = new File(exportpath, files[i]); 129 // check this is a file and ends with zip -> this is a database upload file 130 if (diskFile.isFile() && diskFile.getName().endsWith(".zip")) { 131 result.add(diskFile.getName()); 132 } else if (diskFile.isDirectory() 133 && includeFolders 134 && (!diskFile.getName().equalsIgnoreCase(FOLDER_MODULES)) 135 && ((new File(diskFile + File.separator + FILE_MANIFEST)).exists())) { 136 // this is an unpacked package, add it to uploadable files 137 result.add(diskFile.getName()); 138 } 139 } 140 141 return result; 142 } 143 144 /** 145 * @see org.opencms.workplace.CmsWidgetDialog#actionCommit() 146 */ 147 @Override 148 public void actionCommit() throws IOException, ServletException { 149 150 List errors = new ArrayList(); 151 152 Map params = new HashMap(); 153 params.put(PARAM_FILE, getImportFile()); 154 params.put(PARAM_KEEPPERMISSIONS.toLowerCase(), getKeepPermissions()); 155 // set style to display report in correct layout 156 params.put(PARAM_STYLE, CmsToolDialog.STYLE_NEW); 157 // set close link to get back to overview after finishing the import 158 params.put(PARAM_CLOSELINK, CmsToolManager.linkForToolPath(getJsp(), "/database")); 159 // redirect to the report output JSP 160 getToolManager().jspForwardPage(this, IMPORT_ACTION_REPORT, params); 161 // set the list of errors to display when saving failed 162 setCommitErrors(errors); 163 } 164 165 /** 166 * Returns the importFile parameter.<p> 167 * 168 * @return the importFile parameter 169 */ 170 public String getImportFile() { 171 172 return m_importFile; 173 } 174 175 /** 176 * Returns the keepPermissions parameter.<p> 177 * 178 * @return the keepPermissions parameter 179 */ 180 public String getKeepPermissions() { 181 182 return m_keepPermissions; 183 } 184 185 /** 186 * Sets the importFile parameter.<p> 187 * 188 * @param importFile the importFile parameter 189 */ 190 public void setImportFile(String importFile) { 191 192 m_importFile = importFile; 193 } 194 195 /** 196 * Sets the keepPermissions parameter.<p> 197 * 198 * @param keepPermissions the keepPermissions parameter 199 */ 200 public void setKeepPermissions(String keepPermissions) { 201 202 m_keepPermissions = keepPermissions; 203 } 204 205 /** 206 * Creates the dialog HTML for all defined widgets of the named dialog (page).<p> 207 * 208 * @param dialog the dialog (page) to get the HTML for 209 * @return the dialog HTML for all defined widgets of the named dialog (page) 210 */ 211 @Override 212 protected String createDialogHtml(String dialog) { 213 214 StringBuffer result = new StringBuffer(1024); 215 216 // create table 217 result.append(createWidgetTableStart()); 218 219 // show error header once if there were validation errors 220 result.append(createWidgetErrorHeader()); 221 222 if (dialog.equals(PAGES[0])) { 223 result.append(dialogBlockStart(key("label.block.importFileFromServer"))); 224 result.append(createWidgetTableStart()); 225 result.append(createDialogRowsHtml(0, getFilesFromServer().isEmpty() ? 0 : 1)); 226 result.append(createWidgetTableEnd()); 227 result.append(dialogBlockEnd()); 228 } 229 230 // close table 231 result.append(createWidgetTableEnd()); 232 233 return result.toString(); 234 } 235 236 /** 237 * Creates the list of widgets for this dialog.<p> 238 */ 239 @Override 240 protected void defineWidgets() { 241 242 // get available files from server 243 List files = getFilesFromServer(); 244 245 if (files.isEmpty()) { 246 // no import files available, display message 247 addWidget( 248 new CmsWidgetDialogParameter( 249 this, 250 PARAM_IMPORTFILE, 251 PAGES[0], 252 new CmsDisplayWidget(key(Messages.GUI_IMPORTSERVER_NO_DB_EXPORTS_0)))); 253 } else { 254 // add the file select box widget 255 addWidget(new CmsWidgetDialogParameter(this, PARAM_IMPORTFILE, PAGES[0], new CmsSelectWidget(files))); 256 addWidget(new CmsWidgetDialogParameter(this, PARAM_KEEPPERMISSIONS, PAGES[0], new CmsCheckboxWidget())); 257 } 258 } 259 260 /** 261 * Returns the list of all uploadable zip files and uploadable folders available on the server.<p> 262 * 263 * The list is returned as a String separated by "|" to use as configuration parameter for selectbox widgets.<p> 264 * 265 * @return pipe separated list of file names 266 */ 267 protected List getFilesFromServer() { 268 269 List retVal = new ArrayList(); 270 Iterator i = getFileListFromServer(true).iterator(); 271 while (i.hasNext()) { 272 String fileName = (String)i.next(); 273 retVal.add(new CmsSelectWidgetOption(fileName)); 274 } 275 return retVal; 276 } 277 278 /** 279 * @see org.opencms.workplace.CmsWidgetDialog#getPageArray() 280 */ 281 @Override 282 protected String[] getPageArray() { 283 284 return PAGES; 285 } 286 287 /** 288 * @see org.opencms.workplace.CmsWorkplace#initMessages() 289 */ 290 @Override 291 protected void initMessages() { 292 293 // add specific dialog resource bundle 294 addMessages(Messages.get().getBundleName()); 295 // add default resource bundles 296 super.initMessages(); 297 } 298 299 /** 300 * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest) 301 */ 302 @Override 303 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 304 305 // set the dialog type 306 setParamDialogtype(DIALOG_TYPE); 307 308 super.initWorkplaceRequestValues(settings, request); 309 } 310}