001/*
002 *  Copyright 2016 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.web.workflow;
017
018import javax.jcr.Node;
019import javax.jcr.Repository;
020import javax.jcr.RepositoryException;
021import javax.jcr.Session;
022
023import org.ametys.plugins.workflow.repository.WorkflowAwareAmetysObject;
024import org.ametys.plugins.workflow.store.AmetysObjectWorkflowStore;
025import org.ametys.web.WebConstants;
026
027/**
028 * Web ametys object workflow store which ensures that the session used operates
029 * on the default workspace.
030 */
031public class WebAmetysObjectWorkflowStore extends AmetysObjectWorkflowStore
032{
033    /** Internal session */
034    protected Session _session;
035    
036    /**
037     * Constructor
038     * @param repository The repository
039     * @param ametysObject The ametys object for this store. Can be null in case of an object creation.
040     */
041    public WebAmetysObjectWorkflowStore(Repository repository, WorkflowAwareAmetysObject ametysObject)
042    {
043        super(repository, ametysObject);
044    }
045    
046    @Override
047    protected Session _getSession() throws RepositoryException
048    {
049        if (_session == null)
050        {
051            Session session = super._getSession();
052            
053            if (session != null && WebConstants.LIVE_WORKSPACE.equals(session.getWorkspace().getName()))
054            {
055                session = _repository.login("default");
056            }
057            
058            _session = session;
059        }
060        
061        return _session;
062    }
063    
064    @Override
065    protected void _release(Session session)
066    {
067        try
068        {
069            if (session != null && !session.equals(_ametysObject.getNode().getSession()))
070            {
071                session.logout();
072                _session = null;
073            }
074            else
075            {
076                super._release(session);
077            }
078        }
079        catch (RepositoryException e)
080        {
081            _log.error("Unable to release session for ametys object '" + _ametysObject.getId() + "'.", e);
082        }
083    }
084    
085    @Override
086    protected Node _getAmetysObjectNode() throws RepositoryException
087    {
088        if (_session != null && !_session.equals(_ametysObject.getNode().getSession()))
089        {
090            Node contentNode = _ametysObject.getNode();
091            return _session.getNodeByIdentifier(contentNode.getIdentifier());
092        }
093        else
094        {
095            return super._getAmetysObjectNode();
096        }
097    }
098}