com.bookselling.dao.RoleDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.bookselling.dao.RoleDaoImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bookselling.dao;

import com.bookselling.domain.Permission;
import com.bookselling.domain.Role;
import com.bookselling.form.filter.RoleFilterForm;
import com.bookselling.util.PaginationData;
import java.util.List;
import java.util.Set;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Phan Phat
 */
@Repository
public class RoleDaoImpl extends GenericDao<Role> implements RoleDao {

    @Override
    public Class<Role> registeredClass() {
        return Role.class;
    }

    @Override
    public PaginationData filter(RoleFilterForm criteriaForm, int first, int items) {
        //parse to neccessary form
        RoleFilterForm form = criteriaForm;

        Criteria criteria = getSession().createCriteria(Role.class);

        //get data from filter form
        String keyword = form.getKeyword();
        Set<Permission> permissions = form.getPermissions();
        Integer permissionIds[] = new Integer[permissions.size()];
        for (int i = 0; i < permissions.size(); i++)
            permissionIds[i] = permissions.iterator().next().getId();

        //setup criteria
        criteria.createAlias("permissions", "psm").add(Restrictions.like("name", "%" + keyword + "%"))
                .add(Restrictions.in("psm.id", permissionIds));

        //start querying
        List<Role> roles = criteria.list();

        //init proxy obj
        for (Role role : roles)
            HibernateInitSupport.initRole(role);

        //pagination
        PaginationData paginationData = new PaginationData(
                (long) criteria.setProjection(Projections.rowCount()).uniqueResult(), items, first, roles);

        return paginationData;
    }
}