1,49 → 1,25 |
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.InetDomainStore; |
|
public class InetDomainManager |
implements |
ConfigInit, |
UserBeforeDeleteListener, |
UserDeletingListener |
{ |
private static InetDomainManager inetDomainManager = null; |
private static boolean registered = false; |
private InetDomainStore store; |
|
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 createdListeners = new ArrayList(); |
private Collection modifiedListeners = new ArrayList(); |
private Collection beforeDeleteListeners = new ArrayList(); |
50,7 → 26,8 |
private Collection deletingListeners = new ArrayList(); |
private Collection deletedListeners = new ArrayList(); |
|
private InetDomainManager() |
public InetDomainManager() |
throws ModelException |
{ |
UserManager.getInstance().addBeforeDeleteListener(this); |
UserManager.getInstance().addDeletingListener(this); |
73,17 → 50,8 |
public InetDomain get(User editor, Long id) |
throws ModelException |
{ |
InetDomain domain; |
InetDomain domain = store.get(id); |
|
try { |
domain = (InetDomain)HibernateUtil.currentSession().load( |
InetDomain.class, id); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
|
if(!domain.viewableBy(editor)) |
throw new ModelSecurityException(); |
|
93,42 → 61,18 |
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); |
} |
return store.nameExists(domain, name); |
} |
|
protected InetDomain findForName(String name) |
public InetDomain findForName(User editor, String name) |
throws ModelException |
{ |
try { |
List list = HibernateUtil.currentSession().find( |
"select d from InetDomain d left join fetch d.owner where d.name=?", |
name, Hibernate.STRING); |
InetDomain domain = store.findForName(name); |
|
if(list.size() == 0) |
return null; |
else |
return (InetDomain)list.get(0); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
if(domain != null && !domain.viewableBy(editor)) |
throw new ModelSecurityException(); |
|
return domain; |
} |
|
public void save(User editor, InetDomain domain) |
141,13 → 85,7 |
|
//domain.setModUser(editor); // FIXME |
|
try { |
HibernateUtil.currentSession().saveOrUpdate(domain); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
store.save(domain); |
|
// inform listeners |
if(isNew) { |
248,15 → 186,8 |
InetDomain oldDomain = new InetDomain(domain); |
|
// delete it |
try { |
store.delete(domain); |
|
HibernateUtil.currentSession().delete(domain); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
|
// inform deleted listeners |
for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) { |
InetDomainDeletedListener listener = (InetDomainDeletedListener)i.next(); |
274,71 → 205,26 |
Integer[] sortingKeys, User editor) |
throws ModelException |
{ |
try { |
if(editor.isSuperuser()) { |
if(info != null) { |
info.init(((Integer)HibernateUtil.currentSession().iterate( |
"select count(*) from InetDomain").next()).intValue(), |
pageNumber, rowsPerPage); |
} |
|
|
return HibernateUtil.pageableList(rowsPerPage, pageNumber, |
"select d from InetDomain d left join fetch d.owner" |
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null); |
} |
else { |
if(info != null) { |
info.init(((Integer)HibernateUtil.currentSession().iterate( |
"select count(*) from InetDomain where d.owner=?", |
editor, Hibernate.entity(User.class)).next()).intValue(), |
pageNumber, rowsPerPage); |
} |
|
return HibernateUtil.pageableList(rowsPerPage, pageNumber, |
"select d from InetDomain d where d.owner=?" |
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys), |
new Object[] { editor }, new Type[] { Hibernate.entity(User.class) } ); |
} |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
if(editor.isSuperuser()) |
return store.listAllInetDomains(info, rowsPerPage, pageNumber, sortingKeys); |
else |
return store.listInetDomains(info, rowsPerPage, pageNumber, sortingKeys, editor); |
} |
|
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); |
} |
if(editor.isSuperuser()) |
return true; |
else |
return store.countInetDomainsAvailable(editor) > 0; |
} |
|
public Collection userBeforeDelete(User editor, User user, Collection known) |
throws ModelException |
{ |
Collection domains; |
Collection domains = store.listOwnInetDomains(user); |
|
try { |
domains = HibernateUtil.currentSession().find( |
"select d from InetDomain d where d.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(); |
361,18 → 247,8 |
public void userDeleting(User editor, User user) |
throws ModelException |
{ |
Collection domains; |
Collection domains = store.listOwnInetDomains(user); |
|
try { |
domains = HibernateUtil.currentSession().find( |
"select d from InetDomain d where d.owner = ?", |
user, Hibernate.entity(User.class) ); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
|
for(Iterator i = domains.iterator(); i.hasNext(); ) { |
delete(editor, (InetDomain)i.next()); |
} |
380,12 → 256,6 |
|
public static final Integer SORT_NAME = new Integer(1); |
|
protected static Map sortKeys = new HashMap(); |
|
static { |
sortKeys.put(SORT_NAME, "d.name"); |
} |
|
public static final Comparator NAME_COMPARATOR = new NameComparator(); |
|
private static class NameComparator |
414,4 → 284,25 |
return (obj instanceof NameComparator); |
} |
} |
|
public void init(Map params) |
throws ModelException |
{ |
try { |
inetDomainManager = this; |
|
Class c = Class.forName((String)params.get("store")); |
store = (InetDomainStore)c.newInstance(); |
} |
catch(Exception ex) { |
throw new ModelException(ex); |
} |
} |
|
private static InetDomainManager inetDomainManager = null; |
|
public static InetDomainManager getInstance() |
{ |
return inetDomainManager; |
} |
} |