Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1020 → Rev 1021

/hostadmiral/trunk/src/ak/hostadmiral/util/HibernateUtil.java
1,8 → 1,14
package ak.hostadmiral.util;
 
import java.util.Iterator;
import java.util.Collection;
import java.util.Map;
 
import java.util.List;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import net.sf.hibernate.type.Type;
135,28 → 141,60
hb.transaction = null;
}
 
public static Collection pageableList(CollectionInfo info, int pageSize, int pageNumber,
String query, String[] returnAliases, Class[] returnClasses,
Object[] values, Type[] types)
public static List sqlQuery(String query, Object[] values)
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);
Connection con;
PreparedStatement stmt;
 
if(values != null && types != null) {
for(int i = 0; i < values.length; i++)
sizeq.setParameter(i, values[i], types[i]);
}
try {
con = currentSession().connection();
}
catch(HibernateException ex) {
throw new ModelException(ex);
}
 
info.setSize(((Integer)sizeq.iterate().next()).intValue());
try {
stmt = con.prepareStatement(query);
}
catch(SQLException ex) {
throw new ModelException(ex);
}
 
try {
if(values != null) {
for(int i = 0; i < values.length; i++)
stmt.setObject(i+1, values[i]);
}
 
// find the collection
Query hq = HibernateUtil.currentSession().createSQLQuery(
List res = new ArrayList();
ResultSet rs = stmt.executeQuery();
while(rs.next()) {
res.add(rs.getObject(1));
}
return res;
}
catch(SQLException ex)
{
throw new ModelException(ex);
}
finally {
try {
stmt.close();
}
catch(SQLException ex) {
ex.printStackTrace();
}
}
}
 
public static List pageableList(int pageSize, int pageNumber,
String query, Class classToLoad, String[] returnAliases, Class[] returnClasses,
Object[] values, Type[] types)
throws ModelException
{
try {
Query hq = currentSession().createSQLQuery(
query, returnAliases, returnClasses);
 
if(values != null && types != null) {
169,7 → 207,8
hq.setMaxResults(pageSize);
}
 
return hq.list();
return selectClassColumn(classToLoad, hq.list());
// FIXME: really no other way in Hibernate?
}
catch(HibernateException ex)
{
177,6 → 216,52
}
}
 
protected static List selectClassColumn(Class classToLoad, List list)
throws ModelException
{
List res = new ArrayList();
 
if(list == null || list.size() == 0) return res;
 
Object o = list.get(0);
if(o == null)
throw new ModelException(
"First element in the list is null, cannot determine it type");
 
if(!(o instanceof Object[])) {
if(classToLoad.isInstance(o))
return list;
else
throw new ModelException("First element in the list is instance of "
+ o.getClass().getName() + ", which is not java.lang.Object[] or "
+ classToLoad.getName());
}
 
Object[] oa = (Object[])o;
int pos = -1;
 
for(int i = 0; i < oa.length; i++) {
if(classToLoad.isInstance(oa[i])) {
if(pos < 0)
pos = i;
else
throw new ModelException("First row of the list has several elements of type "
+ classToLoad.getName());
}
}
if(pos < 0) {
throw new ModelException("First row of the list has no elements of type "
+ classToLoad.getName());
}
 
for(Iterator i = list.iterator(); i.hasNext(); ) {
Object[] e = (Object[])i.next();
res.add(e[pos]);
}
 
return res;
}
 
public static String formOrderClause(Integer[] sortingKeys, Map fieldMap)
throws ModelException
{
/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailboxManager.java
290,33 → 290,69
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);
try {
if(editor.isSuperuser()) {
if(info != null) {
info.setSize(((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from Mailbox").next()).intValue());
}
 
return HibernateUtil.pageableList(pageSize, 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, sortKeys),
Mailbox.class,
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.setSize(((Long)countlist.get(0)).intValue());
}
 
return HibernateUtil.pageableList(pageSize, 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, sortKeys),
Mailbox.class,
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) });
}
}
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) });
catch(HibernateException ex)
{
ex.printStackTrace();
 
throw new ModelException(ex);
}
}
 
493,8 → 529,8
protected static Map sortKeys = new HashMap();
 
static {
sortKeys.put(SORT_LOGIN, "mb.login");
sortKeys.put(SORT_DOMAIN, "d.name");
sortKeys.put(SORT_LOGIN, "login0_");
sortKeys.put(SORT_DOMAIN, "name1_");
}
 
public static final Comparator LOGIN_COMPARATOR = new LoginComparator();
/hostadmiral/trunk/src/ak/hostadmiral/core/action/MailboxAction.java
58,7 → 58,7
User user = (User)request.getSession().getAttribute("user");
if("list".equals(mapping.getParameter())) {
DynaActionForm theForm = (DynaActionForm)form;
Long page = StringConverter.parseLong(theForm.get("page"));
Long page = StringConverter.parseLong(theForm.get("pg"));
CollectionInfo listInfo = new CollectionInfo();
Collection list = MailboxManager.getInstance().listMailboxes(
listInfo, PAGE_SIZE, (page == null) ? 0 : page.intValue(),
/hostadmiral/trunk/webapp/WEB-INF/struts-config.xml
90,7 → 90,7
<form-bean
name="MailboxListForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="page" type="java.lang.String" />
<form-property name="pg" type="java.lang.String" />
</form-bean>
 
<form-bean
/hostadmiral/trunk/webapp/WEB-INF/validation.xml
110,7 → 110,7
</form>
 
<form name="MailboxListForm">
<field property="page" depends="long">
<field property="pg" depends="long">
<msg name="long" key="ak.hostadmiral.page.general.page.wrong" />
</field>
</form>
/hostadmiral/trunk/webapp/mail/box/list.jsp
1,4 → 1,4
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
72,6 → 72,10
</logic:iterate>
</table>
 
<p>
List size: <bean:write name="listInfo" property="size" />
</p>
 
<logic:equal name="allowedToCreate" value="true">
<backpath:link action="/mail/box/edit"><bean:message key="ak.hostadmiral.page.mail.box.list.add" /></backpath:link>
</logic:equal>
79,7 → 83,7
<br>
<backpath:backlink><bean:message key="ak.hostadmiral.page.mail.box.list.back" /></backpath:backlink>
 
<p>
<p>
<bean:message key="ak.hostadmiral.page.general.version" />:
<bean:write name="projectVersion" />
</p>