Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1041 → Rev 1040

/hostadmiral/trunk/webapp/WEB-INF/conf/hostadmiral_config.xml.default
4,9 → 4,6
HostAdmiral configuration file.
There are two files - one is default, the second one is user-defined.
Both files are merged with user-definition preference.
 
DO NOT EDIT THIS FILE
Make all changes to hostadmiral_config.xml
-->
<hostadmiral>
<!--
51,45 → 48,21
<!-- Core classes -->
<initialization>
<class>ak.hostadmiral.core.model.UserManager</class>
<init-param>
<param-name>store</param-name>
<param-value>ak.hostadmiral.core.model.store.hibernate.UserHibernate</param-value>
</init-param>
</initialization>
<initialization>
<class>ak.hostadmiral.core.model.SystemUserManager</class>
<init-param>
<param-name>store</param-name>
<param-value>ak.hostadmiral.core.model.store.hibernate.SystemUserHibernate</param-value>
</init-param>
</initialization>
<initialization>
<class>ak.hostadmiral.core.model.InetDomainManager</class>
<init-param>
<param-name>store</param-name>
<param-value>ak.hostadmiral.core.model.store.hibernate.InetDomainHibernate</param-value>
</init-param>
</initialization>
<initialization>
<class>ak.hostadmiral.core.model.MailboxManager</class>
<init-param>
<param-name>store</param-name>
<param-value>ak.hostadmiral.core.model.store.hibernate.MailboxHibernate</param-value>
</init-param>
</initialization>
<initialization>
<class>ak.hostadmiral.core.model.MailAliasManager</class>
<init-param>
<param-name>store</param-name>
<param-value>ak.hostadmiral.core.model.store.hibernate.MailAliasHibernate</param-value>
</init-param>
</initialization>
<initialization>
<class>ak.hostadmiral.core.model.MailAliasDestinationManager</class>
<init-param>
<param-name>store</param-name>
<param-value>ak.hostadmiral.core.model.store.hibernate.MailAliasDestinationHibernate</param-value>
</init-param>
</initialization>
</initializations>
</hostadmiral>
/hostadmiral/trunk/src/ak/hostadmiral/core/config/ConfigRoot.java
61,7 → 61,7
+ ", second - " + second.versionMajor);
 
this.dataSource = dataSource.merge(second.dataSource);
this.initializations.addAll(second.initializations); // FIXME: replace dublicates
this.initializations.addAll(second.initializations);
 
return this;
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/store/UserStore.java
File deleted
/hostadmiral/trunk/src/ak/hostadmiral/core/model/store/MailAliasDestinationStore.java
File deleted
/hostadmiral/trunk/src/ak/hostadmiral/core/model/store/SystemUserStore.java
File deleted
/hostadmiral/trunk/src/ak/hostadmiral/core/model/store/hibernate/SystemUserHibernate.java
File deleted
/hostadmiral/trunk/src/ak/hostadmiral/core/model/store/hibernate/InetDomainHibernate.java
File deleted
/hostadmiral/trunk/src/ak/hostadmiral/core/model/store/hibernate/MailboxHibernate.java
File deleted
/hostadmiral/trunk/src/ak/hostadmiral/core/model/store/hibernate/MailAliasHibernate.java
File deleted
/hostadmiral/trunk/src/ak/hostadmiral/core/model/store/hibernate/UserHibernate.java
File deleted
/hostadmiral/trunk/src/ak/hostadmiral/core/model/store/hibernate/MailAliasDestinationHibernate.java
File deleted
/hostadmiral/trunk/src/ak/hostadmiral/core/model/store/InetDomainStore.java
File deleted
/hostadmiral/trunk/src/ak/hostadmiral/core/model/store/MailboxStore.java
File deleted
/hostadmiral/trunk/src/ak/hostadmiral/core/model/store/MailAliasStore.java
File deleted
/hostadmiral/trunk/src/ak/hostadmiral/core/model/InetDomainManager.java
1,25 → 1,49
package ak.hostadmiral.core.model;
 
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 java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
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 InetDomainStore store;
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 createdListeners = new ArrayList();
private Collection modifiedListeners = new ArrayList();
private Collection beforeDeleteListeners = new ArrayList();
26,8 → 50,7
private Collection deletingListeners = new ArrayList();
private Collection deletedListeners = new ArrayList();
 
public InetDomainManager()
throws ModelException
private InetDomainManager()
{
UserManager.getInstance().addBeforeDeleteListener(this);
UserManager.getInstance().addDeletingListener(this);
50,8 → 73,17
public InetDomain get(User editor, Long id)
throws ModelException
{
InetDomain domain = store.get(id);
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();
 
61,18 → 93,42
public boolean nameExists(User editor, InetDomain domain, String name)
throws ModelException
{
return store.nameExists(domain, name);
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);
}
}
 
