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.dbmanager;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.main.CmsLog;
033import org.opencms.main.OpenCms;
034import org.opencms.report.A_CmsReportThread;
035import org.opencms.ui.A_CmsUI;
036import org.opencms.ui.CmsVaadinUtils;
037import org.opencms.ui.components.CmsErrorDialog;
038import org.opencms.util.CmsUUID;
039
040import org.apache.commons.logging.Log;
041
042import com.vaadin.ui.Button;
043import com.vaadin.ui.Button.ClickEvent;
044import com.vaadin.ui.Button.ClickListener;
045import com.vaadin.v7.data.util.IndexedContainer;
046import com.vaadin.v7.ui.ComboBox;
047import com.vaadin.v7.ui.VerticalLayout;
048
049/**
050 * Abstract class for a form to import a file.<p>
051 */
052public abstract class A_CmsImportForm extends VerticalLayout {
053
054    /**Log object for class.*/
055    private static final Log LOG = CmsLog.getLog(A_CmsImportForm.class);
056
057    /**vaadin serial id.*/
058    private static final long serialVersionUID = -4074089542763339000L;
059
060    /**App which uses the form.*/
061    protected I_CmsReportApp m_app;
062
063    /**Import file object.*/
064    protected CmsImportFile m_importFile;
065
066    /**
067     * public constructor.<p>
068     *
069     * @param app calling instance of app
070     * */
071    public A_CmsImportForm(I_CmsReportApp app) {
072
073        CmsObject cms = A_CmsUI.getCmsObject();
074        m_app = app;
075        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
076        getOkButton().setEnabled(false);
077
078        final IndexedContainer availableSites = CmsVaadinUtils.getAvailableSitesContainer(cms, "caption");
079
080        getSiteSelector().setContainerDataSource(availableSites);
081        if (availableSites.getItem(cms.getRequestContext().getSiteRoot()) != null) {
082            getSiteSelector().setValue(cms.getRequestContext().getSiteRoot());
083        }
084        getSiteSelector().setNullSelectionAllowed(false);
085        getSiteSelector().setItemCaptionPropertyId("caption");
086
087        getProjectSelector().setContainerDataSource(
088            CmsVaadinUtils.getProjectsContainer(A_CmsUI.getCmsObject(), "caption"));
089        getProjectSelector().setItemCaptionPropertyId("caption");
090        getProjectSelector().select(A_CmsUI.getCmsObject().getRequestContext().getCurrentProject().getUuid());
091        getProjectSelector().setNewItemsAllowed(false);
092        getProjectSelector().setNullSelectionAllowed(false);
093        getProjectSelector().setTextInputAllowed(false);
094
095        if (getCancelButton() != null) {
096            getCancelButton().addClickListener(new ClickListener() {
097
098                private static final long serialVersionUID = -3475214711731413636L;
099
100                public void buttonClick(ClickEvent event) {
101
102                    m_app.goToMainView();
103                }
104            });
105        }
106        getOkButton().addClickListener(new ClickListener() {
107
108            private static final long serialVersionUID = 5651452508587710734L;
109
110            public void buttonClick(ClickEvent event) {
111
112                m_app.openReport(getReportPath(), getThread(), getTitle());
113            }
114        });
115
116    }
117
118    /**
119     * Gets a button for a cancel function.<p>
120     *
121     * @return a vaadin button
122     */
123    protected abstract Button getCancelButton();
124
125    /**
126     * Returns a cms object set to the site corresponding to siteselector.<p>
127     *
128     * @return a cms object
129     */
130    protected CmsObject getCmsObject() {
131
132        try {
133            CmsObject cms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
134            cms.getRequestContext().setSiteRoot((String)getSiteSelector().getValue());
135            cms.getRequestContext().setCurrentProject(cms.readProject((CmsUUID)getProjectSelector().getValue()));
136            return cms;
137        } catch (CmsException e) {
138            LOG.error("Unable to get CmsObject", e);
139        }
140        return null;
141
142    }
143
144    /**
145     * Gets a button for a ok function.<p>
146     *
147     * @return a vaadin button
148     */
149    protected abstract Button getOkButton();
150
151    /**
152     * Gets a combobox used for the site selector.<p>
153     *
154     * @return a vaadin combobox
155     */
156    protected abstract ComboBox getProjectSelector();
157
158    /**
159     * Get the path (state) for the app to show the report for the import thread.<p>
160     *
161     * @return path to be called for showing report of thread
162     */
163    protected abstract String getReportPath();
164
165    /**
166     * Gets a combobox used for the site selector.<p>
167     *
168     * @return a vaadin combobox
169     */
170    protected abstract ComboBox getSiteSelector();
171
172    /**
173     * Gets the thread which gets started by clicking the ok button.<p>
174     *
175     * @return a thread
176     */
177    protected abstract A_CmsReportThread getThread();
178
179    /**
180     * Gets the title of the report to show.<p>
181     *
182     * @return title name
183     */
184    protected abstract String getTitle();
185
186    /**
187     * Processes a filename.<p>
188     *
189     * @param name to be processed
190     * @return a valid file name
191     */
192    protected String processFileName(String name) {
193
194        int pos = name.lastIndexOf("/");
195        if (pos >= 0) {
196            name = name.substring(pos + 1);
197        }
198        pos = name.lastIndexOf("\\");
199        if (pos >= 0) {
200            name = name.substring(pos + 1);
201        }
202        return name;
203    }
204
205    /**
206     * Validates a file for module import.<p>
207     */
208    protected void validateModuleFile() {
209
210        try {
211            m_importFile.loadAndValidate();
212            String importSite = m_importFile.getModule().getImportSite();
213            if (importSite != null) {
214                getSiteSelector().setEnabled(false);
215                getSiteSelector().setValue(importSite);
216            }
217            getOkButton().setEnabled(true);
218        } catch (Exception e) {
219            LOG.info(e.getLocalizedMessage(), e);
220            m_importFile = null;
221            getOkButton().setEnabled(false);
222            CmsErrorDialog.showErrorDialog(e);
223
224        }
225    }
226}