/**
 * encuestame:  system online surveys
 * Copyright (C) 2005-2008 encuestame Development Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 3 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */
package org.encuestame.core.persistence.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.encuestame.core.persistence.dao.imp.ISecUserDao;
import org.encuestame.core.persistence.pojo.SecGroupPermission;
import org.encuestame.core.persistence.pojo.SecGroupUser;
import org.encuestame.core.persistence.pojo.SecUserPermission;
import org.encuestame.core.persistence.pojo.SecUsers;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
 * SecUsers.
 *
 * @author Picado, Juan juan@encuestame.org
 * @since May 05, 2009
 */
public class SecUserDaoImp extends AbstractHibernateDaoSupport implements ISecUserDao {

    /**
     * Assing user to group.
     * @param secGroupUser group user
     * @throws HibernateException
     */
    public void assingGroupToUser(final SecGroupUser secGroupUser)
                throws HibernateException {
        getHibernateTemplate().save(secGroupUser);
    }

    /**
     * Find All Users.
     * @return list of all users
     * @throws HibernateException hibernate
     */
    public List<SecUsers> findAll() throws HibernateException {
        return super.findAll("from SecUsers");
    }

    /**
     * Get User By Id.
     * @param userId userId
     * @return SecUsers
     * @throws HibernateException hibernate exception
     */
    public SecUsers getUserById(final Long userId) throws HibernateException{
        return (SecUsers) getSession().get(SecUsers.class, Integer.valueOf(userId.toString()));
    }

    /**
     * Get list of user by username.
     * @param username username
     * @return list of users
     */
    public SecUsers getUserByUsername(final String username)throws HibernateException {
        return  (SecUsers) getSession()
        .createCriteria(SecUsers.class)
        .add(Restrictions.eq("username", username))
        .uniqueResult();
    }

    /**
     * Obtiene una lista de Permisos de los diferentes grupos a los que
     * pertenece
     *
     * @param lista
     *            de grupos
     * @return lista de permisos
     */
    public List<SecGroupPermission> getGroupPermission(List<SecGroupUser> groups) {
        List<SecGroupPermission> listGroupPermission = new ArrayList<SecGroupPermission>();
        Iterator<SecGroupUser> iList = groups.iterator();
        log.info("iniciando el while");
        while (iList.hasNext()) {
            SecGroupUser secGroups = (SecGroupUser) iList.next();
            log.info("secGroups " + secGroups.getSecGroups().getName());
            List<SecGroupPermission> permission = getHibernateTemplate()
                    .findByNamedQuery("User.loadGroupPermission",
                            secGroups.getSecGroups());
            log.info("permission para " + secGroups.getSecGroups().getName()
                    + "->" + permission.size());
            if (permission != null && permission.size() > 0) {
                Iterator<SecGroupPermission> ilistPermission = permission
                        .iterator();
                while (ilistPermission.hasNext()) {
                    SecGroupPermission secPermission = (SecGroupPermission) ilistPermission
                            .next();
                    listGroupPermission.add(secPermission);
                }
            }
        }
        log.info("lista de permisos " + listGroupPermission.size());
        return listGroupPermission;
    }

    /**
     * Get User Permissions.
     * @param user user
     * @return list of permissions
     */
    @SuppressWarnings("unchecked")
    public List<SecUserPermission> getUserPermission(SecUsers user) {
        return getHibernateTemplate()
                .findByNamedParam("from " +
                        "SecUserPermission  where secUsers.uid "
                        +"= :user ", "user", user.getUid());
    }

    /**
     * List of groups for one user.
     * @param username username
     * @return list of user groups
     */
    public List<SecGroupUser> getUserGroups(final SecUsers user) {
        return getHibernateTemplate()
                .findByNamedParam("from SecGroupUser "
                 +" where secUsers = :user ", "user", user);
    }
}

@@ -2886,147 +2886,84 @@
   * 
-Obtien
+Retriev
 e 
-un
 a list
-a de Permisos de los diferentes grupos a los que%0D%0A     * pertenece%0D%0A     *%0D%0A     * @param lista%0D%0A     *            de
+ of permissions by group.%0D%0A     * @param groups list of
  gr
+o
 up
-o
 s%0D%0A 
@@ -2980,29 +2980,32 @@
 urn list
-a de
+ of
  permis
-o
 s
+ions.
 %0D%0A     *
@@ -2999,32 +2999,68 @@
 ions.%0D%0A     */%0D%0A
+    @SuppressWarnings(%22unchecked%22)%0D%0A
     public List%3C
@@ -3098,16 +3098,22 @@
 mission(
+final 
 List%3CSec
@@ -3134,32 +3134,38 @@
 ups) %7B%0D%0A        
+final 
 List%3CSecGroupPer
@@ -3233,32 +3233,38 @@
 on%3E();%0D%0A        
+final 
 Iterator%3CSecGrou
@@ -3302,49 +3302,8 @@
 );%0D%0A
-        log.info(%22iniciando el while%22);%0D%0A
     
@@ -3337,32 +3337,38 @@
  %7B%0D%0A            
+final 
 SecGroupUser sec
@@ -3410,82 +3410,8 @@
 );%0D%0A
-            log.info(%22secGroups %22 + secGroups.getSecGroups().getName());%0D%0A
     
@@ -3516,221 +3516,134 @@
 amed
-Query(%22User.loadGroupPermission%22,%0D%0A                            secGroups.getSecGroups());%0D%0A            log.info(%22permission para %22 + secGroups.getSecGroups().getName()%0D%0A                    + %22-%3E%22 + permission.size
+Param(%22from SecGroupPermission d where d.secGroups = :groupId%22, %22groupId%22,%0D%0A                            secGroups.getSecGroups
 ());
@@ -3894,16 +3894,22 @@
         
+final 
 SecGroup
@@ -4121,78 +4121,8 @@
  %7D%0D%0A
-        log.info(%22lista de permisos %22 + listGroupPermission.size());%0D%0A
     

Fix hql method.