public InetDomain findForName(User editor, String name)
protected InetDomain findForName(String name)
throws ModelException
{
InetDomain domain = store.findForName(name);
try {
List list = HibernateUtil.currentSession().find(
"select d from InetDomain d left join fetch d.owner where d.name=?",
name, Hibernate.STRING);
 
if(domain != null && !domain.viewableBy(editor))
throw new ModelSecurityException();
 
return domain;
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)
85,7 → 141,13
 
//domain.setModUser(editor); // FIXME
 
store.save(domain);
try {
HibernateUtil.currentSession().saveOrUpdate(domain);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
// inform listeners
if(isNew) {
186,8 → 248,15
InetDomain oldDomain = new InetDomain(domain);
 
// delete it
store.delete(domain);
try {
 
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();
205,26 → 274,71
Integer[] sortingKeys, User editor)
throws ModelException
{
if(editor.isSuperuser())
return store.listAllInetDomains(info, rowsPerPage, pageNumber, sortingKeys);
else
return store.listInetDomains(info, rowsPerPage, pageNumber, sortingKeys, editor);
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);
}
}
 
public boolean areInetDomainsAvailable(User editor)
throws ModelException
{
if(editor.isSuperuser())
return true;
else
return store.countInetDomainsAvailable(editor) > 0;
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 = store.listOwnInetDomains(user);
Collection domains;
 
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();
247,8 → 361,18
public void userDeleting(User editor, User user)
throws ModelException
{
Collection domains = store.listOwnInetDomains(user);
Collection domains;
 
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());
}
256,6 → 380,12
 
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
284,25 → 414,4
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;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailboxManager.java
1,20 → 1,15
package ak.hostadmiral.core.model;
 
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 java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
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.MailboxStore;
 
public class MailboxManager
implements
ConfigInit,
UserBeforeDeleteListener,
UserDeletingListener,
SystemUserBeforeDeleteListener,
22,8 → 17,37
InetDomainBeforeDeleteListener,
InetDomainDeletingListener
{
private MailboxStore store;
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();
30,8 → 54,7
private Collection deletingListeners = new ArrayList();
private Collection deletedListeners = new ArrayList();
 
public MailboxManager()
throws ModelException
private MailboxManager()
{
UserManager.getInstance().addBeforeDeleteListener(this);
SystemUserManager.getInstance().addBeforeDeleteListener(this);
62,8 → 85,16
public Mailbox get(User editor, Long id)
throws ModelException
{
Mailbox mailbox = store.get(id);
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();
 
76,18 → 107,45
if(mailbox.getDomain() == null)
throw new ModelException("Cannot check unique login for mailbox without domain");
 
return store.loginExists(mailbox, login);
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);
}
}
 
public Mailbox findForLogin(User editor, String login)
protected Mailbox findForLogin(String login)
throws ModelException
{
Mailbox mailbox = store.findForLogin(login);
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(!mailbox.viewableBy(editor))
throw new ModelSecurityException();
 
return mailbox;
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)
100,7 → 158,13
 
//mailbox.setModUser(editor); // FIXME
 
store.save(mailbox);
try {
HibernateUtil.currentSession().saveOrUpdate(mailbox);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
// inform listeners
if(isNew) {
201,7 → 265,13
Mailbox oldMailbox = new Mailbox(mailbox);
 
// delete it
store.delete(mailbox);
try {
HibernateUtil.currentSession().delete(mailbox);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
// inform deleted listeners
for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) {
220,26 → 290,112
Integer[] sortingKeys, User editor)
throws ModelException
{
if(editor.isSuperuser())
return store.listAllMailboxes(info, rowsPerPage, pageNumber, sortingKeys);
else
return store.listMailboxes(info, rowsPerPage, pageNumber, sortingKeys, editor);
try {
if(editor.isSuperuser()) {
if(info != null) {
info.init(((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from Mailbox").next()).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableListSql(rowsPerPage, pageNumber,
"select {mb.*}, {d.*}, {o.*}, {su.*}"
+ " from mailboxes as mb"
+ " left join domains as d on mb.domain = d.id"
+ " left join users as o on mb.owner = o.id"
+ " left join systemusers as su on mb.systemUser = su.id"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeysSql),
new String[] { "mb", "d", "o", "su" },
new Class[] { Mailbox.class, InetDomain.class, User.class, SystemUser.class },
null,
null);
}
else {
if(info != null) {
List countlist = HibernateUtil.sqlQuery(
"select count(*) from ("
+ " select mb.id from mailboxes mb"
+ " where mb.owner=?"
+ " union"
+ " select mb.id from mailboxes mb"
+ " left join domains as d on mb.domain = d.id"
+ " where d.owner=?"
+ ") as count_table",
new Object[] { editor.getId(), editor.getId() });
 
info.init(((Long)countlist.get(0)).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableListSql(rowsPerPage, pageNumber,
"(select {mb.*}, {d.*}, {o.*}, {su.*}"
+ " from mailboxes as mb"
+ " left join domains as d on mb.domain = d.id"
+ " left join users as o on mb.owner = o.id"
+ " left join systemusers as su on mb.systemUser = su.id"
+ " where mb.owner=?)"
+ " union "
+ "(select {mb.*}, {d.*}, {o.*}, {su.*}"
+ " from mailboxes as mb"
+ " left join domains as d on mb.domain = d.id"
+ " left join users as o on mb.owner = o.id"
+ " left join systemusers as su on mb.systemUser = su.id"
+ " where d.owner=?)"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeysSql),
new String[] { "mb", "d", "o", "su" },
new Class[] { Mailbox.class, InetDomain.class, User.class, SystemUser.class },
new Object[] { editor, editor },
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) });
}
}
catch(HibernateException ex)
{
ex.printStackTrace();
 
throw new ModelException(ex);
}
}
 
public boolean areMailboxesAvailable(User editor)
throws ModelException
{
if(editor.isSuperuser())
return true;
else
return store.countMailboxesAvailable(editor) > 0;
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 = store.listOwnMailboxes(user);
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);
}
 
246,8 → 402,19
public void userDeleting(User editor, User user)
throws ModelException
{
Collection mailboxes = store.listOwnMailboxes(user);
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());
}
256,8 → 423,19
public Collection inetDomainBeforeDelete(User editor, InetDomain domain, Collection known)
throws ModelException
{
Collection mailboxes = store.listMailboxesForDomain(domain);
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);
}
 
264,8 → 442,19
public void inetDomainDeleting(User editor, InetDomain domain)
throws ModelException
{
Collection mailboxes = store.listMailboxesForDomain(domain);
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());
}
274,8 → 463,19
public Collection systemUserBeforeDelete(User editor, SystemUser user, Collection known)
throws ModelException
{
Collection mailboxes = store.listMailboxesForSystemUser(user);
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);
}
 
