Example usage for org.hibernate.criterion DetachedCriteria forEntityName

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

Introduction

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

Prototype

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

Source Link

Document

Static builder to create a DetachedCriteria for the given entity.

Usage

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

License:Open Source License

/**
 * Get the list of Organisation that has not a calendar defined
 * /* w w w.j  av  a 2  s .co 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 . 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.DaoOrganisationImpl.java

License:Open Source License

/**
 * Get the list of Organisation for a parentId
 * //w w  w .  j  a  va 2 s.  c  om
 * @author mitziuro
 * @return List<Organisation>
 */
public List<Organisation> getAllOrganisationsForParentId(Integer parentId) {
    logger.debug("getAllOrganisationsForForParentId - START");

    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.organisationForDeleteEntity);
    dc.add(Restrictions.eq("parentId", parentId));
    List<Organisation> organisations = getHibernateTemplate().findByCriteria(dc);

    logger.debug("getAllOrganisationsForParentId - END - ".concat(String.valueOf(parentId)));
    return organisations;

}

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

License:Open Source License

/**
 * Get the list of Organisation for a parentId for listing
 * //from w w w.  j a v a 2  s  .c  om
 * @author mitziuro
 * @return List<Organisation>
 */
public List<Organisation> getAllSimpleOrganisationsForParentId(Integer parentId) {
    logger.debug("getAllOrganisationsForForParentId - START");

    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.organisationEntity);
    dc.add(Restrictions.eq("parentId", parentId));
    List<Organisation> organisations = getHibernateTemplate().findByCriteria(dc);

    logger.debug("getAllSimpleOrganisationsForParentId - END - ".concat(String.valueOf(parentId)));
    return organisations;

}

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

License:Open Source License

/**
 * Get the list of Organisation for a parentId
 * /*  w ww  .  j av a  2 s.c  o m*/
 * @author coni
 * @return List<Organisation>
 */
public List<Organisation> getAllOrganisationsByParentIdForOrgTree(Integer parentId) {
    logger.debug("getAllOrganisationsByParentIdForOrgTree - START");

    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.organisationEntity);
    dc.add(Restrictions.eq("parentId", parentId));
    List<Organisation> organisations = getHibernateTemplate().findByCriteria(dc);

    logger.debug("getAllOrganisationsByParentIdForOrgTree - END - ".concat(String.valueOf(parentId)));
    return organisations;

}

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

License:Open Source License

/**
 * //from  w w w  .  j av  a 2  s .  c  om
 * Get all orgainsationIds that have a module identified by id
 * @author mitziuro
 * @param moduleId
 * @return
 */
public List<Integer> getOrganisationIdsByModule(Integer moduleId) {
    logger.debug("getOrgainsationIdsByModule - START for organisation for moduleId:"
            .concat(String.valueOf(moduleId)));

    List<Integer> res = new ArrayList<Integer>();

    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.organisationWithModulesEntity);
    dc.createCriteria("modules").add(Restrictions.eq("moduleId", moduleId));
    List<Organisation> organisations = getHibernateTemplate().findByCriteria(dc);

    for (Organisation organisation : organisations) {
        res.add(organisation.getOrganisationId());
    }

    logger.debug("getOrgainsationIdsByModule - END");
    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//www.  jav  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.DaoPermissionImpl.java

License:Open Source License

/**
 * Get the list of permissions for a module without the default permission for the module
 * //from  w  w  w  . j  ava2  s  . com
 * @author Adelina
 * 
 * @param moduleId
 * 
 * @return List<Permission>, a list of permissions
 */

@SuppressWarnings("unchecked")
public List<Permission> getPermissionByModule(Integer moduleId) {
    logger.debug("getPermissionByModule DAO IMPL - START - ");

    Map<Integer, String> map = new HashMap<Integer, String>();

    PermissionConstant permCt = PermissionConstant.getInstance();

    map.put(new Integer(1), permCt.getOM_Basic());
    map.put(new Integer(2), permCt.getDM_Basic());

    String permissionName = (String) map.get(new Integer(moduleId));

    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.permissionForListingEntity);
    dc.createCriteria("module").add(Restrictions.eq("moduleId", moduleId));
    if (permissionName != null) {
        dc.add(Restrictions.ne("name", permissionName));
    }
    List permissions = getHibernateTemplate().findByCriteria(dc);

    logger.debug("getPermissionByModule DAO IMPL - END - ".concat(String.valueOf(permissions.size())));

    return permissions;

}

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

License:Open Source License

/**
 * Get the default permission for a module
 * //from  ww  w  .  j  a v  a2 s.  c  om
 * @author Adelina
 * 
 * @param moduleId
 * 
 * @param permissionName
 * 
 * @return the default permission for a module
 *  
 */
public Permission getDefaultPermissionByModule(Integer moduleId) {
    logger.debug("getDefaultPermissionByModule DAO IMPL - START - ");

    Map<Integer, String> map = new HashMap<Integer, String>();

    PermissionConstant permCt = PermissionConstant.getInstance();

    map.put(new Integer(1), permCt.getOM_Basic());
    map.put(new Integer(2), permCt.getDM_Basic());
    map.put(new Integer(3), permCt.getAUDIT_Basic());
    map.put(new Integer(4), permCt.getIR_Basic());
    map.put(new Integer(5), permCt.getCM_Basic());
    map.put(new Integer(6), permCt.getTS_Basic());
    map.put(new Integer(7), permCt.getRM_Basic());

    String permissionName = (String) map.get(new Integer(moduleId));

    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.permissionEntity);
    dc.add(Restrictions.eq("moduleId", moduleId));
    dc.add(Restrictions.eq("name", permissionName));
    List permissions = getHibernateTemplate().findByCriteria(dc);

    logger.debug("getDefaultPermissionByModule DAO IMPL - END - ".concat(String.valueOf(permissions.size())));

    return (Permission) permissions.get(0);
}

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

License:Open Source License

/**
 * Returns all persons in the data base. 
 * all related entities.//from  w  w  w.j  a  v a  2 s.  c  o  m
 * 
 * @author dd
 */
@SuppressWarnings("unchecked")
public List<Person> list() {
    logger.debug("List all persons");
    // set criterion
    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.personEntity);
    dc.add(Restrictions.ne("status", IConstant.NOM_PERSON_STATUS_DELETED));
    List<Person> persons = getHibernateTemplate().findByCriteria(dc);
    logger.debug(Integer.toString(persons.size()).concat(" persons"));
    return persons;
}