001 /*
002 * Databinder: a simple bridge from Wicket to Hibernate
003 * Copyright (C) 2006 Nathan Hamblen nathan@technically.us
004
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either
008 * version 2.1 of the License, or (at your option) any later version.
009 *
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 * Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public
016 * License along with this library; if not, write to the Free Software
017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
018 */
019
020 package net.databinder.models.hib;
021
022 import net.databinder.hib.Databinder;
023
024 import org.apache.wicket.model.LoadableDetachableModel;
025 import org.hibernate.Criteria;
026 import org.hibernate.Query;
027 import org.hibernate.Session;
028
029 /**
030 * Model for a List generated by a Hibernate query. This read-only model can be used
031 * to fill ListModel and PropertyListModel components with rows from a database.
032 * @author Nathan Hamblen
033 */
034 public class HibernateListModel extends LoadableDetachableModel {
035 private QueryBuilder queryBuilder;
036 private Class objectClass;
037 private CriteriaBuilder criteriaBuilder;
038
039 private Object factoryKey;
040
041 /**
042 * Contructor for a simple query.
043 * @param queryString query with no parameters
044 */
045 public HibernateListModel(String queryString) {
046 this(new QueryBinderBuilder(queryString));
047 }
048
049 /**
050 * Contructor for a simple query.
051 * @param queryString query with no parameters
052 * @param cacheable sets query to cacheable if true
053 */
054 public HibernateListModel(String queryString, final boolean cacheable) {
055 this(queryString, new QueryBinder() {
056 public void bind(Query query) {
057 query.setCacheable(cacheable);
058 }
059 });
060 }
061
062 /**
063 * Constructor for a parameterized query.
064 * @param queryString Query with parameters
065 * @param queryBinder object that binds the query parameters
066 */
067 public HibernateListModel(String queryString, QueryBinder queryBinder) {
068 this(new QueryBinderBuilder(queryString, queryBinder));
069 }
070
071 /**
072 * Constructor for a list of all results in class. While this query will be too open for
073 * most applications, it can useful in early development.
074 * @param objectClass class objects to return
075 */
076 public HibernateListModel(Class objectClass) {
077 this.objectClass = objectClass;
078 }
079
080 /**
081 * Constructor for a list of results in class matching a built criteria.
082 * @param objectClass class for root criteria
083 * @param criteriaBuilder builder to apply criteria restrictions
084 */
085 public HibernateListModel(Class objectClass, CriteriaBuilder criteriaBuilder) {
086 this.objectClass = objectClass;
087 this.criteriaBuilder = criteriaBuilder;
088 }
089
090 /**
091 * Constructor for a custom query that is built by the calling application.
092 * @param queryBuilder builder to create and bind query object
093 */
094 public HibernateListModel(QueryBuilder queryBuilder) {
095 this.queryBuilder = queryBuilder;
096 }
097
098 /** @return session factory key, or null for the default factory */
099 public Object getFactoryKey() {
100 return factoryKey;
101 }
102
103 /**
104 * Set a factory key other than the default (null).
105 * @param key session factory key
106 * @return this, for chaining
107 */
108 public HibernateListModel setFactoryKey(Object key) {
109 this.factoryKey = key;
110 return this;
111 }
112
113 /**
114 * Load the object List through Hibernate, binding query parameters if available.
115 */
116 @Override
117 protected Object load() {
118 Session session = Databinder.getHibernateSession(factoryKey);
119 if (queryBuilder != null) {
120 return queryBuilder.build(session).list();
121 }
122 Criteria criteria = session.createCriteria(objectClass);
123 if (criteriaBuilder != null)
124 criteriaBuilder.build(criteria);
125 return criteria.list();
126 }
127 }