Example usage for org.hibernate.criterion DetachedCriteria forEntityName

List of usage examples for org.hibernate.criterion DetachedCriteria forEntityName

Introduction

In this page you can find the example usage for org.hibernate.criterion DetachedCriteria forEntityName.

Prototype

@SuppressWarnings("UnusedDeclaration")
public static DetachedCriteria forEntityName(String entityName) 

Source Link

Document

Static builder to create a DetachedCriteria for the given entity.

Usage

From source file:ro.cs.om.model.dao.impl.DaoPersonImpl.java

License:Open Source License

/**
 * Searches for persons after criterion from searchPersonBean.
 * /*from w  w w  .j a v a 2s .  c  o  m*/
 * @author alu
 * 
 * @return A list of log beans 
 * @throws ParseException 
 */
public List<Person> getFromSearch(SearchPersonBean searchPersonBean, boolean isDeleteAction, List<Integer> orgs)
        throws ParseException {
    logger.debug("getFromSearch - START");
    /*Once a Projection is being set to a Detached Criteria object, it cannot be removed anymore, so two identical DetachedCriteria objects 
    must be created: 
    -dcCount ( on which the projection is being set )used to retrieve the number of distinct results which is set when 
    the request didn't come from the pagination area and needed further more to set the current page after a delete action; 
    -dc used to retrieve the result set after the current page has been set in case of a delete action
    */

    // set search criterion
    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.personForListingEntity);
    DetachedCriteria dcCount = DetachedCriteria.forEntityName(IModelConstant.personForListingEntity);

    // Status
    dc.add(Restrictions.ne("status", IConstant.NOM_PERSON_STATUS_DELETED));
    dcCount.add(Restrictions.ne("status", IConstant.NOM_PERSON_STATUS_DELETED));

    if (Tools.getInstance().stringNotEmpty(searchPersonBean.getFirstName())) {
        dc.add(Restrictions.ilike("firstName", "%".concat(searchPersonBean.getFirstName()).concat("%")));
        dcCount.add(Restrictions.ilike("firstName", "%".concat(searchPersonBean.getFirstName()).concat("%")));
        logger.debug("firstName: " + searchPersonBean.getFirstName());
    }

    if (Tools.getInstance().stringNotEmpty(searchPersonBean.getLastName())) {
        dc.add(Restrictions.ilike("lastName", "%".concat(searchPersonBean.getLastName()).concat("%")));
        dcCount.add(Restrictions.ilike("lastName", "%".concat(searchPersonBean.getLastName()).concat("%")));
        logger.debug("lastName: " + searchPersonBean.getLastName());
    }

    if (Tools.getInstance().stringNotEmpty(searchPersonBean.getUsername())) {
        dc.add(Restrictions.ilike("username", "%".concat(searchPersonBean.getUsername()).concat("%")));
        dcCount.add(Restrictions.ilike("username", "%".concat(searchPersonBean.getUsername()).concat("%")));
        logger.debug("username: " + searchPersonBean.getUsername());
    }

    if (searchPersonBean.getDepartmentId() > 0) {
        dc.createCriteria("depts")
                .add(Restrictions.eq("departmentId", new Integer(searchPersonBean.getDepartmentId())));
        dcCount.createCriteria("depts")
                .add(Restrictions.eq("departmentId", new Integer(searchPersonBean.getDepartmentId())));
        logger.debug("departmentId: " + searchPersonBean.getDepartmentId());

    } else if (searchPersonBean.getOrganisationId() > 0) {
        //if the search is in all branches we use it
        if (orgs != null) {
            dc.createCriteria("depts").add(Restrictions.in("organisationId", orgs));
            dcCount.createCriteria("depts").add(Restrictions.in("organisationId", orgs));
            logger.debug("number of branches: " + orgs.size());
        } else {
            dc.createCriteria("depts")
                    .add(Restrictions.eq("organisationId", new Integer(searchPersonBean.getOrganisationId())));
            dcCount.createCriteria("depts")
                    .add(Restrictions.eq("organisationId", new Integer(searchPersonBean.getOrganisationId())));
            logger.debug("organisationId: " + searchPersonBean.getOrganisationId());
        }

    }

    if (searchPersonBean.getSex() != null) {
        if (searchPersonBean.getSex().equals(IConstant.NOM_PERSON_SEX_F)
                || searchPersonBean.getSex().equals(IConstant.NOM_PERSON_SEX_M)) {
            dc.add(Restrictions.eq("sex", searchPersonBean.getSex()));
            dcCount.add(Restrictions.eq("sex", searchPersonBean.getSex()));
            logger.debug("sex: " + searchPersonBean.getSex());
        }
    }

    dc.setProjection(Projections.id());
    dcCount.setProjection(Projections.id());
    // until here, I've created the subquery
    // now, it's time to retrive all the persons that are in the list of the subquery
    DetachedCriteria dc1 = DetachedCriteria.forEntityName(IModelConstant.personForListingEntity);
    dc1.add(Subqueries.propertyIn("personId", dc));

    // check if I have to order the results
    if (searchPersonBean.getSortParam() != null && StringUtils.hasLength(searchPersonBean.getSortParam())) {
        // if I have to, check if I have to order them ascending or descending
        if (searchPersonBean.getSortDirection() == IConstant.ASCENDING) {
            // ascending
            dc1.addOrder(Order.asc(searchPersonBean.getSortParam()));
        } else {
            // descending
            dc1.addOrder(Order.desc(searchPersonBean.getSortParam()));
        }
    }

    // if the request didn't come from the pagination area, 
    // it means that I have to set the number of result and pages
    if (isDeleteAction || searchPersonBean.getNbrOfResults() == -1) {
        boolean isSearch = false;
        if (searchPersonBean.getNbrOfResults() == -1) {
            isSearch = true;
        }
        // set the countDistinct restriction
        dcCount.setProjection(Projections.countDistinct("personId"));

        int nbrOfResults = ((Integer) getHibernateTemplate().findByCriteria(dcCount, 0, 0).get(0)).intValue();
        searchPersonBean.setNbrOfResults(nbrOfResults);
        logger.debug("NbrOfResults " + searchPersonBean.getNbrOfResults());
        logger.debug("----> searchPersonBean.getResults " + searchPersonBean.getResultsPerPage());
        // get the number of pages
        if (nbrOfResults % searchPersonBean.getResultsPerPage() == 0) {
            searchPersonBean.setNbrOfPages(nbrOfResults / searchPersonBean.getResultsPerPage());
        } else {
            searchPersonBean.setNbrOfPages(nbrOfResults / searchPersonBean.getResultsPerPage() + 1);
        }
        // after a person is deleted, the same page has to be displayed;
        //only when all the persons from last page are deleted, the previous page will be shown 
        if (isDeleteAction && (searchPersonBean.getCurrentPage() > searchPersonBean.getNbrOfPages())) {
            searchPersonBean.setCurrentPage(searchPersonBean.getNbrOfPages());
        } else if (isSearch) {
            searchPersonBean.setCurrentPage(1);
        }
    }

    List<Person> res = getHibernateTemplate().findByCriteria(dc1,
            (searchPersonBean.getCurrentPage() - 1) * searchPersonBean.getResultsPerPage(),
            searchPersonBean.getResultsPerPage());

    logger.debug("Res " + res.size());
    logger.debug("getFromSearch - END - results size : ".concat(String.valueOf(res.size())));
    return res;
}

