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