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.setup.updater.dialogs;
029
030import org.opencms.setup.CmsUpdateBean;
031import org.opencms.setup.CmsUpdateUI;
032import org.opencms.setup.db.CmsUpdateDBManager;
033import org.opencms.setup.ui.CmsSetupErrorDialog;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.FontOpenCms;
036
037import java.util.Iterator;
038
039import com.vaadin.ui.Panel;
040import com.vaadin.v7.shared.ui.label.ContentMode;
041import com.vaadin.v7.ui.Label;
042import com.vaadin.v7.ui.VerticalLayout;
043
044/**
045 * DB settings dialog.<p>
046 */
047public class CmsUpdateStep02DBDialog extends A_CmsUpdateDialog {
048
049    /**vaadin serial id. */
050    private static final long serialVersionUID = 1L;
051
052    /**vaadin component. */
053    VerticalLayout m_contentLayout;
054
055    /**vaadin component. */
056    Label m_icon;
057
058    /**DB Manager. */
059    CmsUpdateDBManager m_dbBean;
060
061    /**
062     * @see org.opencms.setup.updater.dialogs.A_CmsUpdateDialog#init(org.opencms.setup.CmsUpdateUI)
063     */
064    @Override
065    public boolean init(CmsUpdateUI ui) {
066
067        CmsVaadinUtils.readAndLocalizeDesign(this, null, null);
068        super.init(ui, true, true);
069        setCaption("OpenCms Update-Wizard - Database upgrade");
070        CmsUpdateBean bean = ui.getUpdateBean();
071        bean.updateDBDriverProperties();
072        m_dbBean = new CmsUpdateDBManager();
073        try {
074            m_dbBean.initialize(bean);
075        } catch (Exception e) {
076            //
077        }
078
079        try {
080            bean.setDetectedVersion(m_dbBean.getDetectedVersion());
081            if (m_dbBean.needUpdate()) {
082                m_icon.setContentMode(ContentMode.HTML);
083                m_icon.setValue(FontOpenCms.WARNING.getHtml());
084                m_contentLayout.addComponent(getDisplayContent(m_dbBean));
085                return true;
086            } else {
087                ui.displayDialog(new CmsUpdateStep04SettingsDialog());
088                return false;
089            }
090        } catch (NullPointerException en) {
091            CmsSetupErrorDialog.showErrorDialog(
092                "Database error",
093                "Your database version is not compatible with OpenCms 11.");
094            return false;
095        }
096    }
097
098    /**
099     * @see org.opencms.setup.updater.dialogs.A_CmsUpdateDialog#getNextDialog()
100     */
101    @Override
102    A_CmsUpdateDialog getNextDialog() {
103
104        if (!m_dbBean.needUpdate()) {
105            return new CmsUpdateStep04SettingsDialog();
106        } else {
107            return new CmsUpdateStep03DBThreadDialog();
108        }
109    }
110
111    /**
112     * @see org.opencms.setup.updater.dialogs.A_CmsUpdateDialog#getPreviousDialog()
113     */
114    @Override
115    A_CmsUpdateDialog getPreviousDialog() {
116
117        return new CmsUpdateStep01LicenseDialog();
118    }
119
120    /**
121     * Get panel for given db pool.<p>
122     *
123     * @param dbBean to get info from
124     * @param pool to show panel for
125     * @return Panel
126     */
127    private Panel getDBPoolPanel(CmsUpdateDBManager dbBean, String pool) {
128
129        Panel res = new Panel();
130        res.setCaption(pool);
131        VerticalLayout layout = new VerticalLayout();
132        layout.setMargin(true);
133        String widthString = "300px";
134        layout.addComponent(getTableLikeLabel("JDBC Driver", dbBean.getDbDriver(pool), widthString));
135        layout.addComponent(getTableLikeLabel("JDBC Connection Url", dbBean.getDbUrl(pool), widthString));
136        layout.addComponent(getTableLikeLabel("JDBC Connection Url Params", dbBean.getDbParams(pool), widthString));
137        layout.addComponent(getTableLikeLabel("Database User", dbBean.getDbUser(pool), widthString));
138        res.setContent(layout);
139        return res;
140    }
141
142    /**
143     * Gets the content.<p>
144     *
145     * @param dbBean to create content for
146     * @return VerticalLayout
147     */
148    private VerticalLayout getDisplayContent(CmsUpdateDBManager dbBean) {
149
150        VerticalLayout res = new VerticalLayout();
151        res.setSpacing(true);
152        Label label = new Label();
153        label.setContentMode(ContentMode.HTML);
154        String html = "";
155        html += "<p>Detected Database is: " + dbBean.getDbName() + "</p>";
156        html += "<p>Following db pool(s) will be upgraded:</p>";
157        label.setValue(html);
158        res.addComponent(label);
159        Iterator<String> it = dbBean.getPools().iterator();
160        while (it.hasNext()) {
161            String pool = it.next();
162            res.addComponent(getDBPoolPanel(dbBean, pool));
163        }
164
165        return res;
166    }
167
168    /**
169     * Get table like labels.<p>
170     *
171     * @param key first Col
172     * @param value second Col
173     * @param keyWidth width of first column
174     * @return Label
175     */
176    private Label getTableLikeLabel(String key, String value, String keyWidth) {
177
178        Label res = new Label();
179        res.setContentMode(ContentMode.HTML);
180        String html = "<div style='display:flex'><div style='width:"
181            + keyWidth
182            + "'>"
183            + key
184            + "</div><div>"
185            + value
186            + "</div></div>";
187        res.setValue(html);
188        return res;
189    }
190}