Rev 1018 | Rev 1041 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package ak.hostadmiral.core.model;
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
import ak.hostadmiral.util.CollectionInfo;
import ak.hostadmiral.util.HibernateUtil;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
public class SystemUserManager
implements
UserBeforeDeleteListener,
UserDeletingListener
{
private static SystemUserManager systemUserManager = null;
private static boolean registered = false;
public static SystemUserManager getInstance()
{
return systemUserManager;
}
protected static void register()
{
synchronized(SystemUserManager.class) {
if(registered) return;
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/SystemUser.hbm.xml");
systemUserManager = new SystemUserManager();
}
catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
}
static {
register();
}
private Collection createdListeners = new ArrayList();
private Collection modifiedListeners = new ArrayList();
private Collection beforeDeleteListeners = new ArrayList();
private Collection deletingListeners = new ArrayList();
private Collection deletedListeners = new ArrayList();
private SystemUserManager()
{
UserManager.getInstance().addBeforeDeleteListener(this);
}
public SystemUser create(User editor)
throws ModelException
{
if(!allowedToCreate(editor)) throw new ModelSecurityException();
return new SystemUser();
}
public boolean allowedToCreate(User editor)
throws ModelException
{
return SystemUser.allowedToCreate(this, editor);
}
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;
}
public boolean nameExists(User editor, SystemUser user, String name)
throws ModelException
{
try {
if(user.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u where name = ?",
name, Hibernate.STRING)
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u where name = ? and u != ?",
new Object[] { name, user },
new Type[] { Hibernate.STRING, Hibernate.entity(SystemUser.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
public boolean uidExists(User editor, SystemUser user, Integer uid)
throws ModelException
{
try {
if(user.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u where uid = ?",
uid, Hibernate.INTEGER)
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u where uid = ? and u != ?",
new Object[] { uid, user },
new Type[] { Hibernate.INTEGER, Hibernate.entity(SystemUser.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
protected SystemUser findForName(String name)
throws ModelException
{
try {
List list = HibernateUtil.currentSession().find(
"select u from SystemUser u left join fetch u.owner where u.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(
"select u from SystemUser u left join fetch u.owner where u.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();
boolean isNew = systemUser.isNew();
//systemUser.setModUser(editor); // FIXME
try {
HibernateUtil.currentSession().saveOrUpdate(systemUser);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
// inform listeners
if(isNew) {
for(Iterator i = createdListeners.iterator(); i.hasNext(); ) {
SystemUserCreatedListener listener = (SystemUserCreatedListener)i.next();
listener.systemUserCreated(editor, systemUser);
}
}
else {
SystemUser oldSystemUser = systemUser.getOrigin();
if(oldSystemUser == null) oldSystemUser = systemUser;
for(Iterator i = modifiedListeners.iterator(); i.hasNext(); ) {
SystemUserModifiedListener listener = (SystemUserModifiedListener)i.next();
listener.systemUserModified(editor, systemUser, oldSystemUser);
}
}
}
public void addCreatedListener(SystemUserCreatedListener listener)
{
createdListeners.add(listener);
}
public void removeCreatedListener(SystemUserCreatedListener listener)
{
createdListeners.remove(listener);
}
public void addModifiedListener(SystemUserModifiedListener listener)
{
modifiedListeners.add(listener);
}
public void removeModifiedListener(SystemUserModifiedListener listener)
{
modifiedListeners.remove(listener);
}
public void addBeforeDeleteListener(SystemUserBeforeDeleteListener listener)
{
beforeDeleteListeners.add(listener);
}
public void removeBeforeDeleteListener(SystemUserBeforeDeleteListener listener)
{
beforeDeleteListeners.remove(listener);
}
public void addDeletingListener(SystemUserDeletingListener listener)
{
deletingListeners.add(listener);
}
public void removeDeletingListener(SystemUserDeletingListener listener)
{
deletingListeners.remove(listener);
}
public void addDeletedListener(SystemUserDeletedListener listener)
{
deletedListeners.add(listener);
}
public void removeDeletedListener(SystemUserDeletedListener listener)
{
deletedListeners.remove(listener);
}
public Collection beforeDelete(User editor, SystemUser user, Collection known)
throws ModelException
{
Collection cascade = new ArrayList();
for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) {
SystemUserBeforeDeleteListener listener = (SystemUserBeforeDeleteListener)i.next();
Collection subcascade = listener.systemUserBeforeDelete(editor, user, known);
if(subcascade != null)
cascade.addAll(subcascade);
}
return cascade;
}
public void delete(User editor, SystemUser systemUser)
throws ModelException
{
// check rights
if(!systemUser.deleteableBy(editor))
throw new ModelSecurityException();
// inform deleting listeners
for(Iterator i = deletingListeners.iterator(); i.hasNext(); ) {
SystemUserDeletingListener listener = (SystemUserDeletingListener)i.next();
listener.systemUserDeleting(editor, systemUser);
}
// backup copy
SystemUser oldSystemUser = new SystemUser(systemUser);
// delete it
try {
HibernateUtil.currentSession().delete(systemUser);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
// inform deleted listeners
for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) {
SystemUserDeletedListener listener = (SystemUserDeletedListener)i.next();
listener.systemUserDeleted(editor, oldSystemUser);
}
}
public Collection listSystemUsers(User editor)
throws ModelException
{
return listSystemUsers(null, 0, 0, null, editor);
}
public Collection listSystemUsers(CollectionInfo info, int rowsPerPage, int pageNumber,
Integer[] sortingKeys, User editor)
throws ModelException
{
try {
if(editor.isSuperuser()) {
if(info != null) {
info.init(((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser").next()).intValue(),
pageNumber, rowsPerPage);
}
return HibernateUtil.pageableList(rowsPerPage, pageNumber,
"select u from SystemUser u left join fetch u.owner"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null);
}
else {
if(info != null) {
info.init(((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser where owner is null or owner = ?",
editor, Hibernate.entity(User.class))
.next()).intValue(),
pageNumber, rowsPerPage);
}
return HibernateUtil.pageableList(rowsPerPage, pageNumber,
"select u from SystemUser u left join u.owner o"
+ " where u.owner is null or u.owner = ?"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys),
new Object[] { editor }, new Type[] { 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);
}
}
public Collection userBeforeDelete(User editor, User user, Collection known)
throws ModelException
{
Collection systemUsers;
try {
systemUsers = HibernateUtil.currentSession().find(
"select u from SystemUser u where u.owner = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
Collection cascade = new ArrayList();
for(Iterator i = systemUsers.iterator(); i.hasNext(); ) {
SystemUser u = (SystemUser)i.next();
if(u.viewableBy(editor)) {
if(u.deleteableBy(editor))
cascade.add(new CascadeDeleteElement(u, CascadeDeleteElement.DELETE,
this.beforeDelete(editor, u, known)));
else
cascade.add(new CascadeDeleteElement(u, CascadeDeleteElement.FORBIDDEN, null));
}
else {
cascade.add(new CascadeDeleteElement(SystemUser.createLimitedCopy(u),
CascadeDeleteElement.FORBIDDEN, null));
}
}
return cascade;
}
public void userDeleting(User editor, User user)
throws ModelException
{
Collection systemUsers;
try {
systemUsers = HibernateUtil.currentSession().find(
"select u from SystemUser u where u.owner = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
for(Iterator i = systemUsers.iterator(); i.hasNext(); ) {
delete(editor, (SystemUser)i.next());
}
}
public static final Integer SORT_UID = new Integer(1);
public static final Integer SORT_NAME = new Integer(2);
protected static Map sortKeys = new HashMap();
static {
sortKeys.put(SORT_UID, "u.uid");
sortKeys.put(SORT_NAME, "u.name");
}
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);
}
}
}