From source file:ro.cs.om.model.dao.impl.DaoPersonImpl.java

License:Open Source License

/**
 * Return a list with all the Persons from the given Organization and Module and all the persons from organization
 * //from  w ww  . j  av a  2  s. co  m
 * @author mitziuro
 * 
 * @param organizationId
 * @param moduleId
 * @return
 */
public Entry<List<Integer>, List<Person>> getPersonsIdsByOrganizationIdAndModuleId(int organizationId,
        int moduleId) {
    logger.debug("getPersonsIdsByOrganizationIdAndModuleId START: organizationId = "
            .concat(String.valueOf(organizationId).concat(" moduleId = ").concat(String.valueOf(moduleId))));

    List<Integer> persons = null;
    List<Person> allPersons = null;

    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.personForListingEntity);
    //we search for organisation id

    dc.createCriteria("depts").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .add(Restrictions.eq("organisationId", new Integer(organizationId)));

    dc.add(Restrictions.ne("status", IConstant.NOM_PERSON_STATUS_DELETED));

    //we search
    allPersons = getHibernateTemplate().findByCriteria(dc);

    DetachedCriteria dcp = DetachedCriteria.forEntityName(IModelConstant.personForListingEntity);
    //we search for organisation id
    dcp.createCriteria("depts").add(Restrictions.eq("organisationId", new Integer(organizationId)));
    dcp.add(Restrictions.ne("status", IConstant.NOM_PERSON_STATUS_DELETED));

    //distinct results
    dcp.setProjection(Projections
            .distinct(Projections.projectionList().add(Projections.property("personId"), "personId")));
    //we search for module id
    dcp.createCriteria("roles").add(Restrictions.eq("moduleId", new Integer(moduleId)));

    //we search
    persons = getHibernateTemplate().findByCriteria(dcp);

    logger.debug("getPersonsIdsByOrganizationIdAndModuleId END: size = "
            .concat(persons != null ? String.valueOf(allPersons.size()) : "null"));

    return new SimpleEntry(persons, allPersons);
}

