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.ts.model.dao.impl.DaoCostSheetImpl.java

License:Open Source License

/**
 * Gets the Project Report cost sheets//from  w w  w  .j av a  2 s.  c  o m
 * @author Coni
 * @param getDataCriteria
 * @return
 * @throws WSClientException 
 * @throws IOException 
 * @throws XmlMappingException 
 * @throws BusinessException
 */
public List<CostSheet> getProjectReportCostSheets(TSReportGetDataCriteria getDataCriteria)
        throws XmlMappingException, IOException, WSClientException {
    logger.debug("getProjectReportCostSheets - START");

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

    //PROJECT
    Integer projectId = (Integer) getDataCriteria.getProperties()
            .get(IConstant.TS_PROJECT_REPORT_SEARCH_CRITERIA_PROJECT_ID);
    if (projectId != null && projectId > 0) {
        dc.createCriteria("projectDetails").add(Restrictions.eq("projectId", projectId));
    }

    //START DATE
    if (getDataCriteria.getProperties().get(IConstant.TS_PROJECT_REPORT_SEARCH_CRITERIA_START_DATE) != null) {
        Date startDate = (Date) ((XMLGregorianCalendar) getDataCriteria.getProperties()
                .get(IConstant.TS_PROJECT_REPORT_SEARCH_CRITERIA_START_DATE)).toGregorianCalendar().getTime();
        if (startDate != null) {
            dc.add(Restrictions.ge("date", startDate));
        }
    }

    //END DATE
    if (getDataCriteria.getProperties().get(IConstant.TS_PROJECT_REPORT_SEARCH_CRITERIA_END_DATE) != null) {
        Date endDate = (Date) ((XMLGregorianCalendar) getDataCriteria.getProperties()
                .get(IConstant.TS_PROJECT_REPORT_SEARCH_CRITERIA_END_DATE)).toGregorianCalendar().getTime();
        if (endDate != null) {
            dc.add(Restrictions.le("date", endDate));
        }
    }

    //BILLABLE
    String billable = (String) getDataCriteria.getProperties()
            .get(IConstant.TS_PROJECT_REPORT_SEARCH_CRITERIA_BILLABLE);
    if (billable != null) {
        if (billable.equals(IConstant.NOM_BILLABLE_YES.toString())
                || billable.equals(IConstant.NOM_BILLABLE_NO.toString())) {
            dc.add(Restrictions.eq("billable", billable));
        }
    }

    //STATUS
    dc.add(Restrictions.ne("status", IConstant.NOM_COST_SHEET_STATUS_DELETED));

    List<CostSheet> result = getHibernateTemplate().findByCriteria(dc);

    if (result != null) {
        List<WSTeamMember> members = null;

        HashSet<Integer> teamMemberIds = new HashSet<Integer>();
        for (CostSheet cost : result) {
            if (cost.getTeamMemberDetail() != null) {
                teamMemberIds.add(new Integer(cost.getTeamMemberDetail().getTeamMemberId()));
            }
        }
        if (teamMemberIds.size() > 0) {
            members = CMWebServiceClient.getInstance().getTeamMembersByMemberIds(teamMemberIds, false);
        }

        for (CostSheet cost : result) {
            if (members != null && !members.isEmpty()) {
                for (WSTeamMember member : members) {
                    if (cost.getTeamMemberDetail().getTeamMemberId().equals(member.getMemberId())) {
                        cost.setCostSheetReporterName(
                                member.getFirstName().concat(" ").concat(member.getLastName()));
                    }
                }
            }
        }
    }

    logger.debug("getProjectReportCostSheets - END");

    return result;
}

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

License:Open Source License

/**
 * Gets all an organization available currencies
 * @author Coni//ww w  .  j  a v a  2 s .c  o  m
 * @param organizationId
 * @return
 * @throws BusinessException
 */
public List<Currency> getByOrganizationId(int organizationId) {
    logger.debug("getByOrganizationId - START");
    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.currencyEntity);

    dc.add(Restrictions.eq("organizationId", organizationId));
    dc.add(Restrictions.ne("status", IConstant.NOM_CURRENCY_STATUS_DELETED));

    List<Currency> res = (List<Currency>) getHibernateTemplate().findByCriteria(dc);
    logger.debug("getByOrganizationId - END");
    return res;
}

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

License:Open Source License

/**
 * Searches for currencies using the criterion defined in searchCurrencyBean
 * @author Coni/* ww  w  .  j av a2s.co m*/
 * @param searchCurrencyBean
 * @param isDeleteAction
 * @return
 * @throws BusinessException
 */
