1,49 → 1,25 |
package ak.hostadmiral.core.model; |
|
import java.util.*; |
import net.sf.hibernate.*; |
import net.sf.hibernate.type.Type; |
import java.util.Collection; |
import java.util.ArrayList; |
import java.util.Iterator; |
import java.util.Map; |
import java.util.Comparator; |
|
import ak.hostadmiral.util.ConfigInit; |
import ak.hostadmiral.util.CollectionInfo; |
import ak.hostadmiral.util.HibernateUtil; |
import ak.hostadmiral.util.ModelException; |
import ak.hostadmiral.util.ModelSecurityException; |
import ak.hostadmiral.core.model.store.SystemUserStore; |
|
public class SystemUserManager |
implements |
ConfigInit, |
UserBeforeDeleteListener, |
UserDeletingListener |
{ |
private static SystemUserManager systemUserManager = null; |
private static boolean registered = false; |
private SystemUserStore store; |
|
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(); |
50,7 → 26,8 |
private Collection deletingListeners = new ArrayList(); |
private Collection deletedListeners = new ArrayList(); |
|
private SystemUserManager() |
public SystemUserManager() |
throws ModelException |
{ |
UserManager.getInstance().addBeforeDeleteListener(this); |
} |
72,16 → 49,8 |
public SystemUser get(User editor, Long id) |
throws ModelException |
{ |
SystemUser user; |
SystemUser user = store.get(id); |
|
try { |
user = (SystemUser)HibernateUtil.currentSession().load(SystemUser.class, id); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
|
if(!user.viewableBy(editor)) |
throw new ModelSecurityException(); |
|
91,84 → 60,36 |
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; |
return store.nameExists(user, name); |
} |
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; |
return store.uidExists(user, uid); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
protected SystemUser findForName(String name) |
public SystemUser findForName(User editor, 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); |
SystemUser user = store.findForName(name); |
|
if(list.size() == 0) |
return null; |
else |
return (SystemUser)list.get(0); |
if(user != null && !user.viewableBy(editor)) |
throw new ModelSecurityException(); |
|
return user; |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
protected SystemUser findForUid(Integer uid) |
public SystemUser findForUid(User editor, 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); |
SystemUser user = store.findForUid(uid); |
|
if(list.size() == 0) |
return null; |
else |
return (SystemUser)list.get(0); |
if(user != null && !user.viewableBy(editor)) |
throw new ModelSecurityException(); |
|
return user; |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public void save(User editor, SystemUser systemUser) |
throws ModelException |
180,13 → 101,7 |
|
//systemUser.setModUser(editor); // FIXME |
|
try { |
HibernateUtil.currentSession().saveOrUpdate(systemUser); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
store.save(systemUser); |
|
// inform listeners |
if(isNew) { |
287,13 → 202,7 |
SystemUser oldSystemUser = new SystemUser(systemUser); |
|
// delete it |
try { |
HibernateUtil.currentSession().delete(systemUser); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
store.delete(systemUser); |
|
// inform deleted listeners |
for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) { |
312,72 → 221,26 |
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); |
if(editor.isSuperuser()) |
return store.listAllSystemUsers(info, rowsPerPage, pageNumber, sortingKeys); |
else |
return store.listSystemUsers(info, rowsPerPage, pageNumber, sortingKeys, editor); |
} |
|
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; |
return store.countSystemUsersAvailable(editor) > 0; |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public Collection userBeforeDelete(User editor, User user, Collection known) |
throws ModelException |
{ |
Collection systemUsers; |
Collection systemUsers = store.listOwnSystemUsers(user); |
|
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(); |
400,18 → 263,8 |
public void userDeleting(User editor, User user) |
throws ModelException |
{ |
Collection systemUsers; |
Collection systemUsers = store.listOwnSystemUsers(user); |
|
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()); |
} |
420,13 → 273,6 |
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(); |
|
483,4 → 329,25 |
return (obj instanceof NameComparator); |
} |
} |
|
public void init(Map params) |
throws ModelException |
{ |
try { |
systemUserManager = this; |
|
Class c = Class.forName((String)params.get("store")); |
store = (SystemUserStore)c.newInstance(); |
} |
catch(Exception ex) { |
throw new ModelException(ex); |
} |
} |
|
private static SystemUserManager systemUserManager = null; |
|
public static SystemUserManager getInstance() |
{ |
return systemUserManager; |
} |
} |