1 24 package org.riotfamily.components.property; 25 26 import java.io.Serializable ; 27 28 import org.hibernate.SessionFactory; 29 import org.riotfamily.riot.hibernate.support.HibernateUtils; 30 import org.springframework.context.ApplicationContext; 31 import org.springframework.context.ApplicationContextAware; 32 import org.springframework.orm.hibernate3.HibernateTemplate; 33 import org.springframework.util.Assert; 34 35 public class HibernatePropertyProcessor extends AbstractSinglePropertyProcessor 36 implements ApplicationContextAware { 37 38 private ApplicationContext applicationContext; 39 40 private SessionFactory sessionFactory; 41 42 private Class entityClass; 43 44 public HibernatePropertyProcessor() { 45 } 46 47 public HibernatePropertyProcessor(String property, Class entityClass, 48 SessionFactory sessionFactory) { 49 50 this.entityClass = entityClass; 51 this.sessionFactory = sessionFactory; 52 setProperty(property); 53 } 54 55 public void setEntityClass(Class entityClass) { 56 this.entityClass = entityClass; 57 } 58 59 public void setSessionFactory(SessionFactory sessionFactory) { 60 this.sessionFactory = sessionFactory; 61 } 62 63 public void setApplicationContext(ApplicationContext applicationContext) { 64 this.applicationContext = applicationContext; 65 } 66 67 protected void initialize() { 68 if (sessionFactory == null) { 69 sessionFactory = (SessionFactory) applicationContext.getBean( 70 "sessionFactory", SessionFactory.class); 71 } 72 Assert.notNull(entityClass, "The property 'entityClass' must be set."); 73 } 74 75 protected String convertToString(Object object) { 76 if (object == null) { 77 return null; 78 } 79 return HibernateUtils.getIdAsString(sessionFactory, object); 80 } 81 82 protected Object resolveString(String s) { 83 if (s == null) { 84 return null; 85 } 86 Serializable sid = HibernateUtils.convertId(entityClass, s, 87 sessionFactory); 88 89 return new HibernateTemplate(sessionFactory).get(entityClass, sid); 90 } 91 92 protected String getCacheTag(String s) { 93 if (s == null) { 94 return null; 95 } 96 return entityClass.getName() + '#' + s; 97 } 98 99 } 100 | Popular Tags |