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 javax.persistence.EntityManager;
024
025 import org.granite.tide.TidePersistenceManager;
026 import org.hibernate.Session;
027 import org.jboss.seam.Component;
028 import org.jboss.seam.Component.BijectedAttribute;
029 import org.jboss.seam.annotations.In;
030 import org.jboss.seam.framework.PersistenceController;
031
032
033 /**
034 * Factory for creating the correct ITidePersistenceManager based on the
035 * persistence strategy passed in. Supported types are
036 * EntityManager,Session, EntityQuery, EntityHome, HibernateEntityHome and a
037 * injected(@In) EntityManager or HibernateSession
038 * @author CIngram
039 */
040 public class TideHibernatePersistenceFactory {
041
042
043 /**
044 * Create the ITidePersistenceManager. Supported types are
045 * EntityManager,Session, EntityQuery, EntityHome, HibernateEntityHome and a
046 * injected(@In) EntityManager or HibernateSession
047 *
048 * @param component
049 * @param persistenceType
050 * @return a ITidePersistenceManager.
051 */
052 public static TidePersistenceManager createTidePersistence(Component component, Object persistenceType) {
053 TidePersistenceManager pm = TidePersistenceFactory.createTidePersistence(component, persistenceType);
054 if (pm != null) {
055 return pm;
056 } else if (persistenceType instanceof Session) {
057 return createTidePersistence(component, (Session)persistenceType);
058 }
059
060 return null;
061 }
062
063
064 /**
065 * Create a ITideInterceptor for a EntityManager.
066 *
067 * @param component
068 * @param persistenceType
069 * @return a ITidePersistenceManager.
070 */
071 public static TidePersistenceManager createTidePersistence(Component component, EntityManager persistenceType) {
072 return TidePersistenceFactory.createTidePersistence(component, persistenceType);
073 }
074
075 /**
076 * Create ITidePersistenceManager for a HibernateSession
077 *
078 * @param component
079 * @param persistenceType
080 * @return a ITidePersistenceManager.
081 */
082 public static TidePersistenceManager createTidePersistence(Component component, Session persistenceType) {
083 return new HibernateContextManager(persistenceType);
084 }
085
086 /**
087 * Create ITidePersistenceManager for a PersistenceController
088 *
089 * @param component
090 * @param controller
091 * @return a ITidePersistenceManager.
092 */
093 public static TidePersistenceManager createTidePersistence(Component component, PersistenceController<?> controller) {
094 TidePersistenceManager pm = TidePersistenceFactory.createTidePersistence(component, controller);
095 if (pm != null)
096 return pm;
097 String controllerName = component.getName();
098 if (controller.getPersistenceContext() instanceof Session)
099 return new HibernatePersistenceControllerManager(controllerName);
100 return null;
101 }
102
103 /**
104 * Create a ITidePersistenceManager for a injected attribute(@In).
105 * Supported Types are EntityManager or Session.
106 *
107 * @param component
108 * @param att
109 * @return a ITidePersistenceManager.
110 */
111 public static TidePersistenceManager createTidePersistence(Component component, BijectedAttribute<In> att) {
112 TidePersistenceManager pm = TidePersistenceFactory.createTidePersistence(component, att);
113 if (pm != null)
114 return pm;
115
116 if (att.getType() == Session.class) {
117 return new SeamHibernateManager(att.getName());
118 }
119 return null;
120 }
121 }