public List<Currency> getCurrencyBeanFromSearch(SearchCurrencyBean searchCurrencyBean, boolean isDeleteAction) {
    logger.debug("getCurrencyBeanFromSearch - START");

    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.currencyEntity);
    DetachedCriteria dcCount = DetachedCriteria.forEntityName(IModelConstant.currencyEntity);

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

    if (searchCurrencyBean.getInitials() != null && !"".equals(searchCurrencyBean.getInitials())) {
        dc.add(Restrictions.ilike("initials", "%".concat(searchCurrencyBean.getInitials()).concat("%")));
        dcCount.add(Restrictions.ilike("initials", "%".concat(searchCurrencyBean.getInitials()).concat("%")));
    }

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

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

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

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

        // get the number of pages
        if (nbrOfResults % searchCurrencyBean.getResultsPerPage() == 0) {
            searchCurrencyBean.setNbrOfPages(nbrOfResults / searchCurrencyBean.getResultsPerPage());
        } else {
            searchCurrencyBean.setNbrOfPages(nbrOfResults / searchCurrencyBean.getResultsPerPage() + 1);
        }
        // after a currency 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 && (searchCurrencyBean.getCurrentPage() > searchCurrencyBean.getNbrOfPages())) {
            searchCurrencyBean.setCurrentPage(searchCurrencyBean.getNbrOfPages());
        } else if (isSearch) {
            searchCurrencyBean.setCurrentPage(1);
        }

    }

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

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

License:Open Source License

/**
 * Gets the currency identified by the name and organisationId
 * @author alexandru.dobre//www  .ja  v  a 2 s . c o m
 * @param name
 * @param currencyId
 * @return the currency 
 */
public Currency getByName(String name, Integer organisationId) {

    logger.debug("getByName - START");

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

    if (name != null && !"".equals(name)) {
        dc.add(Restrictions.eq("name", name));
    }

    if (organisationId != null) {
        dc.add(Restrictions.eq("organizationId", organisationId));
    }

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

    List<Currency> res = (List<Currency>) getHibernateTemplate().findByCriteria(dc);
    logger.debug("getByName - END results size : ".concat(String.valueOf(res.size())));

    if (res.size() > 0) {
        return res.get(0);
    }

    return null;
}

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

License:Open Source License

/**
 * Gets the currency identified by the initials and organisationId
 * @author alexandru.dobre// w w  w  .j a v  a  2  s.c o  m
 * @param initials 
 * @param currencyId
 * @return the currency 
 */
public Currency getByInitials(String initials, Integer organisationId) {

    logger.debug("getByInitials - START");

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

    if (initials != null && !"".equals(initials)) {
        dc.add(Restrictions.eq("initials", initials));
    }

    if (organisationId != null) {
        dc.add(Restrictions.eq("organizationId", organisationId));
    }

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

    List<Currency> res = (List<Currency>) getHibernateTemplate().findByCriteria(dc);
    logger.debug("getByInitials - END results size : ".concat(String.valueOf(res.size())));

    if (res.size() > 0) {
        return res.get(0);
    }

    return null;
}

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

License:Open Source License

/**
 * Gets the currencies that have either the name or the initials specified within the same organisation
 * @param name/*from  w w  w  . j  a v  a  2  s.c om*/
 * @param intitals
 * @param organisationId
 * @return
 */
public List<Currency> getByNameOrInitials(String name, String initials, Integer organisationId) {

    logger.debug("getByNameOrInitials - START");

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

    if (initials != null && !"".equals(initials) && name != null && !"".equals(name)) {
        dc.add(Restrictions.or(Restrictions.eq("name", name), Restrictions.eq("initials", initials)));
    }

    if (organisationId != null) {
        dc.add(Restrictions.eq("organizationId", organisationId));
    }

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

    List<Currency> res = (List<Currency>) getHibernateTemplate().findByCriteria(dc);
    logger.debug("getByNameOrInitials - END results size : ".concat(String.valueOf(res.size())));

    return res;

}

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

License:Open Source License

/**
 * Returns Exchange entities that use the Currency with the id currencyId
 * @author Coni/*from   ww w .j a  v  a2  s .  c  o m*/
 * @param currencyId
 * @return
 */
public List<Exchange> getByCurrencyId(int currencyId) {
    logger.debug("getByCurrencyId - START");
    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.exchangeEntity);

    dc.add(Restrictions.or(Restrictions.eq("firstCurrencyId", currencyId),
            Restrictions.eq("secondCurrencyId", currencyId)));
    dc.add(Restrictions.ne("status", IConstant.NOM_EXCHANGE_STATUS_DELETED));
    List<Exchange> res = getHibernateTemplate().findByCriteria(dc);

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

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

