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 java.io.Serializable;
025
026import javax.persistence.EntityManager;
027import javax.persistence.Query;
028
029import org.granite.context.GraniteContext;
030import org.granite.messaging.amf.io.util.ClassGetter;
031import org.granite.tide.TidePersistenceManager;
032import org.jboss.seam.Entity;
033import org.jboss.seam.util.Reflections;
034
035/**
036 * Manager responsible for the maintaining a refernce for the PersistenceContext(JPA). 
037 * @author CIngram
038 */
039public class PersistenceContextManager implements TidePersistenceManager  {
040        
041        private EntityManager em;
042        
043        public PersistenceContextManager() {
044        }
045        
046        public PersistenceContextManager(EntityManager em) {
047                this.em = em;
048        }
049        
050        /**
051         * Attach the passed in entity with the EntityManager.
052         * @param entity
053         * @return the attached entity object
054         */
055        public Object attachEntity(Object entity, String[] propertyNames) {
056                Object attachedEntity = null;
057        ClassGetter getter = GraniteContext.getCurrentInstance().getGraniteConfig().getClassGetter();
058                
059                //the get is called to give the children a chance to override and
060                //use the implemented method
061                attachedEntity = fetchEntity(entity, propertyNames);
062
063            if (attachedEntity != null && propertyNames != null) {
064                for (int i = 0; i < propertyNames.length; i++) {
065                        try {
066                                Object initializedObj = Reflections.getGetterMethod(attachedEntity.getClass(), propertyNames[i]).invoke(attachedEntity);
067                            
068                                //This is here to make sure the list is forced to return a value while operating inside of a 
069                                //session. Forcing the  initialization of object.
070                        if (getter != null)
071                            getter.initialize(entity, propertyNames[i], initializedObj);
072                        }
073                        catch (Exception e) {
074                                throw new RuntimeException("Could not initialize entity " + attachedEntity, e);
075                        }
076                }
077        }
078                
079                return attachedEntity;
080        } 
081        
082        /**
083         * attaches the entity to the JPA context.
084         * @return the attached entity
085         */
086        public Object fetchEntity(Object entity, String[] fetch) {
087        Serializable id = (Serializable)Entity.forClass(entity.getClass()).getIdentifier(entity);
088        if (id == null)
089            return null;
090        
091        if (fetch == null || em.getDelegate().getClass().getName().indexOf(".hibernate.") < 0)
092                return em.find(entity.getClass(), id);
093        
094        for (String f : fetch) {
095                Query q = em.createQuery("select e from " + entity.getClass().getName() + " e left join fetch e." + f + " where e = :entity");
096                q.setParameter("entity", entity);
097                entity = q.getSingleResult();
098        }
099        return entity;
100        }
101}