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