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.CmsException;
032import org.opencms.security.CmsPermissionSet;
033import org.opencms.workplace.CmsMultiDialog;
034import org.opencms.workplace.CmsWorkplaceSettings;
035
036import java.util.Iterator;
037
038import javax.servlet.http.HttpServletRequest;
039import javax.servlet.http.HttpServletResponse;
040import javax.servlet.jsp.JspException;
041import javax.servlet.jsp.PageContext;
042
043/**
044 * Provides methods for the undelete resources dialog.<p>
045 *
046 * The following files use this class:
047 * <ul>
048 * <li>/commons/undelete.jsp
049 * </ul>
050 * <p>
051 *
052 * @since 6.0.0
053 */
054public class CmsUndelete extends CmsMultiDialog {
055
056    /** Value for the action: undelete resource. */
057    public static final int ACTION_UNDELETE = 100;
058
059    /** The dialog type. */
060    public static final String DIALOG_TYPE = "undelete";
061
062    /**
063     * Public constructor with JSP action element.<p>
064     *
065     * @param jsp an initialized JSP action element
066     */
067    public CmsUndelete(CmsJspActionElement jsp) {
068
069        super(jsp);
070    }
071
072    /**
073     * Public constructor with JSP variables.<p>
074     *
075     * @param context the JSP page context
076     * @param req the JSP request
077     * @param res the JSP response
078     */
079    public CmsUndelete(PageContext context, HttpServletRequest req, HttpServletResponse res) {
080
081        this(new CmsJspActionElement(context, req, res));
082    }
083
084    /**
085     * Performs the undelete action, will be called by the JSP page.<p>
086     *
087     * @throws JspException if problems including sub-elements occur
088     */
089    public void actionUndelete() throws JspException {
090
091        // save initialized instance of this class in request attribute for included sub-elements
092        getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
093        try {
094            if (performDialogOperation()) {
095                // if no exception is caused and "true" is returned delete operation was successful
096                actionCloseDialog();
097            } else {
098                // "false" returned, display "please wait" screen
099                getJsp().include(FILE_DIALOG_SCREEN_WAIT);
100            }
101        } catch (Throwable e) {
102            // error during deletion, show error dialog
103            includeErrorpage(this, e);
104        }
105    }
106
107    /**
108     * Returns the HTML for the localized undelete confirmation message depending on single or multi operation.<p>
109     *
110     * @return the HTML for the localized undelete confirmation message
111     */
112    public String buildConfirmationMessage() {
113
114        if (isMultiOperation()) {
115            return key(Messages.GUI_UNDELETE_MULTI_CONFIRMATION_0);
116        } else {
117            return key(Messages.GUI_UNDELETE_CONFIRMATION_0);
118        }
119    }
120
121    /**
122     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
123     */
124    @Override
125    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
126
127        // fill the parameter values in the get/set methods
128        fillParamValues(request);
129
130        // check the required permissions to undelete the resource
131        if (!checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
132            // no write permissions for the resource, set cancel action to close dialog
133            setParamAction(DIALOG_CANCEL);
134        }
135
136        // set the dialog type
137        setParamDialogtype(DIALOG_TYPE);
138        // set the action for the JSP switch
139        if (DIALOG_TYPE.equals(getParamAction())) {
140            setAction(ACTION_UNDELETE);
141        } else if (DIALOG_WAIT.equals(getParamAction())) {
142            setAction(ACTION_WAIT);
143        } else if (DIALOG_CANCEL.equals(getParamAction())) {
144            setAction(ACTION_CANCEL);
145        } else if (DIALOG_LOCKS_CONFIRMED.equals(getParamAction())) {
146            setAction(ACTION_LOCKS_CONFIRMED);
147        } else {
148            setAction(ACTION_DEFAULT);
149            // build title for delete dialog
150            setDialogTitle(Messages.GUI_UNDELETE_RESOURCE_1, Messages.GUI_UNDELETE_MULTI_2);
151        }
152    }
153
154    /**
155     * Performs the resource undeletion.<p>
156     *
157     * @return true, if the undelete operation is successful, otherwise false
158     * @throws CmsException if undeletion is not successful
159     */
160    @Override
161    protected boolean performDialogOperation() throws CmsException {
162
163        // check if the current resource is a folder for single operation
164        boolean isFolder = isOperationOnFolder();
165        // on folder undelete or multi operation display "please wait" screen, not for simple file undeletion
166        if ((isMultiOperation() || isFolder) && !DIALOG_WAIT.equals(getParamAction())) {
167            // return false, this will trigger the "please wait" screen
168            return false;
169        }
170
171        Iterator<String> i = getResourceList().iterator();
172        // iterate the resources to undelete
173        while (i.hasNext()) {
174            String resName = i.next();
175            try {
176                // lock resource if autolock is enabled
177                checkLock(resName);
178                // undelete the resource
179                getCms().undeleteResource(resName, true);
180            } catch (CmsException e) {
181                if (isMultiOperation()) {
182                    // collect exceptions to create a detailed output
183                    addMultiOperationException(e);
184                } else {
185                    // for single operation, throw the exception immediately
186                    throw e;
187                }
188            }
189        }
190        // check if exceptions occurred
191        checkMultiOperationException(Messages.get(), Messages.ERR_UNDELETE_MULTI_0);
192        return true;
193    }
194}