From source file:ro.cs.om.model.dao.impl.DaoPersonImpl.java

License:Open Source License

/**
 * Return a list with all the deactivated Persons from the given Organization and Module and all the persons from organization
 * //  ww  w. j a  v  a  2 s . c  o m
 * @author liviu
 * 
 * @param organizationId
 * @param moduleId
 * @return
 */
public Entry<List<Integer>, List<Person>> getDeletedPersonsIdsByOrganizationIdAndModuleId(int organizationId,
        int moduleId) {
    logger.debug("getDeletedPersonsIdsByOrganizationIdAndModuleId START: organizationId = "
            .concat(String.valueOf(organizationId).concat(" moduleId = ").concat(String.valueOf(moduleId))));

    List<Integer> persons = null;
    List<Person> allPersons = null;

    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.personForListingEntity);
    //we search for organisation id
    dc.createCriteria("depts").add(Restrictions.eq("organisationId", new Integer(organizationId)));
    dc.add(Restrictions.eq("status", IConstant.NOM_PERSON_STATUS_DELETED));

    //we search
    allPersons = getHibernateTemplate().findByCriteria(dc);

    //we search for module id
    dc.createCriteria("roles").add(Restrictions.eq("moduleId", new Integer(moduleId)));

    //distinct results
    dc.setProjection(Projections
            .distinct(Projections.projectionList().add(Projections.property("personId"), "personId")));

    //we search
    persons = getHibernateTemplate().findByCriteria(dc);

    logger.debug("getDeletedPersonsIdsByOrganizationIdAndModuleId END: size = "
            .concat(persons != null ? String.valueOf(persons.size()) : "null"));

    return new SimpleEntry(persons, allPersons);
}

From source file:ro.cs.om.model.dao.impl.DaoPersonImpl.java

License:Open Source License

/**
 * Return a list of person from a given organization,
 * that has a certain role//  w w  w . j a  va 2 s  . c  om
 * 
 * @author Adelina
 * 
 * @param organisationId
 * @param roleName
 * @return
 */
