001/*
002 *  Copyright 2012 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.repository.jcr;
017
018import javax.jcr.Node;
019
020import org.ametys.core.user.UserIdentity;
021import org.ametys.plugins.repository.AmetysObject;
022import org.ametys.plugins.repository.AmetysRepositoryException;
023import org.ametys.plugins.repository.lock.LockableAmetysObject;
024
025/**
026 * {@link DefaultAmetysObject} which is also a {@link LockableAmetysObject}.
027 * @param <F> the actual type of factory.
028 */
029public class DefaultLockableAmetysObject<F extends DefaultLockableAmetysObjectFactory> extends DefaultAmetysObject<F> implements LockableAmetysObject
030{
031    private boolean _lockAlreadyChecked;
032    
033    /**
034     * Creates an {@link DefaultLockableAmetysObject}.
035     * @param node the node backing this {@link AmetysObject}
036     * @param parentPath the parentPath in the Ametys hierarchy
037     * @param factory the DefaultAmetysObjectFactory which created the AmetysObject
038     */
039    public DefaultLockableAmetysObject(Node node, String parentPath, F factory)
040    {
041        super(node, parentPath, factory);
042    }
043    
044    @Override
045    public void lock() throws AmetysRepositoryException
046    {
047        _getFactory().getLockComponent().lock(this);
048        
049        // the lock component immediately detached the lock token, so we have to check it again at next usage 
050        _lockAlreadyChecked = false;
051    }
052    
053    @Override
054    public void unlock() throws AmetysRepositoryException
055    {
056        _getFactory().getLockComponent().unlock(this);
057        
058        // the lock component removed the lock token on unlock
059        _lockAlreadyChecked = true;
060    }
061    
062    @Override
063    public void setLockInfoOnCurrentContext() throws AmetysRepositoryException
064    {
065        if (!_lockAlreadyChecked)
066        {
067            _getFactory().getLockComponent().setLockTokenOnCurrentSession(this);
068            _lockAlreadyChecked = true;
069        }
070    }
071    
072    @Override
073    public boolean isLocked() throws AmetysRepositoryException
074    {
075        return _getFactory().getLockComponent().isLocked(this);
076    }
077    
078    @Override
079    public UserIdentity getLockOwner() throws AmetysRepositoryException
080    {
081        return _getFactory().getLockComponent().getLockOwner(this);
082    }
083}