0,0 → 1,251 |
package ak.hostadmiral.core.model.store.hibernate; |
|
import java.util.Collection; |
import java.util.List; |
import java.util.Map; |
import java.util.HashMap; |
|
import net.sf.hibernate.Hibernate; |
import net.sf.hibernate.HibernateException; |
import net.sf.hibernate.type.Type; |
|
import ak.hostadmiral.util.CollectionInfo; |
import ak.hostadmiral.util.HibernateUtil; |
import ak.hostadmiral.util.ModelStoreException; |
import ak.hostadmiral.core.model.User; |
import ak.hostadmiral.core.model.SystemUser; |
import ak.hostadmiral.core.model.SystemUserManager; |
import ak.hostadmiral.core.model.store.SystemUserStore; |
|
public class SystemUserHibernate |
implements SystemUserStore |
{ |
public SystemUserHibernate() |
throws ModelStoreException |
{ |
initSortKeys(); |
register(); |
} |
|
public SystemUser get(Long id) |
throws ModelStoreException |
{ |
try { |
return (SystemUser)HibernateUtil.currentSession().load(SystemUser.class, id); |
} |
catch(HibernateException ex) |
{ |
throw new ModelStoreException(ex); |
} |
} |
|
public boolean nameExists(SystemUser user, String name) |
throws ModelStoreException |
{ |
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 ModelStoreException(ex); |
} |
} |
|
public boolean uidExists(SystemUser user, Integer uid) |
throws ModelStoreException |
{ |
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 ModelStoreException(ex); |
} |
} |
|
public SystemUser findForName(String name) |
throws ModelStoreException |
{ |
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 ModelStoreException(ex); |
} |
} |
|
public SystemUser findForUid(Integer uid) |
throws ModelStoreException |
{ |
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 ModelStoreException(ex); |
} |
} |
|
public void save(SystemUser systemUser) |
throws ModelStoreException |
{ |
try { |
HibernateUtil.currentSession().saveOrUpdate(systemUser); |
} |
catch(HibernateException ex) |
{ |
throw new ModelStoreException(ex); |
} |
} |
|
public void delete(SystemUser systemUser) |
throws ModelStoreException |
{ |
try { |
HibernateUtil.currentSession().delete(systemUser); |
} |
catch(HibernateException ex) |
{ |
throw new ModelStoreException(ex); |
} |
} |
|
public Collection listAllSystemUsers(CollectionInfo info, int rowsPerPage, int pageNumber, |
Integer[] sortingKeys) |
throws ModelStoreException |
{ |
try { |
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); |
} |
catch(HibernateException ex) |
{ |
throw new ModelStoreException(ex); |
} |
} |
|
public Collection listSystemUsers(CollectionInfo info, int rowsPerPage, int pageNumber, |
Integer[] sortingKeys, User user) |
throws ModelStoreException |
{ |
try { |
if(info != null) { |
info.init(((Integer)HibernateUtil.currentSession().iterate( |
"select count(*) from SystemUser where owner is null or owner = ?", |
user, 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[] { user }, new Type[] { Hibernate.entity(User.class) } ); |
} |
catch(HibernateException ex) |
{ |
throw new ModelStoreException(ex); |
} |
} |
|
public int countSystemUsersAvailable(User user) |
throws ModelStoreException |
{ |
try { |
return ((Integer)HibernateUtil.currentSession().iterate( |
"select count(*) from SystemUser u left join u.owner o where o is null or o=?", |
user, Hibernate.entity(User.class)).next()).intValue(); |
} |
catch(HibernateException ex) |
{ |
throw new ModelStoreException(ex); |
} |
} |
|
public Collection listOwnSystemUsers(User user) |
throws ModelStoreException |
{ |
try { |
return HibernateUtil.currentSession().find( |
"select u from SystemUser u where u.owner = ?", |
user, Hibernate.entity(User.class) ); |
} |
catch(HibernateException ex) |
{ |
throw new ModelStoreException(ex); |
} |
} |
|
protected static Map sortKeys = new HashMap(); |
private static boolean sortKeysInitialized = false; |
|
private static void initSortKeys() |
{ |
if(!sortKeysInitialized) { |
sortKeys.put(SystemUserManager.SORT_UID, "u.uid"); |
sortKeys.put(SystemUserManager.SORT_NAME, "u.name"); |
sortKeysInitialized = true; |
} |
} |
|
private static boolean registered = false; |
protected static void register() |
throws ModelStoreException |
{ |
synchronized(SystemUserManager.class) { |
if(registered) return; |
|
registered = true; |
try { |
HibernateUtil.getConfiguration().addResource( |
"ak/hostadmiral/core/model/SystemUser.hbm.xml"); |
} |
catch(Exception ex) { |
throw new ModelStoreException(ex); |
} |
} |
} |
} |