282,8 → 482,19
public void systemUserDeleting(User editor, SystemUser user)
throws ModelException
{
Collection mailboxes = store.listMailboxesForSystemUser(user);
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());
}
315,6 → 526,13
public static final Integer SORT_LOGIN = new Integer(1);
public static final Integer SORT_DOMAIN = new Integer(2);
 
protected static Map sortKeysSql = new HashMap();
 
static {
sortKeysSql.put(SORT_LOGIN, "login0_");
sortKeysSql.put(SORT_DOMAIN, "name1_");
}
 
public static final Comparator LOGIN_COMPARATOR = new LoginComparator();
public static final Comparator DOMAIN_COMPARATOR = new DomainComparator();
 
371,25 → 589,4
return (obj instanceof DomainComparator);
}
}
 
public void init(Map params)
throws ModelException
{
try {
mailboxManager = this;
 
Class c = Class.forName((String)params.get("store"));
store = (MailboxStore)c.newInstance();
}
catch(Exception ex) {
throw new ModelException(ex);
}
}
 
private static MailboxManager mailboxManager = null;
 
public static MailboxManager getInstance()
{
return mailboxManager;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/SystemUserManager.java
1,25 → 1,49
package ak.hostadmiral.core.model;
 
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 java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
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.SystemUserStore;
 
public class SystemUserManager
implements
ConfigInit,
UserBeforeDeleteListener,
UserDeletingListener
{
private SystemUserStore store;
private static SystemUserManager systemUserManager = null;
private static boolean registered = false;
 
public static SystemUserManager getInstance()
{
return systemUserManager;
}
 
protected static void register()
{
synchronized(SystemUserManager.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/SystemUser.hbm.xml");
 
systemUserManager = new SystemUserManager();
}
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();
26,8 → 50,7
private Collection deletingListeners = new ArrayList();
private Collection deletedListeners = new ArrayList();
 
public SystemUserManager()
throws ModelException
private SystemUserManager()
{
UserManager.getInstance().addBeforeDeleteListener(this);
}
49,8 → 72,16
public SystemUser get(User editor, Long id)
throws ModelException
{
SystemUser user = store.get(id);
SystemUser user;
 
try {
user = (SystemUser)HibernateUtil.currentSession().load(SystemUser.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
if(!user.viewableBy(editor))
throw new ModelSecurityException();
 
60,35 → 91,83
public boolean nameExists(User editor, SystemUser user, String name)
throws ModelException
{
return store.nameExists(user, name);
try {
if(user.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u where name = ?",
name, Hibernate.STRING)
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u where name = ? and u != ?",
new Object[] { name, user },
new Type[] { Hibernate.STRING, Hibernate.entity(SystemUser.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public boolean uidExists(User editor, SystemUser user, Integer uid)
throws ModelException
{
return store.uidExists(user, uid);
try {
if(user.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u where uid = ?",
uid, Hibernate.INTEGER)
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u where uid = ? and u != ?",
new Object[] { uid, user },
new Type[] { Hibernate.INTEGER, Hibernate.entity(SystemUser.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public SystemUser findForName(User editor, String name)
protected SystemUser findForName(String name)
throws ModelException
{
SystemUser user = store.findForName(name);
try {
List list = HibernateUtil.currentSession().find(
"select u from SystemUser u left join fetch u.owner where u.name=?",
name, Hibernate.STRING);
 
if(user != null && !user.viewableBy(editor))
throw new ModelSecurityException();
 
return user;
if(list.size() == 0)
return null;
else
return (SystemUser)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public SystemUser findForUid(User editor, Integer uid)
protected SystemUser findForUid(Integer uid)
throws ModelException
{
SystemUser user = store.findForUid(uid);
try {
List list = HibernateUtil.currentSession().find(
"select u from SystemUser u left join fetch u.owner where u.uid=?",
uid, Hibernate.INTEGER);
 
if(user != null && !user.viewableBy(editor))
throw new ModelSecurityException();
 
return user;
if(list.size() == 0)
return null;
else
return (SystemUser)list.get(0);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void save(User editor, SystemUser systemUser)
101,7 → 180,13
 
//systemUser.setModUser(editor); // FIXME
 
store.save(systemUser);
try {
HibernateUtil.currentSession().saveOrUpdate(systemUser);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
// inform listeners
if(isNew) {
202,7 → 287,13
SystemUser oldSystemUser = new SystemUser(systemUser);
 
// delete it
store.delete(systemUser);
try {
HibernateUtil.currentSession().delete(systemUser);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
// inform deleted listeners
for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) {
221,26 → 312,72
Integer[] sortingKeys, User editor)
throws ModelException
{
if(editor.isSuperuser())
return store.listAllSystemUsers(info, rowsPerPage, pageNumber, sortingKeys);
else
return store.listSystemUsers(info, rowsPerPage, pageNumber, sortingKeys, editor);
try {
if(editor.isSuperuser()) {
if(info != null) {
info.init(((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser").next()).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableList(rowsPerPage, pageNumber,
"select u from SystemUser u left join fetch u.owner"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null);
}
else {
if(info != null) {
info.init(((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser where owner is null or owner = ?",
editor, Hibernate.entity(User.class))
.next()).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableList(rowsPerPage, pageNumber,
"select u from SystemUser u left join u.owner o"
+ " where u.owner is null or u.owner = ?"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys),
new Object[] { editor }, new Type[] { Hibernate.entity(User.class) } );
}
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public boolean areSystemUsersAvailable(User editor)
throws ModelException
{
if(editor.isSuperuser())
return true;
else
return store.countSystemUsersAvailable(editor) > 0;
try {
if(editor.isSuperuser())
return true;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from SystemUser u left join u.owner o where o is null or o=?",
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 systemUsers = store.listOwnSystemUsers(user);
Collection systemUsers;
 
try {
systemUsers = HibernateUtil.currentSession().find(
"select u from SystemUser u where u.owner = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
Collection cascade = new ArrayList();
for(Iterator i = systemUsers.iterator(); i.hasNext(); ) {
SystemUser u = (SystemUser)i.next();
263,8 → 400,18
public void userDeleting(User editor, User user)
throws ModelException
{
Collection systemUsers = store.listOwnSystemUsers(user);
Collection systemUsers;
 
try {
systemUsers = HibernateUtil.currentSession().find(
"select u from SystemUser u where u.owner = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
for(Iterator i = systemUsers.iterator(); i.hasNext(); ) {
delete(editor, (SystemUser)i.next());
}
273,6 → 420,13
public static final Integer SORT_UID = new Integer(1);
public static final Integer SORT_NAME = new Integer(2);
 
protected static Map sortKeys = new HashMap();
 
static {
sortKeys.put(SORT_UID, "u.uid");
sortKeys.put(SORT_NAME, "u.name");
}
 
public static final Comparator UID_COMPARATOR = new UidComparator();
public static final Comparator NAME_COMPARATOR = new NameComparator();
 
329,25 → 483,4
return (obj instanceof NameComparator);
}
}
 
public void init(Map params)
throws ModelException
{
try {
systemUserManager = this;
 
Class c = Class.forName((String)params.get("store"));
store = (SystemUserStore)c.newInstance();
}
catch(Exception ex) {
throw new ModelException(ex);
}
}
 
private static SystemUserManager systemUserManager = null;
 
public static SystemUserManager getInstance()
{
return systemUserManager;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailAliasManager.java
1,27 → 1,52
package ak.hostadmiral.core.model;
 
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 java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
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.MailAliasStore;
 
public class MailAliasManager
implements
ConfigInit,
UserBeforeDeleteListener,
UserDeletingListener,
InetDomainBeforeDeleteListener,
InetDomainDeletingListener
{
private MailAliasStore store;
private static MailAliasManager mailAliasManager = null;
private static boolean registered = false;
 
public static MailAliasManager getInstance()
{
if(mailAliasManager == null)
mailAliasManager = new MailAliasManager();
 
return mailAliasManager;
}
 
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 Collection createdListeners = new ArrayList();
private Collection modifiedListeners = new ArrayList();
private Collection beforeDeleteListeners = new ArrayList();
28,8 → 53,7
private Collection deletingListeners = new ArrayList();
private Collection deletedListeners = new ArrayList();
 
public MailAliasManager()
throws ModelException
private MailAliasManager()
{
UserManager.getInstance().addBeforeDeleteListener(this);
}
53,8 → 77,16
public MailAlias get(User editor, Long id)
throws ModelException
{
MailAlias alias = store.get(id);
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();
 
67,18 → 99,44
if(alias.getDomain() == null)
throw new ModelException("Cannot check unique address for mail alias without domain");
 
return store.addressExists(alias, address);
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);
}
}
 
public MailAlias findForName(User editor, String name)
protected MailAlias findForName(String name)
throws ModelException
{
MailAlias alias = store.findForName(name);
try {
List list = HibernateUtil.currentSession().find(
"select a from MailAlias a left join fetch a.domain"
+ " left join fetch a.owner where a.name=?", name, Hibernate.STRING);
 
if(!alias.viewableBy(editor))
throw new ModelSecurityException();
 
return alias;
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)
91,7 → 149,13
 
//mailAlias.setModUser(editor); // FIXME
 
store.save(mailAlias);
try {
HibernateUtil.currentSession().saveOrUpdate(mailAlias);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
// inform listeners
if(isNew) {
192,7 → 256,13
MailAlias oldMailAlias = new MailAlias(mailAlias);
 
// delete it
store.delete(mailAlias);
try {
HibernateUtil.currentSession().delete(mailAlias);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
// inform deleted listeners
for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) {
211,26 → 281,100
Integer[] sortingKeys, User editor)
throws ModelException
{
if(editor.isSuperuser())
return store.listAllMailAliases(info, rowsPerPage, pageNumber, sortingKeys);
else
return store.listMailAliases(info, rowsPerPage, pageNumber, sortingKeys, editor);
try {
if(editor.isSuperuser()) {
if(info != null) {
info.init(((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from MailAlias").next()).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableList(rowsPerPage, pageNumber,
"select a from MailAlias a left join fetch a.domain as d"
+ " left join fetch a.owner"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null);
}
else {
if(info != null) {
List countlist = HibernateUtil.sqlQuery(
"select count(*) from ("
+ " select a.id from mailaliases a"
+ " where a.owner=?"
+ " union"
+ " select a.id from mailaliases a"
+ " left join domains as d on a.domain = d.id"
+ " where d.owner=?"
+ ") as count_table",
new Object[] { editor.getId(), editor.getId() });
 
info.init(((Long)countlist.get(0)).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableListSql(rowsPerPage, pageNumber,
"(select {a.*}, {d.*}, {o.*}"
+ " from mailaliases as a"
+ " left join domains as d on a.domain = d.id"
+ " left join users as o on a.owner = o.id"
+ " where a.owner=?)"
+ " union "
+ "(select {a.*}, {d.*}, {o.*}"
+ " from mailaliases as a"
+ " left join domains as d on a.domain = d.id"
+ " left join users as o on a.owner = o.id"
+ " where d.owner=?)"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeysSql),
new String[] { "a", "d", "o" },
new Class[] { MailAlias.class, InetDomain.class, User.class },
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
{
if(editor.isSuperuser())
return true;
else
return store.countMailAliasesAvailable(editor) > 0;
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);
}
}
 
public Collection userBeforeDelete(User editor, User user, Collection known)
throws ModelException
{
Collection mailAliases = store.listOwnMailAliases(user);
Collection mailAliases;
 
try {
mailAliases = HibernateUtil.currentSession().find(
"select a from MailAlias a left join fetch a.domain where a.owner = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
return iterateBeforeDelete(editor, mailAliases, known);
}
 
237,8 → 381,18
public void userDeleting(User editor, User user)
throws ModelException
{
Collection mailAliases = store.listOwnMailAliases(user);
Collection mailAliases;
 
try {
mailAliases = HibernateUtil.currentSession().find(
"select a from MailAlias a left join fetch a.domain where a.owner = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
for(Iterator i = mailAliases.iterator(); i.hasNext(); ) {
delete(editor, (MailAlias)i.next());
}
247,8 → 401,18
public Collection inetDomainBeforeDelete(User editor, InetDomain domain, Collection known)
throws ModelException
{
Collection mailAliases = store.listMailAliasesForDomain(domain);
Collection mailAliases;
 
try {
mailAliases = HibernateUtil.currentSession().find(
"select a from MailAlias a left join fetch a.owner where a.domain = ?",
domain, Hibernate.entity(InetDomain.class) );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
return iterateBeforeDelete(editor, mailAliases, known);
}
 
255,8 → 419,18
public void inetDomainDeleting(User editor, InetDomain domain)
throws ModelException
{
Collection mailAliases = store.listMailAliasesForDomain(domain);
Collection mailAliases;
 
try {
mailAliases = HibernateUtil.currentSession().find(
"select a from MailAlias a left join fetch a.owner where a.domain = ?",
domain, Hibernate.entity(InetDomain.class) );
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
for(Iterator i = mailAliases.iterator(); i.hasNext(); ) {
delete(editor, (MailAlias)i.next());
}
288,6 → 462,20
public static final Integer SORT_ADDRESS = new Integer(1);
public static final Integer SORT_DOMAIN = new Integer(2);
 
protected static Map sortKeys = new HashMap();
 
static {
sortKeys.put(SORT_ADDRESS, "a.address");
sortKeys.put(SORT_DOMAIN, "d.name");
}
 
protected static Map sortKeysSql = new HashMap();
 
static {
sortKeysSql.put(SORT_ADDRESS, "address0_");
sortKeysSql.put(SORT_DOMAIN, "name1_");
}
 
public static final Comparator ADDRESS_COMPARATOR = new AddressComparator();
 
private static class AddressComparator
316,25 → 504,4
return (obj instanceof AddressComparator);
}
}
 
public void init(Map params)
throws ModelException
{
try {
mailAliasManager = this;
 
Class c = Class.forName((String)params.get("store"));
store = (MailAliasStore)c.newInstance();
}
catch(Exception ex) {
throw new ModelException(ex);
}
}
 
private static MailAliasManager mailAliasManager = null;
 
public static MailAliasManager getInstance()
{
return mailAliasManager;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/UserManager.java
1,37 → 1,60
package ak.hostadmiral.core.model;
 
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.Comparator;
import java.util.Date;
 
import ak.hostadmiral.util.ConfigInit;
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
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.UserStore;
 
public class UserManager
implements
ConfigInit,
UserBeforeDeleteListener,
UserDeletingListener
{
private UserStore store;
private static UserManager userManager = null;
private static boolean registered = false;
 
public static UserManager getInstance()
{
return userManager;
}
 
protected static void register()
{
synchronized(UserManager.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/User.hbm.xml");
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/UserLogin.hbm.xml");
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/PasswordStoreAbstract.hbm.xml");
userManager = new UserManager();
}
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 Map loggedinUsers = new WeakHashMap();
 
public UserManager()
throws ModelException
private UserManager()
{
addBeforeDeleteListener(this);
addDeletingListener(this);
65,8 → 88,15
public User get(User editor, Long id)
throws ModelException
{
User user = store.get(id);
User user;
 
try {
user = (User)HibernateUtil.currentSession().load(User.class, id);
}
catch(HibernateException ex) {
throw new ModelException(ex);
}
 
if(!user.viewableBy(editor))
throw new ModelSecurityException();
 
76,18 → 106,41
public boolean loginExists(User editor, User user, String login)
throws ModelException
{
return store.loginExists(user, login);
try {
if(user.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from User u where login = ?",
login, Hibernate.STRING)
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from User u where login = ? and u != ?",
new Object[] { login, user },
new Type[] { Hibernate.STRING, Hibernate.entity(User.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex) {
throw new ModelException(ex);
}
}
 
public User findForLogin(User editor, String login)
public User findForLogin(String login)
throws ModelException
{
User user = store.findForLogin(login);
try {
List list = HibernateUtil.currentSession().find(
"select u from User u left join fetch u.boss where u.login = ? and u.enabled = ?",
new Object[] { login, Boolean.TRUE },
new Type[] { Hibernate.STRING, Hibernate.BOOLEAN } );
 
if(user != null && !user.viewableBy(editor))
throw new ModelSecurityException();
 
return user;
if(list.size() == 0)
return null;
else
return (User)list.get(0);
}
catch(HibernateException ex) {
throw new ModelException(ex);
}
}
 
public void save(User editor, User user)
104,7 → 157,12
//user.setModUser(editor); // FIXME: disabled because hb throws exception
// if user edits itself
 
store.save(user);
try {
HibernateUtil.currentSession().saveOrUpdate(user);
}
catch(HibernateException ex) {
throw new ModelException(ex);
}
 
// update user if he is logged in
for(Iterator i = loggedinUsers.keySet().iterator(); i.hasNext(); ) {
212,7 → 270,12
User oldUser = new User(user);
 
// delete it
store.delete(user);
try {
HibernateUtil.currentSession().delete(user);
}
catch(HibernateException ex) {
throw new ModelException(ex);
}
 
// inform delete listeners
for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) {
231,32 → 294,78
Integer[] sortingKeys, User editor)
throws ModelException
{
if(editor.isSuperuser())
return store.listAllUsers(info, rowsPerPage, pageNumber, sortingKeys);
else
return store.listUsers(info, rowsPerPage, pageNumber, sortingKeys, editor);
try {
if(editor.isSuperuser()) {
if(info != null) {
info.init(((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from User").next()).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableList(rowsPerPage, pageNumber,
"select u from User u left join fetch u.boss"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null);
}
else {
if(info != null) {
info.init(((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from User u where u = ? or u.boss = ?",
new Object[] { editor, editor},
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) }
).next()).intValue(),
pageNumber, rowsPerPage);
}
 
return HibernateUtil.pageableList(rowsPerPage, pageNumber,
"select u from User u left join fetch u.boss where u = ? or u.boss = ?"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys),
new Object[] { editor, editor},
new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } );
}
}
catch(HibernateException ex) {
throw new ModelException(ex);
}
}
 
public boolean areUsersAvailable(User editor)
throws ModelException
{
return true;
try {
if(editor.isSuperuser()) {
return true;
}
else {
// FIXME: always true?
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from User u where u = ? or u.boss = ?",
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 User loginUser(String login, String password, String ip)
throws ModelException
{
User user = (login == null || password == null)
? null : store.findForLogin(login);
 
User user = (login == null || password == null) ? null : findForLogin(login);
boolean success = (user == null) ? false : user.checkPassword(password);
UserLogin userLogin = new UserLogin(user, login, new Date(), Boolean.valueOf(success), ip);
 
// save login information
store.saveUserLogin(userLogin);
try {
HibernateUtil.currentSession().saveOrUpdate(userLogin);
}
catch(HibernateException ex) {
throw new ModelException(ex);
}
 
if(success) {
user = new User(user); // unbind the user from store
user = new User(user); // unbind the user from hibernate
loggedinUsers.put(user, Boolean.TRUE);
return user;
}
269,16 → 378,34
throws ModelException
{
if(!editor.mayViewAllLogins())
{
throw new ModelSecurityException();
}
 
return store.listFailedLogins();
try {
return HibernateUtil.currentSession().find(
"select l from UserLogin l left join fetch l.user where l.success = ?",
Boolean.FALSE, Hibernate.BOOLEAN);
}
catch(HibernateException ex) {
throw new ModelException(ex);
}
}
 
public Collection userBeforeDelete(User editor, User user, Collection known)
throws ModelException
{
Collection subusers = store.listSubusers(user);
Collection subusers;
 
try {
subusers = HibernateUtil.currentSession().find(
"select u from User u where u.boss = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex) {
throw new ModelException(ex);
}
 
Collection cascade = new ArrayList();
for(Iterator i = subusers.iterator(); i.hasNext(); ) {
User u = (User)i.next();
301,8 → 428,17
public void userDeleting(User editor, User user)
throws ModelException
{
Collection subusers = store.listSubusers(user);
Collection subusers;
 
try {
subusers = HibernateUtil.currentSession().find(
"select u from User u where u.boss = ?",
user, Hibernate.entity(User.class) );
}
catch(HibernateException ex) {
throw new ModelException(ex);
}
 
for(Iterator i = subusers.iterator(); i.hasNext(); ) {
delete(editor, (User)i.next());
}
310,6 → 446,12
 
public static final Integer SORT_LOGIN = new Integer(1);
 
protected static Map sortKeys = new HashMap();
 
static {
sortKeys.put(SORT_LOGIN, "u.login");
}
 
public static final Comparator LOGIN_COMPARATOR = new LoginComparator();
public static final Comparator LOGINS_TIME_COMPARATOR = new LoginsTimeComparator();
 
366,25 → 508,4
return (obj instanceof LoginComparator);
}
}
 
public void init(Map params)
throws ModelException
{
try {
userManager = this;
 
Class c = Class.forName((String)params.get("store"));
store = (UserStore)c.newInstance();
}
catch(Exception ex) {
throw new ModelException(ex);
}
}
 
private static UserManager userManager = null;
 
public static UserManager getInstance()
{
return userManager;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailAliasDestinationManager.java
1,28 → 1,39
package ak.hostadmiral.core.model;
 
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 java.util.*;
import net.sf.hibernate.*;
import ak.hostadmiral.util.HibernateUtil;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
import ak.hostadmiral.core.model.store.MailAliasDestinationStore;
 
public class MailAliasDestinationManager
implements
ConfigInit
{
// FIXME create, delete and modify listeners are not implemented, bacause
// all operations are done via MailAliasManager. Do we need them?
 
private MailAliasDestinationStore store;
private static boolean registered = false;
protected static void register()
{
synchronized(MailAliasDestinationManager.class) {
if(registered) return;
 
public MailAliasDestinationManager()
throws ModelException
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/hostadmiral/core/model/MailAliasDestination.hbm.xml");
}
catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
}
 
static {
register();
}
 
private MailAliasDestinationManager()
{
}
 
43,8 → 54,17
public MailAliasDestination get(User editor, Long id)
throws ModelException
{
MailAliasDestination dest = store.get(id);
MailAliasDestination dest;
 
try {
dest = (MailAliasDestination)HibernateUtil.currentSession()
.load(MailAliasDestination.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
 
if(!dest.viewableBy(editor))
throw new ModelSecurityException();
 
60,7 → 80,13
//mailAliasDestination.setModUser(editor); // FIXME
// FIXME: the mod_user is not set when changing a destination as element of collection
 
store.save(mailAliasDestination);
try {
HibernateUtil.currentSession().saveOrUpdate(mailAliasDestination);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void delete(User editor, MailAliasDestination mailAliasDestination)
69,13 → 95,27
if(!mailAliasDestination.deleteableBy(editor))
throw new ModelSecurityException();
 
store.delete(mailAliasDestination);
try {
HibernateUtil.currentSession().delete(mailAliasDestination);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection listMailAliasesDestination(MailAlias alias)
throws ModelException
{
return store.listMailAliasesDestination(alias);
try {
return HibernateUtil.currentSession().find(
"select d from MailAliasDestination d left join fetch d.mailbox where d.alias=?",
alias, Hibernate.entity(MailAlias.class));
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public boolean areMailAliasesDestinationsAvailable(User editor)
84,6 → 124,16
return true;
}
 
private static MailAliasDestinationManager mailAliasDestinationManager = null;
 
public static MailAliasDestinationManager getInstance()
{
if(mailAliasDestinationManager == null)
mailAliasDestinationManager = new MailAliasDestinationManager();
 
return mailAliasDestinationManager;
}
 
public static final Comparator EMAIL_COMPARATOR = new EmailComparator();
 
private static class EmailComparator
112,25 → 162,4
return (obj instanceof EmailComparator);
}
}
 
public void init(Map params)
throws ModelException
{
try {
mailAliasDestinationManager = this;
 
Class c = Class.forName((String)params.get("store"));
store = (MailAliasDestinationStore)c.newInstance();
}
catch(Exception ex) {
throw new ModelException(ex);
}
}
 
private static MailAliasDestinationManager mailAliasDestinationManager = null;
 
public static MailAliasDestinationManager getInstance()
{
return mailAliasDestinationManager;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/action/UserLoginsAction.java
24,7 → 24,6
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
// FIXME: pages here
ActionUtils.prepare(request, response);
User user = (User)request.getSession().getAttribute("user");
 
/hostadmiral/trunk/src/ak/hostadmiral/util/ModelStoreException.java
File deleted
/hostadmiral/trunk/src/ak/hostadmiral/util/ConfigInit.java
10,6 → 10,5
* @param params map String -> String with pairs of param name -> value
* from the initializaion file
*/
public void init(Map params)
throws ModelException;
public void init(Map params);
}
/hostadmiral/trunk/src/ak/hostadmiral/util/HibernateUtil.java
23,7 → 23,7
private static boolean validated = false;
 
private static void validate()
throws HibernateException, ModelStoreException
throws HibernateException, ModelException
{
synchronized(HibernateUtil.class) {
if(validated) return;
31,15 → 31,14
Collection versions = currentSession().find("from DatabaseVersion");
 
if(versions == null || versions.size() == 0)
throw new ModelStoreException("Database structure version not found");
throw new ModelException("Database structure version not found");
 
if(versions.size() > 1)
throw new ModelStoreException(
"Too much entries in database structure version table");
throw new ModelException("Too much entries in database structure version table");
 
int version = ((DatabaseVersion)versions.iterator().next()).getMajor();
if(version != DATABASE_VERSION)
throw new ModelStoreException("Expected database structure version "
throw new ModelException("Expected database structure version "
+ DATABASE_VERSION + ", found " + version);
 
validated = true;
96,12 → 95,12
}
 
public static void closeSession()
throws HibernateException, ModelStoreException
throws HibernateException, ModelException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb == null)
throw new ModelStoreException("No session found for this thread");
throw new ModelException("No session found for this thread");
 
hibernateBean.set(null);
hb.session.close();
108,12 → 107,12
}
 
public static void beginTransaction()
throws HibernateException, ModelStoreException
throws HibernateException, ModelException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb != null && hb.transaction != null)
throw new ModelStoreException("Transaction is already open");
throw new ModelException("Transaction is already open");
 
currentBean().transaction = currentSession().beginTransaction();
 
123,7 → 122,7
}
 
public static boolean isTransactionOpen()
throws HibernateException, ModelStoreException
throws HibernateException, ModelException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
131,12 → 130,12
}
 
public static void commitTransaction()
throws HibernateException, ModelStoreException
throws HibernateException, ModelException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb == null || hb.transaction == null)
throw new ModelStoreException("No open transaction");
throw new ModelException("No open transaction");
 
hb.transaction.commit();
hb.transaction = null;
143,12 → 142,12
}
 
public static void rollbackTransaction()
throws HibernateException, ModelStoreException
throws HibernateException, ModelException
{
HibernateBean hb = (HibernateBean)hibernateBean.get();
 
if(hb == null || hb.transaction == null)
throw new ModelStoreException("No open transaction");
throw new ModelException("No open transaction");
 
hb.transaction.rollback();
hb.transaction = null;
155,16 → 154,23
}
 
public static List sqlQuery(String query, Object[] values)
throws HibernateException, ModelStoreException
throws ModelException
{
Connection con = currentSession().connection();
Connection con;
PreparedStatement stmt;
 
try {
con = currentSession().connection();
}
catch(HibernateException ex) {
throw new ModelException(ex);
}
 
try {
stmt = con.prepareStatement(query);
}
catch(SQLException ex) {
throw new ModelStoreException(ex);
throw new ModelException(ex);
}
 
try {
182,7 → 188,7
}
catch(SQLException ex)
{
throw new ModelStoreException(ex);
throw new ModelException(ex);
}
finally {
try {
197,45 → 203,58
public static List pageableListSql(int pageSize, int pageNumber,
String query, String[] returnAliases, Class[] returnClasses,
Object[] values, Type[] types)
throws HibernateException, ModelStoreException
throws ModelException
{
Query hq = currentSession().createSQLQuery(
query, returnAliases, returnClasses);
try {
Query hq = currentSession().createSQLQuery(
query, returnAliases, returnClasses);
 
if(values != null && types != null) {
for(int i = 0; i < values.length; i++)
hq.setParameter(i, values[i], types[i]);
}
if(values != null && types != null) {
for(int i = 0; i < values.length; i++)
hq.setParameter(i, values[i], types[i]);
}
 
if(pageSize > 0) {
hq.setFirstResult(pageSize * pageNumber);
hq.setMaxResults(pageSize);
if(pageSize > 0) {
hq.setFirstResult(pageSize * pageNumber);
hq.setMaxResults(pageSize);
}
 
return selectFirstClassColumn(hq.list());
// FIXME: really no other way in Hibernate?
}
 
return selectFirstClassColumn(hq.list());
// FIXME: really no other way in Hibernate?
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public static List pageableList(int pageSize, int pageNumber,
String query, Object[] values, Type[] types)
throws HibernateException, ModelStoreException
throws ModelException
{
Query hq = currentSession().createQuery(query);
try {
Query hq = currentSession().createQuery(query);
 
if(values != null && types != null) {
for(int i = 0; i < values.length; i++)
hq.setParameter(i, values[i], types[i]);
}
if(values != null && types != null) {
for(int i = 0; i < values.length; i++)
hq.setParameter(i, values[i], types[i]);
}
 
if(pageSize > 0) {
hq.setFirstResult(pageSize * pageNumber);
hq.setMaxResults(pageSize);
if(pageSize > 0) {
hq.setFirstResult(pageSize * pageNumber);
hq.setMaxResults(pageSize);
}
 
return hq.list();
}
 
return hq.list();
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
protected static List selectFirstClassColumn(List list)
throws ModelException
{
List res = new ArrayList();
 
247,7 → 266,7
}
 
public static String formOrderClause(Integer[] sortingKeys, Map fieldMap)
throws ModelStoreException
throws ModelException
{
if(sortingKeys == null || sortingKeys.length == 0) return "";
 
258,7 → 277,7
 
String field = (String)fieldMap.get(sortingKeys[i]);
if(field == null)
throw new ModelStoreException(
throw new ModelException(
"Field for sorting key " + sortingKeys[i] + " not found");
 
buf.append(field);
/hostadmiral/trunk/conf/hibernate.cfg.xml
5,7 → 5,7
 
<hibernate-configuration>
<session-factory name="java:comp/env/hibernate/SessionFactory">
<property name="show_sql">true</property>
<property name="show_sql">false</property>
<mapping resource="ak/hostadmiral/util/DatabaseVersion.hbm.xml"/>
</session-factory>
</hibernate-configuration>
/hostadmiral/trunk/doc/todo.txt
80,6 → 80,3
e.g. indexes.
 
+ Config in one place. Allow configuration of each listener.
 
+ Split model and store.