Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1019 → Rev 1020

/hostadmiral/trunk/src/ak/hostadmiral/util/CollectionInfo.java
0,0 → 1,16
package ak.hostadmiral.util;
 
public class CollectionInfo
{
private int size;
 
public int getSize()
{
return size;
}
 
public void setSize(int size)
{
this.size = size;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/util/HibernateUtil.java
1,9 → 1,11
package ak.hostadmiral.util;
 
import java.util.Collection;
import java.util.Map;
 
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import net.sf.hibernate.type.Type;
 
public class HibernateUtil
{
133,6 → 135,69
hb.transaction = null;
}
 
public static Collection pageableList(CollectionInfo info, int pageSize, int pageNumber,
String query, String[] returnAliases, Class[] returnClasses,
Object[] values, Type[] types)
throws ModelException
{
try {
// find the collection size if needed
if(info != null) {
Query sizeq = HibernateUtil.currentSession().createSQLQuery(
"select count(*) as {c} from " + query,
"c", Integer.class);
 
if(values != null && types != null) {
for(int i = 0; i < values.length; i++)
sizeq.setParameter(i, values[i], types[i]);
}
 
info.setSize(((Integer)sizeq.iterate().next()).intValue());
}
 
// find the collection
Query hq = HibernateUtil.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(pageSize > 0) {
hq.setFirstResult(pageSize * pageNumber);
hq.setMaxResults(pageSize);
}
 
return hq.list();
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public static String formOrderClause(Integer[] sortingKeys, Map fieldMap)
throws ModelException
{
if(sortingKeys == null || sortingKeys.length == 0) return "";
 
StringBuffer buf = new StringBuffer(" order by ");
 
for(int i = 0; i < sortingKeys.length; i++) {
if(i > 0) buf.append(",");
 
String field = (String)fieldMap.get(sortingKeys[i]);
if(field == null)
throw new ModelException(
"Field for sorting key " + sortingKeys[i] + " not found");
 
buf.append(field);
}
 
return buf.toString();
}
 
static class HibernateBean
{
public Session session;
/hostadmiral/trunk/src/ak/hostadmiral/core/action/MailboxAction.java
1,6 → 1,7
package ak.hostadmiral.core.action;
 
import java.util.List;
import java.util.Collection;
import java.util.Collections;
import java.util.ArrayList;
import java.util.HashSet;
23,6 → 24,7
 
import ak.hostadmiral.util.StringConverter;
import ak.hostadmiral.util.UserException;
import ak.hostadmiral.util.CollectionInfo;
import ak.hostadmiral.core.CoreResources;
import ak.hostadmiral.core.model.User;
import ak.hostadmiral.core.model.UserManager;
35,6 → 37,8
extends Action
implements ErrorHandlerX
{
public static final int PAGE_SIZE = 20;
 
public void handleErrors(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
53,9 → 57,15
ActionUtils.prepare(request, response);
User user = (User)request.getSession().getAttribute("user");
if("list".equals(mapping.getParameter())) {
List list = new ArrayList(MailboxManager.getInstance().listMailboxes(user));
Collections.sort(list, MailboxManager.LOGIN_COMPARATOR);
DynaActionForm theForm = (DynaActionForm)form;
Long page = StringConverter.parseLong(theForm.get("page"));
CollectionInfo listInfo = new CollectionInfo();
Collection list = MailboxManager.getInstance().listMailboxes(
listInfo, PAGE_SIZE, (page == null) ? 0 : page.intValue(),
new Integer[] { MailboxManager.SORT_DOMAIN, MailboxManager.SORT_LOGIN }, user);
 
request.setAttribute("mailboxes", list);
request.setAttribute("listInfo", listInfo);
request.setAttribute("allowedToCreate",
Boolean.valueOf(MailboxManager.getInstance().allowedToCreate(user)));
 
/hostadmiral/trunk/src/ak/hostadmiral/core/CoreResources.properties
59,6 → 59,7
 
ak.hostadmiral.page.general.errors=There are errors in you input
ak.hostadmiral.page.general.version=Project version
ak.hostadmiral.page.general.page.wrong=Wrong page number: {0}
 
ak.hostadmiral.page.error.title=Host Admiral - error
ak.hostadmiral.page.error.back=back
/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailboxManager.java
3,7 → 3,7
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
import ak.hostadmiral.util.CollectionUtils;
import ak.hostadmiral.util.CollectionInfo;
import ak.hostadmiral.util.HibernateUtil;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
283,28 → 283,40
public Collection listMailboxes(User editor)
throws ModelException
{
try {
if(editor.isSuperuser())
return 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");
else
// FIXME: any problems for big lists or by pages?
return CollectionUtils.addUnique(
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.owner=?",
new Object[] { editor }, new Type[] { Hibernate.entity(User.class) } ),
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 d.owner=?",
new Object[] { editor }, new Type[] { Hibernate.entity(User.class) } ));
return listMailboxes(null, 0, 0, null, editor);
}
 
public Collection listMailboxes(CollectionInfo info, int pageSize, int pageNumber,
Integer[] sortingKeys, User editor)
throws ModelException
{
if(editor.isSuperuser()) {
return HibernateUtil.pageableList(info, pageSize, pageNumber,
"select {mb.*}, {d.*}, {o.*}, {su.*}"
+ " from Mailbox mb left join fetch mb.domain as d"
+ " left join fetch mb.owner as o left join fetch mb.systemUser as su"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys),
new String[] { "mb", "d", "o", "su" },
new Class[] { Mailbox.class, InetDomain.class, User.class, SystemUser.class },
null,
null);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
else {
return HibernateUtil.pageableList(info, pageSize, pageNumber,
"select {mb.*}, {d.*}, {o.*}, {su.*}"
+ " from Mailbox mb left join fetch mb.domain as d"
+ " left join fetch mb.owner as o left join fetch mb.systemUser as su"
+ " where mb.owner=?"
+ " union"
+ " select {mb.*}, {d.*}, {o.*}, {su.*}"
+ " from Mailbox mb left join fetch mb.domain as d"
+ " left join fetch mb.owner as o left join fetch mb.systemUser as su"
+ " where d.owner=?"
+ HibernateUtil.formOrderClause(sortingKeys, sortKeys),
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) });
}
}
 
475,8 → 487,19
return cascade;
}
 
public static final Comparator LOGIN_COMPARATOR = new LoginComparator();
public static final Integer SORT_LOGIN = new Integer(1);
public static final Integer SORT_DOMAIN = new Integer(2);
 
protected static Map sortKeys = new HashMap();
 
static {
sortKeys.put(SORT_LOGIN, "mb.login");
sortKeys.put(SORT_DOMAIN, "d.name");
}
 
public static final Comparator LOGIN_COMPARATOR = new LoginComparator();
public static final Comparator DOMAIN_COMPARATOR = new DomainComparator();
 
private static class LoginComparator
implements Comparator
{
503,4 → 526,31
return (obj instanceof LoginComparator);
}
}
 
private static class DomainComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof Mailbox) || !(o2 instanceof Mailbox))
throw new ClassCastException("not a Mailbox");
 
Mailbox a1 = (Mailbox)o1;
Mailbox a2 = (Mailbox)o2;
 
if(a1 == null && a2 == null)
return 0;
else if(a1 == null && a2 != null)
return -1;
else if(a1 != null && a2 == null)
return 1;
else
return a1.getDomain().getName().compareToIgnoreCase(a2.getDomain().getName());
}
 
public boolean equals(Object obj)
{
return (obj instanceof DomainComparator);
}
}
}
/hostadmiral/trunk/webapp/WEB-INF/struts-config.xml
88,6 → 88,12
</form-bean>
 
<form-bean
name="MailboxListForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="page" type="java.lang.String" />
</form-bean>
 
<form-bean
name="MailboxForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="id" type="java.lang.String" />
414,6 → 420,9
path="/mail/box/list"
type="ak.hostadmiral.core.action.MailboxAction"
parameter="list"
name="MailboxListForm"
validate="true"
scope="request"
>
<forward name="default" path="/mail/box/list.jsp" />
</action>
/hostadmiral/trunk/webapp/WEB-INF/validation.xml
109,6 → 109,12
</field>
</form>
 
<form name="MailboxListForm">
<field property="page" depends="long">
<msg name="long" key="ak.hostadmiral.page.general.page.wrong" />
</field>
</form>
 
<form name="MailboxForm">
<field property="id" depends="long">
<msg name="long" key="ak.hostadmiral.core.mailbox.edit.id.wrong" />