001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.convertxml;
029
030import org.opencms.file.types.I_CmsResourceType;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsIllegalArgumentException;
033import org.opencms.main.OpenCms;
034import org.opencms.util.CmsStringUtil;
035import org.opencms.widgets.CmsCheckboxWidget;
036import org.opencms.widgets.CmsSelectWidget;
037import org.opencms.widgets.CmsSelectWidgetOption;
038import org.opencms.widgets.CmsVfsFileWidget;
039import org.opencms.workplace.CmsWidgetDialog;
040import org.opencms.workplace.CmsWidgetDialogParameter;
041import org.opencms.workplace.CmsWorkplaceSettings;
042import org.opencms.workplace.tools.CmsToolDialog;
043import org.opencms.workplace.tools.CmsToolManager;
044
045import java.io.IOException;
046import java.util.ArrayList;
047import java.util.HashMap;
048import java.util.Iterator;
049import java.util.List;
050import java.util.Map;
051
052import jakarta.servlet.ServletException;
053import jakarta.servlet.http.HttpServletRequest;
054import jakarta.servlet.http.HttpServletResponse;
055import jakarta.servlet.jsp.PageContext;
056
057/**
058 * Widget dialog that sets the settings to replace HTML Tags in pages below a folder.
059 * <p>
060 *
061 * @since 7.0.5
062 */
063public class CmsConvertXmlDialog extends CmsWidgetDialog {
064
065    /** Localized message keys prefix. */
066    public static final String KEY_PREFIX = "convertxml";
067
068    /** Defines which pages are valid for this dialog. */
069    public static final String[] PAGES = {"page1"};
070
071    /** The import JSP report workplace URI. */
072    protected static final String CONVERTXML_ACTION_REPORT = PATH_WORKPLACE
073        + "admin/contenttools/reports/convertxml.jsp";
074
075    /** The settings object that is edited on this dialog. */
076    private CmsConvertXmlSettings m_settings;
077
078    /**
079     * Public constructor with JSP action element.
080     * <p>
081     *
082     * @param jsp an initialized JSP action element
083     */
084    public CmsConvertXmlDialog(CmsJspActionElement jsp) {
085
086        super(jsp);
087    }
088
089    /**
090     * Public constructor with JSP variables.
091     * <p>
092     *
093     * @param context the JSP page context
094     * @param req the JSP request
095     * @param res the JSP response
096     */
097    public CmsConvertXmlDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
098
099        this(new CmsJspActionElement(context, req, res));
100    }
101
102    /**
103     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
104     */
105    @Override
106    public void actionCommit() throws IOException, ServletException {
107
108        List errors = new ArrayList();
109        setDialogObject(m_settings);
110
111        try {
112
113            Map params = new HashMap();
114            // set style to display report in correct layout
115            params.put(PARAM_STYLE, CmsToolDialog.STYLE_NEW);
116            // set close link to get back to overview after finishing the import
117            params.put(PARAM_CLOSELINK, CmsToolManager.linkForToolPath(getJsp(), "/contenttools"));
118            // redirect to the report output JSP
119            getToolManager().jspForwardPage(this, CONVERTXML_ACTION_REPORT, params);
120
121        } catch (CmsIllegalArgumentException e) {
122            errors.add(e);
123        }
124        // set the list of errors to display when saving failed
125        setCommitErrors(errors);
126    }
127
128    /**
129     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
130     */
131    @Override
132    protected String createDialogHtml(String dialog) {
133
134        StringBuffer result = new StringBuffer(1024);
135
136        // create table
137        result.append(createWidgetTableStart());
138
139        // show error header once if there were validation errors
140        result.append(createWidgetErrorHeader());
141
142        // create export file name block
143        result.append(
144            createWidgetBlockStart(
145                Messages.get().getBundle(getLocale()).key(Messages.GUI_CONVERTXML_DIALOG_BLOCK_SETTINGS_0)));
146        result.append(createDialogRowsHtml(0, 4));
147        result.append(createWidgetBlockEnd());
148
149        // close table
150        result.append(createWidgetTableEnd());
151
152        return result.toString();
153    }
154
155    /**
156     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
157     */
158    @Override
159    protected void defineWidgets() {
160
161        // initialize the settings object to use for the dialog
162        initSettingsObject();
163
164        // set localized key prefix
165        setKeyPrefix(KEY_PREFIX);
166        // add the widgets to show
167        addWidget(
168            new CmsWidgetDialogParameter(
169                m_settings,
170                "resourceType",
171                PAGES[0],
172                new CmsSelectWidget(buildResourceTypeSelectWidgetList())));
173
174        addWidget(
175            new CmsWidgetDialogParameter(
176                m_settings,
177                "vfsFolder",
178                "/",
179                PAGES[0],
180                new CmsVfsFileWidget(false, getCms().getRequestContext().getSiteRoot()),
181                1,
182                1));
183
184        addWidget(new CmsWidgetDialogParameter(m_settings, "includeSubFolders", PAGES[0], new CmsCheckboxWidget("")));
185
186        addWidget(
187            new CmsWidgetDialogParameter(
188                m_settings,
189                "xslFile",
190                "/",
191                PAGES[0],
192                new CmsVfsFileWidget(false, getCms().getRequestContext().getSiteRoot()),
193                1,
194                1));
195
196        addWidget(new CmsWidgetDialogParameter(m_settings, "onlyCountFiles", PAGES[0], new CmsCheckboxWidget("")));
197    }
198
199    /**
200     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
201     */
202    @Override
203    protected String[] getPageArray() {
204
205        return PAGES;
206    }
207
208    /**
209     * @see org.opencms.workplace.CmsWorkplace#initMessages()
210     */
211    @Override
212    protected void initMessages() {
213
214        // add specific dialog resource bundle
215        addMessages(Messages.get().getBundleName());
216        // add workplace messages
217        addMessages("org.opencms.workplace.workplace");
218        // add default resource bundles
219        super.initMessages();
220    }
221
222    /**
223     * Initializes the settings object to work with depending on the dialog state and request
224     * parameters.
225     * <p>
226     */
227    protected void initSettingsObject() {
228
229        Object o;
230        if (CmsStringUtil.isEmpty(getParamAction())) {
231            o = new CmsConvertXmlSettings(getCms());
232        } else {
233            // this is not the initial call, get the job object from session
234            o = getDialogObject();
235        }
236
237        if (o == null) {
238            // create a new export handler object
239            m_settings = new CmsConvertXmlSettings(getCms());
240        } else {
241            // reuse export handler object stored in session
242            m_settings = (CmsConvertXmlSettings)o;
243        }
244
245    }
246
247    /**
248     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings,
249     *      jakarta.servlet.http.HttpServletRequest)
250     */
251    @Override
252    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
253
254        // initialize parameters and dialog actions in super implementation
255        super.initWorkplaceRequestValues(settings, request);
256
257        // save the current state of the export handler (may be changed because of the widget
258        // values)
259        setDialogObject(m_settings);
260    }
261
262    /**
263     * Builds the file format select widget list.<p>
264     *
265     * @return File format select widget list
266     */
267    private List buildResourceTypeSelectWidgetList() {
268
269        List fileFormats = new ArrayList();
270        // get all OpenCms resource types
271        List resourceTypes = OpenCms.getResourceManager().getResourceTypes();
272        // put for every resource type type id and name into list object for select widget
273        Iterator iter = resourceTypes.iterator();
274        while (iter.hasNext()) {
275            I_CmsResourceType type = (I_CmsResourceType)iter.next();
276            // only xml resource types
277            if (type.isDirectEditable() && !type.getTypeName().toUpperCase().equals("JSP")) {
278                CmsSelectWidgetOption option = new CmsSelectWidgetOption(
279                    Integer.valueOf(type.getTypeId()).toString(),
280                    false,
281                    type.getTypeName());
282                fileFormats.add(option);
283            }
284        }
285        return fileFormats;
286    }
287}