KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > property > HibernatePropertyProcessor


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components.property;
25
26 import java.io.Serializable JavaDoc;
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 JavaDoc entityClass;
43     
44     public HibernatePropertyProcessor() {
45     }
46
47     public HibernatePropertyProcessor(String JavaDoc property, Class JavaDoc entityClass,
48             SessionFactory sessionFactory) {
49         
50         this.entityClass = entityClass;
51         this.sessionFactory = sessionFactory;
52         setProperty(property);
53     }
54
55     public void setEntityClass(Class JavaDoc 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 JavaDoc convertToString(Object JavaDoc object) {
76         if (object == null) {
77             return null;
78         }
79         return HibernateUtils.getIdAsString(sessionFactory, object);
80     }
81     
82     protected Object JavaDoc resolveString(String JavaDoc s) {
83         if (s == null) {
84             return null;
85         }
86         Serializable JavaDoc sid = HibernateUtils.convertId(entityClass, s,
87                 sessionFactory);
88                 
89         return new HibernateTemplate(sessionFactory).get(entityClass, sid);
90     }
91     
92     protected String JavaDoc getCacheTag(String JavaDoc s) {
93         if (s == null) {
94             return null;
95         }
96         return entityClass.getName() + '#' + s;
97     }
98     
99 }
100
Popular Tags