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