/*
* Copyright (C) 2009 eXo Platform SAS.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
/**
*
*/
/*
* Copyright (C) 2003-2009 eXo Platform SAS.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see<http://www.gnu.org/licenses/>.
*/
package org.etk.core.membership.hibernate;
/*
import org.exoplatform.commons.utils.SecurityHelper;
import org.exoplatform.services.database.HibernateService;
import org.exoplatform.services.organization.User;
*/
import org.etk.common.utils.SecurityHelper;
import org.etk.component.database.HibernateService;
import org.etk.core.membership.User;
import org.hibernate.Query;
import org.hibernate.Session;
import java.security.PrivilegedAction;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* Created by The eXo Platform SAS.
*
* @author <a href="mailto:anatoliy.bazko@exoplatform.com.ua">Anatoliy Bazko</a>
* @version $Id: SimpliHibernateUserListAccess.java 111 2008-11-11 11:11:11Z $
*/
public class SimpleHibernateUserListAccess extends HibernateUserListAccess
{
/**
* SimpliHibernateUserListAccess constructor.
*
* @param service
* The Hibernate Service.
* @param findQuery
* Find query string
* @param countQuery
* Count query string
*/
public SimpleHibernateUserListAccess(HibernateService service, String findQuery, String countQuery)
{
super(service, findQuery, countQuery);
}
/**
* SimpliHibernateUserListAccess constructor.
*
* @param service
* The Hibernate Service.
* @param findQuery
* Find query string
* @param countQuery
* Count query string
* @param binding
* Binded fields
*/
public SimpleHibernateUserListAccess(HibernateService service, String findQuery, String countQuery,
Map<String, Object> binding)
{
super(service, findQuery, countQuery, binding);
}
/**
* {@inheritDoc}
*/
@Override
protected int getSize(final Session session) throws Exception
{
Query query = SecurityHelper.doPrivilegedAction(new PrivilegedAction<Query>()
{
public Query run()
{
return session.createQuery(countQuery);
}
});
bindFields(query);
List l = query.list();
Number count = (Number)l.get(0);
return count.intValue();
}
/**
* {@inheritDoc}
*/
@Override
protected User[] load(final Session session, int index, int length) throws Exception
{
if (index < 0)
throw new IllegalArgumentException("Illegal index: index must be a positive number");
if (length < 0)
throw new IllegalArgumentException("Illegal length: length must be a positive number");
User[] users = new User[length];
Query query = SecurityHelper.doPrivilegedAction(new PrivilegedAction<Query>()
{
public Query run()
{
return session.createQuery(findQuery);
}
});
bindFields(query);
Iterator<Object> results = query.iterate();
for (int p = 0, counter = 0; counter < length; p++)
{
if (!results.hasNext())
throw new IllegalArgumentException(
"Illegal index or length: sum of the index and the length cannot be greater than the list size");
Object result = results.next();
if (p >= index)
{
users[counter++] = (User)result;
}
}
return users;
}
/**
* BindFields.
*
* @param query
* Query
*/
private void bindFields(Query query)
{
for (Entry<String, Object> entry : binding.entrySet())
{
query.setParameter(entry.getKey(), entry.getValue());
}
}
}