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.shell;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.main.CmsLog;
033import org.opencms.main.OpenCms;
034import org.opencms.ui.A_CmsUI;
035import org.opencms.ui.CmsVaadinUtils;
036import org.opencms.ui.apps.Messages;
037import org.opencms.ui.components.CmsBasicDialog;
038import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
039import org.opencms.util.CmsUUID;
040
041import org.apache.commons.logging.Log;
042
043import com.vaadin.ui.Button;
044import com.vaadin.ui.Button.ClickEvent;
045import com.vaadin.ui.Window;
046import com.vaadin.v7.ui.ComboBox;
047import com.vaadin.v7.ui.TextArea;
048import com.vaadin.v7.ui.VerticalLayout;
049
050/**
051 * Layout for shell script settings and input dialog.<p>
052 */
053public class CmsShellScriptLayout extends VerticalLayout {
054
055    /** The log instance for this class. */
056    static final Log LOG = CmsLog.getLog(CmsShellScriptLayout.class);
057
058    /**vaadin serial id. */
059    private static final long serialVersionUID = -7284574557422737112L;
060
061    /**Vaadin component. */
062    private ComboBox m_site;
063
064    /**Vaadin component. */
065    private ComboBox m_project;
066
067    /**Vaadin component. */
068    private TextArea m_script;
069
070    /**Vaadin component. */
071    private Button m_ok;
072
073    /**
074     * public constructor.<p>
075     */
076    public CmsShellScriptLayout() {
077
078        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
079        m_site.setContainerDataSource(CmsVaadinUtils.getAvailableSitesContainer(A_CmsUI.getCmsObject(), "caption"));
080        m_site.setItemCaptionPropertyId("caption");
081        m_site.select(A_CmsUI.getCmsObject().getRequestContext().getSiteRoot());
082        m_project.setContainerDataSource(CmsVaadinUtils.getProjectsContainer(A_CmsUI.getCmsObject(), "caption"));
083        m_project.setItemCaptionPropertyId("caption");
084        m_project.select(A_CmsUI.getCmsObject().getRequestContext().getCurrentProject().getUuid());
085        m_site.setNewItemsAllowed(false);
086        m_site.setNullSelectionAllowed(false);
087        m_project.setNewItemsAllowed(false);
088        m_project.setNullSelectionAllowed(false);
089
090        m_script.setValue(CmsVaadinUtils.getMessageText(Messages.GUI_SHELL_SCRIPT_APP_INI_COMMENT_0));
091
092        m_ok.addClickListener(new Button.ClickListener() {
093
094            private static final long serialVersionUID = 6836938102455627631L;
095
096            public void buttonClick(ClickEvent event) {
097
098                try {
099                    Window window = CmsBasicDialog.prepareWindow(DialogWidth.wide);
100
101                    CmsObject cms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
102                    setupCms(cms);
103                    CmsShellScriptThread thread = new CmsShellScriptThread(cms, getScript());
104                    CmsShellScriptReportDialog dialog = new CmsShellScriptReportDialog(thread, window);
105                    window.setContent(dialog);
106
107                    A_CmsUI.get().addWindow(window);
108                    thread.start();
109                } catch (CmsException e) {
110                    LOG.error("Unable to initialize CmsObject", e);
111                }
112            }
113        });
114    }
115
116    /**
117     * Gets the currently entered script.<p>
118     *
119     * @return String
120     */
121    protected String getScript() {
122
123        return m_script.getValue();
124    }
125
126    /**
127     * Sets up given CmsObject with currently set Project and Site.<p>
128     *
129     * @param cms CmsObject to gets adjusted to input fields
130     */
131    protected void setupCms(CmsObject cms) {
132
133        cms.getRequestContext().setUri("/");
134        cms.getRequestContext().setSiteRoot((String)m_site.getValue());
135        try {
136            cms.getRequestContext().setCurrentProject(cms.readProject((CmsUUID)m_project.getValue()));
137        } catch (CmsException e) {
138            LOG.error("Unable to read Project", e);
139        }
140    }
141
142}