Index: lams_common/src/java/org/lamsfoundation/lams/dao/IBaseDAO.java =================================================================== diff -u -r8a37658d37fa3da8260fe7806df498ae20a99f49 -r48f344a789c9af4dcca102b6b5914333e9cbd17a --- lams_common/src/java/org/lamsfoundation/lams/dao/IBaseDAO.java (.../IBaseDAO.java) (revision 8a37658d37fa3da8260fe7806df498ae20a99f49) +++ lams_common/src/java/org/lamsfoundation/lams/dao/IBaseDAO.java (.../IBaseDAO.java) (revision 48f344a789c9af4dcca102b6b5914333e9cbd17a) @@ -1,302 +1,302 @@ -/**************************************************************** - * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) - * ============================================================= - * License Information: http://lamsfoundation.org/licensing/lams/2.0/ - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2.0 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 - * USA - * - * http://www.gnu.org/licenses/gpl.txt - * **************************************************************** - */ - -package org.lamsfoundation.lams.dao; - -import java.io.Serializable; -import java.util.Collection; -import java.util.List; -import java.util.Map; - -import org.hibernate.HibernateException; - -/** - * @version - * - *

- * View Source - *

- * - * @author Fei Yang - * - * Created at 22:50:25 on 16/06/2006 - */ -public interface IBaseDAO { - - /** - * Insert an object into the database. Should only be used if the object has not - * been persisted previously. - * - * @param object - * The object to be inserted - */ - public void insert(Object object); - - /** - * Update a previously inserted object into the database. - * - * @param object - * The object to be updated - */ - public void update(Object object); - - /** - * Insert or update an object into the database. It is up to the persistence - * engine to decide whether to insert or update. - * - * @param object - * The object to be inserted/updated - */ - public void insertOrUpdate(Object object); - - /** - * @param objects - * @return void - */ - public void insertOrUpdateAll(Collection objects); - - public void update(String queryString); - - public void update(String queryString, Object value); - - public void update(String queryString, Object[] values); - - /** - * @param clazz - * @param propertyToChange - * @param newValue - * @param conditionProperty - * @param conditionValue - * @return void - */ - public void update(Class clazz, String propertyToChange, Object newValue, String conditionProperty, - Object conditionValue); - - /** - * @param clazz - * @param propertyToChange - * @param newValue - * @param conditions - * in a map - * @return void - */ - public void update(Class clazz, String propertyToChange, Object newValue, Map conditions); - - /** - * @param clazz - * @param newValues - * in a map - * @param conditionProperty - * @param conditionValue - * @return void - */ - public void update(Class clazz, Map newValues, String conditionProperty, Object conditionValue); - - /** - * @param clazz - * @param newValues - * in a map - * @param conditions - * in a map - * @return void - */ - public void update(Class clazz, Map newValues, Map conditions); - - /** - * These 2 objects have to be instances of the Class - * - * @param clazz - * @param newValues - * in a object - * @param conditions - * in a object - * @return void - */ - public void updateAnythingLike(Class clazz, Object newValues, Object conditions); - - /** - * Force this session to flush. Must be called at the end of a unit of work, before commiting the transaction and - * closing the session (depending on flush-mode, Transaction.commit() calls this method). - * - * @throws HibernateException - * - Indicates problems flushing the session or talking to the database. - */ - void flush(); - - /** - * Remove an object from the database. - * - * @param object - * The object to be deleted - */ - public void delete(Object object); - - /** - * Be careful to use this method. It will clean up the whole table for the Class - * - * @param clazz - * @return void - */ - public void deleteAll(Class clazz); - - /** - * @param objects - * to delete - * @return void - */ - public void deleteAll(Collection objects); - - /** - * @param clazz - * java Class - * @param id - * identifier - * @return void - */ - public void deleteById(Class clazz, Serializable id); - - /** - * @param clazz - * @param name - * @param value - * @return void - */ - public void deleteByProperty(Class clazz, String name, Object value); - - /** - * @param properties - * a map of property names and values - * @return void - */ - public void deleteByProperties(Class clazz, Map properties); - - /** - * Delete any object which has the same non-null property values as the object - * - * @param object - * @return void - */ - public void deleteAnythingLike(Object object); - - /** - * Find an object. If the object is not found - * then it will return null - * - * @param clazz - * @param id - */ - public Object find(Class clazz, Serializable id); - - /** - * @param clazz - * @return all of rows in the table for the Class as objects - */ - public List findAll(Class clazz); - - /** - * @param clazz - * @param name - * @param value - * @return a list of objects which have the same propery value - */ - public List findByProperty(Class clazz, String name, Object value); - - /** - * @param properties - * a map of property names and values - * @return a list of objects which have the same property values - */ - public List findByProperties(Class clazz, Map properties); - - public List find(String queryString); - - public List find(String queryString, Object value); - - public List find(String queryString, Object[] values); - - public List findByNamedQuery(String queryName); - - public List findByNamedQuery(String queryName, Object value); - - public List findByNamedQuery(String queryName, Object[] values); - - /** - * @param clazz - * @param name - * of the property - * @param pattern - * to match - * @return a list of objects - */ - public List searchByStringProperty(Class clazz, String name, String pattern); - - /** - * @param clazz - * @param name - * of the property - * @param pattern - * to match - * @return a list of objects - */ - public List searchByStringProperties(Class clazz, Map properties); - - /** - * @param clazz - * @param name - * of the property - * @param min - * @param minIncluded - * @param max - * @param maxIncluded - * @return a list of objects - */ - public List searchByNumberSpan(Class clazz, String name, Integer min, Boolean minIncluded, Integer max, - Boolean maxIncluded); - - /** - * Force initialization of a Hibernate proxy or persistent collection - * - * @param proxy - * of persistent object or a collection - */ - public void initialize(Object proxy); - - /** - * Count all rows in a table for a hibernate-mapped class - * - * @param clazz - * @return - */ - public long countAll(Class clazz); - - /** - * Create a query based on the properties, and count the result - * - * @param properties - * a map of property names and values - * @return a list of objects which have the same property values - */ - public long countByProperties(Class clazz, Map properties); - - public void releaseFromCache(Object o); +/**************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * License Information: http://lamsfoundation.org/licensing/lams/2.0/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2.0 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * **************************************************************** + */ + +package org.lamsfoundation.lams.dao; + +import java.io.Serializable; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import org.hibernate.HibernateException; + +/** + * @version + * + *

+ * View Source + *

+ * + * @author Fei Yang + * + * Created at 22:50:25 on 16/06/2006 + */ +public interface IBaseDAO { + + /** + * Insert an object into the database. Should only be used if the object has not + * been persisted previously. + * + * @param object + * The object to be inserted + */ + public void insert(Object object); + + /** + * Update a previously inserted object into the database. + * + * @param object + * The object to be updated + */ + public void update(Object object); + + /** + * Insert or update an object into the database. It is up to the persistence + * engine to decide whether to insert or update. + * + * @param object + * The object to be inserted/updated + */ + public void insertOrUpdate(Object object); + + /** + * @param objects + * @return void + */ + public void insertOrUpdateAll(Collection objects); + + public void update(String queryString); + + public void update(String queryString, Object value); + + public void update(String queryString, Object[] values); + + /** + * @param clazz + * @param propertyToChange + * @param newValue + * @param conditionProperty + * @param conditionValue + * @return void + */ + public void update(Class clazz, String propertyToChange, Object newValue, String conditionProperty, + Object conditionValue); + + /** + * @param clazz + * @param propertyToChange + * @param newValue + * @param conditions + * in a map + * @return void + */ + public void update(Class clazz, String propertyToChange, Object newValue, Map conditions); + + /** + * @param clazz + * @param newValues + * in a map + * @param conditionProperty + * @param conditionValue + * @return void + */ + public void update(Class clazz, Map newValues, String conditionProperty, Object conditionValue); + + /** + * @param clazz + * @param newValues + * in a map + * @param conditions + * in a map + * @return void + */ + public void update(Class clazz, Map newValues, Map conditions); + + /** + * These 2 objects have to be instances of the Class + * + * @param clazz + * @param newValues + * in a object + * @param conditions + * in a object + * @return void + */ + public void updateAnythingLike(Class clazz, Object newValues, Object conditions); + + /** + * Force this session to flush. Must be called at the end of a unit of work, before commiting the transaction and + * closing the session (depending on flush-mode, Transaction.commit() calls this method). + * + * @throws HibernateException + * - Indicates problems flushing the session or talking to the database. + */ + void flush(); + + /** + * Remove an object from the database. + * + * @param object + * The object to be deleted + */ + public void delete(Object object); + + /** + * Be careful to use this method. It will clean up the whole table for the Class + * + * @param clazz + * @return void + */ + public void deleteAll(Class clazz); + + /** + * @param objects + * to delete + * @return void + */ + public void deleteAll(Collection objects); + + /** + * @param clazz + * java Class + * @param id + * identifier + * @return void + */ + public void deleteById(Class clazz, Serializable id); + + /** + * @param clazz + * @param name + * @param value + * @return void + */ + public void deleteByProperty(Class clazz, String name, Object value); + + /** + * @param properties + * a map of property names and values + * @return void + */ + public void deleteByProperties(Class clazz, Map properties); + + /** + * Delete any object which has the same non-null property values as the object + * + * @param object + * @return void + */ + public void deleteAnythingLike(Object object); + + /** + * Find an object. If the object is not found + * then it will return null + * + * @param clazz + * @param id + */ + public T find(Class clazz, Serializable id); + + /** + * @param clazz + * @return all of rows in the table for the Class as objects + */ + public List findAll(Class clazz); + + /** + * @param clazz + * @param name + * @param value + * @return a list of objects which have the same propery value + */ + public List findByProperty(Class clazz, String name, Object value); + + /** + * @param properties + * a map of property names and values + * @return a list of objects which have the same property values + */ + public List findByProperties(Class clazz, Map properties); + + public List find(String queryString); + + public List find(String queryString, Object value); + + public List find(String queryString, Object[] values); + + public List findByNamedQuery(String queryName); + + public List findByNamedQuery(String queryName, Object value); + + public List findByNamedQuery(String queryName, Object[] values); + + /** + * @param clazz + * @param name + * of the property + * @param pattern + * to match + * @return a list of objects + */ + public List searchByStringProperty(Class clazz, String name, String pattern); + + /** + * @param clazz + * @param name + * of the property + * @param pattern + * to match + * @return a list of objects + */ + public List searchByStringProperties(Class clazz, Map properties); + + /** + * @param clazz + * @param name + * of the property + * @param min + * @param minIncluded + * @param max + * @param maxIncluded + * @return a list of objects + */ + public List searchByNumberSpan(Class clazz, String name, Integer min, Boolean minIncluded, Integer max, + Boolean maxIncluded); + + /** + * Force initialization of a Hibernate proxy or persistent collection + * + * @param proxy + * of persistent object or a collection + */ + public void initialize(Object proxy); + + /** + * Count all rows in a table for a hibernate-mapped class + * + * @param clazz + * @return + */ + public long countAll(Class clazz); + + /** + * Create a query based on the properties, and count the result + * + * @param properties + * a map of property names and values + * @return a list of objects which have the same property values + */ + public long countByProperties(Class clazz, Map properties); + + public void releaseFromCache(Object o); } \ No newline at end of file Index: lams_common/src/java/org/lamsfoundation/lams/dao/hibernate/LAMSBaseDAO.java =================================================================== diff -u -r8a37658d37fa3da8260fe7806df498ae20a99f49 -r48f344a789c9af4dcca102b6b5914333e9cbd17a --- lams_common/src/java/org/lamsfoundation/lams/dao/hibernate/LAMSBaseDAO.java (.../LAMSBaseDAO.java) (revision 8a37658d37fa3da8260fe7806df498ae20a99f49) +++ lams_common/src/java/org/lamsfoundation/lams/dao/hibernate/LAMSBaseDAO.java (.../LAMSBaseDAO.java) (revision 48f344a789c9af4dcca102b6b5914333e9cbd17a) @@ -1,602 +1,604 @@ -package org.lamsfoundation.lams.dao.hibernate; - -import java.io.Serializable; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.regex.Pattern; - -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Root; - -import org.apache.log4j.Logger; -import org.hibernate.Hibernate; -import org.hibernate.HibernateException; -import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.hibernate.query.Query; -import org.lamsfoundation.lams.dao.IBaseDAO; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Repository; - -@Repository -public class LAMSBaseDAO implements IBaseDAO { - - private static class Qv { - - String queryString; - Object[] values; - - Qv(String queryString, Object[] values) { - super(); - - this.queryString = queryString; - this.values = values; - } - } - - private static final String SELECT = "from "; - private static final String DELETE = "delete "; - private static final String WHERE = " where "; - private static final String AND = " and "; - private static final String SPACE = " "; - private static final String SPOT = "."; - private static final String EQUAL_TO_WHAT = "=?"; - private static final String LIKE_WHAT = " like ?"; - - private static Logger log = Logger.getLogger(LAMSBaseDAO.class); - - @Autowired - @Qualifier("coreSessionFactory") - private SessionFactory sessionFactory; - - /* - * (non-Javadoc) - * - * @see org.lamsfoundation.lams.dao.IBaseDAO#insert(java.lang.Object) - */ - @Override - public void insert(Object object) { - getSession().save(object); - } - - /* - * (non-Javadoc) - * - * @see org.lamsfoundation.lams.dao.IBaseDAO#update(java.lang.Object) - */ - @Override - public void update(Object object) { - getSession().update(object); - } - - /* - * (non-Javadoc) - * - * @see - * org.lamsfoundation.lams.dao.IBaseDAO#insertOrUpdate(java.lang.Object) - */ - @Override - public void insertOrUpdate(Object object) { - getSession().saveOrUpdate(object); - } - - /* - * (non-Javadoc) - * - * @see - * org.lamsfoundation.lams.dao.IBaseDAO#insertOrUpdateAll(java.util.Collection - * ) - */ - @Override - public void insertOrUpdateAll(Collection objects) { - if (objects != null) { - for (Object object : objects) { - getSession().saveOrUpdate(object); - } - } - } - - @Override - public void update(String queryString) { - doBulkUpdate(queryString); - } - - @Override - public void update(String queryString, Object value) { - doBulkUpdate(queryString, value); - } - - @Override - public void update(String queryString, Object[] values) { - doBulkUpdate(queryString, values); - } - - @Override - public void update(Class clazz, String propertyToChange, Object newValue, String conditionProperty, - Object conditionValue) { - // TODO implement me - } - - @Override - public void update(Class clazz, String propertyToChange, Object newValue, Map conditions) { - // TODO implement me - } - - @Override - public void update(Class clazz, Map newValues, String conditionProperty, Object conditionValue) { - // TODO implement me - } - - @Override - public void update(Class clazz, Map newValues, Map conditions) { - // TODO implement me - } - - @Override - public void updateAnythingLike(Class clazz, Object newValues, Object conditions) { - // TODO implement me - } - - @Override - public void flush() { - getSession().flush(); - } - - /* - * (non-Javadoc) - * - * @see org.lamsfoundation.lams.dao.IBaseDAO#delete(java.lang.Object) - */ - @Override - public void delete(Object object) { - getSession().delete(object); - } - - /* - * (non-Javadoc) - * - * @see org.lamsfoundation.lams.dao.IBaseDAO#deleteAll(java.lang.Class) - */ - @Override - public void deleteAll(Class clazz) { - String queryString = buildQueryString(clazz, DELETE); - doBulkUpdate(queryString); - } - - /* - * (non-Javadoc) - * - * @see org.lamsfoundation.lams.dao.IBaseDAO#deleteAll(java.util.Collection) - */ - @Override - public void deleteAll(Collection objects) { - doDeleteAll(objects); - } - - /* - * (non-Javadoc) - * - * @see org.lamsfoundation.lams.dao.IBaseDAO#deleteById(java.lang.Class, - * java.io.Serializable) - */ - @Override - public void deleteById(Class clazz, Serializable id) { - delete(find(clazz, id)); - } - - /* - * (non-Javadoc) - * - * @see - * org.lamsfoundation.lams.dao.IBaseDAO#deleteByProperty(java.lang.Class, - * java.lang.String, java.lang.Object) - */ - @Override - public void deleteByProperty(Class clazz, String name, Object value) { - String queryString = buildQueryString(clazz, name, DELETE); - doBulkUpdate(queryString, value); - } - - /* - * (non-Javadoc) - * - * @see - * org.lamsfoundation.lams.dao.IBaseDAO#deleteByProperties(java.lang.Class, - * java.util.Map) - */ - @Override - public void deleteByProperties(Class clazz, Map properties) { - Qv qv = buildQueryString(clazz, properties, DELETE, EQUAL_TO_WHAT); - doBulkUpdate(qv.queryString, qv.values); - } - - /* - * (non-Javadoc) - * - * @see - * org.lamsfoundation.lams.dao.IBaseDAO#deleteAnythingLike(java.lang.Object) - */ - @Override - public void deleteAnythingLike(Object object) { - try { - Qv qv = buildQueryString(object, DELETE); - doBulkUpdate(qv.queryString, qv.values); - } catch (Exception e) { - log.debug(e); - } - } - - /* - * (non-Javadoc) - * - * @see org.lamsfoundation.lams.dao.IBaseDAO#find(java.lang.Class, - * java.io.Serializable) - */ - @Override - public Object find(Class clazz, Serializable id) { - return getSession().get(clazz, id); - } - - /* - * (non-Javadoc) - * - * @see org.lamsfoundation.lams.dao.IBaseDAO#findAll(java.lang.Class) - */ - @Override - public List findAll(Class clazz) { - return loadAll(clazz); - } - - /* - * (non-Javadoc) - * - * @see org.lamsfoundation.lams.dao.IBaseDAO#findByProperty(java.lang.Class, - * java.lang.String, java.lang.Object) - */ - @Override - public List findByProperty(Class clazz, String name, Object value) { - String queryString = buildQueryString(clazz, name, SELECT); - return doFind(queryString, value); - } - - /* - * (non-Javadoc) - * - * @see - * org.lamsfoundation.lams.dao.IBaseDAO#findByProperties(java.lang.Class, - * java.util.Map) - */ - @Override - public List findByProperties(Class clazz, Map properties) { - Qv qv = buildQueryString(clazz, properties, SELECT, EQUAL_TO_WHAT); - return doFind(qv.queryString, qv.values); - } - - private String buildQueryString(Class clazz, String operation) { - StringBuilder queryString = new StringBuilder(operation).append(clazz.getSimpleName()); - // log.debug(queryString); - return queryString.toString(); - } - - private String buildQueryString(Class clazz, String name, String operation) { - String clazzName = clazz.getSimpleName(); - String objName = createObjectName(clazzName); - StringBuilder queryString = new StringBuilder(operation).append(clazzName).append(SPACE).append(objName) - .append(WHERE).append(objName).append(SPOT).append(name).append(EQUAL_TO_WHAT); - // log.debug(queryString); - return queryString.toString(); - } - - @Override - public List find(String queryString) { - return doFind(queryString); - } - - @Override - public List find(String queryString, Object value) { - return doFind(queryString, value); - } - - @Override - public List find(String queryString, Object[] values) { - return doFind(queryString, values); - } - - @Override - public List findByNamedQuery(String queryName) { - return doFindByNamedQuery(queryName); - } - - @Override - public List findByNamedQuery(String queryName, Object value) { - return doFindByNamedQuery(queryName, value); - } - - @Override - public List findByNamedQuery(String queryName, Object[] values) { - return doFindByNamedQuery(queryName, values); - } - - @Override - public List searchByStringProperty(Class clazz, String name, String pattern) { - // TODO implement me - return null; - } - - @Override - public List searchByStringProperties(Class clazz, Map properties) { - Map p = new HashMap(); - for (Map.Entry entry : properties.entrySet()) { - p.put(entry.getKey(), entry.getValue()); - } - Qv qv = buildQueryString(clazz, p, SELECT, LIKE_WHAT); - return doFind(qv.queryString, qv.values); - } - - @Override - public List searchByNumberSpan(Class clazz, String name, Integer min, Boolean minIncluded, Integer max, - Boolean maxIncluded) { - // TODO implement me - return null; - } - - private Qv buildQueryString(Class clazz, Map properties, String operation, String condition) { - String clazzName = clazz.getSimpleName(); - String objName = createObjectName(clazzName); - StringBuilder queryString = new StringBuilder(operation).append(clazzName).append(SPACE).append(objName) - .append(WHERE); - Object[] values = new Object[properties.size()]; - int i = 0; - for (Map.Entry entry : properties.entrySet()) { - queryString.append(objName).append(SPOT).append(entry.getKey()).append(condition); - if (i != properties.size() - 1) { - queryString.append(AND); - } - values[i] = entry.getValue(); - i++; - } - // log.debug(queryString); - return new Qv(queryString.toString(), values); - } - - private Qv buildQueryString(Object obj, String operation) throws Exception { - String clazzName = obj.getClass().getSimpleName(); - String objName = createObjectName(clazzName); - StringBuilder queryString = new StringBuilder(operation).append(clazzName).append(SPACE).append(objName) - .append(WHERE); - Field[] fields = obj.getClass().getDeclaredFields(); - List values = new ArrayList(); - for (int i = 0; i < fields.length; i++) { - String name = fields[i].getName(); - Method readMethod = getReadMethod(fields[i], name, obj.getClass()); - Object value = readMethod.invoke(obj); - if (value != null) { - queryString.append(objName).append(SPOT).append(name).append(EQUAL_TO_WHAT); - if (i != fields.length - 1) { - queryString.append(AND); - } - values.add(value); - } - } - // log.debug(queryString); - return new Qv(queryString.toString(), values.toArray()); - } - - private Method getReadMethod(Field field, String fieldName, Class clazz) throws Exception { - String convertedName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); - if (field.getType().getSimpleName().equals("Boolean")) { - return clazz.getDeclaredMethod("is" + convertedName); - } else { - return clazz.getDeclaredMethod("get" + convertedName); - } - - } - - private String createObjectName(String clazzName) { - return clazzName.substring(0, 1).toLowerCase(); - } - - @Override - public void initialize(Object proxy) { - Hibernate.initialize(proxy); - } - - /* - * (non-Javadoc) - * - * @see org.lamsfoundation.lams.dao.IBaseDAO#countAll(java.lang.Class) - */ - @Override - public long countAll(Class clazz) { - String query = "select count(*) from " + clazz.getSimpleName(); - - List list = doFind(query); - - if (list != null && list.size() > 0) { - return (Long) list.get(0); - } else { - return 0; - } - } - - /* - * (non-Javadoc) - * - * @see - * org.lamsfoundation.lams.dao.IBaseDAO#countByProperties(java.lang.Class, - * java.util.Map) - */ - @Override - public long countByProperties(Class clazz, Map properties) { - Qv qv = buildQueryString(clazz, properties, SELECT, EQUAL_TO_WHAT); - String query = "select count(*) " + qv.queryString; - - List list = doFind(query, qv.values); - - if (list != null && list.size() > 0) { - return (Long) list.get(0); - } else { - return 0; - } - } - - protected Session getSession() { - return sessionFactory.getCurrentSession(); - } - - public void setSessionFactory(SessionFactory sessionFactory) { - this.sessionFactory = sessionFactory; - } - - public SessionFactory getSessionFactory() { - return sessionFactory; - } - - public List doFind(final String queryString, final Object... values) { - Query queryObject = convertLegacyStyleParameters(queryString, values); - return queryObject.list(); - } - - private Query convertLegacyStyleParameters(final String queryString, final Object... values) { - Query queryObject = null; - if (values == null) { - queryObject = getSession().createQuery(queryString); - } else { - // replace all the current ? with :P1, :P2, etc - String[] parts = Pattern.compile("\\?").split(queryString, 0); - StringBuilder bldr = new StringBuilder(parts[0]); - int i = 1; - if (parts.length > 1) { - for (; i < parts.length; i++) { - bldr.append(":P").append(i).append(" ").append(parts[i]); - } - } - if (queryString.endsWith("?")) { - bldr.append(" :P").append(i).append(" "); - } - queryObject = getSession().createQuery(bldr.toString()); - for (i = 0; i < values.length; i++) { - queryObject.setParameter("P" + Integer.toString(i + 1), values[i]); - } - } - return queryObject; - } - - public int doBulkUpdate(final String queryString, final Object... values) { - Query queryObject = convertLegacyStyleParameters(queryString, values); - return queryObject.executeUpdate(); - } - - public List doFindByNamedQuery(final String queryName, final Object... values) { - Query queryObject = getSession().getNamedQuery(queryName); - if (values != null) { - for (int i = 0; i < values.length; i++) { - queryObject.setParameter(i, values[i]); - } - } - return queryObject.list(); - } - - public List loadAll(final Class entityClass) { - CriteriaBuilder builder = getSession().getCriteriaBuilder(); - CriteriaQuery query = builder.createQuery(entityClass); - Root variableRoot = query.from(entityClass); - query.select(variableRoot); - return getSession().createQuery(query).getResultList(); - - } - - public void doDeleteAll(final Collection entities) { - for (Object entity : entities) { - getSession().delete(entity); - } - } - - public List doFindByNamedParam(final String queryString, final String[] paramNames, final Object[] values) { - - if (paramNames.length != values.length) { - throw new IllegalArgumentException("Length of paramNames array must match length of values array"); - } - Query queryObject = getSession().createQuery(queryString); - if (values != null) { - for (int i = 0; i < values.length; i++) { - applyNamedParameterToQuery(queryObject, paramNames[i], values[i]); - } - } - return queryObject.list(); - } - - private void applyNamedParameterToQuery(Query queryObject, String paramName, Object value) - throws HibernateException { - - if (value instanceof Collection) { - queryObject.setParameterList(paramName, (Collection) value); - } else if (value instanceof Object[]) { - queryObject.setParameterList(paramName, (Object[]) value); - } else { - queryObject.setParameter(paramName, value); - } - } - - public List doFindByNamedQueryAndNamedParam(String queryName, String paramName, Object value) { - - return doFindByNamedQueryAndNamedParam(queryName, new String[] { paramName }, new Object[] { value }); - } - - public List doFindByNamedQueryAndNamedParam(final String queryName, final String[] paramNames, - final Object[] values) { - - if (values != null && (paramNames == null || paramNames.length != values.length)) { - throw new IllegalArgumentException("Length of paramNames array must match length of values array"); - } - Query queryObject = getSession().getNamedQuery(queryName); - if (values != null) { - for (int i = 0; i < values.length; i++) { - applyNamedParameterToQuery(queryObject, paramNames[i], values[i]); - } - } - - return queryObject.list(); - } - - /** - * @see com.edgenius.paradise.dao.DAO#saveObject(java.lang.Object) - */ - public void saveObject(Object o) { - getSession().saveOrUpdate(o); - } - - /** - * @see com.edgenius.paradise.dao.DAO#getObject(java.lang.Class, java.io.Serializable) - */ - public Object getObject(Class clazz, Serializable id) { - Object o = getSession().get(clazz, id); - return o; - } - - /** - * @see com.edgenius.paradise.dao.DAO#getObjects(java.lang.Class) - */ - public List getObjects(Class clazz) { - return loadAll(clazz); - } - - /** - * @see com.edgenius.paradise.dao.DAO#removeObject(java.lang.Class, java.io.Serializable) - */ - public void removeObject(Class clazz, Serializable id) { - getSession().delete(getObject(clazz, id)); - } - - @Override - public void releaseFromCache(Object o) { - getSessionFactory().getCurrentSession().evict(o); - } -} +package org.lamsfoundation.lams.dao.hibernate; + +import java.io.Serializable; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; + +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +import org.apache.log4j.Logger; +import org.hibernate.Hibernate; +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.query.Query; +import org.lamsfoundation.lams.dao.IBaseDAO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Repository; + +@Repository +public class LAMSBaseDAO implements IBaseDAO { + + private static class Qv { + + String queryString; + Object[] values; + + Qv(String queryString, Object[] values) { + super(); + + this.queryString = queryString; + this.values = values; + } + } + + private static final String SELECT = "from "; + private static final String DELETE = "delete "; + private static final String WHERE = " where "; + private static final String AND = " and "; + private static final String SPACE = " "; + private static final String SPOT = "."; + private static final String EQUAL_TO_WHAT = "=?"; + private static final String LIKE_WHAT = " like ?"; + + private static Logger log = Logger.getLogger(LAMSBaseDAO.class); + + @Autowired + @Qualifier("coreSessionFactory") + private SessionFactory sessionFactory; + + /* + * (non-Javadoc) + * + * @see org.lamsfoundation.lams.dao.IBaseDAO#insert(java.lang.Object) + */ + @Override + public void insert(Object object) { + getSession().save(object); + } + + /* + * (non-Javadoc) + * + * @see org.lamsfoundation.lams.dao.IBaseDAO#update(java.lang.Object) + */ + @Override + public void update(Object object) { + getSession().update(object); + } + + /* + * (non-Javadoc) + * + * @see + * org.lamsfoundation.lams.dao.IBaseDAO#insertOrUpdate(java.lang.Object) + */ + @Override + public void insertOrUpdate(Object object) { + getSession().saveOrUpdate(object); + } + + /* + * (non-Javadoc) + * + * @see + * org.lamsfoundation.lams.dao.IBaseDAO#insertOrUpdateAll(java.util.Collection + * ) + */ + @Override + public void insertOrUpdateAll(Collection objects) { + if (objects != null) { + for (Object object : objects) { + getSession().saveOrUpdate(object); + } + } + } + + @Override + public void update(String queryString) { + doBulkUpdate(queryString); + } + + @Override + public void update(String queryString, Object value) { + doBulkUpdate(queryString, value); + } + + @Override + public void update(String queryString, Object[] values) { + doBulkUpdate(queryString, values); + } + + @Override + public void update(Class clazz, String propertyToChange, Object newValue, String conditionProperty, + Object conditionValue) { + // TODO implement me + } + + @Override + public void update(Class clazz, String propertyToChange, Object newValue, Map conditions) { + // TODO implement me + } + + @Override + public void update(Class clazz, Map newValues, String conditionProperty, Object conditionValue) { + // TODO implement me + } + + @Override + public void update(Class clazz, Map newValues, Map conditions) { + // TODO implement me + } + + @Override + public void updateAnythingLike(Class clazz, Object newValues, Object conditions) { + // TODO implement me + } + + @Override + public void flush() { + getSession().flush(); + } + + /* + * (non-Javadoc) + * + * @see org.lamsfoundation.lams.dao.IBaseDAO#delete(java.lang.Object) + */ + @Override + public void delete(Object object) { + getSession().delete(object); + } + + /* + * (non-Javadoc) + * + * @see org.lamsfoundation.lams.dao.IBaseDAO#deleteAll(java.lang.Class) + */ + @Override + public void deleteAll(Class clazz) { + String queryString = buildQueryString(clazz, DELETE); + doBulkUpdate(queryString); + } + + /* + * (non-Javadoc) + * + * @see org.lamsfoundation.lams.dao.IBaseDAO#deleteAll(java.util.Collection) + */ + @Override + public void deleteAll(Collection objects) { + doDeleteAll(objects); + } + + /* + * (non-Javadoc) + * + * @see org.lamsfoundation.lams.dao.IBaseDAO#deleteById(java.lang.Class, + * java.io.Serializable) + */ + @Override + public void deleteById(Class clazz, Serializable id) { + delete(find(clazz, id)); + } + + /* + * (non-Javadoc) + * + * @see + * org.lamsfoundation.lams.dao.IBaseDAO#deleteByProperty(java.lang.Class, + * java.lang.String, java.lang.Object) + */ + @Override + public void deleteByProperty(Class clazz, String name, Object value) { + String queryString = buildQueryString(clazz, name, DELETE); + doBulkUpdate(queryString, value); + } + + /* + * (non-Javadoc) + * + * @see + * org.lamsfoundation.lams.dao.IBaseDAO#deleteByProperties(java.lang.Class, + * java.util.Map) + */ + @Override + public void deleteByProperties(Class clazz, Map properties) { + Qv qv = buildQueryString(clazz, properties, DELETE, EQUAL_TO_WHAT); + doBulkUpdate(qv.queryString, qv.values); + } + + /* + * (non-Javadoc) + * + * @see + * org.lamsfoundation.lams.dao.IBaseDAO#deleteAnythingLike(java.lang.Object) + */ + @Override + public void deleteAnythingLike(Object object) { + try { + Qv qv = buildQueryString(object, DELETE); + doBulkUpdate(qv.queryString, qv.values); + } catch (Exception e) { + log.debug(e); + } + } + + /* + * (non-Javadoc) + * + * @see org.lamsfoundation.lams.dao.IBaseDAO#find(java.lang.Class, + * java.io.Serializable) + */ + @Override + public T find(Class clazz, Serializable id) { + return getSession().get(clazz, id); + } + + /* + * (non-Javadoc) + * + * @see org.lamsfoundation.lams.dao.IBaseDAO#findAll(java.lang.Class) + */ + @Override + public List findAll(Class clazz) { + return loadAll(clazz); + } + + /* + * (non-Javadoc) + * + * @see org.lamsfoundation.lams.dao.IBaseDAO#findByProperty(java.lang.Class, + * java.lang.String, java.lang.Object) + */ + @Override + public List findByProperty(Class clazz, String name, Object value) { + String queryString = buildQueryString(clazz, name, SELECT); + return doFind(queryString, value); + } + + /* + * (non-Javadoc) + * + * @see + * org.lamsfoundation.lams.dao.IBaseDAO#findByProperties(java.lang.Class, + * java.util.Map) + */ + @SuppressWarnings("unchecked") + @Override + public List findByProperties(Class clazz, Map properties) { + Qv qv = buildQueryString(clazz, properties, SELECT, EQUAL_TO_WHAT); + return (List) doFind(qv.queryString, qv.values); + } + + private String buildQueryString(Class clazz, String operation) { + StringBuilder queryString = new StringBuilder(operation).append(clazz.getSimpleName()); + // log.debug(queryString); + return queryString.toString(); + } + + private String buildQueryString(Class clazz, String name, String operation) { + String clazzName = clazz.getSimpleName(); + String objName = createObjectName(clazzName); + StringBuilder queryString = new StringBuilder(operation).append(clazzName).append(SPACE).append(objName) + .append(WHERE).append(objName).append(SPOT).append(name).append(EQUAL_TO_WHAT); + // log.debug(queryString); + return queryString.toString(); + } + + @Override + public List find(String queryString) { + return doFind(queryString); + } + + @Override + public List find(String queryString, Object value) { + return doFind(queryString, value); + } + + @Override + public List find(String queryString, Object[] values) { + return doFind(queryString, values); + } + + @Override + public List findByNamedQuery(String queryName) { + return doFindByNamedQuery(queryName); + } + + @Override + public List findByNamedQuery(String queryName, Object value) { + return doFindByNamedQuery(queryName, value); + } + + @Override + public List findByNamedQuery(String queryName, Object[] values) { + return doFindByNamedQuery(queryName, values); + } + + @Override + public List searchByStringProperty(Class clazz, String name, String pattern) { + // TODO implement me + return null; + } + + @SuppressWarnings("unchecked") + @Override + public List searchByStringProperties(Class clazz, Map properties) { + Map p = new HashMap<>(); + for (Map.Entry entry : properties.entrySet()) { + p.put(entry.getKey(), entry.getValue()); + } + Qv qv = buildQueryString(clazz, p, SELECT, LIKE_WHAT); + return (List) doFind(qv.queryString, qv.values); + } + + @Override + public List searchByNumberSpan(Class clazz, String name, Integer min, Boolean minIncluded, Integer max, + Boolean maxIncluded) { + // TODO implement me + return null; + } + + private Qv buildQueryString(Class clazz, Map properties, String operation, String condition) { + String clazzName = clazz.getSimpleName(); + String objName = createObjectName(clazzName); + StringBuilder queryString = new StringBuilder(operation).append(clazzName).append(SPACE).append(objName) + .append(WHERE); + Object[] values = new Object[properties.size()]; + int i = 0; + for (Map.Entry entry : properties.entrySet()) { + queryString.append(objName).append(SPOT).append(entry.getKey()).append(condition); + if (i != properties.size() - 1) { + queryString.append(AND); + } + values[i] = entry.getValue(); + i++; + } + // log.debug(queryString); + return new Qv(queryString.toString(), values); + } + + private Qv buildQueryString(Object obj, String operation) throws Exception { + String clazzName = obj.getClass().getSimpleName(); + String objName = createObjectName(clazzName); + StringBuilder queryString = new StringBuilder(operation).append(clazzName).append(SPACE).append(objName) + .append(WHERE); + Field[] fields = obj.getClass().getDeclaredFields(); + List values = new ArrayList<>(); + for (int i = 0; i < fields.length; i++) { + String name = fields[i].getName(); + Method readMethod = getReadMethod(fields[i], name, obj.getClass()); + Object value = readMethod.invoke(obj); + if (value != null) { + queryString.append(objName).append(SPOT).append(name).append(EQUAL_TO_WHAT); + if (i != fields.length - 1) { + queryString.append(AND); + } + values.add(value); + } + } + // log.debug(queryString); + return new Qv(queryString.toString(), values.toArray()); + } + + private Method getReadMethod(Field field, String fieldName, Class clazz) throws Exception { + String convertedName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); + if (field.getType().getSimpleName().equals("Boolean")) { + return clazz.getDeclaredMethod("is" + convertedName); + } else { + return clazz.getDeclaredMethod("get" + convertedName); + } + + } + + private String createObjectName(String clazzName) { + return clazzName.substring(0, 1).toLowerCase(); + } + + @Override + public void initialize(Object proxy) { + Hibernate.initialize(proxy); + } + + /* + * (non-Javadoc) + * + * @see org.lamsfoundation.lams.dao.IBaseDAO#countAll(java.lang.Class) + */ + @Override + public long countAll(Class clazz) { + String query = "select count(*) from " + clazz.getSimpleName(); + + List list = doFind(query); + + if (list != null && list.size() > 0) { + return (Long) list.get(0); + } else { + return 0; + } + } + + /* + * (non-Javadoc) + * + * @see + * org.lamsfoundation.lams.dao.IBaseDAO#countByProperties(java.lang.Class, + * java.util.Map) + */ + @Override + public long countByProperties(Class clazz, Map properties) { + Qv qv = buildQueryString(clazz, properties, SELECT, EQUAL_TO_WHAT); + String query = "select count(*) " + qv.queryString; + + List list = doFind(query, qv.values); + + if (list != null && list.size() > 0) { + return (Long) list.get(0); + } else { + return 0; + } + } + + protected Session getSession() { + return sessionFactory.getCurrentSession(); + } + + public void setSessionFactory(SessionFactory sessionFactory) { + this.sessionFactory = sessionFactory; + } + + public SessionFactory getSessionFactory() { + return sessionFactory; + } + + public List doFind(final String queryString, final Object... values) { + Query queryObject = convertLegacyStyleParameters(queryString, values); + return queryObject.list(); + } + + private Query convertLegacyStyleParameters(final String queryString, final Object... values) { + Query queryObject = null; + if (values == null) { + queryObject = getSession().createQuery(queryString); + } else { + // replace all the current ? with :P1, :P2, etc + String[] parts = Pattern.compile("\\?").split(queryString, 0); + StringBuilder bldr = new StringBuilder(parts[0]); + int i = 1; + if (parts.length > 1) { + for (; i < parts.length; i++) { + bldr.append(":P").append(i).append(" ").append(parts[i]); + } + } + if (queryString.endsWith("?")) { + bldr.append(" :P").append(i).append(" "); + } + queryObject = getSession().createQuery(bldr.toString()); + for (i = 0; i < values.length; i++) { + queryObject.setParameter("P" + Integer.toString(i + 1), values[i]); + } + } + return queryObject; + } + + public int doBulkUpdate(final String queryString, final Object... values) { + Query queryObject = convertLegacyStyleParameters(queryString, values); + return queryObject.executeUpdate(); + } + + public List doFindByNamedQuery(final String queryName, final Object... values) { + Query queryObject = getSession().getNamedQuery(queryName); + if (values != null) { + for (int i = 0; i < values.length; i++) { + queryObject.setParameter(i, values[i]); + } + } + return queryObject.list(); + } + + public List loadAll(final Class entityClass) { + CriteriaBuilder builder = getSession().getCriteriaBuilder(); + CriteriaQuery query = builder.createQuery(entityClass); + Root variableRoot = query.from(entityClass); + query.select(variableRoot); + return getSession().createQuery(query).getResultList(); + + } + + public void doDeleteAll(final Collection entities) { + for (Object entity : entities) { + getSession().delete(entity); + } + } + + public List doFindByNamedParam(final String queryString, final String[] paramNames, final Object[] values) { + + if (paramNames.length != values.length) { + throw new IllegalArgumentException("Length of paramNames array must match length of values array"); + } + Query queryObject = getSession().createQuery(queryString); + if (values != null) { + for (int i = 0; i < values.length; i++) { + applyNamedParameterToQuery(queryObject, paramNames[i], values[i]); + } + } + return queryObject.list(); + } + + private void applyNamedParameterToQuery(Query queryObject, String paramName, Object value) + throws HibernateException { + + if (value instanceof Collection) { + queryObject.setParameterList(paramName, (Collection) value); + } else if (value instanceof Object[]) { + queryObject.setParameterList(paramName, (Object[]) value); + } else { + queryObject.setParameter(paramName, value); + } + } + + public List doFindByNamedQueryAndNamedParam(String queryName, String paramName, Object value) { + + return doFindByNamedQueryAndNamedParam(queryName, new String[] { paramName }, new Object[] { value }); + } + + public List doFindByNamedQueryAndNamedParam(final String queryName, final String[] paramNames, + final Object[] values) { + + if (values != null && (paramNames == null || paramNames.length != values.length)) { + throw new IllegalArgumentException("Length of paramNames array must match length of values array"); + } + Query queryObject = getSession().getNamedQuery(queryName); + if (values != null) { + for (int i = 0; i < values.length; i++) { + applyNamedParameterToQuery(queryObject, paramNames[i], values[i]); + } + } + + return queryObject.list(); + } + + /** + * @see com.edgenius.paradise.dao.DAO#saveObject(java.lang.Object) + */ + public void saveObject(Object o) { + getSession().saveOrUpdate(o); + } + + /** + * @see com.edgenius.paradise.dao.DAO#getObject(java.lang.Class, java.io.Serializable) + */ + public Object getObject(Class clazz, Serializable id) { + Object o = getSession().get(clazz, id); + return o; + } + + /** + * @see com.edgenius.paradise.dao.DAO#getObjects(java.lang.Class) + */ + public List getObjects(Class clazz) { + return loadAll(clazz); + } + + /** + * @see com.edgenius.paradise.dao.DAO#removeObject(java.lang.Class, java.io.Serializable) + */ + public void removeObject(Class clazz, Serializable id) { + getSession().delete(getObject(clazz, id)); + } + + @Override + public void releaseFromCache(Object o) { + getSessionFactory().getCurrentSession().evict(o); + } +}