1 24 package org.riotfamily.riot.hibernate.support; 25 26 import java.io.Serializable ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.hibernate.Criteria; 34 import org.hibernate.EntityMode; 35 import org.hibernate.Hibernate; 36 import org.hibernate.Query; 37 import org.hibernate.Session; 38 import org.hibernate.SessionFactory; 39 import org.hibernate.criterion.Expression; 40 import org.hibernate.metadata.ClassMetadata; 41 import org.riotfamily.common.beans.PropertyUtils; 42 import org.riotfamily.riot.security.AccessController; 43 import org.springframework.util.StringUtils; 44 45 public final class HibernateUtils { 46 47 public static final String LIVE_MODE_FILTER_NAME = "liveMode"; 48 49 public static final String PUBLISHED_PARAM_NAME = "published"; 50 51 private static Log log = LogFactory.getLog(HibernateUtils.class); 52 53 private HibernateUtils() { 54 } 55 56 public static Object get(Session session, Class beanClass, String id) { 57 SessionFactory sf = session.getSessionFactory(); 58 Serializable serialId = convertId(beanClass, id, sf); 59 return session.get(beanClass, serialId); 60 } 61 62 public static Serializable convertId(Class beanClass, String id, 63 SessionFactory sessionFactory) { 64 65 Class identifierClass = sessionFactory.getClassMetadata(beanClass) 66 .getIdentifierType().getReturnedClass(); 67 68 return (Serializable ) PropertyUtils.convert(id, identifierClass); 69 } 70 71 public static String getIdAsString(SessionFactory sessionFactory, Object bean) { 72 Class clazz = Hibernate.getClass(bean); 73 ClassMetadata metadata = sessionFactory.getClassMetadata(clazz); 74 return metadata.getIdentifier(bean, EntityMode.POJO).toString(); 75 } 76 77 82 public static String getExampleWhereClause(Object example, String alias, 83 String [] propertyNames) { 84 85 if (example == null || propertyNames == null) { 86 return null; 87 } 88 StringBuffer hql = new StringBuffer (); 89 Map properties; 90 if (example instanceof Map ) { 91 properties = (Map ) example; 92 } 93 else { 94 properties = PropertyUtils.getProperties(example, propertyNames); 95 } 96 for (int i = 0; i < propertyNames.length; i++) { 97 String name = propertyNames[i]; 98 Object value = properties.get(name); 99 if (value != null) { 100 if (hql.length() > 0) { 101 hql.append(" and "); 102 } 103 if (value instanceof Collection ) { 104 Collection c = (Collection ) value; 105 hql.append("1 = 1"); 106 for (int j = 0; j < c.size(); j++) { 107 hql.append(" and :").append(name).append("_").append(j) 108 .append(" in elements(").append(alias) 109 .append('.').append(name).append(')'); 110 } 111 } 112 else { 113 hql.append(alias).append('.').append(name); 114 hql.append(" = :").append(name); 115 } 116 } 117 } 118 return hql.length() > 0 ? hql.toString() : null; 119 } 120 121 134 public static void setCollectionValueParams(Query query, 135 String [] names, Object object) { 136 137 for (int i = 0; i < names.length; i++) { 138 String name = names[i]; 139 Object value; 140 if (object instanceof Map ) { 141 value = ((Map ) object).get(name); 142 } 143 else { 144 value = PropertyUtils.getProperty(object, name); 145 } 146 if (value instanceof Collection ) { 147 int j = 0; 148 Iterator values = ((Collection ) value).iterator(); 149 while (values.hasNext()) { 150 query.setParameter(name + "_" + j++, values.next()); 151 } 152 } 153 } 154 } 155 156 163 public static String getSearchWhereClause(String alias, 164 String [] propertyNames, String searchParamName) { 165 166 if (propertyNames == null || propertyNames.length == 0) { 167 return null; 168 } 169 StringBuffer hql = new StringBuffer ("("); 170 for (int i = 0; i < propertyNames.length; i++) { 171 String name = propertyNames[i]; 172 if (i > 0) { 173 hql.append(" or "); 174 } 175 hql.append("lower(").append(alias).append('.').append(name); 176 hql.append(") like :").append(searchParamName); 177 } 178 hql.append(')'); 179 return hql.toString(); 180 } 181 182 188 public static StringBuffer appendHql(StringBuffer hql, 189 String expression, String term) { 190 191 if (StringUtils.hasText(term)) { 192 if (expression != null && hql.length() > 0) { 193 hql.append(' ').append(expression).append(' '); 194 } 195 hql.append(term); 196 } 197 return hql; 198 } 199 200 public static void addEqOrNull(Criteria c, String name, Object val) { 201 if (val != null) { 202 c.add(Expression.eq(name, val)); 203 } 204 else { 205 c.add(Expression.isNull(name)); 206 } 207 } 208 209 public static boolean isLiveModeFilterDefined(SessionFactory sf) { 210 return sf.getDefinedFilterNames().contains(LIVE_MODE_FILTER_NAME); 211 } 212 213 public static void enableLiveModeFilterIfNecessary(Session session) { 214 if (!AccessController.isAuthenticatedUser()) { 215 if (isLiveModeFilterDefined(session.getSessionFactory())) { 216 session.enableFilter(LIVE_MODE_FILTER_NAME).setParameter( 217 PUBLISHED_PARAM_NAME, Boolean.TRUE); 218 } 219 else { 220 log.warn("No filter named " + LIVE_MODE_FILTER_NAME 221 + " defined for SessionFactory"); 222 } 223 } 224 } 225 } 226 | Popular Tags |