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.hibernate;
023
024import java.io.Serializable;
025import java.util.List;
026
027import org.granite.logging.Logger;
028import org.granite.tide.TideTransactionManager;
029import org.granite.tide.data.AbstractTidePersistenceManager;
030import org.granite.util.Entity;
031import org.hibernate.Query;
032import org.hibernate.SessionFactory;
033
034/**
035 * Responsible for attaching a session with the persistence mangager
036 * @author cingram
037 *
038 */
039public class HibernatePersistenceManager extends AbstractTidePersistenceManager {
040        
041        private static final Logger log = Logger.getLogger(HibernatePersistenceManager.class);
042        
043        private SessionFactory sessionFactory;
044        
045        
046        public HibernatePersistenceManager(TideTransactionManager tm) {
047                super(tm);
048        }
049
050        public HibernatePersistenceManager(SessionFactory sf) {
051                super(null);
052        this.sessionFactory = sf;
053        }
054
055        public HibernatePersistenceManager(SessionFactory sf, TideTransactionManager tm) {
056                super(tm);
057        this.sessionFactory = sf;
058        }
059        
060        @Override
061        public void close() {           
062        }
063        
064        /**
065         * attaches the entity to the JPA context.
066         * @return the attached entity
067         */
068        @Override
069        public Object fetchEntity(Object entity, String[] fetch) {
070                Entity tideEntity = new Entity(entity);
071                Serializable id = (Serializable)tideEntity.getIdentifier();
072                
073        if (id == null)
074            return null;
075
076        if (fetch == null)
077                return sessionFactory.getCurrentSession().load(entity.getClass(), id);
078        
079        for (String f : fetch) {
080                Query q = sessionFactory.getCurrentSession().createQuery("select e from " + entity.getClass().getName() + " e left join fetch e." + f + " where e = :entity");
081                q.setParameter("entity", entity);
082                List<?> results = q.list();
083                if (!results.isEmpty())
084                        entity = results.get(0);
085                else
086                        log.warn("Could not find entity %s to initialize, id: %s", entity.getClass().getName(), id);  
087        }
088        return entity;
089        }
090}