Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 898 → Rev 899

/sun/hostcaptain/trunk/src/ak/webcontrol/core/model/SystemUserManager.java
0,0 → 1,191
package ak.webcontrol.core.model;
 
import java.util.*;
import net.sf.hibernate.*;
import ak.webcontrol.util.HibernateUtil;
import ak.webcontrol.util.ModelException;
 
public class SystemUserManager
{
private static boolean registered = false;
protected static void register()
{
synchronized(SystemUserManager.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"/ak/webcontrol/core/model/SystemUser.hbm.xml");
}
catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
}
 
static {
register();
}
 
private SystemUserManager()
{
}
 
public SystemUser create()
{
return new SystemUser();
}
 
public SystemUser get(Long id)
throws ModelException
{
try {
return (SystemUser)HibernateUtil.currentSession().load(SystemUser.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public SystemUser findForName(String name)
throws ModelException
{
try {
List list = HibernateUtil.currentSession().find(
"from SystemUser where name=?", name, Hibernate.STRING);
 
if(list.size() == 0)
return null;
else
return (SystemUser)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public SystemUser findForUid(Integer uid)
throws ModelException
{
try {
List list = HibernateUtil.currentSession().find(
"from SystemUser where uid=?", uid, Hibernate.INTEGER);
 
if(list.size() == 0)
return null;
else
return (SystemUser)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void save(SystemUser systemUser)
throws ModelException
{
try {
HibernateUtil.currentSession().saveOrUpdate(systemUser);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void delete(SystemUser systemUser)
throws ModelException
{
try {
HibernateUtil.currentSession().delete(systemUser);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection listSystemUsers()
throws ModelException
{
try {
return HibernateUtil.currentSession().find("from SystemUser");
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
private static SystemUserManager systemUserManager = null;
 
public static SystemUserManager getInstance()
{
if(systemUserManager == null)
systemUserManager = new SystemUserManager();
 
return systemUserManager;
}
 
public static final Comparator UID_COMPARATOR = new UidComparator();
public static final Comparator NAME_COMPARATOR = new NameComparator();
 
private static class UidComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof SystemUser) || !(o2 instanceof SystemUser))
throw new ClassCastException("not a SystemUser");
 
SystemUser a1 = (SystemUser)o1;
SystemUser a2 = (SystemUser)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.getUid().compareTo(a2.getUid());
}
 
public boolean equals(Object obj)
{
return (obj instanceof UidComparator);
}
}
 
private static class NameComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof SystemUser) || !(o2 instanceof SystemUser))
throw new ClassCastException("not a SystemUser");
 
SystemUser a1 = (SystemUser)o1;
SystemUser a2 = (SystemUser)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);
}
}
}