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.explorer.menu;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.lock.CmsLock;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.security.CmsPermissionSet;
036import org.opencms.workplace.explorer.CmsResourceUtil;
037
038import org.apache.commons.logging.Log;
039
040/**
041 * Defines a menu item rule that sets the visibility to active
042 * if the current resource can be directly published by the current user.<p>
043 *
044 * @since 6.5.6
045 */
046public class CmsMirDirectPublish extends A_CmsMenuItemRule {
047
048    /** The log object for this class. */
049    private static final Log LOG = CmsLog.getLog(CmsMirDirectPublish.class);
050
051    /**
052     * @see org.opencms.workplace.explorer.menu.I_CmsMenuItemRule#getVisibility(org.opencms.file.CmsObject, CmsResourceUtil[])
053     */
054    @Override
055    public CmsMenuItemVisibilityMode getVisibility(CmsObject cms, CmsResourceUtil[] resourceUtil) {
056
057        if (!resourceUtil[0].isInsideProject()) {
058            return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE.addMessageKey(
059                Messages.GUI_CONTEXTMENU_TITLE_INACTIVE_PUBLISH_OTHERPROJECT_0);
060        }
061
062        CmsLock lock = resourceUtil[0].getLock();
063        if (lock.isNullLock()
064            || (lock.isExclusiveOwnedInProjectBy(
065                cms.getRequestContext().getCurrentUser(),
066                cms.getRequestContext().getCurrentProject()))) {
067            // resource is not locked or exclusively locked by current user in current project
068
069            try {
070                if (cms.hasPermissions(resourceUtil[0].getResource(), CmsPermissionSet.ACCESS_DIRECT_PUBLISH)) {
071                    // only activate if user has direct publish permissions
072                    if (resourceUtil[0].getResource().isFolder()
073                        || !resourceUtil[0].getResource().getState().isUnchanged()) {
074                        // resource is a folder or not unchanged
075                        CmsResource parent = cms.readFolder(
076                            CmsResource.getParentFolder(cms.getSitePath(resourceUtil[0].getResource())));
077                        if (parent.getState().isNew()) {
078                            return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE.addMessageKey(
079                                Messages.GUI_CONTEXTMENU_TITLE_INACTIVE_PUBLISH_PARENTFOLDER_0);
080                        }
081                        return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
082                    } else if (!resourceUtil[0].getResource().isFolder()
083                        && resourceUtil[0].getResource().getState().isUnchanged()) {
084                        return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE.addMessageKey(
085                            Messages.GUI_CONTEXTMENU_TITLE_INACTIVE_UNCHANGED_0);
086                    }
087                } else {
088                    return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE.addMessageKey(
089                        Messages.GUI_CONTEXTMENU_TITLE_INACTIVE_PERM_PUBLISH_0);
090                }
091            } catch (CmsException e) {
092                if (LOG.isErrorEnabled()) {
093                    LOG.error(e.getLocalizedMessage(), e);
094                }
095            }
096        }
097
098        if (lock.isInherited()) {
099            return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE.addMessageKey(
100                Messages.GUI_CONTEXTMENU_TITLE_INACTIVE_PUBLISH_INHERITED_0);
101        }
102        return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE.addMessageKey(
103            Messages.GUI_CONTEXTMENU_TITLE_INACTIVE_PUBLISH_NOT_LOCKED_0);
104    }
105
106    /**
107     * @see org.opencms.workplace.explorer.menu.I_CmsMenuItemRule#matches(org.opencms.file.CmsObject, CmsResourceUtil[])
108     */
109    public boolean matches(CmsObject cms, CmsResourceUtil[] resourceUtil) {
110
111        return true;
112    }
113
114}