Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 12 → Rev 13

/kickup/trunk/src/ak/kickup/core/model/ActManager.java
0,0 → 1,151
package ak.kickup.core.model;
 
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
import ak.kickup.util.HibernateUtil;
import ak.kickup.util.ModelException;
import ak.kickup.util.ModelSecurityException;
 
public class ActManager
{
private static ActManager actManager = null;
private static boolean registered = false;
 
public static ActManager getInstance()
{
return actManager;
}
 
protected static void register()
{
synchronized(ActManager.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/kickup/core/model/Act.hbm.xml");
 
actManager = new ActManager();
}
catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
}
 
static {
register();
}
 
private ActManager()
{
}
 
public Act create()
throws ModelException
{
return new Act();
}
 
public Act get(Long id)
throws ModelException
{
try {
return (Act)HibernateUtil.currentSession().load(Act.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public boolean nameExists(Act act, String name)
throws ModelException
{
try {
if(act.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from Act where name = ?",
name, Hibernate.STRING)
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from Act a where name = ? and a != ?",
new Object[] { name, act },
new Type[] { Hibernate.STRING, Hibernate.entity(Act.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void save(Act act)
throws ModelException
{
try {
HibernateUtil.currentSession().saveOrUpdate(act);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void delete(Act act)
throws ModelException
{
try {
HibernateUtil.currentSession().delete(act);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection listActs()
throws ModelException
{
try {
return HibernateUtil.currentSession().find("from Act");
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public static final Comparator NAME_COMPARATOR = new NameComparator();
 
private static class NameComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof Act) || !(o2 instanceof Act))
throw new ClassCastException("not a Act");
 
Act a1 = (Act)o1;
Act a2 = (Act)o2;
 
if(a1 == null && a2 == null)
return 0;
else if(a1 == null && a2 != null)
return -1;
else if(a1 != null && a2 == null)
return 1;
else
return a1.getName().compareToIgnoreCase(a2.getName());
}
 
public boolean equals(Object obj)
{
return (obj instanceof NameComparator);
}
}
}