KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > hibernate > support > SetupBean


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.riot.hibernate.support;
25
26 import java.sql.SQLException JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.hibernate.HibernateException;
31 import org.hibernate.Query;
32 import org.hibernate.Session;
33 import org.springframework.beans.factory.InitializingBean;
34 import org.springframework.orm.hibernate3.HibernateCallback;
35 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
36
37 public class SetupBean extends HibernateDaoSupport implements InitializingBean {
38
39     private List JavaDoc objects;
40
41     private String JavaDoc condition;
42         
43     public void setObjects(List JavaDoc objects) {
44         this.objects = objects;
45     }
46
47     public void setCondition(String JavaDoc condition) {
48         this.condition = condition;
49     }
50
51     protected void initDao() throws Exception JavaDoc {
52         if (setupRequired()) {
53             Iterator JavaDoc it = objects.iterator();
54             while (it.hasNext()) {
55                 Object JavaDoc object = it.next();
56                 getHibernateTemplate().save(object);
57             }
58         }
59     }
60     
61     protected boolean setupRequired() {
62         if (condition == null) {
63             return true;
64         }
65         Object JavaDoc test = getHibernateTemplate().execute(new HibernateCallback() {
66             public Object JavaDoc doInHibernate(Session session)
67                     throws HibernateException, SQLException JavaDoc {
68                 
69                 Query query = session.createQuery(condition);
70                 query.setMaxResults(1);
71                 return query.uniqueResult();
72             };
73         });
74         if (test instanceof Number JavaDoc) {
75             return ((Number JavaDoc) test).intValue() == 0;
76         }
77         if (test instanceof Boolean JavaDoc) {
78             return ((Boolean JavaDoc) test).booleanValue();
79         }
80         return test == null;
81     }
82     
83 }
84
Popular Tags