Example usage for org.hibernate.criterion DetachedCriteria setProjection

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

Introduction

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

Prototype

public DetachedCriteria setProjection(Projection projection) 

Source Link

Document

Set the projection to use.

Usage

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

License:Open Source License

/**
 * Returns a list of jobs//from   ww  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

/**
  * Returns a list of persons that figure as replacements in at least one OOO profile
  * Receives as param a list of persons ids to look for
  * @author coni//from w  ww.  j a v a2 s . c  o  m
  * 
  * @param personReplacementId
  * @return
  */

public List<Person> getOOOPersonReplacementsFromIds(Integer[] personReplacementId) {
    logger.debug("getByPersonReplacementID - START");
    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.oooAllEntity);
    dc.createCriteria("personReplacement").add(Restrictions.in("personId", personReplacementId));
    dc.setProjection(Projections.distinct(Projections.property("personReplacement")));
    List<Person> list = getHibernateTemplate().findByCriteria(dc);
    logger.debug("getByPersonReplacementID - END");
    return list;
}

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  www . jav a  2 s.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

/**
 * Get the list of Organisation that has not a calendar defined
 * //www .j  av a 2  s. c o m
 * @author Adelina   
 * @return List<Organisation>
 */
public List<Organisation> getAllOrganisationsForNomWithoutCalendar() {
    logger.debug("getAllOrganisationsForNomWithoutCalendar - START");

    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.organisationForNomEntity);
    dc.addOrder(Order.asc("name"));
    DetachedCriteria subquery = DetachedCriteria.forEntityName(IModelConstant.calendarEntity);
    subquery.setProjection(Projections.property("organisation.organisationId"));
    dc.add(Subqueries.propertyNotIn("organisationId", subquery));

    List organisations = getHibernateTemplate().findByCriteria(dc);

    logger.debug(
            "getAllOrganisationsForNomWithoutCalendar - END - ".concat(String.valueOf(organisations.size())));
    return organisations;

}

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

License:Open Source License

/**
 * Searches for organisations after criterion from searchOrganisationBean.
 * //from   w w  w  .j a va 2 s .  com
 * @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 w w  w.j av  a2 s  . c  o m*/
 * @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.
 * /*  w w  w  . java  2 s  .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
 * /*ww  w . jav a 2s. 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
 * /*from   ww w.  j a  va  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

/**
 * Retrieves from search a list of person with basic info and pagination
 * /* w ww.j a v  a2s . co m*/
 * @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;
}