License:Open Source License

/**
 * Searches for exchanges using the criterion defined in searchExchangeBean
 * @author Coni//w w w .  j  a va 2  s. c  o m
 * @param searchCostSheetBean
 * @param isDeleteAction
 * @return
 * @throws BusinessException 
 * 
 */
public List<Exchange> getExchangeBeanFromSearch(SearchExchangeBean searchExchangeBean, boolean isDeleteAction)
        throws BusinessException {
    logger.debug("getExchangeBeanFromSearch - START");
    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.exchangeForListingEntity);
    UserAuth userAuth = (UserAuth) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    //PROJECT
    List<Project> projects = null;
    if (searchExchangeBean.getProjectId() != null
            && searchExchangeBean.getProjectId().equals(new Integer(IConstant.NOM_EXCHANGE_SEARCH_ALL))) { //search in all
        //get the user available projects
        if (!userAuth.hasAuthority(PermissionConstant.getInstance().getTS_ExchangeSearchAll())) {
            //if the user is USER_ALL, i will search for exchanges registered for its organization or for its organization's projects; hence
            //only the organizationId restriction it is required; otherwise i will search only for exchanges registered only for the projects
            //to whom the user is PM

            projects = BLProject.getInstance().getProjectsByManager(userAuth.getPersonId(), true, true);

            if (projects != null && !projects.isEmpty()) {
                List<Integer> projectIds = new ArrayList<Integer>();
                for (Project project : projects) {
                    projectIds.add(project.getProjectId());
                }
                dc.createCriteria("projectDetail").add(Restrictions.in("projectId", projectIds));
            } else {
                // if the user it is not PM for at least one project, no results must be displayed
                return new ArrayList<Exchange>();
            }
        }
    }
    if (searchExchangeBean.getProjectId() != null
            && searchExchangeBean.getProjectId().equals(new Integer(IConstant.NOM_EXCHANGE_SEARCH_FOR_ORG))) { //exchanges from organization
        //if the user isn't USER_ALL, i won't show any exchanges; otherwise the projectId must be null
        if (!userAuth.hasAuthority(PermissionConstant.getInstance().getTS_ExchangeSearchAll())) {
            return new ArrayList<Exchange>();
        } else {
            dc.add(Restrictions.isNull("projectDetail"));
        }
    } else if (searchExchangeBean.getProjectId() != null && searchExchangeBean.getProjectId() > 0) {
        dc.createCriteria("projectDetail").add(Restrictions.eq("projectId", searchExchangeBean.getProjectId()));
    }

    //FIRST CURRENCY
    if (searchExchangeBean.getFirstCurrencyId() != null
            && !searchExchangeBean.getFirstCurrencyId().equals(-1)) {
        dc.createCriteria("firstCurrency")
                .add(Restrictions.eq("currencyId", searchExchangeBean.getFirstCurrencyId()));
    }

    //SECOND CURRENCY
    if (searchExchangeBean.getSecondCurrencyId() != null
            && !searchExchangeBean.getSecondCurrencyId().equals(-1)) {
        dc.createCriteria("secondCurrency")
                .add(Restrictions.eq("currencyId", searchExchangeBean.getSecondCurrencyId()));
    }

    //STATUS
    dc.add(Restrictions.ne("status", IConstant.NOM_EXCHANGE_STATUS_DELETED));

    //ORGANIZATIONID
    if (searchExchangeBean.getOrganizationId() != null) {
        dc.add(Restrictions.eq("organizationId", searchExchangeBean.getOrganizationId()));
    }

    List<Exchange> res = (List<Exchange>) 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 || searchExchangeBean.getNbrOfResults() == -1) {
        boolean isSearch = false;
        if (searchExchangeBean.getNbrOfResults() == -1) {
            isSearch = true;
        }
        int nbrOfResults = res.size();
        logger.debug("search results: ".concat(String.valueOf(nbrOfResults)));
        searchExchangeBean.setNbrOfResults(nbrOfResults);

        // get the number of pages
        if (nbrOfResults % searchExchangeBean.getResultsPerPage() == 0) {
            searchExchangeBean.setNbrOfPages(nbrOfResults / searchExchangeBean.getResultsPerPage());
        } else {
            searchExchangeBean.setNbrOfPages(nbrOfResults / searchExchangeBean.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 && (searchExchangeBean.getCurrentPage() > searchExchangeBean.getNbrOfPages())) {
            searchExchangeBean.setCurrentPage(searchExchangeBean.getNbrOfPages());
        } else if (isSearch) {
            searchExchangeBean.setCurrentPage(1);
        }
    }

    //the returned list of exchanges
    List<Exchange> exchanges = new ArrayList<Exchange>();

    if (res != null && !res.isEmpty()) {
        if (projects == null) {
            HashSet<Integer> projectIds = new HashSet<Integer>();
            for (Exchange exchange : res) {
                if (exchange.getProjectDetail() != null) {
                    projectIds.add(new Integer(exchange.getProjectDetail().getProjectId()));
                }
            }
            if (projectIds.size() > 0) {
                projects = BLProject.getInstance().getProjectsSimpleByProjectIds(projectIds);
            }
        }

        for (Exchange exchange : res) {
            //setting the project name
            if (exchange.getProjectDetail() != null && exchange.getProjectDetail().getProjectId() != null
                    && projects != null) {
                for (Project project : projects) {
                    if (exchange.getProjectDetail().getProjectId().equals(project.getProjectId())) {
                        exchange.setProjectName(project.getName());
                        break;
                    }
                }
            }
        }

        //sorting the exchanges list
        //------sort the list
        if (searchExchangeBean.getSortParam().equals("projectName")) {

            List<Exchange> exchangesForProject = new ArrayList<Exchange>();
            List<Exchange> exchangesForOrg = new ArrayList<Exchange>();
            for (Exchange exchange : res) {
                if (exchange.getProjectDetail() != null && exchange.getProjectDetail().getProjectId() != null) {
                    exchangesForProject.add(exchange);
                } else {
                    exchangesForOrg.add(exchange);
                }
            }
            Collections.sort(exchangesForProject,
                    ExchangeComparator.getInstance().exchangeProjectNameComparator());
            exchangesForOrg.addAll(exchangesForProject);
            res = exchangesForOrg;
        }

        //ascending or descending
        if (searchExchangeBean.getSortDirection() == IConstant.DESCENDING) {
            Collections.reverse(res);
        }

        long start = (searchExchangeBean.getCurrentPage() - 1) * searchExchangeBean.getResultsPerPage();
        long end = searchExchangeBean.getCurrentPage() * searchExchangeBean.getResultsPerPage();

        // go over the entries            
        for (int i = (int) start; i < end; i++) {
            if (i >= res.size()) {
                continue;
            }
            exchanges.add(res.get(i));
        }
    }
    logger.debug("getExchangeBeanFromSearch - END");
    return exchanges;
}

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

