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.repository.AmetysObjectResolver; 024import org.ametys.plugins.workflow.repository.WorkflowAwareAmetysObject; 025import org.ametys.plugins.workflow.store.AmetysObjectWorkflowStore; 026import org.ametys.web.WebConstants; 027 028/** 029 * Web ametys object workflow store which ensures that the session used operates 030 * on the default workspace. 031 */ 032public class WebAmetysObjectWorkflowStore extends AmetysObjectWorkflowStore 033{ 034 /** Internal session */ 035 protected Session _session; 036 037 /** 038 * Creates a workflow store for Web Ametys object. 039 * The history steps will be clear on workflow completion. 040 * @param repository the JCR Repository to use. 041 * @param ametysObject The ametys object for this store. Can be null in case of an object creation. 042 * @param resolver the ametys object resolver 043 */ 044 public WebAmetysObjectWorkflowStore(Repository repository, WorkflowAwareAmetysObject ametysObject, AmetysObjectResolver resolver) 045 { 046 super(repository, ametysObject, resolver); 047 } 048 049 /** 050 * Creates a workflow store for Web Ametys object. 051 * @param repository The repository 052 * @param ametysObject The ametys object for this store. Can be null in case of an object creation. 053 * @param resolver the ametys object resolver 054 * @param preserveHistory Set to true to preserve history steps when workflow is complete. 055 */ 056 public WebAmetysObjectWorkflowStore(Repository repository, WorkflowAwareAmetysObject ametysObject, AmetysObjectResolver resolver, boolean preserveHistory) 057 { 058 super(repository, ametysObject, resolver, preserveHistory); 059 } 060 061 062 @Override 063 protected Session _getSession() throws RepositoryException 064 { 065 if (_session == null) 066 { 067 Session session = super._getSession(); 068 069 if (session != null && WebConstants.LIVE_WORKSPACE.equals(session.getWorkspace().getName())) 070 { 071 session = _repository.login("default"); 072 } 073 074 _session = session; 075 } 076 077 return _session; 078 } 079 080 @Override 081 protected void _release(Session session) 082 { 083 try 084 { 085 if (session != null && !session.equals(_ametysObject.getNode().getSession())) 086 { 087 session.logout(); 088 _session = null; 089 } 090 else 091 { 092 super._release(session); 093 } 094 } 095 catch (RepositoryException e) 096 { 097 _log.error("Unable to release session for ametys object '" + _ametysObject.getId() + "'.", e); 098 } 099 } 100 101 @Override 102 protected Node _getAmetysObjectNode() throws RepositoryException 103 { 104 if (_session != null && !_session.equals(_ametysObject.getNode().getSession())) 105 { 106 Node contentNode = _ametysObject.getNode(); 107 return _session.getNodeByIdentifier(contentNode.getIdentifier()); 108 } 109 else 110 { 111 return super._getAmetysObjectNode(); 112 } 113 } 114}