Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 960 → Rev 961

/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailAliasManager.java
0,0 → 1,231
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 MailAliasManager
{
private static boolean registered = false;
protected static void register()
{
synchronized(MailAliasManager.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/MailAlias.hbm.xml");
}
catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
}
 
static {
register();
}
 
private MailAliasManager()
{
}
 
public MailAlias create(User editor)
throws ModelException
{
if(!allowedToCreate(editor)) throw new ModelSecurityException();
 
MailAlias alias = new MailAlias();
alias.setDestinations(new ArrayList());
return alias;
}
 
public boolean allowedToCreate(User editor)
throws ModelException
{
return MailAlias.allowedToCreate(this, editor);
}
 
public MailAlias get(User editor, Long id)
throws ModelException
{
MailAlias alias;
 
try {
alias = (MailAlias)HibernateUtil.currentSession().load(MailAlias.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
if(!alias.viewableBy(editor))
throw new ModelSecurityException();
 
return alias;
}
 
public boolean addressExists(User editor, MailAlias alias, String address)
throws ModelException
{
if(alias.getDomain() == null)
throw new ModelException("Cannot check unique address for mail alias without domain");
 
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 ModelException(ex);
}
}
 
protected MailAlias findForName(String name)
throws ModelException
{
try {
List list = HibernateUtil.currentSession().find(
"from MailAlias where name=?", name, Hibernate.STRING);
 
if(list.size() == 0)
return null;
else
return (MailAlias)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void save(User editor, MailAlias mailAlias)
throws ModelException
{
if(!mailAlias.editableBy(editor))
throw new ModelSecurityException();
 
mailAlias.setModUser(editor);
 
try {
HibernateUtil.currentSession().saveOrUpdate(mailAlias);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void delete(User editor, MailAlias mailAlias)
throws ModelException
{
if(!mailAlias.deleteableBy(editor))
throw new ModelSecurityException();
 
try {
HibernateUtil.currentSession().delete(mailAlias);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection listMailAliases(User editor)
throws ModelException
{
try {
if(editor.isSuperuser())
return HibernateUtil.currentSession().find("from MailAlias");
else
return HibernateUtil.currentSession().find(
"select a from MailAlias a left join a.domain as d"
+ " where d.owner=? or a.owner=?",
new Object[] { editor, editor },
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public boolean areMailAliasesAvailable(User editor)
throws ModelException
{
try {
if(editor.isSuperuser()
|| InetDomainManager.getInstance().areInetDomainsAvailable(editor))
{
return true;
}
else {
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from MailAlias a left join a.domain as d"
+ " where d.owner=? or a.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);
}
}
 
private static MailAliasManager mailAliasManager = null;
 
public static MailAliasManager getInstance()
{
if(mailAliasManager == null)
mailAliasManager = new MailAliasManager();
 
return mailAliasManager;
}
 
public static final Comparator ADDRESS_COMPARATOR = new AddressComparator();
 
private static class AddressComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof MailAlias) || !(o2 instanceof MailAlias))
throw new ClassCastException("not a MailAlias");
 
MailAlias a1 = (MailAlias)o1;
MailAlias a2 = (MailAlias)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.getAddress().compareToIgnoreCase(a2.getAddress());
}
 
public boolean equals(Object obj)
{
return (obj instanceof AddressComparator);
}
}
}