public List<Person> getPersonsWithRoleByOrganisation(Integer organisationId, String roleName) {
    logger.debug("getPersonsWithRoleByOrganisation - START: organizationId = "
            .concat(String.valueOf(organisationId)));

    List<Person> persons = null;
    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.personForListingEntity);
    dc.add(Restrictions.ne("status", IConstant.NOM_PERSON_STATUS_DELETED));

    // we search for the organizationId
    dc.createCriteria("depts").add(Restrictions.eq("organisationId", new Integer(organisationId)));

    // and roleName
    dc.createCriteria("roles").add(Restrictions.eq("name", roleName));

    // the search
    persons = (List<Person>) getHibernateTemplate().findByCriteria(dc);
    logger.debug("getPersonsWithRoleByOrganisation - END");
    return persons;
}

From source file:ro.cs.om.model.dao.impl.DaoPersonImpl.java

License:Open Source License

/**
 * Returns a list of the persons from the user group with the specified id
 * /*  w  ww .ja v a 2s.  com*/
 * @author Coni
 * 
 * @param userGroupId
 * @return
 */
public List<Person> getByUserGroupId(Integer userGroupId) {
    logger.debug("getByUserGroupId - START - user group id: ".concat(userGroupId.toString()));

    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.personWithUserGroupsEntity);
    dc.add(Restrictions.ne("status", IConstant.NOM_PERSON_STATUS_DELETED));
    dc.createCriteria("userGroups").add(Restrictions.eq("userGroupId", userGroupId));

    List<Person> persons = (List<Person>) getHibernateTemplate().findByCriteria(dc);
    logger.debug("getByUserGroupId - START");
    return persons;
}

From source file:ro.cs.om.model.dao.impl.DaoPersonImpl.java

License:Open Source License

/**
 * Returns persons identified by personId. Person contains general info attributes.
 * //  w  w  w.j  a v  a 2s  . c o m
 * @author Coni
 */

public List<Person> getByPersonId(Integer[] personIds) {
    logger.debug("getByPersonId - START");
    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.personEntity);

    dc.add(Restrictions.in("personId", personIds));

    List<Person> persons = (List<Person>) getHibernateTemplate().findByCriteria(dc);
    logger.debug("getByPersonId - END");
    return persons;
}

From source file:ro.cs.om.model.dao.impl.DaoPersonImpl.java

License:Open Source License

/**
 * Retrieves from search a list of person with basic info 
 * //from w w  w .jav  a  2  s .com
 * @author Coni
 * @param searchPersonBean
 * @return
 */
public List<Person> getFromSearchSimple(SearchPersonBean searchPersonBean, boolean withDeleted) {
    logger.debug("getFromSearchSimple - START");

    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.personFromOrganisationEntity);

    // Status; in all the other modules the disabled persons are treated the same as the deleted persons
    if (!withDeleted) {
        dc.add(Restrictions.eq("status", IConstant.NOM_PERSON_STATUS_ACTIVATED));
    }

    if (Tools.getInstance().stringNotEmpty(searchPersonBean.getFirstName())) {
        dc.add(Restrictions.ilike("firstName", "%".concat(searchPersonBean.getFirstName()).concat("%")));
        logger.debug("firstName: " + searchPersonBean.getFirstName());
    }

    if (Tools.getInstance().stringNotEmpty(searchPersonBean.getLastName())) {
        dc.add(Restrictions.ilike("lastName", "%".concat(searchPersonBean.getLastName()).concat("%")));
        logger.debug("lastName: " + searchPersonBean.getLastName());
    }

    if (Tools.getInstance().stringNotEmpty(searchPersonBean.getUsername())) {
        dc.add(Restrictions.ilike("username", "%".concat(searchPersonBean.getUsername()).concat("%")));
        logger.debug("username: " + searchPersonBean.getUsername());
    }

    if (searchPersonBean.getOrganisationId() > 0) {
        dc.createCriteria("depts")
                .add(Restrictions.eq("organisationId", new Integer(searchPersonBean.getOrganisationId())));
        logger.debug("organizationId: " + searchPersonBean.getOrganisationId());
    }

    List<Person> res = getHibernateTemplate().findByCriteria(dc);

    logger.debug("getFromSearchSimple - END");
    return res;
}

