Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 960 → Rev 961

/hostadmiral/trunk/src/ak/hostadmiral/core/model/InetDomainManager.java
0,0 → 1,278
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 InetDomainManager
implements UserBeforeDeleteListener
{
private static InetDomainManager inetDomainManager = null;
private static boolean registered = false;
 
public static InetDomainManager getInstance()
{
return inetDomainManager;
}
 
protected static void register()
{
synchronized(InetDomainManager.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/InetDomain.hbm.xml");
 
inetDomainManager = new InetDomainManager();
}
catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
}
 
static {
register();
}
 
private Collection beforeDeleteListeners = new ArrayList();
 
private InetDomainManager()
{
UserManager.getInstance().addBeforeDeleteListener(this);
}
 
public InetDomain create(User editor)
throws ModelException
{
if(!allowedToCreate(editor)) throw new ModelSecurityException();
 
return new InetDomain();
}
 
public boolean allowedToCreate(User editor)
throws ModelException
{
return InetDomain.allowedToCreate(this, editor);
}
 
public InetDomain get(User editor, Long id)
throws ModelException
{
InetDomain domain;
 
try {
domain = (InetDomain)HibernateUtil.currentSession().load(
InetDomain.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
if(!domain.viewableBy(editor))
throw new ModelSecurityException();
 
return domain;
}
 
public boolean nameExists(User editor, InetDomain domain, String name)
throws ModelException
{
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 ModelException(ex);
}
}
 
protected InetDomain findForName(String name)
throws ModelException
{
try {
List list = HibernateUtil.currentSession().find(
"from InetDomain where name=?", name, Hibernate.STRING);
 
if(list.size() == 0)
return null;
else
return (InetDomain)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void save(User editor, InetDomain domain)
throws ModelException
{
if(!domain.editableBy(editor))
throw new ModelSecurityException();
 
domain.setModUser(editor);
 
try {
HibernateUtil.currentSession().saveOrUpdate(domain);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void addBeforeDeleteListener(SystemUserBeforeDeleteListener listener)
{
beforeDeleteListeners.add(listener);
}
 
public void removeBeforeDeleteListener(SystemUserBeforeDeleteListener listener)
{
beforeDeleteListeners.remove(listener);
}
 
public Collection beforeDelete(User editor, InetDomain domain, Collection known)
throws ModelException
{
Collection cascade = new ArrayList();
 
for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) {
InetDomainBeforeDeleteListener listener = (InetDomainBeforeDeleteListener)i.next();
Collection subcascade = listener.inetDomainBeforeDelete(editor, domain, known);
if(subcascade != null)
cascade.addAll(subcascade);
}
 
return cascade;
}
 
public void delete(User editor, InetDomain domain)
throws ModelException
{
if(!domain.deleteableBy(editor))
throw new ModelSecurityException();
 
try {
 
HibernateUtil.currentSession().delete(domain);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection listInetDomains(User editor)
throws ModelException
{
try {
if(editor.isSuperuser())
return HibernateUtil.currentSession().find("from InetDomain");
else
return HibernateUtil.currentSession().find(
"from InetDomain where owner=?", editor, Hibernate.entity(User.class));
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public boolean areInetDomainsAvailable(User editor)
throws ModelException
{
try {
if(editor.isSuperuser())
return true;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from InetDomain where owner=?",
editor, 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 domains;
 
try {
domains = HibernateUtil.currentSession().find(
"from InetDomain where owner = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
Collection cascade = new ArrayList();
for(Iterator i = domains.iterator(); i.hasNext(); ) {
InetDomain d = (InetDomain)i.next();
if(d.viewableBy(editor)) {
if(d.deleteableBy(editor))
cascade.add(new CascadeDeleteElement(d, CascadeDeleteElement.DELETE,
this.beforeDelete(editor, d, known)));
else
cascade.add(new CascadeDeleteElement(d, CascadeDeleteElement.FORBIDDEN, null));
}
else {
cascade.add(new CascadeDeleteElement(InetDomain.createLimitedCopy(d),
CascadeDeleteElement.FORBIDDEN, null));
}
}
 
return cascade;
}
 
public static final Comparator NAME_COMPARATOR = new NameComparator();
 
private static class NameComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof InetDomain) || !(o2 instanceof InetDomain))
throw new ClassCastException("not a InetDomain");
 
InetDomain a1 = (InetDomain)o1;
InetDomain a2 = (InetDomain)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.getName().compareToIgnoreCase(a2.getName());
}
 
public boolean equals(Object obj)
{
return (obj instanceof NameComparator);
}
}
}