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.module.CmsModule;
031import org.opencms.setup.CmsUpdateUI;
032import org.opencms.setup.CmsVaadinUpdateThread;
033import org.opencms.ui.CmsVaadinUtils;
034import org.opencms.ui.report.CmsStreamReportWidget;
035import org.opencms.util.CmsStringUtil;
036
037import java.io.FileNotFoundException;
038import java.io.FileOutputStream;
039import java.io.IOException;
040import java.io.OutputStream;
041import java.util.ArrayList;
042import java.util.HashMap;
043import java.util.HashSet;
044import java.util.List;
045import java.util.Map;
046import java.util.Set;
047
048import com.vaadin.server.FontAwesome;
049import com.vaadin.ui.CheckBox;
050import com.vaadin.ui.FormLayout;
051import com.vaadin.ui.Panel;
052import com.vaadin.ui.VerticalLayout;
053import com.vaadin.v7.shared.ui.label.ContentMode;
054import com.vaadin.v7.ui.HorizontalLayout;
055import com.vaadin.v7.ui.Label;
056
057/**
058 * The Module update dialog.<p>
059 */
060public class CmsUpdateStep05Modules extends A_CmsUpdateDialog {
061
062    /**vaadin serial id. */
063    private static final long serialVersionUID = 1L;
064
065    /**vaadin component. */
066    private FormLayout m_components;
067
068    /**vaadin component. */
069    private VerticalLayout m_mainLayout;
070
071    /**vaadin component. */
072    private VerticalLayout m_reportLayout;
073
074    /**vaadin component. */
075    private List<CheckBox> m_componentCheckboxes = new ArrayList<>();
076
077    /**Module map. */
078    private Map<String, CmsModule> m_componentMap = new HashMap<>();
079
080    /**vaadin component. */
081    Panel m_reportPanel;
082
083    /**vaadin component. */
084    Label m_icon;
085
086    /**vaadin component. */
087    Label m_iconFin;
088
089    /**vaadin component. */
090    HorizontalLayout m_running;
091
092    /**vaadin component. */
093    HorizontalLayout m_finished;
094
095    /**flag if XML changes are done. */
096    protected boolean m_isDone;
097
098    /**
099     * @see org.opencms.setup.updater.dialogs.A_CmsUpdateDialog#init(org.opencms.setup.CmsUpdateUI)
100     */
101    @Override
102    public boolean init(CmsUpdateUI ui) {
103
104        CmsVaadinUtils.readAndLocalizeDesign(this, null, null);
105        super.init(ui, true, true);
106        setCaption("OpenCms Update-Wizard - Update Modules");
107        Map<String, CmsModule> modules = ui.getUpdateBean().getAvailableModules();
108        initComponents(modules);
109        m_icon.setContentMode(ContentMode.HTML);
110        m_icon.setValue(FontAwesome.CLOCK_O.getHtml());
111        m_iconFin.setContentMode(ContentMode.HTML);
112        m_iconFin.setValue(FontAwesome.CHECK_CIRCLE_O.getHtml());
113        return true;
114    }
115
116    /**
117     * @see org.opencms.setup.updater.dialogs.A_CmsUpdateDialog#submitDialog()
118     */
119    @Override
120    protected boolean submitDialog() {
121
122        if (!m_isDone) {
123            fetchModulesFromDialog();
124            m_ui.getUpdateBean().prepareUpdateStep5();
125            m_mainLayout.setVisible(false);
126            m_reportLayout.setVisible(true);
127            m_finished.setVisible(false);
128            m_reportPanel.setContent(getReportContent());
129        }
130        return m_isDone;
131    }
132
133    /**
134     * @see org.opencms.setup.updater.dialogs.A_CmsUpdateDialog#getNextDialog()
135     */
136    @Override
137    A_CmsUpdateDialog getNextDialog() {
138
139        return new CmsUpdateStep06FinishDialog();
140    }
141
142    /**
143     * @see org.opencms.setup.updater.dialogs.A_CmsUpdateDialog#getPreviousDialog()
144     */
145    @Override
146    A_CmsUpdateDialog getPreviousDialog() {
147
148        return new CmsUpdateStep04SettingsDialog();
149    }
150
151    /**
152     *
153     */
154    private void fetchModulesFromDialog() {
155
156        Set<String> modules = new HashSet<>();
157
158        for (CheckBox checkbox : m_componentCheckboxes) {
159            CmsModule component = (CmsModule)(checkbox.getData());
160            if (checkbox.getValue().booleanValue()) {
161                modules.add(component.getName());
162            }
163        }
164
165        List<String> moduleList = new ArrayList<>(modules);
166        m_ui.getUpdateBean().setInstallModules(CmsStringUtil.listAsString(moduleList, "|"));
167    }
168
169    /**
170     * Get content.<p>
171     *
172     * @return VerticalLayout
173     */
174    private VerticalLayout getReportContent() {
175
176        VerticalLayout layout = new VerticalLayout();
177        layout.setHeight("100%");
178        final CmsStreamReportWidget report = new CmsStreamReportWidget();
179        report.setWidth("100%");
180        report.setHeight("100%");
181        enableOK(false);
182        Thread thread = new CmsVaadinUpdateThread(m_ui.getUpdateBean(), report);
183        thread.start();
184        try {
185            OutputStream logStream = new FileOutputStream(m_ui.getUpdateBean().getLogName());
186            report.setDelegateStream(logStream);
187            report.addReportFinishedHandler(() -> {
188
189                try {
190                    logStream.close();
191                    report.getStream().close();
192                    enableOK(true);
193                    m_finished.setVisible(true);
194                    m_running.setVisible(false);
195                    m_isDone = true;
196                } catch (IOException e) {
197                    //
198                }
199            });
200
201        } catch (FileNotFoundException e1) {
202            //
203        }
204        layout.addComponent(report);
205        return layout;
206    }
207
208    /**
209     * Init the module list.<p>
210     *
211     * @param modules to be displayed
212     */
213    private void initComponents(Map<String, CmsModule> modules) {
214
215        for (String moduleName : modules.keySet()) {
216            CmsModule module = modules.get(moduleName);
217            CheckBox checkbox = new CheckBox();
218            checkbox.setCaption(module.getNiceName() + " (" + module.getName() + " " + module.getVersionStr() + ")");
219            checkbox.setDescription(module.getDescription());
220            checkbox.setData(module);
221            checkbox.setValue(Boolean.TRUE);
222            m_components.addComponent(checkbox);
223            m_componentCheckboxes.add(checkbox);
224            m_componentMap.put(module.getName(), module);
225
226        }
227    }
228
229}