Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1221 → Rev 1223

/hostadmiral/branches/hibernate3/src/ak/hostadmiral/core/model/store/hibernate/MailAliasDestinationHibernate.java
0,0 → 1,97
package ak.hostadmiral.core.model.store.hibernate;
 
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
 
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.type.Type;
 
import ak.hostadmiral.util.CollectionInfo;
import ak.hostadmiral.util.ModelStoreException;
import ak.hostadmiral.util.hibernate.HibernateUtil;
import ak.hostadmiral.core.model.MailAlias;
import ak.hostadmiral.core.model.MailAliasDestination;
import ak.hostadmiral.core.model.MailAliasDestinationManager;
import ak.hostadmiral.core.model.store.MailAliasDestinationStore;
 
public class MailAliasDestinationHibernate
implements MailAliasDestinationStore
{
public MailAliasDestinationHibernate()
throws ModelStoreException
{
register();
}
 
public MailAliasDestination get(Long id)
throws ModelStoreException
{
try {
return (MailAliasDestination)HibernateUtil.currentSession()
.load(MailAliasDestination.class, id);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public void save(MailAliasDestination mailAliasDestination)
throws ModelStoreException
{
try {
HibernateUtil.currentSession().saveOrUpdate(mailAliasDestination);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public void delete(MailAliasDestination mailAliasDestination)
throws ModelStoreException
{
try {
HibernateUtil.currentSession().delete(mailAliasDestination);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public Collection listMailAliasesDestination(MailAlias alias)
throws ModelStoreException
{
try {
return HibernateUtil.currentSession().find(
"select d from MailAliasDestination d left join fetch d.mailbox where d.alias=?",
alias, Hibernate.entity(MailAlias.class));
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
private static boolean registered = false;
protected static void register()
throws ModelStoreException
{
synchronized(MailAliasDestinationHibernate.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/MailAliasDestination.hbm.xml");
}
catch(Exception ex) {
throw new ModelStoreException(ex);
}
}
}
}
/hostadmiral/branches/hibernate3/src/ak/hostadmiral/core/model/store/hibernate/UserHibernate.java
0,0 → 1,245
package ak.hostadmiral.core.model.store.hibernate;
 
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
 
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.type.Type;
 
import ak.hostadmiral.util.CollectionInfo;
import ak.hostadmiral.util.ModelStoreException;
import ak.hostadmiral.util.hibernate.HibernateUtil;
import ak.hostadmiral.core.model.User;
import ak.hostadmiral.core.model.UserLogin;
import ak.hostadmiral.core.model.UserManager;
import ak.hostadmiral.core.model.store.UserStore;
 
public class UserHibernate
implements UserStore
{
public UserHibernate()
throws ModelStoreException
{
initSortKeys();
register();
}
 
public User get(Long id)
throws ModelStoreException
{
try {
return (User)HibernateUtil.currentSession().load(User.class, id);
}
catch(HibernateException ex) {
throw new ModelStoreException(ex);
}
}
 
public boolean loginExists(User user, String login)
throws ModelStoreException
{
try {
if(user.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from User u where login = ?",
login, Hibernate.STRING)
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from User u where login = ? and u != ?",
new Object[] { login, user },
new Type[] { Hibernate.STRING, Hibernate.entity(User.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex) {
throw new ModelStoreException(ex);
}
}
 
public User findForLogin(String login)
throws ModelStoreException
{
try {
List list = HibernateUtil.currentSession().find(
"select u from User u left join fetch u.boss where u.login = ? and u.enabled = ?",
new Object[] { login, Boolean.TRUE },
new Type[] { Hibernate.STRING, Hibernate.BOOLEAN } );
 
if(list.size() == 0)
return null;
else
return (User)list.get(0);
}
catch(HibernateException ex) {
throw new ModelStoreException(ex);
}
}
 
public void save(User user)
throws ModelStoreException
{
try {
HibernateUtil.currentSession().saveOrUpdate(user);
}
catch(HibernateException ex) {
throw new ModelStoreException(ex);
}
}
 
public void delete(User user)
throws ModelStoreException
{
try {
HibernateUtil.currentSession().delete(user);
}
catch(HibernateException ex) {
throw new ModelStoreException(ex);
}
}
 
public Collection listAllUsers(CollectionInfo info, int rowsPerPage, int pageNumber,
Integer[] sortingKeys)
throws ModelStoreException
{
try {
if(info != null) {
info.init(((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from User").next()).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableList(rowsPerPage, pageNumber,
"select u from User u left join fetch u.boss"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null);
}
catch(HibernateException ex) {
throw new ModelStoreException(ex);
}
}
 
public Collection listUsers(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 User u where u = ? or u.boss = ?",
new Object[] { user, user},
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) }
).next()).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableList(rowsPerPage, pageNumber,
"select u from User u left join fetch u.boss where u = ? or u.boss = ?"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys),
new Object[] { user, user},
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } );
}
catch(HibernateException ex) {
throw new ModelStoreException(ex);
}
}
 
public void saveUserLogin(UserLogin userLogin)
throws ModelStoreException
{
try {
HibernateUtil.currentSession().saveOrUpdate(userLogin);
}
catch(HibernateException ex) {
throw new ModelStoreException(ex);
}
}
 
public Collection listFailedLogins()
throws ModelStoreException
{
try {
return HibernateUtil.currentSession().find(
"select l from UserLogin l left join fetch l.user where l.success = ?",
Boolean.FALSE, Hibernate.BOOLEAN);
}
catch(HibernateException ex) {
throw new ModelStoreException(ex);
}
}
 
public Collection listSubusers(User user)
throws ModelStoreException
{
try {
return HibernateUtil.currentSession().find(
"select u from User u where u.boss = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex) {
throw new ModelStoreException(ex);
}
}
 
public Collection listUserLogins(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 UserLogin where usr = ?",
new Object[] { user },
new Type[] { Hibernate.entity(User.class) }
).next()).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableList(rowsPerPage, pageNumber,
"select l from UserLogin l where usr = ?"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeysLogins),
new Object[] { user },
new Type[] { Hibernate.entity(User.class) } );
}
catch(HibernateException ex) {
throw new ModelStoreException(ex);
}
}
 
protected static Map sortKeys = new HashMap();
protected static Map sortKeysLogins = new HashMap();
private static boolean sortKeysInitialized = false;
 
private static void initSortKeys()
{
if(!sortKeysInitialized) {
sortKeys.put(UserManager.SORT_LOGIN, "u.login");
sortKeysLogins.put(UserManager.SORT_LOGINS_TIME, "l.loginTime");
sortKeysLogins.put(UserManager.SORT_LOGINS_TIME_REVERSE, "l.loginTime desc");
sortKeysInitialized = true;
}
}
 
private static boolean registered = false;
protected static void register()
throws ModelStoreException
{
synchronized(UserHibernate.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/User.hbm.xml");
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/UserLogin.hbm.xml");
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/PasswordStoreAbstract.hbm.xml");
}
catch(Exception ex) {
throw new ModelStoreException(ex);
}
}
}
}
/hostadmiral/branches/hibernate3/src/ak/hostadmiral/core/model/store/hibernate/SystemUserHibernate.java
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 org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.type.Type;
 
import ak.hostadmiral.util.CollectionInfo;
import ak.hostadmiral.util.ModelStoreException;
import ak.hostadmiral.util.hibernate.HibernateUtil;
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);
}
}
}
}
/hostadmiral/branches/hibernate3/src/ak/hostadmiral/core/model/store/hibernate/InetDomainHibernate.java
0,0 → 1,208
package ak.hostadmiral.core.model.store.hibernate;
 
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
 
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.type.Type;
 
