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.sqlconsole;
029
030import org.opencms.db.CmsDbPoolV11;
031import org.opencms.main.OpenCms;
032import org.opencms.report.CmsStringBufferReport;
033import org.opencms.ui.A_CmsUI;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.apps.Messages;
036import org.opencms.ui.components.CmsBasicDialog;
037import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
038import org.opencms.ui.components.CmsErrorDialog;
039
040import java.util.ArrayList;
041import java.util.List;
042import java.util.Locale;
043
044import com.vaadin.ui.Button;
045import com.vaadin.ui.ComboBox;
046import com.vaadin.ui.TextArea;
047import com.vaadin.ui.VerticalLayout;
048import com.vaadin.ui.Window;
049
050/**
051 * Widget for the SQL console.<p>
052 */
053public class CmsSqlConsoleLayout extends VerticalLayout {
054
055    /** Serial version id. */
056    private static final long serialVersionUID = 1L;
057
058    /** The button to execute the SQL. */
059    protected Button m_ok;
060
061    /** Combo box for selecting the pool. */
062    protected ComboBox<String> m_pool;
063
064    /** Text area containing the SQL statements. */
065    protected TextArea m_script;
066
067    /** The executor. */
068    private CmsSqlConsoleExecutor m_console;
069
070    /**
071     * Creates a new instance.<p>
072     *
073     * @param console the SQL executor
074     */
075    public CmsSqlConsoleLayout(CmsSqlConsoleExecutor console) {
076
077        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
078        m_console = console;
079        m_ok.addClickListener(evt -> runQuery());
080        m_pool.setItems(OpenCms.getDbPoolNames());
081        m_pool.setValue(CmsDbPoolV11.getDefaultDbPoolName());
082        m_pool.setEmptySelectionAllowed(false);
083    }
084
085    /**
086     * Runs the currently entered query and displays the results.
087     */
088    protected void runQuery() {
089
090        String pool = m_pool.getValue();
091        String stmt = m_script.getValue();
092        if (stmt.trim().isEmpty()) {
093            return;
094        }
095        CmsStringBufferReport report = new CmsStringBufferReport(Locale.ENGLISH);
096        List<Throwable> errors = new ArrayList<>();
097        CmsSqlConsoleResults result = m_console.execute(stmt, pool, report, errors);
098        if (errors.size() > 0) {
099            CmsErrorDialog.showErrorDialog(report.toString() +  errors.get(0).getMessage(), errors.get(0));
100        } else {
101            Window window = CmsBasicDialog.prepareWindow(DialogWidth.max);
102            window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_SQLCONSOLE_QUERY_RESULTS_0));
103            window.setContent(new CmsSqlConsoleResultsForm(result, report.toString()));
104            A_CmsUI.get().addWindow(window);
105            window.center();
106        }
107
108    }
109
110}