Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1040 → Rev 1041

/hostadmiral/trunk/src/ak/hostadmiral/util/HibernateUtil.java
23,7 → 23,7
private static boolean validated = false;
 
private static void validate()
throws HibernateException, ModelException
throws HibernateException, ModelStoreException
{
synchronized(HibernateUtil.class) {
if(validated) return;
31,14 → 31,15
Collection versions = currentSession().find("from DatabaseVersion");
 
if(versions == null || versions.size() == 0)
throw new ModelException("Database structure version not found");
throw new ModelStoreException("Database structure version not found");
 
if(versions.size() > 1)
throw new ModelException("Too much entries in database structure version table");
throw new ModelStoreException(
"Too much entries in database structure version table");
 
int version = ((DatabaseVersion)versions.iterator().next()).getMajor();
if(version != DATABASE_VERSION)
throw new ModelException("Expected database structure version "
throw new ModelStoreException("Expected database structure version "
+ DATABASE_VERSION + ", found " + version);
 
validated = true;
95,12 → 96,12
}
 
public static void closeSession()
throws HibernateException, ModelException
throws HibernateException, ModelStoreException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb == null)
throw new ModelException("No session found for this thread");
throw new ModelStoreException("No session found for this thread");
 
hibernateBean.set(null);
hb.session.close();
107,12 → 108,12
}
 
public static void beginTransaction()
throws HibernateException, ModelException
throws HibernateException, ModelStoreException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb != null && hb.transaction != null)
throw new ModelException("Transaction is already open");
throw new ModelStoreException("Transaction is already open");
 
currentBean().transaction = currentSession().beginTransaction();
 
122,7 → 123,7
}
 
public static boolean isTransactionOpen()
throws HibernateException, ModelException
throws HibernateException, ModelStoreException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
130,12 → 131,12
}
 
public static void commitTransaction()
throws HibernateException, ModelException
throws HibernateException, ModelStoreException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb == null || hb.transaction == null)
throw new ModelException("No open transaction");
throw new ModelStoreException("No open transaction");
 
hb.transaction.commit();
hb.transaction = null;
142,12 → 143,12
}
 
public static void rollbackTransaction()
throws HibernateException, ModelException
throws HibernateException, ModelStoreException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb == null || hb.transaction == null)
throw new ModelException("No open transaction");
throw new ModelStoreException("No open transaction");
 
hb.transaction.rollback();
hb.transaction = null;
154,23 → 155,16
}
 
public static List sqlQuery(String query, Object[] values)
throws ModelException
throws HibernateException, ModelStoreException
{
Connection con;
Connection con = currentSession().connection();
PreparedStatement stmt;
 
try {
con = currentSession().connection();
}
catch(HibernateException ex) {
throw new ModelException(ex);
}
 
try {
stmt = con.prepareStatement(query);
}
catch(SQLException ex) {
throw new ModelException(ex);
throw new ModelStoreException(ex);
}
 
try {
188,7 → 182,7
}
catch(SQLException ex)
{
throw new ModelException(ex);
throw new ModelStoreException(ex);
}
finally {
try {
203,58 → 197,45
public static List pageableListSql(int pageSize, int pageNumber,
String query, String[] returnAliases, Class[] returnClasses,
Object[] values, Type[] types)
throws ModelException
throws HibernateException, ModelStoreException
{
try {
Query hq = currentSession().createSQLQuery(
query, returnAliases, returnClasses);
Query hq = currentSession().createSQLQuery(
query, returnAliases, returnClasses);
 
if(values != null && types != null) {
for(int i = 0; i < values.length; i++)
hq.setParameter(i, values[i], types[i]);
}
if(values != null && types != null) {
for(int i = 0; i < values.length; i++)
hq.setParameter(i, values[i], types[i]);
}
 
if(pageSize > 0) {
hq.setFirstResult(pageSize * pageNumber);
hq.setMaxResults(pageSize);
}
if(pageSize > 0) {
hq.setFirstResult(pageSize * pageNumber);
hq.setMaxResults(pageSize);
}
 
return selectFirstClassColumn(hq.list());
// FIXME: really no other way in Hibernate?
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
return selectFirstClassColumn(hq.list());
// FIXME: really no other way in Hibernate?
}
 
public static List pageableList(int pageSize, int pageNumber,
String query, Object[] values, Type[] types)
throws ModelException
throws HibernateException, ModelStoreException
{
try {
Query hq = currentSession().createQuery(query);
Query hq = currentSession().createQuery(query);
 
if(values != null && types != null) {
for(int i = 0; i < values.length; i++)
hq.setParameter(i, values[i], types[i]);
}
if(values != null && types != null) {
for(int i = 0; i < values.length; i++)
hq.setParameter(i, values[i], types[i]);
}
 
if(pageSize > 0) {
hq.setFirstResult(pageSize * pageNumber);
hq.setMaxResults(pageSize);
}
if(pageSize > 0) {
hq.setFirstResult(pageSize * pageNumber);
hq.setMaxResults(pageSize);
}
 
return hq.list();
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
return hq.list();
}
 
protected static List selectFirstClassColumn(List list)
throws ModelException
{
List res = new ArrayList();
 
266,7 → 247,7
}
 
public static String formOrderClause(Integer[] sortingKeys, Map fieldMap)
throws ModelException
throws ModelStoreException
{
if(sortingKeys == null || sortingKeys.length == 0) return "";
 
277,7 → 258,7
 
String field = (String)fieldMap.get(sortingKeys[i]);
if(field == null)
throw new ModelException(
throw new ModelStoreException(
"Field for sorting key " + sortingKeys[i] + " not found");
 
buf.append(field);
/hostadmiral/trunk/src/ak/hostadmiral/util/ConfigInit.java
10,5 → 10,6
* @param params map String -> String with pairs of param name -> value
* from the initializaion file
*/
public void init(Map params);
public void init(Map params)
throws ModelException;
}
/hostadmiral/trunk/src/ak/hostadmiral/util/ModelStoreException.java
0,0 → 1,25
package ak.hostadmiral.util;
 
public class ModelStoreException
extends ModelException
{
public ModelStoreException()
{
this(null, null);
}
 
public ModelStoreException(String message)
{
this(message, null);
}
 
public ModelStoreException(Exception chainedException)
{
this(null, chainedException);
}
 
public ModelStoreException(String message, Exception chainedException)
{
super(message, chainedException);
}
}