0,0 → 1,339 |
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 MailboxManager |
implements |
UserBeforeDeleteListener, |
SystemUserBeforeDeleteListener, |
InetDomainBeforeDeleteListener |
{ |
private static MailboxManager mailboxManager = null; |
private static boolean registered = false; |
|
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 beforeDeleteListeners = new ArrayList(); |
|
private MailboxManager() |
{ |
UserManager.getInstance().addBeforeDeleteListener(this); |
SystemUserManager.getInstance().addBeforeDeleteListener(this); |
InetDomainManager.getInstance().addBeforeDeleteListener(this); |
} |
|
public Mailbox create(User editor) |
throws ModelException |
{ |
if(!allowedToCreate(editor)) throw new ModelSecurityException(); |
|
return new Mailbox(); |
} |
|
public boolean allowedToCreate(User editor) |
throws ModelException |
{ |
return Mailbox.allowedToCreate(this, editor); |
} |
|
public Mailbox get(User editor, Long id) |
throws ModelException |
{ |
Mailbox mailbox; |
|
try { |
mailbox = (Mailbox)HibernateUtil.currentSession().load(Mailbox.class, id); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
|
if(!mailbox.viewableBy(editor)) |
throw new ModelSecurityException(); |
|
return mailbox; |
} |
|
public boolean loginExists(User editor, Mailbox mailbox, String login) |
throws ModelException |
{ |
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); |
} |
} |
|
protected Mailbox findForLogin(String login) |
throws ModelException |
{ |
try { |
List list = HibernateUtil.currentSession().find( |
"from Mailbox where login=?", login, Hibernate.STRING); |
|
if(list.size() == 0) |
return null; |
else |
return (Mailbox)list.get(0); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public void save(User editor, Mailbox mailbox) |
throws ModelException |
{ |
if(!mailbox.editableBy(editor)) |
throw new ModelSecurityException(); |
|
mailbox.setModUser(editor); |
|
try { |
HibernateUtil.currentSession().saveOrUpdate(mailbox); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public void addBeforeDeleteListener(MailboxBeforeDeleteListener listener) |
{ |
beforeDeleteListeners.add(listener); |
} |
|
public void removeBeforeDeleteListener(MailboxBeforeDeleteListener listener) |
{ |
beforeDeleteListeners.remove(listener); |
} |
|
public Collection beforeDelete(User editor, Mailbox mailbox, Collection known) |
throws ModelException |
{ |
Collection cascade = new ArrayList(); |
|
for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) { |
MailboxBeforeDeleteListener listener = (MailboxBeforeDeleteListener)i.next(); |
Collection subcascade = listener.mailboxBeforeDelete(editor, mailbox, known); |
if(subcascade != null) |
cascade.addAll(subcascade); |
} |
|
return cascade; |
} |
|
public void delete(User editor, Mailbox mailbox) |
throws ModelException |
{ |
if(!mailbox.deleteableBy(editor)) |
throw new ModelSecurityException(); |
|
try { |
HibernateUtil.currentSession().delete(mailbox); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public Collection listMailboxes(User editor) |
throws ModelException |
{ |
try { |
if(editor.isSuperuser()) |
return HibernateUtil.currentSession().find("from Mailbox"); |
else |
return HibernateUtil.currentSession().find( |
"select mb 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) } ); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
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); |
} |
} |
|
public Collection userBeforeDelete(User editor, User user, Collection known) |
throws ModelException |
{ |
Collection mailboxes; |
|
try { |
mailboxes = HibernateUtil.currentSession().find( |
"from Mailbox where owner = ?", |
user, Hibernate.entity(User.class) ); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
|
return iterateBeforeDelete(editor, mailboxes, known); |
} |
|
public Collection inetDomainBeforeDelete(User editor, InetDomain domain, Collection known) |
throws ModelException |
{ |
Collection mailboxes; |
|
try { |
mailboxes = HibernateUtil.currentSession().find( |
"from Mailbox where domain = ?", |
domain, Hibernate.entity(InetDomain.class) ); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
|
return iterateBeforeDelete(editor, mailboxes, known); |
} |
|
public Collection systemUserBeforeDelete(User editor, SystemUser user, Collection known) |
throws ModelException |
{ |
Collection mailboxes; |
|
try { |
mailboxes = HibernateUtil.currentSession().find( |
"from Mailbox where systemUser = ?", |
user, Hibernate.entity(SystemUser.class) ); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
|
return iterateBeforeDelete(editor, mailboxes, known); |
} |
|
private Collection iterateBeforeDelete(User editor, Collection mailboxes, Collection known) |
throws ModelException |
{ |
Collection cascade = new ArrayList(); |
for(Iterator i = mailboxes.iterator(); i.hasNext(); ) { |
Mailbox mailbox = (Mailbox)i.next(); |
if(mailbox.viewableBy(editor)) { |
if(mailbox.deleteableBy(editor)) |
cascade.add(new CascadeDeleteElement(mailbox, CascadeDeleteElement.DELETE, |
this.beforeDelete(editor, mailbox, known))); |
else |
cascade.add(new CascadeDeleteElement(mailbox, CascadeDeleteElement.FORBIDDEN, |
null)); |
} |
else { |
cascade.add(new CascadeDeleteElement(Mailbox.createLimitedCopy(mailbox), |
CascadeDeleteElement.FORBIDDEN, null)); |
} |
} |
|
return cascade; |
} |
|
public static final Comparator LOGIN_COMPARATOR = new LoginComparator(); |
|
private static class LoginComparator |
implements Comparator |
{ |
public int compare(Object o1, Object o2) |
{ |
if(!(o1 instanceof Mailbox) || !(o2 instanceof Mailbox)) |
throw new ClassCastException("not a Mailbox"); |
|
Mailbox a1 = (Mailbox)o1; |
Mailbox a2 = (Mailbox)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.getLogin().compareToIgnoreCase(a2.getLogin()); |
} |
|
public boolean equals(Object obj) |
{ |
return (obj instanceof LoginComparator); |
} |
} |
} |