001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.tide.seam.lazy;
022    
023    import static org.jboss.seam.annotations.Install.FRAMEWORK;
024    
025    import javax.persistence.EntityManager;
026    
027    import org.granite.tide.TidePersistenceManager;
028    import org.hibernate.Session;
029    import org.jboss.seam.Component;
030    import org.jboss.seam.ScopeType;
031    import org.jboss.seam.annotations.Install;
032    import org.jboss.seam.annotations.Name;
033    import org.jboss.seam.annotations.Scope;
034    import org.jboss.seam.annotations.intercept.BypassInterceptors;
035    
036    /**
037     * Initializes a request for a passed in entity and a lazy property.
038     
039     * @author CIngram,VDanda
040     */
041    @Name("org.granite.tide.seam.seamInitializer")
042    @Scope(ScopeType.CONVERSATION)
043    @Install(precedence=FRAMEWORK+1, classDependencies="org.hibernate.Session")
044    @BypassInterceptors
045    public class SeamHibernateInitializer extends SeamInitializer {
046    
047        private static final long serialVersionUID = 1L;
048            
049            
050            /**
051             * Try to determine what type of persistence the application is using. 
052             * If the EntityManager is stored under entityManager or if the Hibernate session is 
053             * stored under session. Then the context will be found and used. This is only called if a 
054             * ITidePersistenceManager is not found, probably because the query was not run in a conversation.
055             * @return The appropriate manager for the persistence context being used, if it can be determined
056             * otherwise a null is returned. 
057             */  
058        @Override
059            protected TidePersistenceManager tryToDetermineInitiailzer() {
060            EntityManager em = findEntityManager();
061            if (em != null) 
062                return TideHibernatePersistenceFactory.createTidePersistence(null, em);
063    
064                    Session session = findHibernateSession();
065                    if (session != null)
066                            return TideHibernatePersistenceFactory.createTidePersistence(null, session);
067                    
068                    return null;
069            }
070            
071        
072            /**
073             * Try to find the hibernateSession if possible. Assume that the session is stored under
074             * session.
075             * @return The Current Session
076             */
077            private Session findHibernateSession() {
078                    return (Session) Component.getInstance("session");
079            }
080    }
081