001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH (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.lock; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.file.CmsUser; 033import org.opencms.gwt.Messages; 034import org.opencms.lock.CmsLockActionRecord.LockChange; 035import org.opencms.main.CmsException; 036 037import java.util.List; 038 039/** 040 * Locking utility class.<p> 041 */ 042public final class CmsLockUtil { 043 044 /** 045 * Hidden constructor. 046 */ 047 private CmsLockUtil() { 048 // Hide constructor for util class 049 } 050 051 /** 052 * Static helper method to lock a resource.<p> 053 * 054 * @param cms the CMS context to use 055 * @param resource the resource to lock 056 * @return the action that was taken 057 * 058 * @throws CmsException if something goes wrong 059 */ 060 public static CmsLockActionRecord ensureLock(CmsObject cms, CmsResource resource) throws CmsException { 061 062 LockChange change = LockChange.unchanged; 063 List<CmsResource> blockingResources = cms.getBlockingLockedResources(resource); 064 if ((blockingResources != null) && !blockingResources.isEmpty()) { 065 throw new CmsException( 066 Messages.get().container( 067 Messages.ERR_RESOURCE_HAS_BLOCKING_LOCKED_CHILDREN_1, 068 cms.getSitePath(resource))); 069 } 070 CmsUser user = cms.getRequestContext().getCurrentUser(); 071 CmsLock lock = cms.getLock(resource); 072 if (!lock.isOwnedBy(user)) { 073 cms.lockResourceTemporary(resource); 074 change = LockChange.locked; 075 lock = cms.getLock(resource); 076 } else if (!lock.isOwnedInProjectBy(user, cms.getRequestContext().getCurrentProject())) { 077 cms.changeLock(resource); 078 change = LockChange.changed; 079 lock = cms.getLock(resource); 080 } 081 return new CmsLockActionRecord(lock, change); 082 } 083 084}