From source file:ro.cs.om.model.dao.impl.DaoPersonImpl.java

License:Open Source License

/**
 * Retrieves from search a list of person with basic info and pagination
 * /*from   ww w.  j  a  v  a  2  s  . com*/
 * @author Coni
 * @param searchPersonBean
 * @return
 */
public List<Person> getFromSearchSimpleWithPagination(SearchPersonBean searchPersonBean, boolean withDeleted) {
    logger.debug("getFromSearch - START");

    // set search criterion
    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.personFromOrganisationEntity);
    DetachedCriteria dcCount = DetachedCriteria.forEntityName(IModelConstant.personFromOrganisationEntity);

    // Status; in all the other modules the disabled persons are treated the same as the deleted persons
    if (!withDeleted) {
        dc.add(Restrictions.eq("status", IConstant.NOM_PERSON_STATUS_ACTIVATED));
        dcCount.add(Restrictions.eq("status", IConstant.NOM_PERSON_STATUS_ACTIVATED));
    }

    if (Tools.getInstance().stringNotEmpty(searchPersonBean.getFirstName())) {
        dc.add(Restrictions.ilike("firstName", "%".concat(searchPersonBean.getFirstName()).concat("%")));
        dcCount.add(Restrictions.ilike("firstName", "%".concat(searchPersonBean.getFirstName()).concat("%")));
        logger.debug("firstName: " + searchPersonBean.getFirstName());
    }

    if (Tools.getInstance().stringNotEmpty(searchPersonBean.getLastName())) {
        dc.add(Restrictions.ilike("lastName", "%".concat(searchPersonBean.getLastName()).concat("%")));
        dcCount.add(Restrictions.ilike("lastName", "%".concat(searchPersonBean.getLastName()).concat("%")));
        logger.debug("lastName: " + searchPersonBean.getLastName());
    }

    if (Tools.getInstance().stringNotEmpty(searchPersonBean.getUsername())) {
        dc.add(Restrictions.ilike("username", "%".concat(searchPersonBean.getUsername()).concat("%")));
        dcCount.add(Restrictions.ilike("username", "%".concat(searchPersonBean.getUsername()).concat("%")));
        logger.debug("username: " + searchPersonBean.getUsername());
    }

    if (searchPersonBean.getDepartmentId() > 0) {
        dc.createCriteria("depts")
                .add(Restrictions.eq("departmentId", new Integer(searchPersonBean.getDepartmentId())));
        dcCount.createCriteria("depts")
                .add(Restrictions.eq("departmentId", new Integer(searchPersonBean.getDepartmentId())));
        logger.debug("departmentId: " + searchPersonBean.getDepartmentId());

    } else if (searchPersonBean.getOrganisationId() > 0) {
        //if the search is in all branches we use it
        dc.createCriteria("depts")
                .add(Restrictions.eq("organisationId", new Integer(searchPersonBean.getOrganisationId())));
        dcCount.createCriteria("depts")
                .add(Restrictions.eq("organisationId", new Integer(searchPersonBean.getOrganisationId())));
        logger.debug("organisationId: " + searchPersonBean.getOrganisationId());
    }

    if (searchPersonBean.getSex() != null) {
        if (searchPersonBean.getSex().equals(IConstant.NOM_PERSON_SEX_F)
                || searchPersonBean.getSex().equals(IConstant.NOM_PERSON_SEX_M)) {
            dc.add(Restrictions.eq("sex", searchPersonBean.getSex()));
            dcCount.add(Restrictions.eq("sex", searchPersonBean.getSex()));
            logger.debug("sex: " + searchPersonBean.getSex());
        }
    }

    dc.setProjection(Projections.id());
    dcCount.setProjection(Projections.id());
    // until here, I've created the subquery
    // now, it's time to retrive all the persons that are in the list of the subquery
    DetachedCriteria dc1 = DetachedCriteria.forEntityName(IModelConstant.personFromOrganisationEntity);
    dc1.add(Subqueries.propertyIn("personId", dc));

    // check if I have to order the results
    if (searchPersonBean.getSortParam() != null && StringUtils.hasLength(searchPersonBean.getSortParam())) {
        // if I have to, check if I have to order them ascending or descending
        if (searchPersonBean.getSortDirection() == IConstant.ASCENDING) {
            // ascending
            dc1.addOrder(Order.asc(searchPersonBean.getSortParam()));
        } else {
            // descending
            dc1.addOrder(Order.desc(searchPersonBean.getSortParam()));
        }
    }

    // if the request didn't come from the pagination area, 
    // it means that I have to set the number of result and pages
    if (searchPersonBean.getNbrOfResults() == -1) {
        // set the countDistinct restriction
        dcCount.setProjection(Projections.countDistinct("personId"));

        int nbrOfResults = ((Integer) getHibernateTemplate().findByCriteria(dcCount, 0, 0).get(0)).intValue();
        searchPersonBean.setNbrOfResults(nbrOfResults);
        logger.debug("NbrOfResults " + searchPersonBean.getNbrOfResults());
        logger.debug("----> searchPersonBean.getResults " + searchPersonBean.getResultsPerPage());
        // get the number of pages
        if (nbrOfResults % searchPersonBean.getResultsPerPage() == 0) {
            searchPersonBean.setNbrOfPages(nbrOfResults / searchPersonBean.getResultsPerPage());
        } else {
            searchPersonBean.setNbrOfPages(nbrOfResults / searchPersonBean.getResultsPerPage() + 1);
        }
        searchPersonBean.setCurrentPage(1);
    }

    List<Person> res = getHibernateTemplate().findByCriteria(dc1,
            (searchPersonBean.getCurrentPage() - 1) * searchPersonBean.getResultsPerPage(),
            searchPersonBean.getResultsPerPage());

    logger.debug("Res " + res.size());
    logger.debug("getFromSearch - END - results size : ".concat(String.valueOf(res.size())));
    return res;
}

