Rev 904 | Rev 915 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package ak.hostcaptain.core.model;
import java.util.*;
import net.sf.hibernate.*;
import ak.hostcaptain.util.HibernateUtil;
import ak.hostcaptain.util.ModelException;
import ak.hostcaptain.util.ModelSecurityException;
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/hostcaptain/core/model/SystemUser.hbm.xml");
}
catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
}
static {
register();
}
private SystemUserManager()
{
}
public SystemUser create(User editor)
throws ModelException
{
if(!allowedToCreate(editor)) throw new ModelSecurityException();
return new SystemUser();
}
public boolean allowedToCreate(User editor)
throws ModelException
{
return editor.isSuperuser();
}
public SystemUser get(User editor, Long id)
throws ModelException
{
SystemUser user;
try {
user = (SystemUser)HibernateUtil.currentSession().load(SystemUser.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
if(!user.viewableBy(editor))
throw new ModelSecurityException();
return user;
}
protected 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);
}
}
protected 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(User editor, SystemUser systemUser)
throws ModelException
{
if(!systemUser.editableBy(editor))
throw new ModelSecurityException();
try {
HibernateUtil.currentSession().saveOrUpdate(systemUser);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
public void delete(User editor, SystemUser systemUser)
throws ModelException
{
if(!systemUser.deleteableBy(editor))
throw new ModelSecurityException();
try {
HibernateUtil.currentSession().delete(systemUser);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
public Collection listSystemUsers(User editor)
throws ModelException
{
try {
if(editor.isSuperuser())
return HibernateUtil.currentSession().find("from SystemUser");
else
return HibernateUtil.currentSession().find(
"select u from SystemUser u left join u.owner o where o is null or o=?",
editor, Hibernate.entity(User.class));
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
public boolean areSystemUsersAvailable(User editor)
throws ModelException
{
try {
if(editor.isSuperuser())
return true;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u left join u.owner o where o is null or o=?",
editor, Hibernate.entity(User.class)).next()).intValue() > 0;
}
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);
}
}
}