Example usage for org.hibernate.criterion Projections countDistinct

List of usage examples for org.hibernate.criterion Projections countDistinct

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections countDistinct.

Prototype

public static CountProjection countDistinct(String propertyName) 

Source Link

Document

A distinct property value count projection

Usage

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

License:Open Source License

/**
* Searches for Departments after criterion from searchOrganisationBean.
* 
* @author alu//from w w w.  j av  a  2 s.c om
* @author dan.damian
*/
public List<Department> getFromSearch(SearchDepartmentBean searchDepartmentBean, boolean isDeleteAction)
        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.departmentAllEntity);
    DetachedCriteria dcCount = DetachedCriteria.forEntityName(IModelConstant.departmentAllEntity);

    dc.createAlias("manager", "manager", Criteria.LEFT_JOIN);
    dcCount.createAlias("manager", "manager", Criteria.LEFT_JOIN);

    if (Tools.getInstance().stringNotEmpty(searchDepartmentBean.getName())) {
        dc.add(Restrictions.ilike("name", "%".concat(searchDepartmentBean.getName()).concat("%")));
        dcCount.add(Restrictions.ilike("name", "%".concat(searchDepartmentBean.getName()).concat("%")));
        logger.debug("name: " + searchDepartmentBean.getName());
    }

    if (searchDepartmentBean.getOrganisationId() != -1) {
        dc.add(Restrictions.eq("organisation.organisationId", searchDepartmentBean.getOrganisationId()));
        dcCount.add(Restrictions.eq("organisation.organisationId", searchDepartmentBean.getOrganisationId()));
        logger.debug("Organisation Id: " + searchDepartmentBean.getOrganisationId());
    }

    if (searchDepartmentBean.getManagerFirstName() != null
            && !"".equals(searchDepartmentBean.getManagerFirstName())) {
        dc.add(Restrictions.eq("manager.firstName", searchDepartmentBean.getManagerFirstName()));
        dcCount.add(Restrictions.eq("manager.firstName", searchDepartmentBean.getManagerFirstName()));
        logger.debug("Manager first name: ".concat(searchDepartmentBean.getManagerFirstName()));
    }

    if (searchDepartmentBean.getManagerLastName() != null
            && !"".equals(searchDepartmentBean.getManagerLastName())) {
        dc.add(Restrictions.eq("manager.lastName", searchDepartmentBean.getManagerLastName()));
        dcCount.add(Restrictions.eq("manager.lastName", searchDepartmentBean.getManagerLastName()));
        logger.debug("Manager last name: ".concat(searchDepartmentBean.getManagerLastName()));
    }

    if (Tools.getInstance().stringNotEmpty(searchDepartmentBean.getParentDepartmentName())) {
        dc.createCriteria("parentDepartment").add(Restrictions.ilike("name",
                "%".concat(searchDepartmentBean.getParentDepartmentName()).concat("%")));
        dcCount.createCriteria("parentDepartment").add(Restrictions.ilike("name",
                "%".concat(searchDepartmentBean.getParentDepartmentName()).concat("%")));
        logger.debug("Parent Department Name: " + searchDepartmentBean.getParentDepartmentName());
    }

    if (searchDepartmentBean.getParentDepartmentId() != -1) {
        dc.add(Restrictions.eq("parentDepartment.departmentId", searchDepartmentBean.getParentDepartmentId()));
        dcCount.add(
                Restrictions.eq("parentDepartment.departmentId", searchDepartmentBean.getParentDepartmentId()));
        logger.debug("Parent Department Id: " + searchDepartmentBean.getParentDepartmentId());
    }

    dc.add(Restrictions.eq("status", IConstant.NOM_DEPARTMENT_ACTIVE));
    dcCount.add(Restrictions.eq("status", IConstant.NOM_DEPARTMENT_ACTIVE));

    // check if I have to order the results
    if (searchDepartmentBean.getSortParam() != null && !"".equals(searchDepartmentBean.getSortParam())) {
        // if I have to, check if I have to order them ascending or descending
        if (searchDepartmentBean.getSortDirection() == -1) {
            // ascending
            dc.addOrder(Order.asc(searchDepartmentBean.getSortParam()));
        } else {
            // descending
            dc.addOrder(Order.desc(searchDepartmentBean.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 || searchDepartmentBean.getNbrOfResults() == -1) {
        boolean isSearch = false;
        if (searchDepartmentBean.getNbrOfResults() == -1) {
            isSearch = true;
        }
        // set the countDistinct restriction
        dcCount.setProjection(Projections.distinct(Projections.countDistinct("departmentId")));

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

    List<Department> res = getHibernateTemplate().findByCriteria(dc,
            (searchDepartmentBean.getCurrentPage() - 1) * searchDepartmentBean.getResultsPerPage(),
            searchDepartmentBean.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.DaoJobImpl.java

License:Open Source License

/**
 * Returns a list of jobs/*from  w w  w.  j a  v  a2 s.  c  o  m*/
 * 
 * @author mitziuro
 */
public List<Job> getJobBeanFromSearch(SearchJobBean searchJobBean, boolean isChangeAction) {
    logger.debug("getJobBeanFromSearch - START - name:".concat(searchJobBean.getName()).concat(" orgId - ")
            .concat(String.valueOf(searchJobBean.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.jobForListingEntity);
    DetachedCriteria dcCount = DetachedCriteria.forEntityName(IModelConstant.jobForListingEntity);

    if (searchJobBean.getName() != null && !"".equals(searchJobBean.getName())) {
        dc.add(Restrictions.ilike("name", "%".concat(searchJobBean.getName()).concat("%")));
        dcCount.add(Restrictions.ilike("name", "%".concat(searchJobBean.getName()).concat("%")));
    }

    if (searchJobBean.getOrganisationId() != -1) {
        logger.debug("getJobBeanFromSearch - START - name:" + searchJobBean.getOrganisationId());
        dc.add(Restrictions.eq("organisation.organisationId", searchJobBean.getOrganisationId()));
        dcCount.add(Restrictions.eq("organisation.organisationId", searchJobBean.getOrganisationId()));
    }

    if (searchJobBean.getStatus() != -1) {
        dc.add(Restrictions.eq("status", searchJobBean.getStatus()));
        dcCount.add(Restrictions.eq("status", searchJobBean.getStatus()));
    }

    // check if I have to order the results
    if (searchJobBean.getSortParam() != null && !"".equals(searchJobBean.getSortParam())) {
        // if I have to, check if I have to order them ascending or
        // descending
        if (searchJobBean.getSortDirection() == -1) {
            // ascending
            dc.addOrder(Order.asc(searchJobBean.getSortParam()));
        } else {
            // descending
            dc.addOrder(Order.desc(searchJobBean.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 (isChangeAction || searchJobBean.getNbrOfResults() == -1) {
        boolean isSearch = false;
        if (searchJobBean.getNbrOfResults() == -1) {
            isSearch = true;
        }

        // set the count(*) restriction
        dcCount.setProjection(Projections.countDistinct("jobId"));

        //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)));
        searchJobBean.setNbrOfResults(nbrOfResults);

        // get the number of pages
        if (nbrOfResults % searchJobBean.getResultsPerPage() == 0) {
            searchJobBean.setNbrOfPages(nbrOfResults / searchJobBean.getResultsPerPage());
        } else {
            searchJobBean.setNbrOfPages(nbrOfResults / searchJobBean.getResultsPerPage() + 1);
        }

        // after a job is deleted, the same page has to be displayed;
        //only when all the jobs from last page are deleted, the previous page will be shown 
        if (isChangeAction && (searchJobBean.getCurrentPage() > searchJobBean.getNbrOfPages())) {
            searchJobBean.setCurrentPage(searchJobBean.getNbrOfPages());
        } else if (isSearch) {
            searchJobBean.setCurrentPage(1);
        }
    }
    List<Job> res = (List<Job>) getHibernateTemplate().findByCriteria(dc,
            (searchJobBean.getCurrentPage() - 1) * searchJobBean.getResultsPerPage(),
            searchJobBean.getResultsPerPage());

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

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

License:Open Source License

/**
* Searches for out of office profiles after criterion from searchOOOBean
* @author alu//from   w  w  w  . j  a v a 2s  .  c  o m
* @return A list of ooo beans 
* @throws ParseException 
*/
public List getOOOBeanFromSearch(SearchOOOBean searchOOOBean, boolean isDeleteAction) throws ParseException {
    logger.debug("getOOOBeanFromSearch - 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.oooAllEntity);
    DetachedCriteria dcCount = DetachedCriteria.forEntityName(IModelConstant.oooAllEntity);
    dc.createAlias("person", "person");
    dcCount.createAlias("person", "person");
    if (searchOOOBean.getOrganisationId() != -1) {
        dc.createCriteria("person.depts")
                .add(Restrictions.eq("organisationId", searchOOOBean.getOrganisationId()));
        dcCount.createCriteria("person.depts")
                .add(Restrictions.eq("organisationId", searchOOOBean.getOrganisationId()));
    }

    if (searchOOOBean.getOwnerFirstName() != null && !"".equals(searchOOOBean.getOwnerFirstName())) {
        dc.add(Restrictions.eq("person.firstName", searchOOOBean.getOwnerFirstName()));
        dcCount.add(Restrictions.eq("person.firstName", searchOOOBean.getOwnerFirstName()));
        logger.debug("Owner first name: ".concat(searchOOOBean.getOwnerFirstName()));
    }
    if (searchOOOBean.getOwnerLastName() != null && !"".equals(searchOOOBean.getOwnerLastName())) {
        dc.add(Restrictions.eq("person.lastName", searchOOOBean.getOwnerLastName()));
        dcCount.add(Restrictions.eq("person.lastName", searchOOOBean.getOwnerLastName()));
        logger.debug("Owner last name: ".concat(searchOOOBean.getOwnerLastName()));
    }

    if (searchOOOBean.getReplacementFirstName() != null && !"".equals(searchOOOBean.getReplacementFirstName())
            && searchOOOBean.getReplacementLastName() != null
            && !"".equals(searchOOOBean.getReplacementLastName())) {
        dc.createCriteria("personReplacement")
                .add(Restrictions.eq("firstName", searchOOOBean.getReplacementFirstName()))
                .add(Restrictions.eq("lastName", searchOOOBean.getReplacementLastName()));
        dcCount.createCriteria("personReplacement")
                .add(Restrictions.eq("firstName", searchOOOBean.getReplacementFirstName()))
                .add(Restrictions.eq("lastName", searchOOOBean.getReplacementLastName()));
    }

    if (searchOOOBean.getStartPeriod() != null) {
        dc.add(Expression.ge("startPeriod", searchOOOBean.getStartPeriod()));
        dcCount.add(Expression.ge("startPeriod", searchOOOBean.getStartPeriod()));
    }

    if (searchOOOBean.getEndPeriod() != null) {
        dc.add(Expression.le("endPeriod", searchOOOBean.getEndPeriod()));
        dcCount.add(Expression.le("endPeriod", searchOOOBean.getEndPeriod()));
    }

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

    // check if I have to order the results
    if (searchOOOBean.getSortParam() != null && !"".equals(searchOOOBean.getSortParam())) {
        logger.debug("Add sorting ! 234234");
        // if I have to, check if I have to order them ascending or descending
        if (searchOOOBean.getSortDirection() == -1) {
            // ascending
            dc1.addOrder(Order.asc(searchOOOBean.getSortParam()));
        } else {
            // descending
            dc1.addOrder(Order.desc(searchOOOBean.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 || searchOOOBean.getNbrOfResults() == -1) {
        boolean isSearch = false;
        if (searchOOOBean.getNbrOfResults() == -1) {
            isSearch = true;
        }

        // set the count(*) restriction
        dcCount.setProjection(Projections.countDistinct("outOfOfficeId"));
        //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)));
        searchOOOBean.setNbrOfResults(nbrOfResults);

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

    }
    List res = getHibernateTemplate().findByCriteria(dc1,
            (searchOOOBean.getCurrentPage() - 1) * searchOOOBean.getResultsPerPage(),
            searchOOOBean.getResultsPerPage());

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

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

License:Open Source License

/**
 * Searches for organisations after criterion from searchOrganisationBean.
 * // www .ja  va 2s . co  m
 * @author alu
 * @author dan.damian
 * @author Adelina
 * 
 * @param searchOrganisationBean
 * @param isChanged
 * @param typeIds
 * @return A list of log beans
 * @throws ParseException
 */
public List<Organisation> getFromSearch(SearchOrganisationBean searchOrganisationBean, boolean isChanged,
        Set<Byte> typeIds) 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.organisationEntity);
    DetachedCriteria dcCount = DetachedCriteria.forEntityName(IModelConstant.organisationEntity);

    if (Tools.getInstance().stringNotEmpty(searchOrganisationBean.getName())) {
        dc.add(Restrictions.ilike("name", "%".concat(searchOrganisationBean.getName()).concat("%")));
        dcCount.add(Restrictions.ilike("name", "%".concat(searchOrganisationBean.getName()).concat("%")));
        logger.debug("name: " + searchOrganisationBean.getName());
    }

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

    if (Tools.getInstance().stringNotEmpty(searchOrganisationBean.getEmail())) {
        dc.add(Restrictions.ilike("email", "%".concat(searchOrganisationBean.getEmail()).concat("%")));
        dcCount.add(Restrictions.ilike("email", "%".concat(searchOrganisationBean.getEmail()).concat("%")));
        logger.debug("email: " + searchOrganisationBean.getEmail());
    }

    if (searchOrganisationBean.getType() != -1) {
        dc.add(Restrictions.eq("type", searchOrganisationBean.getType()));
        dcCount.add(Restrictions.eq("type", searchOrganisationBean.getType()));
    } else {
        dc.add(Restrictions.in("type", typeIds));
        dcCount.add(Restrictions.in("type", typeIds));
    }

    // check if I have to order the results
    if (searchOrganisationBean.getSortParam() != null && !"".equals(searchOrganisationBean.getSortParam())) {
        // if I have to, check if I have to order them ascending or descending
        if (searchOrganisationBean.getSortDirection() == -1) {
            // ascending
            dc.addOrder(Order.asc(searchOrganisationBean.getSortParam()));
        } else {
            // descending
            dc.addOrder(Order.desc(searchOrganisationBean.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 (isChanged || searchOrganisationBean.getNbrOfResults() == -1) {
        boolean isSearch = false;
        if (searchOrganisationBean.getNbrOfResults() == -1) {
            isSearch = true;
        }
        // set the countDistinct restriction
        dcCount.setProjection(Projections.countDistinct("organisationId"));

        int nbrOfResults = ((Integer) getHibernateTemplate().findByCriteria(dcCount, 0, 0).get(0)).intValue();
        searchOrganisationBean.setNbrOfResults(nbrOfResults);
        logger.debug("NbrOfResults " + searchOrganisationBean.getNbrOfResults());
        logger.debug("----> searchOrganisationBean.getResults " + searchOrganisationBean.getResultsPerPage());
        // get the number of pages
        if (nbrOfResults % searchOrganisationBean.getResultsPerPage() == 0) {
            searchOrganisationBean.setNbrOfPages(nbrOfResults / searchOrganisationBean.getResultsPerPage());
        } else {
            searchOrganisationBean.setNbrOfPages(nbrOfResults / searchOrganisationBean.getResultsPerPage() + 1);
        }
        // after an organisation is deleted, the same page has to be displayed;
        //only when all the organisations from last page are deleted, the previous page will be shown 
        if (isChanged && (searchOrganisationBean.getCurrentPage() > searchOrganisationBean.getNbrOfPages())) {
            searchOrganisationBean.setCurrentPage(searchOrganisationBean.getNbrOfPages());
        } else if (isSearch) {
            searchOrganisationBean.setCurrentPage(1);
        }
    }
    List<Organisation> res = getHibernateTemplate().findByCriteria(dc,
            (searchOrganisationBean.getCurrentPage() - 1) * searchOrganisationBean.getResultsPerPage(),
            searchOrganisationBean.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.DaoPermissionImpl.java

License:Open Source License

/**
 * Searches for permissions after criterion from searchPermissionBean
 * @author alu/*from ww w .ja  v a2  s  .com*/
 * @return A list of permission beans 
 * @throws ParseException 
 */
public List getPermissionBeanFromSearch(SearchPermissionBean searchPermissionBean, boolean isDeleteAction,
        boolean isSuper, Set<Integer> modulesIds) throws ParseException {

    logger.debug("getPermissionBeanFromSearch - 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.permissionForListingEntity);
    DetachedCriteria dcCount = DetachedCriteria.forEntityName(IModelConstant.permissionForListingEntity);

    if (searchPermissionBean.getName() != null && !"".equals(searchPermissionBean.getName())) {
        dc.add(Restrictions.ilike("name", "%".concat(searchPermissionBean.getName()).concat("%")));
        dcCount.add(Restrictions.ilike("name", "%".concat(searchPermissionBean.getName()).concat("%")));
    }
    if (searchPermissionBean.getModuleId() != -1) {
        dc.add(Restrictions.eq("module.moduleId", searchPermissionBean.getModuleId()));
        dcCount.add(Restrictions.eq("module.moduleId", searchPermissionBean.getModuleId()));
    } else {
        dc.add(Restrictions.in("module.moduleId", modulesIds));
        dcCount.add(Restrictions.in("module.moduleId", modulesIds));
    }
    if (!isSuper) {
        dc.add(Restrictions.ne("module.moduleId", IConstant.MODULE_ID_FAKE));
        dcCount.add(Restrictions.ne("module.moduleId", IConstant.MODULE_ID_FAKE));
    }

    // check if I have to order the results
    if (searchPermissionBean.getSortParam() != null && !"".equals(searchPermissionBean.getSortParam())) {
        // if I have to, check if I have to order them ascending or descending
        if (searchPermissionBean.getSortDirection() == -1) {
            // ascending
            dc.addOrder(Order.asc(searchPermissionBean.getSortParam()));
        } else {
            // descending
            dc.addOrder(Order.desc(searchPermissionBean.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 || searchPermissionBean.getNbrOfResults() == -1) {

        boolean isSearch = false;
        if (searchPermissionBean.getNbrOfResults() == -1) {
            isSearch = true;
        }
        // set the count(*) restriction
        dcCount.setProjection(Projections.countDistinct("permissionId"));

        //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();
        searchPermissionBean.setNbrOfResults(nbrOfResults);

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

    List res = getHibernateTemplate().findByCriteria(dc,
            (searchPermissionBean.getCurrentPage() - 1) * searchPermissionBean.getResultsPerPage(),
            searchPermissionBean.getResultsPerPage());

    logger.debug("getPermissionBeanFromSearch - 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

/**
 * Searches for persons after criterion from searchPersonBean.
 * /*from ww w  .j  a va2 s.co  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

/**
 * Retrieves from search a list of person with basic info and pagination
 * //  www .  j  av  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/*ww  w.j  a  v a2  s. co m*/
 * @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.DaoUserGroupImpl.java

License:Open Source License

public List<UserGroup> getUserGroupBeanFromSearch(SearchUserGroupBean searchUserGroupBean,
        boolean isDeleteAction) {
    logger.debug("DaoUserGroupImpl - getUserGroupBeanFromSearch - START - name: "
            .concat(String.valueOf(searchUserGroupBean.getName()))
            .concat(" - organisationId: ".concat(String.valueOf(searchUserGroupBean.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: /* ww w  .  j a  v a  2 s.co m*/
    -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.userGroupForListingEntity);
    DetachedCriteria dcCount = DetachedCriteria.forEntityName(IModelConstant.userGroupForListingEntity);

    if (searchUserGroupBean.getName() != null && !"".equals(searchUserGroupBean.getName())) {
        dc.add(Restrictions.ilike("name", "%".concat(searchUserGroupBean.getName()).concat("%")));
        dcCount.add(Restrictions.ilike("name", "%".concat(searchUserGroupBean.getName()).concat("%")));
    }
    if (searchUserGroupBean.getOrganisationId() != -1) {
        dc.add(Restrictions.eq("organisation.organisationId", searchUserGroupBean.getOrganisationId()));
        dcCount.add(Restrictions.eq("organisation.organisationId", searchUserGroupBean.getOrganisationId()));
    }
    //the organization default user group mustn't be displayed
    dc.add(Restrictions.eq("status", IConstant.NOM_USER_GROUP_NORMAL));
    dcCount.add(Restrictions.eq("status", IConstant.NOM_USER_GROUP_NORMAL));

    // check if I have to order the results
    if (searchUserGroupBean.getSortParam() != null && !"".equals(searchUserGroupBean.getSortParam())) {
        // if I have to, check if I have to order them ascending or descending
        if (searchUserGroupBean.getSortDirection() == -1) {
            // ascending
            dc.addOrder(Order.asc(searchUserGroupBean.getSortParam()));
        } else {
            // descending
            dc.addOrder(Order.desc(searchUserGroupBean.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 || searchUserGroupBean.getNbrOfResults() == -1) {
        boolean isSearch = false;
        if (searchUserGroupBean.getNbrOfResults() == -1) {
            isSearch = true;
        }
        // set the count(*) restriction         
        dcCount.setProjection(Projections.countDistinct("userGroupId"));

        //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)));
        searchUserGroupBean.setNbrOfResults(nbrOfResults);

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

    }

    List<UserGroup> res = (List<UserGroup>) getHibernateTemplate().findByCriteria(dc,
            (searchUserGroupBean.getCurrentPage() - 1) * searchUserGroupBean.getResultsPerPage(),
            searchUserGroupBean.getResultsPerPage());
    logger.debug("DaoUserGroupImpl - getUserGroupBeanFromSearch - END");
    return res;
}

From source file:ro.cs.ts.model.dao.impl.DaoActivityImpl.java

License:Open Source License

/**
* Search for activities/*ww w. ja  v  a  2 s.com*/
* 
* @author Adelina
* 
* @param searchActivityBean
* @param projectIds
* @param isDeleteAction
* @return
* @throws BusinessException 
*/
public List<Activity> getFromSearch(SearchActivityBean searchActivityBean, Set<Integer> projectIds,
        Integer organizationId, boolean isDeleteAction, MessageSource messageSource) throws BusinessException {
    logger.debug("getFromSearch - START ");

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

    UserAuth userAuth = (UserAuth) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    // organization      
    if (searchActivityBean.getOrganizationId() != null) {
        dc.add(Restrictions.eq("organizationId", searchActivityBean.getOrganizationId()));
        dcCount.add(Restrictions.eq("organizationId", searchActivityBean.getOrganizationId()));
    }

    // name      
    if (Tools.getInstance().stringNotEmpty(searchActivityBean.getName())) {
        dc.add(Restrictions.ilike("name", "%".concat(searchActivityBean.getName().concat("%"))));
        dcCount.add(Restrictions.ilike("name", "%".concat(searchActivityBean.getName().concat("%"))));
        logger.debug("name = " + searchActivityBean.getName());
    }

    // project   
    if (searchActivityBean.getProjectId() != null) {
        if (searchActivityBean.getProjectId() != IConstant.NOM_ACTIVITY_SEARCH_ALL
                && searchActivityBean.getProjectId() != IConstant.NOM_ACTIVITY_SEARCH_IN_ORGANIZATION) {
            //set the selected projectId as search criteria               
            dc.createCriteria("projectDetails")
                    .add(Restrictions.eq("projectId", searchActivityBean.getProjectId()));
            dcCount.createCriteria("projectDetails")
                    .add(Restrictions.eq("projectId", searchActivityBean.getProjectId()));
        } else if (searchActivityBean.getProjectId() == IConstant.NOM_ACTIVITY_SEARCH_ALL) {
            if (!userAuth.hasAuthority(PermissionConstant.getInstance().getTS_ActivityAdvancedSearch())) {
                DetachedCriteria subquery = DetachedCriteria
                        .forEntityName(IModelConstant.activityForListingEntity);
                subquery.createCriteria("projectDetails").add(Restrictions.in("projectId", projectIds));
                subquery.add(Restrictions.disjunction());
                subquery.setProjection(Projections.id());
                dc.add(Expression.or(Subqueries.propertyIn("activityId", subquery),
                        Restrictions.isNull("projectDetails")));
                dcCount.add(Expression.or(Subqueries.propertyIn("activityId", subquery),
                        Restrictions.isNull("projectDetails")));
            }
        } else if (searchActivityBean.getProjectId() == IConstant.NOM_ACTIVITY_SEARCH_IN_ORGANIZATION) {
            dc.add(Restrictions.isNull("projectDetails"));
            dcCount.add(Restrictions.isNull("projectDetails"));
        }
    }

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

    // Billable
    if (searchActivityBean.getBillable() != null) {
        if (searchActivityBean.getBillable().equals(IConstant.NOM_BILLABLE_YES)
                || searchActivityBean.getBillable().equals(IConstant.NOM_BILLABLE_NO)) {
            dc.add(Restrictions.eq("billable", searchActivityBean.getBillable()));
            dcCount.add(Restrictions.eq("billable", searchActivityBean.getBillable()));
        }
    }

    //the results list
    List<Activity> res = null;

    //the order can be done while retrieving the data from the database only if the sort parameter is the activity name or billable;
    //this cannot be done if the results must be ordered by project name
    if (searchActivityBean.getSortParam() != null && !"".equals(searchActivityBean.getSortParam())
            && !"projectName".equals(searchActivityBean.getSortParam())) {
        // check if I have to order the results
        // if I have to, check if I have to order them ascending or descending
        if (searchActivityBean.getSortDirection() == -1) {
            // ascending
            dc.addOrder(Order.asc(searchActivityBean.getSortParam()));
        } else {
            // descending
            dc.addOrder(Order.desc(searchActivityBean.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 || searchActivityBean.getNbrOfResults() == -1) {
            boolean isSearch = false;
            if (searchActivityBean.getNbrOfResults() == -1) {
                isSearch = true;
            }
            // set the count(*) restriction         
            dcCount.setProjection(Projections.countDistinct("activityId"));

            //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)));
            searchActivityBean.setNbrOfResults(nbrOfResults);

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

        res = (List<Activity>) getHibernateTemplate().findByCriteria(dc,
                (searchActivityBean.getCurrentPage() - 1) * searchActivityBean.getResultsPerPage(),
                searchActivityBean.getResultsPerPage());
    } else {
        res = (List<Activity>) getHibernateTemplate().findByCriteria(dc);
        // 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 || searchActivityBean.getNbrOfResults() == -1) {
            boolean isSearch = false;
            if (searchActivityBean.getNbrOfResults() == -1) {
                isSearch = true;
            }
            int nbrOfResults = res.size();
            logger.debug("search results: ".concat(String.valueOf(nbrOfResults)));
            searchActivityBean.setNbrOfResults(nbrOfResults);

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

    List<Project> projects = new ArrayList<Project>();

    if (projectIds.size() > 0) {
        projects = BLProject.getInstance().getProjectsSimpleByProjectIds(projectIds);
        logger.debug("projects = " + projects);
    }

    // setting the project name 
    for (Activity activity : res) {
        if (activity.getProjectDetails() != null && activity.getProjectDetails().getProjectId() != null) {
            if (projects != null && projects.size() > 0) {
                for (Project project : projects) {
                    if (activity.getProjectDetails().getProjectId().equals(project.getProjectId())) {
                        activity.setProjectName(project.getName());
                        activity.setProjectId(project.getProjectId());
                        activity.setProjectManagerId(project.getManagerId());
                        logger.debug("activity = " + activity);
                        break;
                    }
                }
            }
        } else {
            activity.setProjectId(-1);
            activity.setProjectName(messageSource.getMessage(PROJECT_FROM_ORG, null, null));
        }

        logger.debug("projectName = " + activity.getProjectName());
    }

    List<Activity> activities = new ArrayList<Activity>();
    if (searchActivityBean.getSortParam() != null && "projectName".equals(searchActivityBean.getSortParam())) {
        //sorting the exchanges list
        //------sort the list
        if (searchActivityBean.getSortParam().equals("projectName")) {
            Collections.sort(res, ActivityComparator.getInstance().activityProjectNameComparator());
        }

        //ascending or descending
        if (searchActivityBean.getSortDirection() == IConstant.DESCENDING) {
            Collections.reverse(res);
        }
        long start = (searchActivityBean.getCurrentPage() - 1) * searchActivityBean.getResultsPerPage();
        long end = searchActivityBean.getCurrentPage() * searchActivityBean.getResultsPerPage();

        // go over the entries            
        for (int i = (int) start; i < end; i++) {
            if (i >= res.size()) {
                continue;
            }
            activities.add(res.get(i));
        }
    } else {
        activities = res;
    }

    logger.debug("getFromSearch - END");
    return activities;
}