License:Open Source License

/**
 * /*  w  w w  .  jav a 2  s.  c o  m*/
 * @author Coni
 * @param firstCurrencyId
 * @param secondCurrencyId
 * @param projectDetailId
 * @return
 */
public List<Exchange> getProjectExchangeByCurrencies(int firstCurrencyId, int secondCurrencyId,
        int projectDetailId) {
    logger.debug("getProjectExchangeByCurrencies - START");
    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.exchangeEntity);

    dc.add(Restrictions.eq("projectDetailId", projectDetailId));
    dc.add(Restrictions.eq("firstCurrencyId", firstCurrencyId));
    dc.add(Restrictions.eq("secondCurrencyId", secondCurrencyId));
    dc.add(Restrictions.ne("status", IConstant.NOM_EXCHANGE_STATUS_DELETED));

    List<Exchange> res = (List<Exchange>) getHibernateTemplate().findByCriteria(dc);
    logger.debug("getProjectExchangeByCurrencies - END");
    return res;
}

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

License:Open Source License

/**
 * //w  w w.j a v a 2s  .  c om
 * @author Coni
 * @param firstCurrencyId
 * @param secondCurrencyId
 * @param organizationId
 * @return
 */
public List<Exchange> getOrganizationExchangeByCurrencies(int firstCurrencyId, int secondCurrencyId,
        int organizationId) {
    logger.debug("getOrganizationExchangeByCurrencies - START");
    DetachedCriteria dc = DetachedCriteria.forEntityName(IModelConstant.exchangeEntity);

    dc.add(Restrictions.isNull("projectDetailId"));
    dc.add(Restrictions.eq("firstCurrencyId", firstCurrencyId));
    dc.add(Restrictions.eq("secondCurrencyId", secondCurrencyId));
    dc.add(Restrictions.eq("organizationId", organizationId));
    dc.add(Restrictions.ne("status", IConstant.NOM_EXCHANGE_STATUS_DELETED));

    List<Exchange> res = (List<Exchange>) getHibernateTemplate().findByCriteria(dc);
    logger.debug("getOrganizationExchangeByCurrencies - END");
    return res;
}