001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2014 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.tide.seam.lazy;
023
024import javax.persistence.EntityManager;
025
026import org.granite.tide.TidePersistenceManager;
027import org.hibernate.Session;
028import org.jboss.seam.Component;
029import org.jboss.seam.Component.BijectedAttribute;
030import org.jboss.seam.annotations.In;
031import org.jboss.seam.framework.PersistenceController;
032
033
034/**
035 * Factory for creating the correct ITidePersistenceManager based on the 
036 * persistence strategy passed in. Supported types are 
037 * EntityManager,Session, EntityQuery, EntityHome, HibernateEntityHome and a
038 * injected(@In) EntityManager or HibernateSession
039 * @author CIngram
040 */
041public class TideHibernatePersistenceFactory {
042
043        
044        /**
045         * Create the ITidePersistenceManager. Supported types are 
046     * EntityManager,Session, EntityQuery, EntityHome, HibernateEntityHome and a
047     * injected(@In) EntityManager or HibernateSession
048         * 
049         * @param component
050         * @param persistenceType
051         * @return a ITidePersistenceManager.
052         */
053        public static TidePersistenceManager createTidePersistence(Component component, Object persistenceType) {
054                TidePersistenceManager pm = TidePersistenceFactory.createTidePersistence(component, persistenceType);
055                if (pm != null) {
056                    return pm;
057                } else if (persistenceType instanceof Session) {
058                        return createTidePersistence(component, (Session)persistenceType);
059                }
060                
061                return null;
062        }
063        
064        
065        /**
066         * Create a ITideInterceptor for a EntityManager.
067         * 
068         * @param component
069         * @param persistenceType
070         * @return a ITidePersistenceManager.
071         */
072        public static TidePersistenceManager createTidePersistence(Component component, EntityManager persistenceType) {
073            return TidePersistenceFactory.createTidePersistence(component, persistenceType);
074        }
075        
076        /**
077         * Create  ITidePersistenceManager for a HibernateSession
078         * 
079         * @param component
080         * @param persistenceType
081         * @return a ITidePersistenceManager.
082         */
083        public static TidePersistenceManager createTidePersistence(Component component, Session persistenceType) {
084            return new HibernateContextManager(persistenceType);
085        } 
086    
087    /**
088     * Create  ITidePersistenceManager for a PersistenceController
089         * 
090         * @param component
091     * @param controller
092     * @return a ITidePersistenceManager.
093     */
094    public static TidePersistenceManager createTidePersistence(Component component, PersistenceController<?> controller) {
095        TidePersistenceManager pm = TidePersistenceFactory.createTidePersistence(component, controller);
096        if (pm != null)
097            return pm;
098        String controllerName = component.getName();
099        if (controller.getPersistenceContext() instanceof Session)
100            return new HibernatePersistenceControllerManager(controllerName);
101        return null;
102    } 
103        
104        /**
105         * Create a ITidePersistenceManager for a injected attribute(@In). 
106         * Supported Types are EntityManager or Session.
107         * 
108         * @param component
109         * @param att
110         * @return a ITidePersistenceManager.
111         */
112        public static TidePersistenceManager createTidePersistence(Component component, BijectedAttribute<In> att) {
113                TidePersistenceManager pm = TidePersistenceFactory.createTidePersistence(component, att);
114                if (pm != null)
115                    return pm;
116                
117                if (att.getType() == Session.class) {
118                        return new SeamHibernateManager(att.getName());
119            } 
120                return null;
121        }
122}