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, 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.ui.apps.modules; 029 030import org.opencms.file.CmsObject; 031import org.opencms.main.CmsException; 032import org.opencms.main.CmsLog; 033import org.opencms.main.OpenCms; 034import org.opencms.module.CmsModule; 035import org.opencms.ui.A_CmsUI; 036import org.opencms.ui.CmsVaadinUtils; 037import org.opencms.ui.apps.modules.edit.CmsSiteSelectorNewValueHandler; 038import org.opencms.ui.components.CmsAutoItemCreatingComboBox; 039import org.opencms.ui.components.CmsErrorDialog; 040import org.opencms.ui.report.CmsReportWidget; 041 042import java.util.Arrays; 043import java.util.List; 044 045import org.apache.commons.logging.Log; 046 047import com.vaadin.ui.Button; 048import com.vaadin.ui.Button.ClickEvent; 049import com.vaadin.ui.Button.ClickListener; 050import com.vaadin.ui.CssLayout; 051import com.vaadin.v7.data.util.IndexedContainer; 052import com.vaadin.v7.ui.VerticalLayout; 053 054/** 055 * Abstract superclass for the module import forms.<p> 056 */ 057public abstract class A_CmsModuleImportForm extends CssLayout { 058 059 /** The logger instance for this class. */ 060 private static final Log LOG = CmsLog.getLog(A_CmsModuleImportForm.class); 061 062 /** The serial version id. */ 063 private static final long serialVersionUID = 1L; 064 065 /** The module manager app instance. */ 066 protected CmsModuleApp m_app; 067 068 /** A bean representing the module zip file to be imported. */ 069 protected CmsModuleImportFile m_importFile; 070 071 /** 072 * Constructor.<p> 073 * 074 * @param app the app instance for which this form is opened 075 */ 076 public A_CmsModuleImportForm( 077 CmsModuleApp app, 078 final VerticalLayout start, 079 final VerticalLayout report, 080 Runnable run) { 081 082 report.setVisible(false); 083 CmsObject cms = A_CmsUI.getCmsObject(); 084 m_app = app; 085 CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null); 086 getOkButton().setEnabled(false); 087 088 final IndexedContainer availableSites = CmsVaadinUtils.getAvailableSitesContainer(cms, "name"); 089 090 getSiteSelector().setContainerDataSource(availableSites); 091 if (availableSites.getItem(cms.getRequestContext().getSiteRoot()) != null) { 092 getSiteSelector().setValue(cms.getRequestContext().getSiteRoot()); 093 } 094 getSiteSelector().setNullSelectionAllowed(false); 095 getSiteSelector().setItemCaptionPropertyId("name"); 096 getSiteSelector().setNewValueHandler(new CmsSiteSelectorNewValueHandler("name")); 097 098 getCancelButton().addClickListener(new ClickListener() { 099 100 private static final long serialVersionUID = 1L; 101 102 public void buttonClick(ClickEvent event) { 103 104 CmsVaadinUtils.getWindow(getCancelButton()).close(); 105 106 } 107 }); 108 109 getOkButton().addClickListener(new ClickListener() { 110 111 private static final long serialVersionUID = 1L; 112 113 @SuppressWarnings("synthetic-access") 114 public void buttonClick(ClickEvent event) { 115 116 try { 117 start.setVisible(false); 118 report.setVisible(true); 119 getOkButton().setVisible(false); 120 CmsObject importCms = OpenCms.initCmsObject(A_CmsUI.getCmsObject()); 121 importCms.getRequestContext().setSiteRoot((String)(getSiteSelector().getValue())); 122 CmsModuleImportThread thread = new CmsModuleImportThread( 123 importCms, 124 m_importFile.getModule(), 125 m_importFile.getPath()); 126 127 CmsReportWidget reportWidget = new CmsReportWidget(thread); 128 reportWidget.setWidth("100%"); 129 reportWidget.setHeight("100%"); 130 131 report.addComponent(reportWidget); 132 thread.start(); 133 getOkButton().setEnabled(false); 134 getCancelButton().setCaption(CmsVaadinUtils.messageClose()); 135 getCancelButton().addClickListener(new ClickListener() { 136 137 private static final long serialVersionUID = 1L; 138 139 public void buttonClick(ClickEvent event) { 140 141 run.run(); 142 143 } 144 }); 145 146 } catch (CmsException e) { 147 LOG.error(e.getLocalizedMessage(), e); 148 CmsErrorDialog.showErrorDialog(e); 149 } 150 } 151 152 }); 153 154 } 155 156 /** 157 * Gets the list of buttons for the form.<p> 158 * 159 * @return the buttons 160 */ 161 public List<Button> getButtons() { 162 163 return Arrays.asList(getOkButton(), getCancelButton()); 164 } 165 166 /** 167 * Gets the cancel button.<p> 168 * 169 * @return the cancel button 170 */ 171 protected abstract Button getCancelButton(); 172 173 /** 174 * Gets the OK button.<p> 175 * 176 * @return the OK button 177 */ 178 protected abstract Button getOkButton(); 179 180 /** 181 * Returns the site selector for the module import.<p> 182 * 183 * @return the site selector 184 **/ 185 protected abstract CmsAutoItemCreatingComboBox getSiteSelector(); 186 187 /** 188 * Takes the file name given in the upload and processes it to return the file name under which the upload should be stored in the file system.<p> 189 * 190 * @param name the upload file name 191 * @return the RFS file name 192 */ 193 protected String processFileName(String name) { 194 195 int pos = name.lastIndexOf("/"); 196 if (pos >= 0) { 197 name = name.substring(pos + 1); 198 } 199 pos = name.lastIndexOf("\\"); 200 if (pos >= 0) { 201 name = name.substring(pos + 1); 202 } 203 return name; 204 } 205 206 /** 207 * Validates the module file to be imported.<p> 208 */ 209 protected void validateModuleFile() { 210 211 try { 212 m_importFile.loadAndValidate(); 213 CmsModule importModule = m_importFile.getModule(); 214 String site = importModule.getSite(); 215 if (site != null) { 216 if (importModule.hasImportSite()) { 217 getSiteSelector().setEnabled(false); 218 getSiteSelector().setValue(site); 219 } else { 220 String itemId = CmsVaadinUtils.getPathItemId(getSiteSelector().getContainerDataSource(), site); 221 if (itemId != null) { 222 getSiteSelector().setValue(itemId); 223 } 224 } 225 } 226 getOkButton().setEnabled(true); 227 } catch (Exception e) { 228 LOG.info(e.getLocalizedMessage(), e); 229 m_importFile = null; 230 getOkButton().setEnabled(false); 231 CmsErrorDialog.showErrorDialog(e); 232 233 } 234 } 235}