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;
029
030import org.opencms.setup.ui.A_CmsSetupStep;
031import org.opencms.setup.ui.CmsSetupErrorDialog;
032import org.opencms.setup.ui.CmsSetupStep01License;
033import org.opencms.setup.ui.CmsSetupStep02ComponentCheck;
034import org.opencms.setup.ui.CmsSetupStep03Database;
035import org.opencms.setup.ui.CmsSetupStep04Modules;
036import org.opencms.setup.ui.CmsSetupStep05ServerSettings;
037import org.opencms.setup.ui.CmsSetupStep06ImportReport;
038import org.opencms.setup.ui.CmsSetupStep07ConfigNotes;
039import org.opencms.setup.ui.I_SetupUiContext;
040import org.opencms.ui.A_CmsUI;
041import org.opencms.ui.components.CmsBasicDialog;
042import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
043import org.opencms.util.CmsStringUtil;
044
045import java.util.ArrayList;
046import java.util.Arrays;
047import java.util.List;
048
049import com.vaadin.annotations.Theme;
050import com.vaadin.server.ExternalResource;
051import com.vaadin.server.Resource;
052import com.vaadin.server.VaadinRequest;
053import com.vaadin.ui.Window;
054
055/**
056 * UI class for the setup wizard.
057 */
058@Theme("opencms")
059public class CmsSetupUI extends A_CmsUI implements I_SetupUiContext {
060
061    /** Serial version id. */
062    private static final long serialVersionUID = 1L;
063
064    /** List of setup step classes. */
065    private List<Class<? extends A_CmsSetupStep>> m_steps = new ArrayList<>();
066
067    /** Currently active window. */
068    private Window m_window;
069
070    /** Current step number. */
071    private int m_stepNo = 0;
072
073    /** The setup bean. */
074    private CmsSetupBean m_setupBean;
075
076    /**
077     * Gets external resource for an HTML page in the setup-resources folder.
078     *
079     * @param context the context
080     * @param name the file name
081     *
082     * @return the resource for the HTML page
083     */
084    public static Resource getSetupPage(I_SetupUiContext context, String name) {
085
086        String path = CmsStringUtil.joinPaths(context.getSetupBean().getContextPath(), CmsSetupBean.FOLDER_SETUP, name);
087        Resource resource = new ExternalResource(path);
088        return resource;
089    }
090
091    /**
092     * @see org.opencms.setup.ui.I_SetupUiContext#getSetupBean()
093     */
094    public CmsSetupBean getSetupBean() {
095
096        return m_setupBean;
097    }
098
099    public void stepBack() {
100
101        updateStep(m_stepNo - 1);
102    }
103
104    /**
105     * @see org.opencms.setup.ui.I_SetupUiContext#stepForward()
106     */
107    @Override
108    public void stepForward() {
109
110        updateStep(m_stepNo + 1);
111    }
112
113    @Override
114    protected void init(VaadinRequest request) {
115
116        try {
117            getPage().setTitle("OpenCms Setup");
118            this.addStyleName("opencms");
119            m_steps = Arrays.asList(
120                CmsSetupStep01License.class,
121                CmsSetupStep02ComponentCheck.class,
122                CmsSetupStep03Database.class,
123                CmsSetupStep04Modules.class,
124                CmsSetupStep05ServerSettings.class,
125                CmsSetupStep06ImportReport.class,
126                CmsSetupStep07ConfigNotes.class);
127
128            m_setupBean = new CmsSetupBean();
129            CmsSetupServlet servlet = CmsSetupServlet.getInstance();
130            m_setupBean.init(servlet.getServletContext(), servlet.getServletConfig());
131            if (!m_setupBean.getWizardEnabled()) {
132                throw new Exception(
133                    "The OpenCms setup wizard is not enabled! Please enable it in your opencms.properties.");
134            }
135
136            m_stepNo = 0;
137            updateStep(0);
138        } catch (Exception e) {
139            e.printStackTrace();
140            CmsSetupErrorDialog.showErrorDialog(e);
141
142        }
143    }
144
145    /**
146     * Shows the given step.
147     *
148     * @param step the step
149     */
150    protected void showStep(A_CmsSetupStep step) {
151
152        Window window = newWindow();
153        window.setContent(step);
154        window.setCaption(step.getTitle());
155        A_CmsUI.get().addWindow(window);
156        window.center();
157    }
158
159    /**
160     * Moves to the step with the given number.
161     *
162     * <p>The step number is only updated if no exceptions are thrown when instantiating/displaying the given step
163     *
164     * @param stepNo the step number to move to
165     */
166    protected void updateStep(int stepNo) {
167
168        if ((0 <= stepNo) && (stepNo < m_steps.size())) {
169            Class<? extends A_CmsSetupStep> cls = m_steps.get(stepNo);
170            A_CmsSetupStep step;
171            try {
172                step = cls.getConstructor(I_SetupUiContext.class).newInstance(this);
173                showStep(step);
174                m_stepNo = stepNo; // Only update step number if no exceptions
175            } catch (Exception e) {
176                CmsSetupErrorDialog.showErrorDialog(e);
177            }
178
179        }
180    }
181
182    /**
183     * Replaces active window with a new one and returns it.
184     *
185     * @return the new window
186     */
187    private Window newWindow() {
188
189        if (m_window != null) {
190            m_window.close();
191        }
192        Window window = CmsBasicDialog.prepareWindow(DialogWidth.wide);
193        m_window = window;
194        window.setDraggable(false);
195        window.setResizable(false);
196        window.setClosable(false);
197        return m_window;
198    }
199
200}