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 GmbH & Co. KG, 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.workplace.tools.workplace;
029
030import org.opencms.db.CmsUserSettings;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.synchronize.CmsSynchronizeSettings;
033import org.opencms.widgets.CmsCheckboxWidget;
034import org.opencms.widgets.CmsInputWidget;
035import org.opencms.widgets.CmsVfsFileWidget;
036import org.opencms.workplace.CmsWidgetDialog;
037import org.opencms.workplace.CmsWidgetDialogParameter;
038import org.opencms.workplace.CmsWorkplaceSettings;
039
040import java.util.ArrayList;
041import java.util.List;
042
043import javax.servlet.http.HttpServletRequest;
044import javax.servlet.http.HttpServletResponse;
045import javax.servlet.jsp.PageContext;
046
047/**
048 * Dialog to edit the synchronize settings of the OpenCms Workplace.<p>
049 *
050 * @since 6.0.0
051 */
052public class CmsSynchronizeSettingsDialog extends CmsWidgetDialog {
053
054    /** Defines which pages are valid for this dialog. */
055    public static final String[] PAGES = {"page1"};
056
057    /** localized messages Keys prefix. */
058    public static final String KEY_PREFIX = "sync";
059
060    /** The synchronize settings which are edited on this dialog. */
061    private CmsSynchronizeSettings m_synchronizeSettings;
062
063    /**
064     * Public constructor with JSP action element.<p>
065     *
066     * @param jsp an initialized JSP action element
067     */
068    public CmsSynchronizeSettingsDialog(CmsJspActionElement jsp) {
069
070        super(jsp);
071    }
072
073    /**
074     * Public constructor with JSP variables.<p>
075     *
076     * @param context the JSP page context
077     * @param req the JSP request
078     * @param res the JSP response
079     */
080    public CmsSynchronizeSettingsDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
081
082        this(new CmsJspActionElement(context, req, res));
083    }
084
085    /**
086     * Commits the edited synchronize settings to the user settings.<p>
087     */
088    @Override
089    public void actionCommit() {
090
091        List<Throwable> errors = new ArrayList<Throwable>();
092
093        try {
094            // set the synchronize settings
095            CmsUserSettings userSettings = new CmsUserSettings(getCms());
096            m_synchronizeSettings.checkValues(getCms());
097            userSettings.setSynchronizeSettings(m_synchronizeSettings);
098            userSettings.save(getCms());
099            setDialogObject(null);
100        } catch (Throwable t) {
101            errors.add(t);
102        }
103
104        // set the list of errors to display when saving failed
105        setCommitErrors(errors);
106    }
107
108    /**
109     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
110     *
111     * This overwrites the method from the super class to create a layout variation for the widgets.<p>
112     *
113     * @param dialog the dialog (page) to get the HTML for
114     * @return the dialog HTML for all defined widgets of the named dialog (page)
115     */
116    @Override
117    protected String createDialogHtml(String dialog) {
118
119        StringBuffer result = new StringBuffer(1024);
120
121        // create widget table
122        result.append(createWidgetTableStart());
123
124        // show error header once if there were validation errors
125        result.append(createWidgetErrorHeader());
126
127        // create the widgets for the first dialog page
128        result.append(dialogBlockStart(key(Messages.GUI_EDITOR_LABEL_ACTIVATE_SYNC_BLOCK_0)));
129        result.append(createWidgetTableStart());
130        result.append(createDialogRowsHtml(0, 0));
131        result.append(createWidgetTableEnd());
132        result.append(dialogBlockEnd());
133        result.append(dialogBlockStart(key(Messages.GUI_EDITOR_LABEL_SOURCE_LIST_VFS_BLOCK_0)));
134        result.append(createWidgetTableStart());
135        result.append(createDialogRowsHtml(1, 1));
136        result.append(createWidgetTableEnd());
137        result.append(dialogBlockEnd());
138        result.append(dialogBlockStart(key(Messages.GUI_EDITOR_LABEL_DESTINATION_RFS_BLOCK_0)));
139        result.append(createWidgetTableStart());
140        result.append(createDialogRowsHtml(2, 2));
141        result.append(createWidgetTableEnd());
142        result.append(dialogBlockEnd());
143
144        // close widget table
145        result.append(createWidgetTableEnd());
146
147        return result.toString();
148    }
149
150    /**
151     * Creates the list of widgets for this dialog.<p>
152     */
153    @Override
154    protected void defineWidgets() {
155
156        // initialize the object to use for the dialog
157        initSynchronizeSettingsObject();
158        setKeyPrefix(KEY_PREFIX);
159        addWidget(new CmsWidgetDialogParameter(m_synchronizeSettings, "enabled", PAGES[0], new CmsCheckboxWidget()));
160        addWidget(
161            new CmsWidgetDialogParameter(
162                m_synchronizeSettings,
163                "destinationPathInRfs",
164                PAGES[0],
165                new CmsInputWidget()));
166        addWidget(
167            new CmsWidgetDialogParameter(
168                m_synchronizeSettings,
169                "sourceListInVfs",
170                "/",
171                PAGES[0],
172                new CmsVfsFileWidget(false, ""),
173                1,
174                CmsWidgetDialogParameter.MAX_OCCURENCES));
175
176    }
177
178    /**
179     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
180     */
181    @Override
182    protected String[] getPageArray() {
183
184        return PAGES;
185    }
186
187    /**
188     * @see org.opencms.workplace.CmsWorkplace#initMessages()
189     */
190    @Override
191    protected void initMessages() {
192
193        // add specific dialog resource bundle
194        addMessages(Messages.get().getBundleName());
195        // add default resource bundles
196        super.initMessages();
197    }
198
199    /**
200     * Initializes the synchronize settings object for this dialog.<p>
201     */
202    protected void initSynchronizeSettingsObject() {
203
204        Object o = getDialogObject();
205
206        if ((o == null) || !(o instanceof CmsSynchronizeSettings)) {
207            CmsUserSettings userSettings = new CmsUserSettings(getCms());
208            o = userSettings.getSynchronizeSettings();
209        }
210
211        if (o != null) {
212            m_synchronizeSettings = (CmsSynchronizeSettings)o;
213        } else {
214            m_synchronizeSettings = new CmsSynchronizeSettings();
215        }
216    }
217
218    /**
219     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
220     */
221    @Override
222    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
223
224        // initialize parameters and dialog actions in super implementation
225        super.initWorkplaceRequestValues(settings, request);
226
227        // save the current synchronize settings (may be changed because of the widget values)
228        setDialogObject(m_synchronizeSettings);
229    }
230
231}