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.commons;
029
030import org.opencms.jsp.CmsJspActionElement;
031import org.opencms.main.CmsRuntimeException;
032import org.opencms.util.CmsStringUtil;
033import org.opencms.util.CmsUUID;
034import org.opencms.workplace.CmsDialog;
035import org.opencms.workplace.CmsWorkplace;
036import org.opencms.workplace.CmsWorkplaceSettings;
037import org.opencms.workplace.list.A_CmsListDialog;
038import org.opencms.workplace.list.CmsHtmlList;
039
040import java.io.IOException;
041import java.util.ArrayList;
042import java.util.Iterator;
043import java.util.List;
044
045import javax.servlet.ServletException;
046import javax.servlet.http.HttpServletRequest;
047import javax.servlet.http.HttpServletResponse;
048import javax.servlet.jsp.JspException;
049import javax.servlet.jsp.PageContext;
050
051/**
052 * Creates the dialogs for showing and restoring deleted resources.<p>
053 *
054 * The following files use this class:
055 * <ul>
056 * <li>/commons/show-deleted.jsp
057 * </ul>
058 * <p>
059 *
060 * @since 6.9.1
061 */
062public class CmsDeletedResources extends CmsDialog {
063
064    /** Request parameter name for the relatedresources parameter. */
065    public static final String PARAM_READTREE = "readtree";
066
067    /** The list action. */
068    private String m_paramListAction;
069
070    /** Parameter value, if the deleted resources should be displayed for the subtree. */
071    private String m_paramReadtree;
072
073    /** The selected items, comma separated list. */
074    private String m_paramSelItems;
075
076    /**
077     * Public constructor with JSP action element.<p>
078     *
079     * @param jsp an initialized JSP action element
080     */
081    public CmsDeletedResources(CmsJspActionElement jsp) {
082
083        super(jsp);
084    }
085
086    /**
087     * Public constructor with JSP variables.<p>
088     *
089     * @param context the JSP page context
090     * @param req the JSP request
091     * @param res the JSP response
092     */
093    public CmsDeletedResources(PageContext context, HttpServletRequest req, HttpServletResponse res) {
094
095        this(new CmsJspActionElement(context, req, res));
096    }
097
098    /**
099     * Returns the html code to include the needed js code.<p>
100     *
101     * @return html code
102     */
103    public String buildIncludeJs() {
104
105        StringBuffer html = new StringBuffer(512);
106
107        html.append("<script  src='");
108        html.append(CmsWorkplace.getSkinUri());
109        html.append("commons/ajax.js'></script>\n");
110
111        html.append("<script  src='");
112        html.append(CmsWorkplace.getSkinUri());
113        html.append("editors/xmlcontent/help.js'></script>\n");
114
115        html.append("<script  src='");
116        html.append(CmsWorkplace.getSkinUri());
117        html.append("admin/javascript/general.js'></script>\n");
118
119        html.append("<script  src='");
120        html.append(CmsWorkplace.getSkinUri());
121        html.append("admin/javascript/list.js'></script>\n");
122
123        html.append("<script ><!--\n");
124
125        html.append("\tvar ");
126        html.append(CmsHtmlList.NO_SELECTION_HELP_VAR);
127        html.append(" = '");
128        html.append(
129            CmsStringUtil.escapeJavaScript(key(org.opencms.workplace.list.Messages.GUI_LIST_ACTION_NO_SELECTION_0)));
130        html.append("';\n");
131
132        html.append("function doReportUpdate(msg, state) {\n");
133        html.append("\tvar img = state + '.png';\n");
134        html.append("\tvar txt = '';\n");
135        html.append("\tvar elem = document.getElementById('ajaxreport');\n");
136        html.append("\tif (state != 'ok') {\n");
137        html.append("\t\tif (state == 'fatal') {\n");
138        html.append("\t\t\timg = 'error.png';\n");
139        html.append("\t\t\ttxt = '");
140        html.append(key(org.opencms.workplace.Messages.GUI_AJAX_REPORT_GIVEUP_0));
141        html.append("';\n");
142        html.append("\t\t} else if (state == 'wait') {\n");
143        html.append("\t\t\timg = 'wait.gif';\n");
144        html.append("\t\t\ttxt = '");
145        html.append(key(org.opencms.workplace.Messages.GUI_AJAX_REPORT_WAIT_0));
146        html.append("'\n");
147        html.append("\t\t} else if (state == 'error') {\n");
148        html.append("\t\t\ttxt = '");
149        html.append(key(org.opencms.workplace.Messages.GUI_AJAX_REPORT_ERROR_0));
150        html.append("' + msg;\n");
151        html.append("\t\t}\n");
152        html.append("\t} else {");
153        html.append("\t\telem.innerHTML = msg;\n");
154        html.append("\t}\n");
155        html.append("\tif (txt != '') {\n");
156        html.append("\t\tvar html = \"<table border='0' style='vertical-align:middle; height: 200px;'>\";");
157        html.append("\t\thtml += \"<tr><td width='40' align='center' valign='middle'><img src='");
158        html.append(getSkinUri());
159        html.append("commons/\";");
160        html.append("\t\thtml += img;");
161        html.append("\t\thtml += \"' width='32' height='32' alt=''></td>\";");
162        html.append("\t\thtml += \"<td valign='middle'><span style='color: #000099; font-weight: bold;'>\";");
163        html.append("\t\thtml += txt;");
164        html.append("\t\thtml += \"</span><br></td></tr></table>\";");
165        html.append("\t\telem.innerHTML = html;");
166        html.append("\t}\n");
167        html.append("}\n");
168
169        html.append("function reloadReport() {\n");
170        html.append("\tvar readtree = document.forms[\"");
171        html.append(CmsDeletedResourcesList.LIST_ID);
172        html.append("-form\"].");
173        html.append(PARAM_READTREE);
174        html.append(".checked;\n");
175        html.append("\tmakeRequest('");
176        html.append(getJsp().link("/system/workplace/commons/report-deleted.jsp"));
177        html.append("', '");
178        html.append(CmsDialog.PARAM_RESOURCE);
179        html.append("=");
180        html.append(getParamResource());
181        html.append("&");
182        html.append(PARAM_READTREE);
183        html.append("=");
184        html.append("' + readtree");
185        html.append(", 'doReportUpdate');\n");
186        html.append("}\n");
187
188        html.append("// -->\n");
189        html.append("</script>\n");
190
191        return html.toString();
192    }
193
194    /**
195     * Override to display additional options in the dialog.<p>
196     *
197     * @return html code to display additional options
198     */
199    public String buildOptions() {
200
201        StringBuffer result = new StringBuffer(128);
202
203        result.append("<input type='checkbox' name='");
204        result.append(PARAM_READTREE);
205        result.append("' value='true' onclick=\"reloadReport();\"");
206        if (Boolean.valueOf(getParamReadtree()).booleanValue()) {
207            result.append(" checked='checked'");
208        }
209        result.append(">&nbsp;");
210        result.append(key(Messages.GUI_DELETED_RESOURCES_READ_TREE_0));
211
212        return result.toString();
213    }
214
215    /**
216     * Builds the html code for the report with the list of deleted resources.<p>
217     *
218     * @return html code for the report with the list of deleted resources
219     *
220     * @throws JspException if dialog actions fail
221     * @throws IOException in case of errros forwarding to the required result page
222     * @throws ServletException in case of errros forwarding to the required result page
223     */
224    public String buildReport() throws JspException, ServletException, IOException {
225
226        CmsDeletedResourcesList list = new CmsDeletedResourcesList(
227            getJsp(),
228            getParamResource(),
229            Boolean.valueOf(getParamReadtree()).booleanValue());
230
231        list.actionDialog();
232        list.getList().setBoxed(false);
233
234        StringBuffer result = new StringBuffer(512);
235        result.append("<div style='height:200px; overflow: auto;'>\n");
236        result.append(list.getList().listHtml());
237        result.append("</div>\n");
238        return result.toString();
239    }
240
241    /**
242     * Builds the java script code to build the report.<p>
243     *
244     * @return html code with the java script to use ajax to create the report
245     */
246    public String buildReportRequest() {
247
248        StringBuffer html = new StringBuffer(512);
249
250        html.append("<script ><!--\n");
251        html.append("makeRequest('");
252        html.append(getJsp().link("/system/workplace/commons/report-deleted.jsp"));
253        html.append("', '");
254        html.append(CmsDialog.PARAM_RESOURCE);
255        html.append("=");
256        html.append(getParamResource());
257        html.append("', 'doReportUpdate');\n");
258        html.append("// -->\n");
259        html.append("</script>\n");
260
261        return html.toString();
262    }
263
264    /**
265     * Builds the necessary button row.<p>
266     *
267     * @return the button row
268     */
269    public String dialogButtons() {
270
271        return dialogButtonsOkCancel(
272            " onclick=\"listMAction('"
273                + CmsDeletedResourcesList.LIST_ID
274                + "','"
275                + CmsDeletedResourcesList.LIST_MACTION_RESTORE
276                + "', '', noSelHelp);\"",
277            null);
278    }
279
280    /**
281     * Executes the actions from the deleted resources list.<p>
282     *
283     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
284     *
285     * @throws JspException if problems including sub-elements occur
286     */
287    public void executeListMultiActions() throws JspException {
288
289        if (getParamListAction().equals(CmsDeletedResourcesList.LIST_MACTION_RESTORE)) {
290
291            StringBuffer errors = new StringBuffer();
292
293            Iterator<String> iter = getSelectedItems().iterator();
294            while (iter.hasNext()) {
295                CmsUUID id = new CmsUUID(iter.next());
296                try {
297                    getCms().restoreDeletedResource(id);
298                } catch (Exception e) {
299                    errors.append(e.getLocalizedMessage());
300                }
301            }
302
303            if (errors.length() > 0) {
304                throw new CmsRuntimeException(
305                    Messages.get().container(Messages.ERR_RESTORE_SELECTED_RESOURCES_1, errors));
306            } else {
307                actionCloseDialog();
308            }
309        }
310    }
311
312    /**
313     * Returns the paramListAction.<p>
314     *
315     * @return the paramListAction
316     */
317    public String getParamListAction() {
318
319        return m_paramListAction;
320    }
321
322    /**
323     * Returns the paramReadtree.<p>
324     *
325     * @return the paramReadtree
326     */
327    public String getParamReadtree() {
328
329        if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_paramReadtree)) {
330            return "false";
331        }
332
333        return m_paramReadtree;
334    }
335
336    /**
337     * Returns the paramSelItems.<p>
338     *
339     * @return the paramSelItems
340     */
341    public String getParamSelItems() {
342
343        return m_paramSelItems;
344    }
345
346    /**
347     * Returns a list of resource ids of the current selected items.<p>
348     *
349     * @return a list of resource ids of the current selected items
350     */
351    public List<String> getSelectedItems() {
352
353        Iterator<?> it = CmsStringUtil.splitAsList(getParamSelItems(), CmsHtmlList.ITEM_SEPARATOR, true).iterator();
354        List<String> items = new ArrayList<String>();
355        while (it.hasNext()) {
356            String id = (String)it.next();
357            items.add(id);
358        }
359        return items;
360    }
361
362    /**
363     * @see org.opencms.workplace.CmsWorkplace#paramsAsHidden()
364     */
365    @Override
366    public String paramsAsHidden() {
367
368        List<String> excludes = new ArrayList<String>();
369        excludes.add(PARAM_READTREE);
370        return paramsAsHidden(excludes);
371    }
372
373    /**
374     * Sets the paramListAction.<p>
375     *
376     * @param paramListAction the paramListAction to set
377     */
378    public void setParamListAction(String paramListAction) {
379
380        m_paramListAction = paramListAction;
381    }
382
383    /**
384     * Sets the paramReadtree.<p>
385     *
386     * @param paramReadtree the paramReadtree to set
387     */
388    public void setParamReadtree(String paramReadtree) {
389
390        m_paramReadtree = paramReadtree;
391    }
392
393    /**
394     * Sets the paramSelItems.<p>
395     *
396     * @param paramSelItems the paramSelItems to set
397     */
398    public void setParamSelItems(String paramSelItems) {
399
400        m_paramSelItems = paramSelItems;
401    }
402
403    /**
404     * @see org.opencms.workplace.CmsDialog#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
405     */
406    @Override
407    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
408
409        super.initWorkplaceRequestValues(settings, request);
410
411        // set title
412        setParamTitle(key(Messages.GUI_DELETED_RESOURCES_TITLE_1, new Object[] {getParamResource()}));
413
414        // init params to appear as hidden field
415        if (getParamListAction() == null) {
416            setParamListAction("");
417        }
418        if (getParamSelItems() == null) {
419            setParamSelItems("");
420        }
421
422        if (A_CmsListDialog.LIST_MULTI_ACTION.equals(getParamAction())) {
423            setAction(A_CmsListDialog.ACTION_LIST_MULTI_ACTION);
424        }
425    }
426}