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.content.updatexml;
029
030import org.opencms.jsp.CmsJspActionElement;
031import org.opencms.main.CmsIllegalArgumentException;
032import org.opencms.util.CmsStringUtil;
033import org.opencms.widgets.CmsCheckboxWidget;
034import org.opencms.widgets.CmsVfsFileWidget;
035import org.opencms.workplace.CmsWidgetDialog;
036import org.opencms.workplace.CmsWidgetDialogParameter;
037import org.opencms.workplace.CmsWorkplaceSettings;
038import org.opencms.workplace.tools.CmsToolDialog;
039import org.opencms.workplace.tools.CmsToolManager;
040
041import java.io.IOException;
042import java.util.ArrayList;
043import java.util.HashMap;
044import java.util.List;
045import java.util.Map;
046
047import javax.servlet.ServletException;
048import javax.servlet.http.HttpServletRequest;
049import javax.servlet.http.HttpServletResponse;
050import javax.servlet.jsp.PageContext;
051
052/**
053 * Widget dialog that sets the settings to replace HTML Tags in pages below a folder.
054 * <p>
055 *
056 * @since 7.0.5
057 */
058public class CmsUpdateXmlDialog extends CmsWidgetDialog {
059
060    /** Localized message keys prefix. */
061    public static final String KEY_PREFIX = "updatexml";
062
063    /** Defines which pages are valid for this dialog. */
064    public static final String[] PAGES = {"page1"};
065
066    /** The import JSP report workplace URI. */
067    protected static final String UPDATEXML_ACTION_REPORT = PATH_WORKPLACE + "admin/contenttools/reports/updatexml.jsp";
068
069    /** The settings object that is edited on this dialog. */
070    private CmsUpdateXmlSettings m_settings;
071
072    /**
073     * Public constructor with JSP action element.
074     * <p>
075     *
076     * @param jsp an initialized JSP action element
077     */
078    public CmsUpdateXmlDialog(CmsJspActionElement jsp) {
079
080        super(jsp);
081    }
082
083    /**
084     * Public constructor with JSP variables.
085     * <p>
086     *
087     * @param context the JSP page context
088     * @param req the JSP request
089     * @param res the JSP response
090     */
091    public CmsUpdateXmlDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
092
093        this(new CmsJspActionElement(context, req, res));
094    }
095
096    /**
097     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
098     */
099    @Override
100    public void actionCommit() throws IOException, ServletException {
101
102        List errors = new ArrayList();
103        setDialogObject(m_settings);
104
105        try {
106
107            Map params = new HashMap();
108            // set style to display report in correct layout
109            params.put(PARAM_STYLE, CmsToolDialog.STYLE_NEW);
110            // set close link to get back to overview after finishing the import
111            params.put(PARAM_CLOSELINK, CmsToolManager.linkForToolPath(getJsp(), "/contenttools"));
112            // redirect to the report output JSP
113            getToolManager().jspForwardPage(this, UPDATEXML_ACTION_REPORT, params);
114
115        } catch (CmsIllegalArgumentException e) {
116            errors.add(e);
117        }
118        // set the list of errors to display when saving failed
119        setCommitErrors(errors);
120    }
121
122    /**
123     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
124     */
125    @Override
126    protected String createDialogHtml(String dialog) {
127
128        StringBuffer result = new StringBuffer(1024);
129
130        // create table
131        result.append(createWidgetTableStart());
132
133        // show error header once if there were validation errors
134        result.append(createWidgetErrorHeader());
135
136        // create export file name block
137        result.append(createWidgetBlockStart(
138            Messages.get().getBundle(getLocale()).key(Messages.GUI_UPDATEXML_DIALOG_BLOCK_SETTINGS_0)));
139        result.append(createDialogRowsHtml(0, 1));
140        result.append(createWidgetBlockEnd());
141
142        // close table
143        result.append(createWidgetTableEnd());
144
145        return result.toString();
146    }
147
148    /**
149     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
150     */
151    @Override
152    protected void defineWidgets() {
153
154        // initialize the settings object to use for the dialog
155        initSettingsObject();
156
157        // set localized key prefix
158        setKeyPrefix(KEY_PREFIX);
159        // add the widgets to show
160        addWidget(new CmsWidgetDialogParameter(
161            m_settings,
162            "vfsFolder",
163            "/",
164            PAGES[0],
165            new CmsVfsFileWidget(false, getCms().getRequestContext().getSiteRoot()),
166            1,
167            1));
168
169        addWidget(new CmsWidgetDialogParameter(m_settings, "includeSubFolders", PAGES[0], new CmsCheckboxWidget("")));
170    }
171
172    /**
173     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
174     */
175    @Override
176    protected String[] getPageArray() {
177
178        return PAGES;
179    }
180
181    /**
182     * @see org.opencms.workplace.CmsWorkplace#initMessages()
183     */
184    @Override
185    protected void initMessages() {
186
187        // add specific dialog resource bundle
188        addMessages(Messages.get().getBundleName());
189        // add workplace messages
190        addMessages("org.opencms.workplace.workplace");
191        // add default resource bundles
192        super.initMessages();
193    }
194
195    /**
196     * Initializes the settings object to work with depending on the dialog state and request
197     * parameters.
198     * <p>
199     */
200    protected void initSettingsObject() {
201
202        Object o;
203        if (CmsStringUtil.isEmpty(getParamAction())) {
204            o = new CmsUpdateXmlSettings(getCms());
205        } else {
206            // this is not the initial call, get the job object from session
207            o = getDialogObject();
208        }
209
210        if (o == null) {
211            // create a new export handler object
212            m_settings = new CmsUpdateXmlSettings(getCms());
213        } else {
214            // reuse export handler object stored in session
215            m_settings = (CmsUpdateXmlSettings)o;
216        }
217
218    }
219
220    /**
221     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings,
222     *      javax.servlet.http.HttpServletRequest)
223     */
224    @Override
225    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
226
227        // initialize parameters and dialog actions in super implementation
228        super.initWorkplaceRequestValues(settings, request);
229
230        // save the current state of the export handler (may be changed because of the widget
231        // values)
232        setDialogObject(m_settings);
233    }
234}