Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1040 → Rev 1041

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