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 org.granite.context.GraniteContext; 027import org.granite.messaging.amf.io.util.ClassGetter; 028import org.granite.tide.TidePersistenceManager; 029import org.hibernate.Session; 030import org.jboss.seam.Entity; 031import org.jboss.seam.util.Reflections; 032 033/** 034 * Manager responsible for the maintaining a reference for the HibernateContext. 035 * @author CIngram 036 */ 037public class HibernateContextManager implements TidePersistenceManager { 038 039 private Session session = null; 040 041 public HibernateContextManager() { 042 043 } 044 045 public HibernateContextManager(Session session) { 046 this.session = session; 047 } 048 049 /** 050 * Attach the passed in entity with the HibernateSession. 051 * @param entity 052 * @return the attached entity object 053 */ 054 public Object attachEntity(Object entity, String[] propertyNames) { 055 Object attachedEntity = null; 056 ClassGetter getter = GraniteContext.getCurrentInstance().getGraniteConfig().getClassGetter(); 057 058 try { 059 attachedEntity = fetchEntity(entity, propertyNames); 060 061 if (propertyNames != null) { 062 for (int i = 0; i < propertyNames.length; i++) { 063 Object initializedObj = Reflections.getGetterMethod(attachedEntity.getClass(), propertyNames[i]).invoke(attachedEntity); 064 065 getter.initialize(entity, propertyNames[i], initializedObj); 066 } 067 } 068 } 069 catch(Exception e) { 070 throw new RuntimeException("Unable to attach entity and init collection", e); 071 } 072 disconnectSession(); 073 074 return attachedEntity; 075 } 076 077 /** 078 * attaches the entity to the HibernateSession. 079 * @return the attached entity 080 */ 081 public Object fetchEntity(Object entity, String[] fetch) { 082 Serializable id = (Serializable)Entity.forClass(entity.getClass()).getIdentifier(entity); 083 if (id == null) 084 return null; 085 return session.get(entity.getClass(), id); 086 } 087 088 /** 089 * disconnects from the Hibernate Session. 090 */ 091 protected void disconnectSession() { 092 session.disconnect(); 093 } 094}