001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.actions;
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.A_CmsUI;
035import org.opencms.ui.CmsVaadinUtils;
036import org.opencms.ui.I_CmsDialogContext;
037import org.opencms.ui.Messages;
038import org.opencms.ui.components.CmsLockedResourcesList;
039import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
040import org.opencms.ui.contextmenu.CmsStandardVisibilityCheck;
041import org.opencms.util.CmsUUID;
042
043import java.util.ArrayList;
044import java.util.List;
045
046import org.apache.commons.logging.Log;
047
048import com.google.common.collect.Lists;
049
050/**
051 * Action to lock a folder if it isn't already locked, with a dialog asking to confirm your choice
052 * if the folder contains resources locked by other users.
053 */
054public class CmsLockAction extends A_CmsWorkplaceAction {
055
056    /** Context data for an action. */
057    class Context {
058
059        /**
060         * Action for the cancel button.<p>
061         */
062        class ActionCancel implements Runnable {
063
064            /**
065             * @see java.lang.Runnable#run()
066             */
067            public void run() {
068
069                m_context.finish(new ArrayList<CmsUUID>());
070            }
071        }
072
073        /**
074         * OK button action.<p>
075         */
076        class ActionOk implements Runnable {
077
078            /**
079             * @see java.lang.Runnable#run()
080             */
081            public void run() {
082
083                doLock();
084            }
085
086        }
087
088        /** The dialog context. */
089        I_CmsDialogContext m_context;
090
091        /**
092         * Creates a new instance.<p>
093         *
094         * @param context the dialog context
095         */
096        public Context(I_CmsDialogContext context) {
097
098            m_context = context;
099        }
100
101        /**
102         * Actually locks the resources.<p>
103         */
104        protected void doLock() {
105
106            CmsObject cms = A_CmsUI.getCmsObject();
107            CmsException storedException = null;
108            List<CmsUUID> changedIds = Lists.newArrayList();
109            for (CmsResource res : m_context.getResources()) {
110                try {
111                    cms.lockResource(res);
112                    changedIds.add(res.getStructureId());
113                } catch (CmsException e) {
114                    if (storedException == null) {
115                        storedException = e;
116                    }
117                    LOG.warn(e.getLocalizedMessage(), e);
118                }
119
120            }
121            if (storedException != null) {
122                m_context.finish(changedIds);
123                m_context.error(storedException);
124            } else {
125                m_context.finish(changedIds);
126            }
127        }
128
129        /**
130         * Executes the action.<p>
131         */
132        void executeAction() {
133
134            CmsObject cms = A_CmsUI.getCmsObject();
135            List<CmsResource> blockingLocked = Lists.newArrayList();
136            for (CmsResource res : m_context.getResources()) {
137                try {
138                    List<CmsResource> blockingLockedForCurrentResource = cms.getBlockingLockedResources(res);
139                    blockingLocked.addAll(blockingLockedForCurrentResource);
140                } catch (CmsException e) {
141                    LOG.warn(e.getLocalizedMessage(), e);
142                }
143            }
144            if (blockingLocked.isEmpty()) {
145                doLock();
146            } else {
147                String messageKey = m_context.getResources().size() > 1
148                ? org.opencms.workplace.commons.Messages.GUI_LOCK_MULTI_INFO_LOCKEDSUBRESOURCES_0
149                : org.opencms.workplace.commons.Messages.GUI_LOCK_INFO_LOCKEDSUBRESOURCES_0;
150                CmsLockedResourcesList widget = new CmsLockedResourcesList(
151                    cms,
152                    blockingLocked,
153                    CmsVaadinUtils.getMessageText(messageKey),
154                    new ActionOk(),
155                    new ActionCancel());
156                widget.displayResourceInfo(m_context.getResources());
157                m_context.start(CmsVaadinUtils.getMessageText(Messages.GUI_LOCK_DIALOG_TITLE_0), widget);
158            }
159        }
160    }
161
162    /** The action id. */
163    public static final String ACTION_ID = "lock";
164
165    /** The logger instance for this class. */
166    static final Log LOG = CmsLog.getLog(CmsLockAction.class);
167
168    /**
169     * @see org.opencms.ui.contextmenu.I_CmsContextMenuAction#executeAction(org.opencms.ui.I_CmsDialogContext)
170     */
171    public void executeAction(I_CmsDialogContext context) {
172
173        new Context(context).executeAction();
174    }
175
176    /**
177     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#getId()
178     */
179    public String getId() {
180
181        return ACTION_ID;
182    }
183
184    /**
185     * @see org.opencms.ui.contextmenu.I_CmsHasMenuItemVisibility#getVisibility(org.opencms.file.CmsObject, java.util.List)
186     */
187    public CmsMenuItemVisibilityMode getVisibility(CmsObject cms, List<CmsResource> resources) {
188
189        return CmsStandardVisibilityCheck.LOCK.getVisibility(cms, resources);
190    }
191
192    /**
193     * @see org.opencms.ui.actions.A_CmsWorkplaceAction#getTitleKey()
194     */
195    @Override
196    protected String getTitleKey() {
197
198        return org.opencms.workplace.explorer.Messages.GUI_EXPLORER_CONTEXT_LOCK_0;
199    }
200
201}