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.sitemanager;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.site.CmsSSLMode;
037import org.opencms.site.CmsSite;
038import org.opencms.ui.A_CmsUI;
039import org.opencms.ui.CmsVaadinUtils;
040import org.opencms.ui.components.CmsBasicDialog;
041import org.opencms.ui.components.CmsErrorDialog;
042import org.opencms.ui.components.editablegroup.CmsEditableGroup;
043import org.opencms.ui.components.editablegroup.I_CmsEditableGroupRow;
044
045import java.util.ArrayList;
046import java.util.LinkedHashMap;
047import java.util.List;
048import java.util.Map;
049
050import org.apache.commons.logging.Log;
051
052import com.google.common.base.Supplier;
053import com.vaadin.ui.Button;
054import com.vaadin.ui.Button.ClickEvent;
055import com.vaadin.ui.Button.ClickListener;
056import com.vaadin.ui.Component;
057import com.vaadin.v7.data.util.BeanItemContainer;
058import com.vaadin.v7.ui.ComboBox;
059import com.vaadin.v7.ui.VerticalLayout;
060
061/**
062 *Class for the Global configuration dialog.<p>
063 */
064
065public class CmsGlobalForm extends CmsBasicDialog {
066
067    /** The logger for this class. */
068    private static Log LOG = CmsLog.getLog(CmsGlobalForm.class.getName());
069
070    /**vaadin serial id.*/
071    private static final long serialVersionUID = -3553152729226102382L;
072
073    /** Forbidden folder names.*/
074    List<String> m_forbiddenFolder = new ArrayList<String>() {
075
076        private static final long serialVersionUID = -52452684907673071L;
077
078        {
079            add("/sites/");
080            add("/system/");
081        }
082    };
083
084    /**Vaadin field.*/
085    private Button m_cancel;
086
087    /**Vaadin field.*/
088    private ComboBox m_fieldDefaultURI;
089
090    /**Vaadin field.*/
091    private ComboBox m_fieldSharedFolder;
092
093    /**Site manager instance. */
094    private CmsSiteManager m_manager;
095
096    /**Vaadin field.*/
097    private Button m_ok;
098
099    /**Vaadin layout for server.*/
100    private VerticalLayout m_serverLayout;
101
102    /**Edit group for workplace servers.*/
103    private CmsEditableGroup m_workplaceServerGroup;
104
105    /**CmsObject.*/
106    private CmsObject m_cms;
107
108    /**
109     * Constructor.<p>
110     *
111     * @param manager CmsSiteManager instance
112     */
113    public CmsGlobalForm(CmsSiteManager manager) {
114
115        m_manager = manager;
116
117        try {
118            m_cms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
119        } catch (CmsException e) {
120            LOG.error("Error on cloning CmsObject", e);
121        }
122        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
123
124        m_cms.getRequestContext().setSiteRoot("/");
125
126        List<CmsSite> allSites_temp = OpenCms.getSiteManager().getAvailableSites(
127            m_cms,
128            true,
129            false,
130            m_cms.getRequestContext().getOuFqn());
131
132        List<CmsSite> allSites = new ArrayList<CmsSite>(allSites_temp);
133
134        for (CmsSite site : allSites_temp) {
135            if ((site.getSiteRoot() == null) || site.getSiteRoot().equals("") || site.getSiteRoot().equals("/")) {
136
137                if (allSites_temp.indexOf(site) == (allSites_temp.size() - 1)) {
138                    allSites.remove(site);
139                    break;
140                } else {
141                    allSites.remove(site);
142                }
143            }
144        }
145        allSites_temp.clear();
146        setUpDefaultUriComboBox(allSites);
147        setUpSharedFolderComboBox();
148
149        setServerLayout(allSites);
150        m_fieldSharedFolder.setValue(OpenCms.getSiteManager().getSharedFolder().replace("/", ""));
151
152        m_ok.addClickListener(new ClickListener() {
153
154            private static final long serialVersionUID = -143413645462471704L;
155
156            public void buttonClick(ClickEvent event) {
157
158                submit();
159                closeDialog();
160            }
161
162        });
163        m_cancel.addClickListener(new ClickListener() {
164
165            private static final long serialVersionUID = 8880622123062328256L;
166
167            public void buttonClick(ClickEvent event) {
168
169                closeDialog();
170                return;
171            }
172        });
173    }
174
175    /**
176     * Selects given item of ComboBox.<p>
177     *
178     * @param site to be selected
179     * @param combo Combobox
180     */
181    static void selectNewWorkplaceServer(CmsSite site, ComboBox combo) {
182
183        combo.select(site);
184    }
185
186    /**
187     * Cancels site edit.<p>
188     */
189    void closeDialog() {
190
191        m_manager.closeDialogWindow(false);
192    }
193
194    /**
195     * Returns a list with all entered workplace servers.<p>
196     *
197     * @return a string list
198     */
199    Map<String, CmsSSLMode> getWebserverList() {
200
201        Map<String, CmsSSLMode> ret = new LinkedHashMap<String, CmsSSLMode>();
202        for (I_CmsEditableGroupRow row : m_workplaceServerGroup.getRows()) {
203            CmsWorkplaceServerWidget widget = (CmsWorkplaceServerWidget)row.getComponent();
204            ret.put(widget.getServer(), widget.getSSLMode());
205        }
206        return ret;
207    }
208
209    /**
210     * Save results.<p>
211     */
212    void submit() {
213
214        try {
215            m_manager.updateGeneralSettings(
216                m_cms,
217                ((CmsSite)m_fieldDefaultURI.getValue()).getSiteRoot(),
218                getWebserverList(),
219                "/" + (String)m_fieldSharedFolder.getValue() + "/");
220            if (!CmsEditSiteForm.FORBIDDEN_FOLDER_NAMES.isEmpty()) {
221                CmsEditSiteForm.FORBIDDEN_FOLDER_NAMES.set(1, (String)m_fieldSharedFolder.getValue());
222            }
223            // write the system configuration
224            // OpenCms.writeConfiguration(CmsSitesConfiguration.class);
225        } catch (Exception e) {
226            CmsErrorDialog.showErrorDialog(e);
227            //
228        }
229
230    }
231
232    /**
233     * Fills the layout with combo boxes for all setted workplace servers + one combo box for adding further urls.<p>
234     *
235     * @param sites from sitemanager
236     */
237    private void setServerLayout(final List<CmsSite> sites) {
238
239        m_workplaceServerGroup = new CmsEditableGroup(m_serverLayout, new Supplier<Component>() {
240
241            public Component get() {
242
243                CmsWorkplaceServerWidget row = new CmsWorkplaceServerWidget(sites, null);
244                return row;
245            }
246        }, "Add");
247
248        for (String server : OpenCms.getSiteManager().getWorkplaceServers()) {
249            CmsWorkplaceServerWidget row = new CmsWorkplaceServerWidget(sites, server);
250            m_workplaceServerGroup.addRow(row);
251        }
252    }
253
254    /**
255     * Set up of combo box for default uri.<p>
256     *
257     * @param allSites alls available sites
258     */
259    private void setUpDefaultUriComboBox(List<CmsSite> allSites) {
260
261        BeanItemContainer<CmsSite> objects = new BeanItemContainer<CmsSite>(CmsSite.class, allSites);
262        m_fieldDefaultURI.setContainerDataSource(objects);
263        m_fieldDefaultURI.setNullSelectionAllowed(false);
264        m_fieldDefaultURI.setTextInputAllowed(false);
265        m_fieldDefaultURI.setItemCaptionPropertyId("title");
266
267        //set value
268        String siteRoot = OpenCms.getSiteManager().getDefaultUri();
269        if (siteRoot.endsWith("/")) {
270            siteRoot = siteRoot.substring(0, siteRoot.length() - 1);
271        }
272        CmsSite site = OpenCms.getSiteManager().getSiteForSiteRoot(siteRoot);
273        m_fieldDefaultURI.setValue(site);
274    }
275
276    /**
277     * Set up of combo box for shared folder.<p>
278     */
279    private void setUpSharedFolderComboBox() {
280
281        m_fieldSharedFolder.setNullSelectionAllowed(false);
282        m_fieldSharedFolder.setTextInputAllowed(false);
283        try {
284            List<CmsResource> folderUnderRoot = m_cms.readResources("/", CmsResourceFilter.DEFAULT_FOLDERS, false);
285            for (CmsResource folder : folderUnderRoot) {
286                if (!m_forbiddenFolder.contains(folder.getRootPath())) {
287                    m_fieldSharedFolder.addItem(folder.getRootPath().replace("/", ""));
288                }
289            }
290        } catch (CmsException e) {
291            LOG.error("Error reading resource.", e);
292        }
293    }
294
295}