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
021package org.granite.tide.seam.lazy;
022
023import javax.persistence.EntityManager;
024
025import org.granite.tide.TidePersistenceManager;
026import org.jboss.seam.Component;
027import org.jboss.seam.Component.BijectedAttribute;
028import org.jboss.seam.annotations.In;
029import org.jboss.seam.framework.PersistenceController;
030
031
032/**
033 * Factory for creating the correct ITidePersistenceManager based on the 
034 * persistence strategy passed in. Supported types are 
035 * EntityManager,Session, EntityQuery, EntityHome, HibernateEntityHome and a
036 * injected(@In) EntityManager or HibernateSession
037 * @author CIngram
038 */
039public class TidePersistenceFactory {
040
041        
042        /**
043         * Create the ITidePersistenceManager. Supported types are 
044     * EntityManager,Session, EntityQuery, EntityHome, HibernateEntityHome and a
045     * injected(@In) EntityManager or HibernateSession
046         * 
047         * @param component
048         * @param persistenceType
049         * @return a ITidePersistenceManager.
050         */
051        @SuppressWarnings({ "unchecked", "rawtypes" })
052        public static TidePersistenceManager createTidePersistence(Component component, Object persistenceType) {
053                if (persistenceType instanceof EntityManager){
054                        return createTidePersistence(component, (EntityManager)persistenceType);
055                } else if (persistenceType instanceof PersistenceController) {
056                        return createTidePersistence(component, (PersistenceController)persistenceType);
057        } else if (persistenceType instanceof BijectedAttribute) {
058                        return createTidePersistence(component, ((BijectedAttribute<In>)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 new PersistenceContextManager(persistenceType);
074        }
075    
076    /**
077     * Create  ITidePersistenceManager for a PersistenceController
078         * 
079         * @param component
080     * @param controller
081     * @return a ITidePersistenceManager.
082     */
083    public static TidePersistenceManager createTidePersistence(Component component, PersistenceController<?> controller) {
084        String controllerName = component.getName();
085        if (controller.getPersistenceContext() instanceof EntityManager)
086            return new PersistenceControllerManager(controllerName);
087        return null;
088    } 
089        
090        /**
091         * Create a ITidePersistenceManager for a injected attribute(@In). 
092         * Supported Types are EntityManager or Session.
093         * 
094         * @param component
095         * @param att
096         * @return a ITidePersistenceManager.
097         */
098        public static TidePersistenceManager createTidePersistence(Component component, BijectedAttribute<In> att) {
099                if (att.getType() == EntityManager.class) {
100                        return new SeamEntityManager(att.getName());
101                }
102                return null;
103        }
104}