From source file:ro.cs.om.model.dao.impl.DaoRoleImpl.java

License:Open Source License

/**
 * Searches for roles after criterion from searchRoleBean
 * @author alu//from   www  . jav  a2  s. c om
 * @return A list of role beans 
 * @throws ParseException 
 */
public List<Role> getRoleBeanFromSearch(SearchRoleBean searchRoleBean, boolean isDeleteAction,
        Set<Integer> modulesIds) throws ParseException {

    logger.debug("getRoleBeanFromSearch - START - name:".concat(searchRoleBean.getName()).concat(" moduleId - ")
            .concat(String.valueOf(searchRoleBean.getModuleId())).concat(" orgId - ")
            .concat(String.valueOf(searchRoleBean.getOrganisationId())));
    /*Once a Projection is being set to a Detached Criteria object, it cannot be removed anymore, so two identical DetachedCriteria objects 
    must be created: 
    -dcCount ( on which the projection is being set )used to retrieve the number of distinct results which is set when 
    the request didn't come from the pagination area and needed further more to set the current page after a delete action; 
    -dc used to retrieve the result set after the current page has been set in case of a delete action
    */

    // set search criterion
    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.roleForListingEntity);
    DetachedCriteria dcCount = DetachedCriteria.forEntityName(IModelConstant.roleForListingEntity);

    if (searchRoleBean.getName() != null && !"".equals(searchRoleBean.getName())) {
        dc.add(Restrictions.ilike("name", "%".concat(searchRoleBean.getName()).concat("%")));
        dcCount.add(Restrictions.ilike("name", "%".concat(searchRoleBean.getName()).concat("%")));
    }
    if (searchRoleBean.getModuleId() != -1) {
        dc.add(Restrictions.eq("module.moduleId", searchRoleBean.getModuleId()));
        dcCount.add(Restrictions.eq("module.moduleId", searchRoleBean.getModuleId()));
    } else {
        dc.add(Restrictions.in("module.moduleId", modulesIds));
        dcCount.add(Restrictions.in("module.moduleId", modulesIds));
    }
    if (searchRoleBean.getOrganisationId() != -1) {
        dc.add(Restrictions.eq("organisation.organisationId", searchRoleBean.getOrganisationId()));
        dcCount.add(Restrictions.eq("organisation.organisationId", searchRoleBean.getOrganisationId()));
    }

    // check if I have to order the results
    if (searchRoleBean.getSortParam() != null && !"".equals(searchRoleBean.getSortParam())) {
        // if I have to, check if I have to order them ascending or descending
        if (searchRoleBean.getSortDirection() == -1) {
            // ascending
            dc.addOrder(Order.asc(searchRoleBean.getSortParam()));
        } else {
            // descending
            dc.addOrder(Order.desc(searchRoleBean.getSortParam()));
        }
    }

    // if the request didn't come from the pagination area, 
    // it means that I have to set the number of results and pages
    if (isDeleteAction || searchRoleBean.getNbrOfResults() == -1) {
        boolean isSearch = false;
        if (searchRoleBean.getNbrOfResults() == -1) {
            isSearch = true;
        }
        // set the count(*) restriction         
        dcCount.setProjection(Projections.countDistinct("roleId"));

        //findByCriteria must be called with firstResult and maxResults parameters; the default findByCriteria(DetachedCriteria criteria) implementation
        //sets firstResult and maxResults to -1, which kills the countDistinct Projection         
        int nbrOfResults = ((Integer) getHibernateTemplate().findByCriteria(dcCount, 0, 0).get(0)).intValue();
        logger.debug("search results: ".concat(String.valueOf(nbrOfResults)));
        searchRoleBean.setNbrOfResults(nbrOfResults);

        // get the number of pages
        if (nbrOfResults % searchRoleBean.getResultsPerPage() == 0) {
            searchRoleBean.setNbrOfPages(nbrOfResults / searchRoleBean.getResultsPerPage());
        } else {
            searchRoleBean.setNbrOfPages(nbrOfResults / searchRoleBean.getResultsPerPage() + 1);
        }
        // after a role is deleted, the same page has to be displayed;
        //only when all the roles from last page are deleted, the previous page will be shown 
        if (isDeleteAction && (searchRoleBean.getCurrentPage() > searchRoleBean.getNbrOfPages())) {
            searchRoleBean.setCurrentPage(searchRoleBean.getNbrOfPages());
        } else if (isSearch) {
            searchRoleBean.setCurrentPage(1);
        }

    }

    List<Role> res = (List<Role>) getHibernateTemplate().findByCriteria(dc,
            (searchRoleBean.getCurrentPage() - 1) * searchRoleBean.getResultsPerPage(),
            searchRoleBean.getResultsPerPage());
    logger.debug("getRoleBeanFromSearch - END results size : ".concat(String.valueOf(res.size())));
    return res;
}

From source file:ro.cs.om.model.dao.impl.DaoRoleImpl.java

License:Open Source License

/**
 * Gets the default roles (those that have status = 2)
 * @author Adelina//from   ww w  .  ja  v a2s.c  o m
 * @return List<RoleWeb>
 */
@SuppressWarnings("unchecked")
public List<RoleWeb> getDefaultRolesByModule(Integer moduleId) {
    logger.debug("getDefaultRoles DAO IMPL - START - ");

    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.roleWebEntity);
    dc.add(Restrictions.eq("status", (int) IConstant.ROLE_STATUS_DEFAULT));
    dc.add(Restrictions.eq("moduleId", moduleId));
    List roles = getHibernateTemplate().findByCriteria(dc);
    logger.debug("getDefaultRoles DAO IMPL - END - ".concat(String.valueOf(roles.size())));

    return roles;
}