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.check;
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.CmsDialog;
036import org.opencms.workplace.CmsWidgetDialog;
037import org.opencms.workplace.CmsWidgetDialogParameter;
038import org.opencms.workplace.CmsWorkplaceSettings;
039import org.opencms.workplace.tools.CmsToolDialog;
040import org.opencms.workplace.tools.database.CmsDatabaseExportReport;
041
042import java.util.ArrayList;
043import java.util.HashMap;
044import java.util.Iterator;
045import java.util.List;
046import java.util.Map;
047
048import javax.servlet.http.HttpServletRequest;
049import javax.servlet.http.HttpServletResponse;
050import javax.servlet.jsp.PageContext;
051
052/**
053 * Dialog for selecting the content checks.<p>
054 *
055 * @since 6.1.2
056 */
057public class CmsContentCheckDialog extends CmsWidgetDialog {
058
059    /** The dialog type. */
060    public static final String DIALOG_TYPE = "contentcheck";
061
062    /** Defines which pages are valid for this dialog. */
063    public static final String[] PAGES = {"page1"};
064
065    /** The content check JSP report workplace URI. */
066    protected static final String CONTENT_CHECK_REPORT = PATH_WORKPLACE + "admin/contenttools/check/report.jsp";
067
068    /** The content check JSP result workplace URI. */
069    protected static final String CONTENT_CHECK_RESULT = PATH_WORKPLACE + "admin/contenttools/check/result.jsp";
070
071    /** The Content Check object. */
072    private CmsContentCheck m_contentCheck;
073
074    /**
075     * Public constructor with JSP action element.<p>
076     *
077     * @param jsp an initialized JSP action element
078     */
079    public CmsContentCheckDialog(CmsJspActionElement jsp) {
080
081        super(jsp);
082
083    }
084
085    /**
086     * Public constructor with JSP variables.<p>
087     *
088     * @param context the JSP page context
089     * @param req the JSP request
090     * @param res the JSP response
091     */
092    public CmsContentCheckDialog(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() {
102
103        List errors = new ArrayList();
104        try {
105            // check if there are any vfs paths entered
106            List paths = m_contentCheck.getPaths();
107            if (paths.size() == 0) {
108                throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NO_VFSPATH_0));
109            } else {
110                Iterator i = paths.iterator();
111                while (i.hasNext()) {
112                    String path = (String)i.next();
113                    if (!getCms().existsResource(path)) {
114                        throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NO_VFSPATH_0));
115                    }
116                }
117            }
118
119            // check if there is at least one test activated
120            boolean isActive = false;
121            List plugins = m_contentCheck.getPlugins();
122            Iterator i = plugins.iterator();
123            while (i.hasNext()) {
124                I_CmsContentCheck plugin = (I_CmsContentCheck)i.next();
125                if (plugin.isActive()) {
126                    isActive = true;
127                }
128            }
129            if (!isActive) {
130                throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NO_TEST_0));
131            }
132
133            // if there was no error, store the content check object in the session and forward it to the
134            // check thread
135            setDialogObject(m_contentCheck);
136            Map params = new HashMap();
137            // set the name of this class to get dialog object in report
138            params.put(CmsDatabaseExportReport.PARAM_CLASSNAME, this.getClass().getName());
139            // set style to display report in correct layout
140            params.put(PARAM_STYLE, CmsToolDialog.STYLE_NEW);
141            // set close link to get back to overview after finishing the import
142            params.put(PARAM_CLOSELINK, getJsp().link(CONTENT_CHECK_RESULT));
143            //params.put(PARAM_CLOSELINK, CmsToolManager.linkForToolPath(getJsp(), "/contenttools"));
144            getToolManager().jspForwardPage(this, CONTENT_CHECK_REPORT, params);
145
146        } catch (Throwable t) {
147            errors.add(t);
148        }
149        // set the list of errors to display when saving failed
150        setCommitErrors(errors);
151    }
152
153    /**
154     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
155     *
156     * @param dialog the dialog (page) to get the HTML for
157     * @return the dialog HTML for all defined widgets of the named dialog (page)
158     */
159    @Override
160    protected String createDialogHtml(String dialog) {
161
162        StringBuffer result = new StringBuffer(1024);
163
164        // create table
165        result.append(createWidgetTableStart());
166
167        // show error header once if there were validation errors
168        result.append(createWidgetErrorHeader());
169
170        if (dialog.equals(PAGES[0])) {
171            result.append(dialogBlockStart(key("label.vfsresources")));
172            result.append(createWidgetTableStart());
173            result.append(createDialogRowsHtml(0, 0));
174            result.append(createWidgetTableEnd());
175            result.append(dialogBlockEnd());
176            result.append(dialogBlockStart(key("label.contentcheck")));
177            result.append(createWidgetTableStart());
178            result.append(createDialogRowsHtml(1, m_contentCheck.getPluginsCount()));
179            result.append(createWidgetTableEnd());
180            result.append(dialogBlockEnd());
181        }
182
183        // close table
184        result.append(createWidgetTableEnd());
185
186        return result.toString();
187    }
188
189    /**
190     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
191     */
192    @Override
193    protected void defineWidgets() {
194
195        initContentCheck();
196        addWidget(new CmsWidgetDialogParameter(m_contentCheck, "paths", PAGES[0], new CmsVfsFileWidget()));
197        // get a list of all plugins and build a widget for each plugin
198        List plugins = m_contentCheck.getPlugins();
199        for (int i = 0; i < plugins.size(); i++) {
200            I_CmsContentCheck plugin = (I_CmsContentCheck)plugins.get(i);
201            addWidget(
202                new CmsWidgetDialogParameter(
203                    plugin,
204                    I_CmsContentCheck.PARAMETER,
205                    plugin.getDialogParameterName(),
206                    PAGES[0],
207                    new CmsCheckboxWidget()));
208        }
209
210    }
211
212    /**
213     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
214     */
215    @Override
216    protected String[] getPageArray() {
217
218        return new String[] {"page1"};
219    }
220
221    /**
222     * Initializes the content check object or takes an exiting one which is stored in the sesstion.<p>
223     */
224    protected void initContentCheck() {
225
226        Object o;
227        if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
228            // this is the initial dialog call
229            o = null;
230        } else {
231            // this is not the initial call, get module from session
232            o = getDialogObject();
233        }
234
235        if (!(o instanceof CmsContentCheck)) {
236            // create a new content check object
237            m_contentCheck = new CmsContentCheck(getCms());
238
239        } else {
240            // reuse content check object stored in session
241            m_contentCheck = (CmsContentCheck)o;
242        }
243    }
244
245    /**
246     * @see org.opencms.workplace.CmsWorkplace#initMessages()
247     */
248    @Override
249    protected void initMessages() {
250
251        // add specific dialog resource bundle
252        addMessages(org.opencms.workplace.tools.content.Messages.get().getBundleName());
253        addMessages("org.opencms.workplace.workplace");
254        addMessages(Messages.get().getBundleName());
255
256        // now add the additional message bundles for each plugin
257        CmsContentCheck dummy = new CmsContentCheck(getCms());
258        List plugins = dummy.getPlugins();
259        Iterator i = plugins.iterator();
260        while (i.hasNext()) {
261            I_CmsContentCheck plugin = (I_CmsContentCheck)i.next();
262            List bundles = plugin.getMessageBundles();
263            Iterator j = bundles.iterator();
264            while (j.hasNext()) {
265                String bundle = (String)j.next();
266                addMessages(bundle);
267            }
268        }
269
270        // add default resource bundles
271        super.initMessages();
272    }
273
274    /**
275     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
276     */
277    @Override
278    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
279
280        // set the dialog type
281        setParamDialogtype(DIALOG_TYPE);
282
283        super.initWorkplaceRequestValues(settings, request);
284
285        // save the current state of the content check object (may be changed because of the widget values)
286        setDialogObject(m_contentCheck);
287    }
288
289}