Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1020 → Rev 1021

/hostadmiral/trunk/src/ak/hostadmiral/util/HibernateUtil.java
1,8 → 1,14
package ak.hostadmiral.util;
 
import java.util.Iterator;
import java.util.Collection;
import java.util.Map;
 
import java.util.List;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import net.sf.hibernate.type.Type;
135,28 → 141,60
hb.transaction = null;
}
 
public static Collection pageableList(CollectionInfo info, int pageSize, int pageNumber,
String query, String[] returnAliases, Class[] returnClasses,
Object[] values, Type[] types)
public static List sqlQuery(String query, Object[] values)
throws ModelException
{
try {
// find the collection size if needed
if(info != null) {
Query sizeq = HibernateUtil.currentSession().createSQLQuery(
"select count(*) as {c} from " + query,
"c", Integer.class);
Connection con;
PreparedStatement stmt;
 
if(values != null && types != null) {
for(int i = 0; i < values.length; i++)
sizeq.setParameter(i, values[i], types[i]);
}
try {
con = currentSession().connection();
}
catch(HibernateException ex) {
throw new ModelException(ex);
}
 
info.setSize(((Integer)sizeq.iterate().next()).intValue());
try {
stmt = con.prepareStatement(query);
}
catch(SQLException ex) {
throw new ModelException(ex);
}
 
try {
if(values != null) {
for(int i = 0; i < values.length; i++)
stmt.setObject(i+1, values[i]);
}
 
// find the collection
Query hq = HibernateUtil.currentSession().createSQLQuery(
List res = new ArrayList();
ResultSet rs = stmt.executeQuery();
while(rs.next()) {
res.add(rs.getObject(1));
}
return res;
}
catch(SQLException ex)
{
throw new ModelException(ex);
}
finally {
try {
stmt.close();
}
catch(SQLException ex) {
ex.printStackTrace();
}
}
}
 
public static List pageableList(int pageSize, int pageNumber,
String query, Class classToLoad, String[] returnAliases, Class[] returnClasses,
Object[] values, Type[] types)
throws ModelException
{
try {
Query hq = currentSession().createSQLQuery(
query, returnAliases, returnClasses);
 
if(values != null && types != null) {
169,7 → 207,8
hq.setMaxResults(pageSize);
}
 
return hq.list();
return selectClassColumn(classToLoad, hq.list());
// FIXME: really no other way in Hibernate?
}
catch(HibernateException ex)
{
177,6 → 216,52
}
}
 
protected static List selectClassColumn(Class classToLoad, List list)
throws ModelException
{
List res = new ArrayList();
 
if(list == null || list.size() == 0) return res;
 
Object o = list.get(0);
if(o == null)
throw new ModelException(
"First element in the list is null, cannot determine it type");
 
if(!(o instanceof Object[])) {
if(classToLoad.isInstance(o))
return list;
else
throw new ModelException("First element in the list is instance of "
+ o.getClass().getName() + ", which is not java.lang.Object[] or "
+ classToLoad.getName());
}
 
Object[] oa = (Object[])o;
int pos = -1;
 
for(int i = 0; i < oa.length; i++) {
if(classToLoad.isInstance(oa[i])) {
if(pos < 0)
pos = i;
else
throw new ModelException("First row of the list has several elements of type "
+ classToLoad.getName());
}
}
if(pos < 0) {
throw new ModelException("First row of the list has no elements of type "
+ classToLoad.getName());
}
 
for(Iterator i = list.iterator(); i.hasNext(); ) {
Object[] e = (Object[])i.next();
res.add(e[pos]);
}
 
return res;
}
 
public static String formOrderClause(Integer[] sortingKeys, Map fieldMap)
throws ModelException
{