001/*
002 * File   : $Source$
003 * Date   : $Date$
004 * Version: $Revision$
005 *
006 * This library is part of OpenCms -
007 * the Open Source Content Management System
008 *
009 * Copyright (C) 2002 - 2009 Alkacon Software (http://www.alkacon.com)
010 *
011 * This library is free software; you can redistribute it and/or
012 * modify it under the terms of the GNU Lesser General Public
013 * License as published by the Free Software Foundation; either
014 * version 2.1 of the License, or (at your option) any later version.
015 *
016 * This library is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 * Lesser General Public License for more details.
020 *
021 * For further information about Alkacon Software, please see the
022 * company website: http://www.alkacon.com
023 *
024 * For further information about OpenCms, please see the
025 * project website: http://www.opencms.org
026 *
027 * You should have received a copy of the GNU Lesser General Public
028 * License along with this library; if not, write to the Free Software
029 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030 */
031
032package org.opencms.workplace.tools.sites;
033
034import org.opencms.configuration.CmsSitesConfiguration;
035import org.opencms.jsp.CmsJspActionElement;
036import org.opencms.main.CmsException;
037import org.opencms.main.OpenCms;
038import org.opencms.site.CmsSite;
039import org.opencms.util.CmsFileUtil;
040import org.opencms.widgets.CmsComboWidget;
041import org.opencms.widgets.CmsSelectWidget;
042import org.opencms.widgets.CmsSelectWidgetOption;
043import org.opencms.widgets.CmsVfsFileWidget;
044import org.opencms.workplace.CmsWidgetDialog;
045import org.opencms.workplace.CmsWidgetDialogParameter;
046
047import java.util.ArrayList;
048import java.util.Collections;
049import java.util.List;
050
051import javax.servlet.http.HttpServletRequest;
052import javax.servlet.http.HttpServletResponse;
053import javax.servlet.jsp.PageContext;
054
055/**
056 * Configuration dialog for general site settings.<p>
057 *
058 * @since 9.0.0
059 */
060public class CmsSitesSettingsDialog extends CmsWidgetDialog {
061
062    /** Defines which pages are valid for this dialog. */
063    public static final String[] PAGES = {"page1"};
064
065    /** The URI of the site to be used as default site, default: '/sites/default/'. */
066    private String m_defaultUri;
067
068    /** The URI used as shared folder, default: '/shared/'. */
069    private String m_sharedFolder;
070
071    /** The server address of the workplace server, default: 'http://localhost:8080'. */
072    private String m_workplaceServer;
073
074    /**
075     * Public constructor with JSP action element.<p>
076     *
077     * @param jsp an initialized JSP action element
078     */
079    public CmsSitesSettingsDialog(CmsJspActionElement jsp) {
080
081        super(jsp);
082
083    }
084
085    /**
086     * Public constructor with JSP variables.<p>
087     *
088     * @param context the JSP page context
089     * @param req the JSP request
090     * @param res the JSP response
091     */
092    public CmsSitesSettingsDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
093
094        this(new CmsJspActionElement(context, req, res));
095    }
096
097    /**
098     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
099     */
100    @Override
101    public void actionCommit() {
102
103        try {
104            OpenCms.getSiteManager().updateGeneralSettings(
105                getCms(),
106                m_defaultUri,
107                Collections.singletonList(m_workplaceServer),
108                m_sharedFolder);
109        } catch (CmsException e) {
110            addCommitError(e);
111        }
112
113        if (!hasCommitErrors()) {
114            // write the system configuration
115            OpenCms.writeConfiguration(CmsSitesConfiguration.class);
116        }
117    }
118
119    /**
120     * Returns the defaultUri.<p>
121     *
122     * @return the defaultUri
123     */
124    public String getDefaultUri() {
125
126        return m_defaultUri;
127    }
128
129    /**
130     * Returns the sharedFolder.<p>
131     *
132     * @return the sharedFolder
133     */
134    public String getSharedFolder() {
135
136        return m_sharedFolder;
137    }
138
139    /**
140     * Returns the workplaceServer.<p>
141     *
142     * @return the workplaceServer
143     */
144    public String getWorkplaceServer() {
145
146        return m_workplaceServer;
147    }
148
149    /**
150     * Sets the defaultUri.<p>
151     *
152     * @param defaultUri the defaultUri to set
153     */
154    public void setDefaultUri(String defaultUri) {
155
156        m_defaultUri = defaultUri;
157    }
158
159    /**
160     * Sets the sharedFolder.<p>
161     *
162     * @param sharedFolder the sharedFolder to set
163     */
164    public void setSharedFolder(String sharedFolder) {
165
166        m_sharedFolder = sharedFolder;
167    }
168
169    /**
170     * Sets the workplaceServer.<p>
171     *
172     * @param workplaceServer the workplaceServer to set
173     */
174    public void setWorkplaceServer(String workplaceServer) {
175
176        m_workplaceServer = workplaceServer;
177    }
178
179    /**
180     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
181     */
182    @Override
183    protected String createDialogHtml(String dialog) {
184
185        StringBuffer result = new StringBuffer(1024);
186        result.append(createWidgetTableStart());
187        result.append(
188            dialogBlockStart(
189                Messages.get().getBundle(getCms().getRequestContext().getLocale()).key(
190                    Messages.GUI_SITES_GENERAL_SETTINGS_0)));
191        result.append(createWidgetTableStart());
192        result.append(createDialogRowsHtml(0, 2));
193        result.append(createWidgetTableEnd());
194        result.append(dialogBlockEnd());
195        result.append(createWidgetTableEnd());
196        return result.toString();
197    }
198
199    /**
200     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
201     */
202    @Override
203    protected void defineWidgets() {
204
205        setKeyPrefix(CmsSitesOverviewList.KEY_PREFIX_SITES);
206        setDialogObject(this);
207        // initialize members
208        m_workplaceServer = OpenCms.getSiteManager().getWorkplaceServer();
209        m_defaultUri = OpenCms.getSiteManager().getDefaultUri();
210        m_sharedFolder = OpenCms.getSiteManager().getSharedFolder();
211
212        List<CmsSelectWidgetOption> wpServerOptions = new ArrayList<CmsSelectWidgetOption>();
213        List<CmsSelectWidgetOption> defaultUriOptions = new ArrayList<CmsSelectWidgetOption>();
214
215        List<CmsSite> sites = OpenCms.getSiteManager().getAvailableSites(
216            getCms(),
217            true,
218            false,
219            getCms().getRequestContext().getOuFqn());
220        for (CmsSite site : sites) {
221            if (!((site.getSiteRoot() == null) || site.getSiteRoot().equals("") || site.getSiteRoot().equals("/"))) {
222                // is not null and not the root site => potential option
223                if (site.getSiteRoot().startsWith(
224                    CmsFileUtil.removeTrailingSeparator(OpenCms.getSiteManager().getDefaultUri()))) {
225                    // is the current default site use as default option
226                    CmsSelectWidgetOption option = new CmsSelectWidgetOption(
227                        site.getSiteRoot() + "/",
228                        true,
229                        site.getTitle(),
230                        site.getTitle());
231                    defaultUriOptions.add(option);
232                } else {
233                    // no default, create a option
234                    CmsSelectWidgetOption option = new CmsSelectWidgetOption(
235                        site.getSiteRoot() + "/",
236                        false,
237                        site.getTitle(),
238                        site.getTitle());
239                    defaultUriOptions.add(option);
240                }
241                if (site.getUrl().equals(OpenCms.getSiteManager().getWorkplaceServer())) {
242                    // is the current wp server use as default option
243                    CmsSelectWidgetOption option = new CmsSelectWidgetOption(
244                        site.getUrl(),
245                        true,
246                        site.getTitle(),
247                        site.getTitle());
248                    wpServerOptions.add(option);
249                } else {
250                    // no default, create a option
251                    CmsSelectWidgetOption option = new CmsSelectWidgetOption(
252                        site.getUrl(),
253                        false,
254                        site.getTitle(),
255                        site.getTitle());
256                    wpServerOptions.add(option);
257
258                }
259            }
260        }
261
262        addWidget(new CmsWidgetDialogParameter(this, "workplaceServer", PAGES[0], new CmsComboWidget(wpServerOptions)));
263        addWidget(new CmsWidgetDialogParameter(this, "defaultUri", PAGES[0], new CmsSelectWidget(defaultUriOptions)));
264        addWidget(
265            new CmsWidgetDialogParameter(
266                this,
267                "sharedFolder",
268                PAGES[0],
269                new CmsVfsFileWidget(false, "", false, false)));
270    }
271
272    /**
273     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
274     */
275    @Override
276    protected String[] getPageArray() {
277
278        return PAGES;
279    }
280}