import ak.hostadmiral.util.CollectionInfo;
import ak.hostadmiral.util.ModelStoreException;
import ak.hostadmiral.util.hibernate.HibernateUtil;
import ak.hostadmiral.core.model.User;
import ak.hostadmiral.core.model.InetDomain;
import ak.hostadmiral.core.model.InetDomainManager;
import ak.hostadmiral.core.model.store.InetDomainStore;
 
public class InetDomainHibernate
implements InetDomainStore
{
public InetDomainHibernate()
throws ModelStoreException
{
initSortKeys();
register();
}
 
public InetDomain get(Long id)
throws ModelStoreException
{
try {
return (InetDomain)HibernateUtil.currentSession().load(
InetDomain.class, id);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public boolean nameExists(InetDomain domain, String name)
throws ModelStoreException
{
try {
if(domain.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from InetDomain d where name = ?",
name, Hibernate.STRING)
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from InetDomain d where name = ? and d != ?",
new Object[] { name, domain },
new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public InetDomain findForName(String name)
throws ModelStoreException
{
try {
List list = HibernateUtil.currentSession().find(
"select d from InetDomain d left join fetch d.owner where d.name=?",
name, Hibernate.STRING);
 
if(list.size() == 0)
return null;
else
return (InetDomain)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public void save(InetDomain domain)
throws ModelStoreException
{
try {
HibernateUtil.currentSession().saveOrUpdate(domain);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public void delete(InetDomain domain)
throws ModelStoreException
{
try {
HibernateUtil.currentSession().delete(domain);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public Collection listAllInetDomains(CollectionInfo info, int rowsPerPage, int pageNumber,
Integer[] sortingKeys)
throws ModelStoreException
{
try {
if(info != null) {
info.init(((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from InetDomain").next()).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableList(rowsPerPage, pageNumber,
"select d from InetDomain d left join fetch d.owner"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public Collection listInetDomains(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 InetDomain d where d.owner=?",
user, Hibernate.entity(User.class)).next()).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableList(rowsPerPage, pageNumber,
"select d from InetDomain d where d.owner=?"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys),
new Object[] { user }, new Type[] { Hibernate.entity(User.class) } );
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public int countInetDomainsAvailable(User user)
throws ModelStoreException
{
try {
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from InetDomain where owner=?",
user, Hibernate.entity(User.class)).next()).intValue();
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public Collection listOwnInetDomains(User user)
throws ModelStoreException
{
try {
return HibernateUtil.currentSession().find(
"select d from InetDomain d where d.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(InetDomainManager.SORT_NAME, "d.name");
sortKeysInitialized = true;
}
}
 
private static boolean registered = false;
protected static void register()
throws ModelStoreException
{
synchronized(InetDomainHibernate.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/InetDomain.hbm.xml");
}
catch(Exception ex) {
throw new ModelStoreException(ex);
}
}
}
}
/hostadmiral/branches/hibernate3/src/ak/hostadmiral/core/model/store/hibernate/MailboxHibernate.java
0,0 → 1,289
package ak.hostadmiral.core.model.store.hibernate;
 
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
 
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.type.Type;
 
import ak.hostadmiral.util.CollectionInfo;
import ak.hostadmiral.util.ModelStoreException;
import ak.hostadmiral.util.hibernate.HibernateUtil;
import ak.hostadmiral.core.model.User;
import ak.hostadmiral.core.model.SystemUser;
import ak.hostadmiral.core.model.InetDomain;
import ak.hostadmiral.core.model.Mailbox;
import ak.hostadmiral.core.model.MailboxManager;
import ak.hostadmiral.core.model.store.MailboxStore;
 
public class MailboxHibernate
implements MailboxStore
{
public MailboxHibernate()
throws ModelStoreException
{
initSortKeys();
register();
}
 
public Mailbox get(Long id)
throws ModelStoreException
{
try {
return (Mailbox)HibernateUtil.currentSession().load(Mailbox.class, id);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public boolean loginExists(Mailbox mailbox, String login)
throws ModelStoreException
{
try {
if(mailbox.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from Mailbox where login = ? and domain = ?",
new Object[] { login, mailbox.getDomain() },
new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } )
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from Mailbox b where login = ? and domain = ? and b != ?",
new Object[] { login, mailbox.getDomain(), mailbox },
new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class),
Hibernate.entity(Mailbox.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public Mailbox findForLogin(String login)
throws ModelStoreException
{
try {
List list = HibernateUtil.currentSession().find(
"select mb from Mailbox mb left join fetch mb.domain as d"
+ " left join fetch mb.owner left join fetch mb.systemUser where mb.login=?",
login, Hibernate.STRING);
 
if(list.size() == 0)
return null;
else
return (Mailbox)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public void save(Mailbox mailbox)
throws ModelStoreException
{
try {
HibernateUtil.currentSession().saveOrUpdate(mailbox);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public void delete(Mailbox mailbox)
throws ModelStoreException
{
try {
HibernateUtil.currentSession().delete(mailbox);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public Collection listAllMailboxes(CollectionInfo info, int rowsPerPage, int pageNumber,
Integer[] sortingKeys)
throws ModelStoreException
{
try {
if(info != null) {
info.init(((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from Mailbox").next()).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableListSql(rowsPerPage, pageNumber,
"select {mb.*}, {d.*}, {o.*}, {su.*}"
+ " from mailboxes as mb"
+ " left join domains as d on mb.domain = d.id"
+ " left join users as o on mb.owner = o.id"
+ " left join systemusers as su on mb.systemUser = su.id"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeysSql),
new String[] { "mb", "d", "o", "su" },
new Class[] { Mailbox.class, InetDomain.class, User.class, SystemUser.class },
null,
null);
}
catch(HibernateException ex)
{
ex.printStackTrace();
 
throw new ModelStoreException(ex);
}
}
 
public Collection listMailboxes(CollectionInfo info, int rowsPerPage, int pageNumber,
Integer[] sortingKeys, User user)
throws ModelStoreException
{
try {
if(info != null) {
List countlist = HibernateUtil.sqlQuery(
"select count(*) from ("
+ " select mb.id from mailboxes mb"
+ " where mb.owner=?"
+ " union"
+ " select mb.id from mailboxes mb"
+ " left join domains as d on mb.domain = d.id"
+ " where d.owner=?"
+ ") as count_table",
new Object[] { user.getId(), user.getId() });
 
info.init(((Long)countlist.get(0)).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableListSql(rowsPerPage, pageNumber,
"(select {mb.*}, {d.*}, {o.*}, {su.*}"
+ " from mailboxes as mb"
+ " left join domains as d on mb.domain = d.id"
+ " left join users as o on mb.owner = o.id"
+ " left join systemusers as su on mb.systemUser = su.id"
+ " where mb.owner=?)"
+ " union "
+ "(select {mb.*}, {d.*}, {o.*}, {su.*}"
+ " from mailboxes as mb"
+ " left join domains as d on mb.domain = d.id"
+ " left join users as o on mb.owner = o.id"
+ " left join systemusers as su on mb.systemUser = su.id"
+ " where d.owner=?)"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeysSql),
new String[] { "mb", "d", "o", "su" },
new Class[] { Mailbox.class, InetDomain.class, User.class, SystemUser.class },
new Object[] { user, user },
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) });
}
catch(HibernateException ex)
{
ex.printStackTrace();
 
throw new ModelStoreException(ex);
}
}
 
public int countMailboxesAvailable(User user)
throws ModelStoreException
{
try {
List countlist = HibernateUtil.sqlQuery(
"select count(*) from ("
+ " select mb.id from mailboxes mb"
+ " where mb.owner=?"
+ " union"
+ " select mb.id from mailboxes mb"
+ " left join domains as d on mb.domain = d.id"
+ " where d.owner=?"
+ ") as count_table",
new Object[] { user.getId(), user.getId() });
 
return ((Long)countlist.get(0)).intValue();
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public Collection listOwnMailboxes(User user)
throws ModelStoreException
{
try {
return HibernateUtil.currentSession().find(
"select mb from Mailbox mb left join fetch mb.domain"
+ " left join fetch mb.systemUser where mb.owner = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public Collection listMailboxesForDomain(InetDomain domain)
throws ModelStoreException
{
try {
return HibernateUtil.currentSession().find(
"select mb from Mailbox mb left join fetch mb.owner"
+ " left join fetch mb.systemUser where mb.domain = ?",
domain, Hibernate.entity(InetDomain.class) );
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public Collection listMailboxesForSystemUser(SystemUser user)
throws ModelStoreException
{
try {
return HibernateUtil.currentSession().find(
"select mb from Mailbox mb left join fetch mb.domain"
+ " left join fetch mb.owner where mb.systemUser = ?",
user, Hibernate.entity(SystemUser.class) );
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
protected static Map sortKeysSql = new HashMap();
private static boolean sortKeysInitialized = false;
 
private static void initSortKeys()
{
if(!sortKeysInitialized) {
sortKeysSql.put(MailboxManager.SORT_LOGIN, "login0_");
sortKeysSql.put(MailboxManager.SORT_DOMAIN, "name1_");
sortKeysInitialized = true;
}
}
 
private static boolean registered = false;
protected static void register()
throws ModelStoreException
{
synchronized(MailboxHibernate.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/Mailbox.hbm.xml");
}
catch(Exception ex) {
throw new ModelStoreException(ex);
}
}
}
}
/hostadmiral/branches/hibernate3/src/ak/hostadmiral/core/model/store/hibernate/MailAliasHibernate.java
0,0 → 1,276
package ak.hostadmiral.core.model.store.hibernate;
 
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
 
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.type.Type;
 
import ak.hostadmiral.util.CollectionInfo;
import ak.hostadmiral.util.ModelStoreException;
import ak.hostadmiral.util.hibernate.HibernateUtil;
import ak.hostadmiral.core.model.User;
import ak.hostadmiral.core.model.InetDomain;
import ak.hostadmiral.core.model.Mailbox;
import ak.hostadmiral.core.model.MailAlias;
import ak.hostadmiral.core.model.MailAliasManager;
import ak.hostadmiral.core.model.store.MailAliasStore;
 
public class MailAliasHibernate
implements MailAliasStore
{
public MailAliasHibernate()
throws ModelStoreException
{
initSortKeys();
register();
}
 
public MailAlias get(Long id)
throws ModelStoreException
{
try {
return (MailAlias)HibernateUtil.currentSession().load(MailAlias.class, id);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public boolean addressExists(MailAlias alias, String address)
throws ModelStoreException
{
try {
if(alias.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from MailAlias where address = ? and domain = ?",
new Object[] { address, alias.getDomain() },
new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } )
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from MailAlias a where address = ? and domain = ? and a != ?",
new Object[] { address, alias.getDomain(), alias },
new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class),
Hibernate.entity(MailAlias.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public MailAlias findForName(String name)
throws ModelStoreException
{
try {
List list = HibernateUtil.currentSession().find(
"select a from MailAlias a left join fetch a.domain"
+ " left join fetch a.owner where a.name=?", name, Hibernate.STRING);
 
if(list.size() == 0)
return null;
else
return (MailAlias)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public void save(MailAlias mailAlias)
throws ModelStoreException
{
try {
HibernateUtil.currentSession().saveOrUpdate(mailAlias);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public void delete(MailAlias mailAlias)
throws ModelStoreException
{
try {
HibernateUtil.currentSession().delete(mailAlias);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public Collection listAllMailAliases(CollectionInfo info, int rowsPerPage, int pageNumber,
Integer[] sortingKeys)
throws ModelStoreException
{
try {
if(info != null) {
info.init(((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from MailAlias").next()).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableList(rowsPerPage, pageNumber,
"select a from MailAlias a left join fetch a.domain as d"
+ " left join fetch a.owner"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null);
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public Collection listMailAliases(CollectionInfo info, int rowsPerPage, int pageNumber,
Integer[] sortingKeys, User user)
throws ModelStoreException
{
try {
if(info != null) {
List countlist = HibernateUtil.sqlQuery(
"select count(*) from ("
+ " select a.id from mailaliases a"
+ " where a.owner=?"
+ " union"
+ " select a.id from mailaliases a"
+ " left join domains as d on a.domain = d.id"
+ " where d.owner=?"
+ ") as count_table",
new Object[] { user.getId(), user.getId() });
 
info.init(((Long)countlist.get(0)).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableListSql(rowsPerPage, pageNumber,
"(select {a.*}, {d.*}, {o.*}"
+ " from mailaliases as a"
+ " left join domains as d on a.domain = d.id"
+ " left join users as o on a.owner = o.id"
+ " where a.owner=?)"
+ " union "
+ "(select {a.*}, {d.*}, {o.*}"
+ " from mailaliases as a"
+ " left join domains as d on a.domain = d.id"
+ " left join users as o on a.owner = o.id"
+ " where d.owner=?)"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeysSql),
new String[] { "a", "d", "o" },
new Class[] { MailAlias.class, InetDomain.class, User.class },
new Object[] { user, user },
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) });
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public int countMailAliasesAvailable(User user)
throws ModelStoreException
{
try {
List countlist = HibernateUtil.sqlQuery(
"select count(*) from ("
+ " select a.id from mailaliases a"
+ " where a.owner=?"
+ " union"
+ " select a.id from mailaliases a"
+ " left join domains as d on a.domain = d.id"
+ " where d.owner=?"
+ ") as count_table",
new Object[] { user.getId(), user.getId() });
 
return ((Long)countlist.get(0)).intValue();
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public Collection listOwnMailAliases(User user)
throws ModelStoreException
{
try {
return HibernateUtil.currentSession().find(
"select a from MailAlias a left join fetch a.domain where a.owner = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public Collection listMailAliasesForDomain(InetDomain domain)
throws ModelStoreException
{
try {
return HibernateUtil.currentSession().find(
"select a from MailAlias a left join fetch a.owner where a.domain = ?",
domain, Hibernate.entity(InetDomain.class) );
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
public Collection listMailAliasesForMailbox(Mailbox mailbox)
throws ModelStoreException
{
try {
return HibernateUtil.currentSession().find(
"select a from MailAlias a left join fetch a.owner"
+ " left join fetch a.destinations as dest where dest.mailbox = ?",
mailbox, Hibernate.entity(Mailbox.class) );
}
catch(HibernateException ex)
{
throw new ModelStoreException(ex);
}
}
 
protected static Map sortKeys = new HashMap();
protected static Map sortKeysSql = new HashMap();
private static boolean sortKeysInitialized = false;
 
private static void initSortKeys()
{
if(!sortKeysInitialized) {
sortKeys.put(MailAliasManager.SORT_ADDRESS, "a.address");
sortKeys.put(MailAliasManager.SORT_DOMAIN, "d.name");
sortKeysSql.put(MailAliasManager.SORT_ADDRESS, "address0_");
sortKeysSql.put(MailAliasManager.SORT_DOMAIN, "name1_");
sortKeysInitialized = true;
}
}
 
private static boolean registered = false;
protected static void register()
throws ModelStoreException
{
synchronized(MailAliasHibernate.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/MailAlias.hbm.xml");
}
catch(Exception ex) {
throw new ModelStoreException(ex);
}
}
}
}