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.Repository;
019import javax.jcr.RepositoryException;
020import javax.jcr.Session;
021
022import org.ametys.plugins.repository.provider.WorkspaceSelector;
023import org.ametys.plugins.workflow.store.GenericWorkflowStore;
024import org.ametys.plugins.workflow.support.WorkflowProvider;
025import org.ametys.web.WebConstants;
026
027/**
028 * Web generic workflow store which force default workspace session
029 * @see WorkflowProvider
030 */
031public class WebGenericWorkflowStore extends GenericWorkflowStore
032{
033    /** workspace selector. */
034    protected WorkspaceSelector _workspaceSelector;
035    
036    /**
037     * Constructor
038     * @param repository The repository
039     * @param workspaceSelector The workspace selector 
040     */
041    public WebGenericWorkflowStore(Repository repository, WorkspaceSelector workspaceSelector)
042    {
043        super(repository);
044        _workspaceSelector = workspaceSelector;
045    }
046    
047    @Override
048    protected Session _getSession() throws RepositoryException
049    {
050        // Use always default workspace if in live workspace.
051        // Because live does not contains the workflow instances nodes
052        if (WebConstants.LIVE_WORKSPACE.equals(_workspaceSelector.getWorkspace()))
053        {
054            return _repository.login("default");
055        }
056        else
057        {
058            return _repository.login();
059        }
060    }
061}