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