Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1040 → Rev 1041

/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailboxManager.java
1,15 → 1,20
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.MailboxStore;
 
public class MailboxManager
implements
ConfigInit,
UserBeforeDeleteListener,
UserDeletingListener,
SystemUserBeforeDeleteListener,
17,37 → 22,8
InetDomainBeforeDeleteListener,
InetDomainDeletingListener
{
private static MailboxManager mailboxManager = null;
private static boolean registered = false;
private MailboxStore store;
 
public static MailboxManager getInstance()
{
return mailboxManager;
}
 
protected static void register()
{
synchronized(MailboxManager.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/Mailbox.hbm.xml");
 
mailboxManager = new MailboxManager();
}
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();
54,7 → 30,8
private Collection deletingListeners = new ArrayList();
private Collection deletedListeners = new ArrayList();
 
private MailboxManager()
public MailboxManager()
throws ModelException
{
UserManager.getInstance().addBeforeDeleteListener(this);
SystemUserManager.getInstance().addBeforeDeleteListener(this);
85,16 → 62,8
public Mailbox get(User editor, Long id)
throws ModelException
{
Mailbox mailbox;
Mailbox mailbox = store.get(id);
 
try {
mailbox = (Mailbox)HibernateUtil.currentSession().load(Mailbox.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
if(!mailbox.viewableBy(editor))
throw new ModelSecurityException();
 
107,45 → 76,18
if(mailbox.getDomain() == null)
throw new ModelException("Cannot check unique login for mailbox without domain");
 
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 ModelException(ex);
}
return store.loginExists(mailbox, login);
}
 
protected Mailbox findForLogin(String login)
public Mailbox findForLogin(User editor, String login)
throws ModelException
{
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);
Mailbox mailbox = store.findForLogin(login);
 
if(list.size() == 0)
return null;
else
return (Mailbox)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
if(!mailbox.viewableBy(editor))
throw new ModelSecurityException();
 
return mailbox;
}
 
public void save(User editor, Mailbox mailbox)
158,13 → 100,7
 
//mailbox.setModUser(editor); // FIXME
 
try {
HibernateUtil.currentSession().saveOrUpdate(mailbox);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
store.save(mailbox);
 
// inform listeners
if(isNew) {
265,13 → 201,7
Mailbox oldMailbox = new Mailbox(mailbox);
 
// delete it
try {
HibernateUtil.currentSession().delete(mailbox);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
store.delete(mailbox);
 
// inform deleted listeners
for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) {
290,112 → 220,26
Integer[] sortingKeys, User editor)
throws ModelException
{
try {
if(editor.isSuperuser()) {
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);
}
else {
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[] { editor.getId(), editor.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[] { editor, editor },
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) });
}
}
catch(HibernateException ex)
{
ex.printStackTrace();
 
throw new ModelException(ex);
}
if(editor.isSuperuser())
return store.listAllMailboxes(info, rowsPerPage, pageNumber, sortingKeys);
else
return store.listMailboxes(info, rowsPerPage, pageNumber, sortingKeys, editor);
}
 
public boolean areMailboxesAvailable(User editor)
throws ModelException
{
try {
if(editor.isSuperuser()
|| InetDomainManager.getInstance().areInetDomainsAvailable(editor))
{
return true;
}
else {
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from Mailbox mb left join mb.domain as d"
+ " where d.owner=? or mb.owner=?",
new Object[] { editor, editor },
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) })
.next()).intValue() > 0;
}
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
if(editor.isSuperuser())
return true;
else
return store.countMailboxesAvailable(editor) > 0;
}
 
public Collection userBeforeDelete(User editor, User user, Collection known)
throws ModelException
{
Collection mailboxes;
Collection mailboxes = store.listOwnMailboxes(user);
 
try {
mailboxes = 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 ModelException(ex);
}
 
return iterateBeforeDelete(editor, mailboxes, known);
}
 
402,19 → 246,8
public void userDeleting(User editor, User user)
throws ModelException
{
Collection mailboxes;
Collection mailboxes = store.listOwnMailboxes(user);
 
try {
mailboxes = 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 ModelException(ex);
}
 
for(Iterator i = mailboxes.iterator(); i.hasNext(); ) {
delete(editor, (Mailbox)i.next());
}
423,19 → 256,8
public Collection inetDomainBeforeDelete(User editor, InetDomain domain, Collection known)
throws ModelException
{
Collection mailboxes;
Collection mailboxes = store.listMailboxesForDomain(domain);
 
try {
mailboxes = 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 ModelException(ex);
}
 
return iterateBeforeDelete(editor, mailboxes, known);
}
 
442,19 → 264,8
public void inetDomainDeleting(User editor, InetDomain domain)
throws ModelException
{
Collection mailboxes;
Collection mailboxes = store.listMailboxesForDomain(domain);
 
try {
mailboxes = 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 ModelException(ex);
}
 
for(Iterator i = mailboxes.iterator(); i.hasNext(); ) {
delete(editor, (Mailbox)i.next());
}
463,19 → 274,8
public Collection systemUserBeforeDelete(User editor, SystemUser user, Collection known)
throws ModelException
{
Collection mailboxes;
Collection mailboxes = store.listMailboxesForSystemUser(user);
 
try {
mailboxes = 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 ModelException(ex);
}
 
return iterateBeforeDelete(editor, mailboxes, known);
}
 
482,19 → 282,8
public void systemUserDeleting(User editor, SystemUser user)
throws ModelException
{
Collection mailboxes;
Collection mailboxes = store.listMailboxesForSystemUser(user);
 
try {
mailboxes = 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 ModelException(ex);
}
 
for(Iterator i = mailboxes.iterator(); i.hasNext(); ) {
delete(editor, (Mailbox)i.next());
}
526,13 → 315,6
public static final Integer SORT_LOGIN = new Integer(1);
public static final Integer SORT_DOMAIN = new Integer(2);
 
protected static Map sortKeysSql = new HashMap();
 
static {
sortKeysSql.put(SORT_LOGIN, "login0_");
sortKeysSql.put(SORT_DOMAIN, "name1_");
}
 
public static final Comparator LOGIN_COMPARATOR = new LoginComparator();
public static final Comparator DOMAIN_COMPARATOR = new DomainComparator();
 
589,4 → 371,25
return (obj instanceof DomainComparator);
}
}
 
public void init(Map params)
throws ModelException
{
try {
mailboxManager = this;
 
Class c = Class.forName((String)params.get("store"));
store = (MailboxStore)c.newInstance();
}
catch(Exception ex) {
throw new ModelException(ex);
}
}
 
private static MailboxManager mailboxManager = null;
 
public static MailboxManager getInstance()
{
return mailboxManager;
}
}