Rev 1018 | Rev 1020 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package ak.hostadmiral.core.model;
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
import ak.hostadmiral.util.CollectionUtils;
import ak.hostadmiral.util.HibernateUtil;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
public class MailboxManager
implements
UserBeforeDeleteListener,
UserDeletingListener,
SystemUserBeforeDeleteListener,
SystemUserDeletingListener,
InetDomainBeforeDeleteListener,
InetDomainDeletingListener
{
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 createdListeners = new ArrayList();
private Collection modifiedListeners = new ArrayList();
private Collection beforeDeleteListeners = new ArrayList();
private Collection deletingListeners = new ArrayList();
private Collection deletedListeners = 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();
Mailbox mailbox = new Mailbox();
// FIXME: make this configurable
mailbox.addPasswordStore(new PasswordStoreMd5());
mailbox.addPasswordStore(new PasswordStoreCrypt());
mailbox.addPasswordStore(new PasswordStorePlain());
return 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(
"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 ModelException(ex);
}
}
public void save(User editor, Mailbox mailbox)
throws ModelException
{
if(!mailbox.editableBy(editor))
throw new ModelSecurityException();
boolean isNew = mailbox.isNew();
//mailbox.setModUser(editor); // FIXME
try {
HibernateUtil.currentSession().saveOrUpdate(mailbox);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
// inform listeners
if(isNew) {
for(Iterator i = createdListeners.iterator(); i.hasNext(); ) {
MailboxCreatedListener listener = (MailboxCreatedListener)i.next();
listener.mailboxCreated(editor, mailbox);
}
}
else {
Mailbox oldMailbox = mailbox.getOrigin();
if(oldMailbox == null) oldMailbox = mailbox;
for(Iterator i = modifiedListeners.iterator(); i.hasNext(); ) {
MailboxModifiedListener listener = (MailboxModifiedListener)i.next();
listener.mailboxModified(editor, mailbox, oldMailbox);
}
}
}
public void addCreatedListener(MailboxCreatedListener listener)
{
createdListeners.add(listener);
}
public void removeCreatedListener(MailboxCreatedListener listener)
{
createdListeners.remove(listener);
}
public void addModifiedListener(MailboxModifiedListener listener)
{
modifiedListeners.add(listener);
}
public void removeModifiedListener(MailboxModifiedListener listener)
{
modifiedListeners.remove(listener);
}
public void addBeforeDeleteListener(MailboxBeforeDeleteListener listener)
{
beforeDeleteListeners.add(listener);
}
public void removeBeforeDeleteListener(MailboxBeforeDeleteListener listener)
{
beforeDeleteListeners.remove(listener);
}
public void addDeletingListener(MailboxDeletingListener listener)
{
deletingListeners.add(listener);
}
public void removeDeletingListener(MailboxDeletingListener listener)
{
deletingListeners.remove(listener);
}
public void addDeletedListener(MailboxDeletedListener listener)
{
deletedListeners.add(listener);
}
public void removeDeletedListener(MailboxDeletedListener listener)
{
deletedListeners.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
{
// check rights
if(!mailbox.deleteableBy(editor))
throw new ModelSecurityException();
// inform deleting listeners
for(Iterator i = deletingListeners.iterator(); i.hasNext(); ) {
MailboxDeletingListener listener = (MailboxDeletingListener)i.next();
listener.mailboxDeleting(editor, mailbox);
}
// backup copy
Mailbox oldMailbox = new Mailbox(mailbox);
// delete it
try {
HibernateUtil.currentSession().delete(mailbox);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
// inform deleted listeners
for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) {
MailboxDeletedListener listener = (MailboxDeletedListener)i.next();
listener.mailboxDeleted(editor, oldMailbox);
}
}
public Collection listMailboxes(User editor)
throws ModelException
{
try {
if(editor.isSuperuser())
return 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");
else
// FIXME: any problems for big lists or by pages?
return CollectionUtils.addUnique(
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.owner=?",
new Object[] { editor }, new Type[] { Hibernate.entity(User.class) } ),
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 d.owner=?",
new Object[] { editor }, new Type[] { 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(
"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);
}
public void userDeleting(User editor, User user)
throws ModelException
{
Collection mailboxes;
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());
}
}
public Collection inetDomainBeforeDelete(User editor, InetDomain domain, Collection known)
throws ModelException
{
Collection mailboxes;
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);
}
public void inetDomainDeleting(User editor, InetDomain domain)
throws ModelException
{
Collection mailboxes;
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());
}
}
public Collection systemUserBeforeDelete(User editor, SystemUser user, Collection known)
throws ModelException
{
Collection mailboxes;
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);
}
public void systemUserDeleting(User editor, SystemUser user)
throws ModelException
{
Collection mailboxes;
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());
}
}
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);
}
}
}