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.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 javax.servlet.ServletException;
053import javax.servlet.http.HttpServletRequest;
054import javax.servlet.http.HttpServletResponse;
055import javax.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(createWidgetBlockStart(
144            Messages.get().getBundle(getLocale()).key(Messages.GUI_CONVERTXML_DIALOG_BLOCK_SETTINGS_0)));
145        result.append(createDialogRowsHtml(0, 4));
146        result.append(createWidgetBlockEnd());
147
148        // close table
149        result.append(createWidgetTableEnd());
150
151        return result.toString();
152    }
153
154    /**
155     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
156     */
157    @Override
158    protected void defineWidgets() {
159
160        // initialize the settings object to use for the dialog
161        initSettingsObject();
162
163        // set localized key prefix
164        setKeyPrefix(KEY_PREFIX);
165        // add the widgets to show
166        addWidget(new CmsWidgetDialogParameter(
167            m_settings,
168            "resourceType",
169            PAGES[0],
170            new CmsSelectWidget(buildResourceTypeSelectWidgetList())));
171
172        addWidget(new CmsWidgetDialogParameter(
173            m_settings,
174            "vfsFolder",
175            "/",
176            PAGES[0],
177            new CmsVfsFileWidget(false, getCms().getRequestContext().getSiteRoot()),
178            1,
179            1));
180
181        addWidget(new CmsWidgetDialogParameter(m_settings, "includeSubFolders", PAGES[0], new CmsCheckboxWidget("")));
182
183        addWidget(
184            new CmsWidgetDialogParameter(
185                m_settings,
186                "xslFile",
187                "/",
188                PAGES[0],
189                new CmsVfsFileWidget(false, getCms().getRequestContext().getSiteRoot()),
190                1,
191                1));
192
193        addWidget(new CmsWidgetDialogParameter(m_settings, "onlyCountFiles", PAGES[0], new CmsCheckboxWidget("")));
194    }
195
196    /**
197     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
198     */
199    @Override
200    protected String[] getPageArray() {
201
202        return PAGES;
203    }
204
205    /**
206     * @see org.opencms.workplace.CmsWorkplace#initMessages()
207     */
208    @Override
209    protected void initMessages() {
210
211        // add specific dialog resource bundle
212        addMessages(Messages.get().getBundleName());
213        // add workplace messages
214        addMessages("org.opencms.workplace.workplace");
215        // add default resource bundles
216        super.initMessages();
217    }
218
219    /**
220     * Initializes the settings object to work with depending on the dialog state and request
221     * parameters.
222     * <p>
223     */
224    protected void initSettingsObject() {
225
226        Object o;
227        if (CmsStringUtil.isEmpty(getParamAction())) {
228            o = new CmsConvertXmlSettings(getCms());
229        } else {
230            // this is not the initial call, get the job object from session
231            o = getDialogObject();
232        }
233
234        if (o == null) {
235            // create a new export handler object
236            m_settings = new CmsConvertXmlSettings(getCms());
237        } else {
238            // reuse export handler object stored in session
239            m_settings = (CmsConvertXmlSettings)o;
240        }
241
242    }
243
244    /**
245     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings,
246     *      javax.servlet.http.HttpServletRequest)
247     */
248    @Override
249    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
250
251        // initialize parameters and dialog actions in super implementation
252        super.initWorkplaceRequestValues(settings, request);
253
254        // save the current state of the export handler (may be changed because of the widget
255        // values)
256        setDialogObject(m_settings);
257    }
258
259    /**
260     * Builds the file format select widget list.<p>
261     *
262     * @return File format select widget list
263     */
264    private List buildResourceTypeSelectWidgetList() {
265
266        List fileFormats = new ArrayList();
267        // get all OpenCms resource types
268        List resourceTypes = OpenCms.getResourceManager().getResourceTypes();
269        // put for every resource type type id and name into list object for select widget
270        Iterator iter = resourceTypes.iterator();
271        while (iter.hasNext()) {
272            I_CmsResourceType type = (I_CmsResourceType)iter.next();
273            // only xml resource types
274            if (type.isDirectEditable() && !type.getTypeName().toUpperCase().equals("JSP")) {
275                CmsSelectWidgetOption option = new CmsSelectWidgetOption(
276                    Integer.valueOf(type.getTypeId()).toString(),
277                    false,
278                    type.getTypeName());
279                fileFormats.add(option);
280            }
281        }
282        return fileFormats;
283    }
284}