0,0 → 1,347 |
package ak.hostadmiral.core.model; |
|
import java.util.*; |
import net.sf.hibernate.*; |
import net.sf.hibernate.type.Type; |
import ak.hostadmiral.util.HibernateUtil; |
import ak.hostadmiral.util.ModelException; |
import ak.hostadmiral.util.ModelSecurityException; |
|
public class SystemUserManager |
implements UserBeforeDeleteListener |
{ |
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 beforeDeleteListeners = 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( |
"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(); |
|
systemUser.setModUser(editor); |
|
try { |
HibernateUtil.currentSession().saveOrUpdate(systemUser); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public void addBeforeDeleteListener(SystemUserBeforeDeleteListener listener) |
{ |
beforeDeleteListeners.add(listener); |
} |
|
public void removeBeforeDeleteListener(SystemUserBeforeDeleteListener listener) |
{ |
beforeDeleteListeners.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 |
{ |
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); |
} |
} |
|
public Collection userBeforeDelete(User editor, User user, Collection known) |
throws ModelException |
{ |
Collection systemUsers; |
|
try { |
systemUsers = HibernateUtil.currentSession().find( |
"from SystemUser where 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 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); |
} |
} |
} |