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.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            m_context = context;
098        }
099
100        /**
101         * Actually locks the resources.<p>
102         */
103        protected void doLock() {
104
105            CmsObject cms = A_CmsUI.getCmsObject();
106            CmsException storedException = null;
107            List<CmsUUID> changedIds = Lists.newArrayList();
108            for (CmsResource res : m_context.getResources()) {
109                try {
110                    cms.lockResource(res);
111                    changedIds.add(res.getStructureId());
112                } catch (CmsException e) {
113                    if (storedException == null) {
114                        storedException = e;
115                    }
116                    LOG.warn(e.getLocalizedMessage(), e);
117                }
118
119            }
120            if (storedException != null) {
121                m_context.finish(changedIds);
122                m_context.error(storedException);
123            } else {
124                m_context.finish(changedIds);
125            }
126        }
127
128        /**
129         * Executes the action.<p>
130         */
131        void executeAction() {
132
133            CmsObject cms = A_CmsUI.getCmsObject();
134            List<CmsResource> blockingLocked = Lists.newArrayList();
135            for (CmsResource res : m_context.getResources()) {
136                try {
137                    List<CmsResource> blockingLockedForCurrentResource = cms.getBlockingLockedResources(res);
138                    blockingLocked.addAll(blockingLockedForCurrentResource);
139                } catch (CmsException e) {
140                    LOG.warn(e.getLocalizedMessage(), e);
141                }
142            }
143            if (blockingLocked.isEmpty()) {
144                doLock();
145            } else {
146                String messageKey = m_context.getResources().size() > 1
147                ? org.opencms.workplace.commons.Messages.GUI_LOCK_MULTI_INFO_LOCKEDSUBRESOURCES_0
148                : org.opencms.workplace.commons.Messages.GUI_LOCK_INFO_LOCKEDSUBRESOURCES_0;
149                CmsLockedResourcesList widget = new CmsLockedResourcesList(
150                    cms,
151                    blockingLocked,
152                    CmsVaadinUtils.getMessageText(messageKey),
153                    new ActionOk(),
154                    new ActionCancel());
155                widget.displayResourceInfo(m_context.getResources());
156                m_context.start(CmsVaadinUtils.getMessageText(Messages.GUI_LOCK_DIALOG_TITLE_0), widget);
157            }
158        }
159    }
160
161    /** The action id. */
162    public static final String ACTION_ID = "lock";
163
164    /** The logger instance for this class. */
165    static final Log LOG = CmsLog.getLog(CmsLockAction.class);
166
167    /**
168     * @see org.opencms.ui.contextmenu.I_CmsContextMenuAction#executeAction(org.opencms.ui.I_CmsDialogContext)
169     */
170    public void executeAction(I_CmsDialogContext context) {
171
172        new Context(context).executeAction();
173    }
174
175    /**
176     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#getId()
177     */
178    public String getId() {
179
180        return ACTION_ID;
181    }
182
183    /**
184     * @see org.opencms.ui.contextmenu.I_CmsHasMenuItemVisibility#getVisibility(org.opencms.file.CmsObject, java.util.List)
185     */
186    public CmsMenuItemVisibilityMode getVisibility(CmsObject cms, List<CmsResource> resources) {
187
188        return CmsStandardVisibilityCheck.LOCK.getVisibility(cms, resources);
189    }
190
191    /**
192     * @see org.opencms.ui.actions.A_CmsWorkplaceAction#getTitleKey()
193     */
194    @Override
195    protected String getTitleKey() {
196
197        return org.opencms.workplace.explorer.Messages.GUI_EXPLORER_CONTEXT_LOCK_0;
198    }
199
200}