001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 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.jboss.seam.Component; 028import org.jboss.seam.Component.BijectedAttribute; 029import org.jboss.seam.annotations.In; 030import 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 */ 040public class TidePersistenceFactory { 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 @SuppressWarnings({ "unchecked", "rawtypes" }) 053 public static TidePersistenceManager createTidePersistence(Component component, Object persistenceType) { 054 if (persistenceType instanceof EntityManager){ 055 return createTidePersistence(component, (EntityManager)persistenceType); 056 } else if (persistenceType instanceof PersistenceController) { 057 return createTidePersistence(component, (PersistenceController)persistenceType); 058 } else if (persistenceType instanceof BijectedAttribute) { 059 return createTidePersistence(component, ((BijectedAttribute<In>)persistenceType)); 060 } 061 062 return null; 063 } 064 065 066 /** 067 * Create a ITideInterceptor for a EntityManager. 068 * 069 * @param component 070 * @param persistenceType 071 * @return a ITidePersistenceManager. 072 */ 073 public static TidePersistenceManager createTidePersistence(Component component, EntityManager persistenceType) { 074 return new PersistenceContextManager(persistenceType); 075 } 076 077 /** 078 * Create ITidePersistenceManager for a PersistenceController 079 * 080 * @param component 081 * @param controller 082 * @return a ITidePersistenceManager. 083 */ 084 public static TidePersistenceManager createTidePersistence(Component component, PersistenceController<?> controller) { 085 String controllerName = component.getName(); 086 if (controller.getPersistenceContext() instanceof EntityManager) 087 return new PersistenceControllerManager(controllerName); 088 return null; 089 } 090 091 /** 092 * Create a ITidePersistenceManager for a injected attribute(@In). 093 * Supported Types are EntityManager or Session. 094 * 095 * @param component 096 * @param att 097 * @return a ITidePersistenceManager. 098 */ 099 public static TidePersistenceManager createTidePersistence(Component component, BijectedAttribute<In> att) { 100 if (att.getType() == EntityManager.class) { 101 return new SeamEntityManager(att.getName()); 102 } 103 return null; 104 } 105}