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, 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.ui.contextmenu;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.I_CmsDialogContext;
036import org.opencms.ui.Messages;
037import org.opencms.ui.components.CmsLockedResourcesList;
038import org.opencms.util.CmsUUID;
039
040import java.util.Collections;
041import java.util.List;
042
043import org.apache.commons.logging.Log;
044
045import com.google.common.collect.Lists;
046
047/**
048 * A wrapper context menu action which first checks whether the resources for which the action is executed have any children
049 * locked by different users.<p>
050 *
051 * If so, a dialog showing these resources will be displayed; otherwise the wrapped context menu action
052 * will be executed.<p>
053 */
054public class CmsBlockingLockCheck implements I_CmsContextMenuAction {
055
056    /** Log instance for this class. */
057    private static final Log LOG = CmsLog.getLog(CmsBlockingLockCheck.class);
058
059    /** Context menu to execute if we don't have blocking locked resources. */
060    private I_CmsContextMenuAction m_nextAction;
061
062    /**
063     * Creates a new instance.<p>
064     *
065     * @param nextAction the action to execute if we don't have blocking locked resources
066     *
067     */
068    public CmsBlockingLockCheck(I_CmsContextMenuAction nextAction) {
069        m_nextAction = nextAction;
070    }
071
072    /**
073     * @see org.opencms.ui.contextmenu.I_CmsContextMenuAction#executeAction(org.opencms.ui.I_CmsDialogContext)
074     */
075    public void executeAction(final I_CmsDialogContext context) {
076
077        CmsObject cms = context.getCms();
078        List<CmsResource> resources = context.getResources();
079        List<CmsResource> blocked = Lists.newArrayList();
080        for (CmsResource resource : resources) {
081            try {
082                blocked.addAll(cms.getBlockingLockedResources(resource));
083            } catch (CmsException e) {
084                LOG.error(e.getLocalizedMessage(), e);
085            }
086        }
087        if (blocked.isEmpty()) {
088            m_nextAction.executeAction(context);
089        } else {
090
091            CmsLockedResourcesList dialog = new CmsLockedResourcesList(
092                cms,
093                blocked,
094                CmsVaadinUtils.getMessageText(Messages.GUI_CANT_PERFORM_OPERATION_BECAUSE_OF_LOCKED_RESOURCES_0),
095                new Runnable() {
096
097                    public void run() {
098
099                        List<CmsUUID> noStructureIds = Collections.emptyList();
100                        context.finish(noStructureIds);
101                    }
102
103                },
104                null);
105
106            context.start(
107                CmsVaadinUtils.getMessageText(org.opencms.workplace.explorer.Messages.GUI_EXPLORER_CONTEXT_LOCKS_0),
108                dialog);
109        }
110    }
111
112    /**
113     * @see java.lang.Object#toString()
114     */
115    @Override
116    public String toString() {
117
118        return "CmsBlockingLockCheck[" + m_nextAction.toString() + "]";
119    }
120}