Example usage for org.hibernate.criterion Restrictions between

List of usage examples for org.hibernate.criterion Restrictions between

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions between.

Prototype

public static Criterion between(String propertyName, Object low, Object high) 

Source Link

Document

Apply a "between" constraint to the named property

Usage

From source file:com.codefupanda.persistor.util.HibernateUtil.java

License:Open Source License

private static Criterion convert(BetweenCriteria criteria) {
    return Restrictions.between(criteria.getProperty(), criteria.getLoValue(), criteria.getHiValue());
}

From source file:com.core.controller.Kimera.java

public <T> T entityBetweenDates(String field, Date first, Date second, Class type) {
    List<T> result = null;/* www .  j a v a2  s .  co m*/
    try {
        Session s = sf.openSession();
        result = s.createCriteria(type).add(Restrictions.between(field, first, second)).list();
        s.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return (T) result;
}

From source file:com.court.controller.HomeFXMLController.java

private Map<String, Number> getLoanReleasedData() {
    LocalDate now = LocalDate.now();
    Map<String, Number> map = new HashMap<>();
    Session s = HibernateUtil.getSessionFactory().openSession();
    Criteria c = s.createCriteria(MemberLoan.class);

    ProjectionList pList = Projections.projectionList();
    ClassMetadata lpMeta = s.getSessionFactory().getClassMetadata(MemberLoan.class);
    pList.add(Projections.property(lpMeta.getIdentifierPropertyName()));
    for (String prop : lpMeta.getPropertyNames()) {
        pList.add(Projections.property(prop), prop);
    }//from www . ja v  a2 s  .c  o  m
    c.add(Restrictions.eq("status", true));
    c.add(Restrictions.between("grantedDate", FxUtilsHandler.getDateFrom(now.with(firstDayOfYear())),
            FxUtilsHandler.getDateFrom(now.with(lastDayOfYear()))));
    c.setProjection(pList
            .add(Projections.sqlGroupProjection("DATE_FORMAT(granted_date, '%Y-%m-01') AS groupPro", "groupPro",
                    new String[] { "groupPro" }, new Type[] { StringType.INSTANCE }))
            .add(Projections.sqlProjection("SUM(loan_amount) AS lSum", new String[] { "lSum" },
                    new Type[] { DoubleType.INSTANCE })));

    c.addOrder(Order.asc("grantedDate"));
    c.setResultTransformer(Transformers.aliasToBean(MemberLoan.class));
    List<MemberLoan> list = (List<MemberLoan>) c.list();
    for (MemberLoan ml : list) {
        map.put(ml.getGroupPro(), ml.getlSum());
    }
    s.close();
    return map;
}

From source file:com.court.controller.HomeFXMLController.java

private Map<String, Number> getLoanCollectionData() {
    LocalDate now = LocalDate.now();
    Map<String, Number> map = new HashMap<>();
    Session s = HibernateUtil.getSessionFactory().openSession();
    Criteria c = s.createCriteria(LoanPayment.class);

    ProjectionList pList = Projections.projectionList();
    ClassMetadata lpMeta = s.getSessionFactory().getClassMetadata(LoanPayment.class);
    pList.add(Projections.property(lpMeta.getIdentifierPropertyName()));
    for (String prop : lpMeta.getPropertyNames()) {
        pList.add(Projections.property(prop), prop);
    }/* ww w.  ja  v  a  2  s. com*/
    c.add(Restrictions.between("paymentDate", FxUtilsHandler.getDateFrom(now.with(firstDayOfYear())),
            FxUtilsHandler.getDateFrom(now.with(lastDayOfYear()))));
    c.setProjection(pList
            .add(Projections.sqlGroupProjection("DATE_FORMAT(payment_date, '%Y-%m-01') AS groupPro", "groupPro",
                    new String[] { "groupPro" }, new Type[] { StringType.INSTANCE }))
            .add(Projections.sqlProjection("SUM(paid_amt) AS lSum", new String[] { "lSum" },
                    new Type[] { DoubleType.INSTANCE })));

    c.addOrder(Order.asc("paymentDate"));
    c.setResultTransformer(Transformers.aliasToBean(LoanPayment.class));
    List<LoanPayment> list = (List<LoanPayment>) c.list();
    for (LoanPayment lp : list) {
        map.put(lp.getGroupPro(), lp.getlSum());
    }
    s.close();
    return map;
}

From source file:com.court.controller.PaymentsFxmlController.java

private void performPaymentSearchByDuration(LocalDate from, LocalDate to) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria c = session.createCriteria(LoanPayment.class);
    c.add(Restrictions.between("paymentDate", FxUtilsHandler.getDateFrom(from),
            FxUtilsHandler.getDateFrom(to)));

    List<LoanPayment> lPayList = c.list();
    initPaymentTable(FXCollections.observableArrayList(lPayList));
    session.close();/*from w ww. j a  va2 s  .  c  o m*/
}

From source file:com.court.controller.PaymentsFxmlController.java

private void performSubscriptionSearchByDuration(LocalDate from, LocalDate to) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria c = session.createCriteria(SubscriptionPay.class);
    c.add(Restrictions.between("addedDate", FxUtilsHandler.getDateFrom(from), FxUtilsHandler.getDateFrom(to)));

    List<SubscriptionPay> sPayList = c.list();
    initSubscriptionTable(FXCollections.observableArrayList(sPayList));
    session.close();//from  ww w . ja v a2  s.c  o  m
}

From source file:com.court.controller.ReportFormFxmlController.java

@FXML
private void onLoanAction(ActionEvent event) {

    Dialog<AllLoansReport> dialog = new Dialog<>();
    dialog.setTitle("Loans");
    dialog.setHeaderText("Select Office");
    ButtonType viewBtn = new ButtonType("View Report", ButtonData.OK_DONE);
    dialog.getDialogPane().getButtonTypes().addAll(viewBtn, ButtonType.CANCEL);
    GridPane grid = new GridPane();
    grid.setHgap(10);// w  w  w . j av  a2s.c om
    grid.setVgap(10);
    grid.setPadding(new Insets(20, 150, 10, 10));

    TextField bField = new TextField();
    TextFields.bindAutoCompletion(bField, getBranches(true));
    DatePicker fDate = new DatePicker();
    DatePicker tDate = new DatePicker();

    FxUtilsHandler.setDatePickerTimeFormat(fDate, tDate);

    grid.add(new Label("Office:"), 0, 0);
    grid.add(bField, 1, 0);
    grid.add(new Label("Start Date:"), 0, 1);
    grid.add(fDate, 1, 1);
    grid.add(new Label("End Date:"), 0, 2);
    grid.add(tDate, 1, 2);
    dialog.getDialogPane().setContent(grid);
    fDate.setValue(LocalDate.now());
    tDate.setValue(LocalDate.now());
    dialog.setResultConverter(db -> {
        if (db == viewBtn) {
            return new AllLoansReport(bField.getText().split("-")[0], fDate.getValue(), tDate.getValue());

        }
        return null;
    });

    Optional<AllLoansReport> result = dialog.showAndWait();
    result.ifPresent(b -> {
        // String reportPath = "com/court/reports/LoansReport.jasper";
        String reportPath = null;
        try {
            reportPath = PropHandler.getStringProperty("report_path") + "LoansReport.jasper";
        } catch (IOException ex) {
            Logger.getLogger(ReportFormFxmlController.class.getName()).log(Level.SEVERE, null, ex);
        }
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria c = session.createCriteria(MemberLoan.class, "ml");
        c.createAlias("ml.member", "m");
        c.createAlias("m.branch", "b");
        if (!b.getBranch().equalsIgnoreCase("All")) {
            c.add(Restrictions.eq("b.branchCode", b.getBranch()));
        }
        c.add(Restrictions.between("ml.grantedDate", FxUtilsHandler.getDateFrom(b.getFd()),
                FxUtilsHandler.getDateFrom(b.getTd())));
        c.addOrder(Order.asc("b.branchName"));

        List<MemberLoan> list = (List<MemberLoan>) c.list();
        JRBeanCollectionDataSource memberLoanBeanCollection = new JRBeanCollectionDataSource(list);
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        Map<String, Object> map = new HashMap<>();
        map.put("companyName", ReportHandler.COMPANY_NAME);
        map.put("companyAddress", ReportHandler.ADDRESS);
        map.put("reportTitle",
                "Welfare Loan List Granted From " + sdf.format(FxUtilsHandler.getDateFrom(b.getFd())) + " To "
                        + sdf.format(FxUtilsHandler.getDateFrom(b.getTd())));
        ReportHandler rh = new ReportHandler(reportPath, map, memberLoanBeanCollection);
        //            rh.genarateReport();
        boolean blah = rh.genReport();
        if (blah) {
            rh.viewReport();
        }
        session.close();
    });

}

From source file:com.eharmony.matching.seeking.translator.hibernate.HibernateQueryTranslator.java

License:Apache License

@Override
public Criterion between(String fieldName, Object from, Object to) {
    return Restrictions.between(fieldName, from, to);
}

From source file:com.ephesoft.dcma.da.dao.hibernate.BatchInstanceDaoImpl.java

License:Open Source License

/**
 * An API to fetch count of the batch instance table for batch instance status and batch priority. API will return those batch
 * instance having access by the user roles on the basis of ephesoft user.
 * //from  ww  w . ja v  a  2s .c o  m
 * @param batchName {@link String}
 * @param batchInstanceStatus {@link BatchInstanceStatus}
 * @param userName {@link String}
 * @param priority {@link BatchPriority}
 * @param userRoles Set<{@link String}>
 * @param ephesoftUser {@link EphesoftUser}
 * @return int, count of the batch instance present for the batch instance status.
 */
@Override
public int getCount(String batchName, BatchInstanceStatus batchInstanceStatus, String userName,
        BatchPriority batchPriority, Set<String> userRoles, EphesoftUser ephesoftUser) {
    int count = 0;
    DetachedCriteria criteria = criteria();
    if (batchName != null && !batchName.isEmpty()) {
        String batchNameLocal = batchName.replaceAll(DataAccessConstant.PERCENTAGE, REPLACEMENT_STRING);
        Criterion nameLikeCriteria = Restrictions.like(BATCH_NAME,
                DataAccessConstant.PERCENTAGE + batchNameLocal + DataAccessConstant.PERCENTAGE);
        Criterion idLikeCriteria = Restrictions.like(BATCH_INSTANCE_IDENTIFIER,
                DataAccessConstant.PERCENTAGE + batchNameLocal + DataAccessConstant.PERCENTAGE);
        LogicalExpression searchCriteria = Restrictions.or(nameLikeCriteria, idLikeCriteria);
        criteria.add(searchCriteria);
    } else if (null != batchPriority) {
        Disjunction disjunction = Restrictions.disjunction();
        Integer lowValue = batchPriority.getLowerLimit();
        Integer upperValue = batchPriority.getUpperLimit();
        disjunction.add(Restrictions.between(PRIORITY, lowValue, upperValue));
        criteria.add(disjunction);
    }
    Set<String> batchClassIdentifiers = batchClassGroupsDao.getBatchClassIdentifierForUserRoles(userRoles);

    if (EphesoftUser.NORMAL_USER.equals(ephesoftUser)) {
        Set<String> batchInstanceIdentifiers = batchInstanceGroupsDao
                .getBatchInstanceIdentifierForUserRoles(userRoles);

        if ((null != batchClassIdentifiers && batchClassIdentifiers.size() > 0)
                || (null != batchInstanceIdentifiers && batchInstanceIdentifiers.size() > 0)) {
            criteria.add(Restrictions.eq(STATUS, batchInstanceStatus));
            criteria.add(Restrictions.eq(IS_REMOTE, false));
            criteria.add(Restrictions.or(Restrictions.isNull(CURRENT_USER),
                    Restrictions.eq(CURRENT_USER, userName)));
            final Disjunction disjunction = Restrictions.disjunction();
            if (null != batchClassIdentifiers && batchClassIdentifiers.size() > 0) {
                criteria.createAlias(BATCH_CLASS, BATCH_CLASS);
                disjunction.add(Restrictions.in(BATCH_CLASS_IDENTIFIER, batchClassIdentifiers));
            }
            if (null != batchInstanceIdentifiers && batchInstanceIdentifiers.size() > 0) {
                disjunction.add(Restrictions.in(BATCH_INSTANCE_IDENTIFIER, batchInstanceIdentifiers));
            }
            criteria.add(disjunction);
            count = count(criteria);

        } else {
            if (null != batchClassIdentifiers && batchClassIdentifiers.size() > 0) {
                criteria.add(Restrictions.eq(STATUS, batchInstanceStatus));
                criteria.add(Restrictions.eq(IS_REMOTE, false));
                criteria.add(Restrictions.or(Restrictions.isNull(CURRENT_USER),
                        Restrictions.eq(CURRENT_USER, userName)));
                criteria.createAlias(BATCH_CLASS, BATCH_CLASS);
                criteria.add(Restrictions.in(BATCH_CLASS_IDENTIFIER, batchClassIdentifiers));
                count = count(criteria);
            }
        }
    }
    return count;

}

From source file:com.ephesoft.dcma.da.dao.hibernate.BatchInstanceDaoImpl.java

License:Open Source License

/**
 * An API to fetch all the batch instances excluding remotely executing batches by status list. Parameter firstResult set a limit
 * upon the number of objects to be retrieved. Parameter maxResults set the first result to be retrieved. Parameter orderList set
 * the sort property and order of that property. If orderList parameter is null or empty then this parameter is avoided. This will
 * return only those batch instance which having access by the user roles on the basis of the ephesoft user.
 * //from w  w w  .  java2 s. com
 * @param searchString {@link String}
 * @param statusList List<{@link BatchInstanceStatus}> status list of batch instance status.
 * @param firstResult the first result to retrieve, numbered from <tt>0</tt>
 * @param maxResults maxResults the maximum number of results
 * @param orderList List<{@link Order}> orderList set the sort property and order of that property. If orderList parameter is null
 *            or empty then this parameter is avoided.
 * @param filterClauseList List<{@link BatchInstanceFilter}> this will add the where clause to the criteria query based on the
 *            property name and value. If filterClauseList parameter is null or empty then this parameter is avoided.
 * @param batchPriorities List<{@link BatchPriority}> this will add the where clause to the criteria query based on the priority
 *            list selected. If batchPriorities parameter is null or empty then this parameter is avoided.
 * @param userName {@link String}
 * @param userRoles Set<{@link String}>
 * @param ephesoftUser {@link EphesoftUser}
 * @return List<{@link BatchInstance}> return the batch instance list.
 */
@Override
public List<BatchInstance> getBatchInstancesExcludedRemoteBatch(final String searchString,
        List<BatchInstanceStatus> statusList, final int firstResult, final int maxResults,
        final List<Order> orderList, final List<BatchInstanceFilter> filterClauseList,
        final List<BatchPriority> batchPriorities, String userName, final Set<String> userRoles,
        EphesoftUser ephesoftUser) {
    EphesoftCriteria criteria = criteria();

    if (searchString != null && !searchString.isEmpty()) {
        String batchNameLocal = searchString.replaceAll("%", "\\\\%");
        Criterion nameLikeCriteria = Restrictions.like(BATCH_NAME, "%" + batchNameLocal + "%");
        Criterion idLikeCriteria = Restrictions.like(BATCH_INSTANCE_IDENTIFIER, "%" + batchNameLocal + "%");

        LogicalExpression searchCriteria = Restrictions.or(nameLikeCriteria, idLikeCriteria);
        criteria.add(searchCriteria);
    }

    if (null != statusList) {
        criteria.add(Restrictions.in(STATUS, statusList));
        criteria.add(
                Restrictions.or(Restrictions.isNull(CURRENT_USER), Restrictions.eq(CURRENT_USER, userName)));
        criteria.add(Restrictions.eq(IS_REMOTE, false));
    }

    if (null != batchPriorities && !(batchPriorities.isEmpty())) {
        Disjunction disjunction = Restrictions.disjunction();
        for (BatchPriority batchPriority : batchPriorities) {
            if (null != batchPriority) {
                Integer lowValue = batchPriority.getLowerLimit();
                Integer upperValue = batchPriority.getUpperLimit();
                disjunction.add(Restrictions.between(PRIORITY, lowValue, upperValue));
            } else {
                disjunction = Restrictions.disjunction();
                break;
            }
        }
        criteria.add(disjunction);

    }
    List<BatchInstance> batchInstanceList = new ArrayList<BatchInstance>();

    Set<String> batchClassIdentifiers = batchClassGroupsDao.getBatchClassIdentifierForUserRoles(userRoles);

    switch (ephesoftUser) {
    case NORMAL_USER:
        batchClassIdentifiers = batchClassGroupsDao.getBatchClassIdentifierForUserRoles(userRoles);
        Set<String> batchInstanceIdentifierSet = batchInstanceGroupsDao
                .getBatchInstanceIdentifiersExceptUserRoles(userRoles);
        Set<String> batchInstanceIdentifiers = batchInstanceGroupsDao
                .getBatchInstanceIdentifierForUserRoles(userRoles);

        if ((null != batchClassIdentifiers && batchClassIdentifiers.size() > 0)
                || (null != batchInstanceIdentifiers && batchInstanceIdentifiers.size() > 0)) {
            BatchInstanceFilter[] filters = null;
            if (filterClauseList != null) {
                filters = filterClauseList.toArray(new BatchInstanceFilter[filterClauseList.size()]);
            }
            Order[] orders = null;
            if (orderList != null) {
                orders = orderList.toArray(new Order[orderList.size()]);
            }

            if (null != batchClassIdentifiers && batchClassIdentifiers.size() > 0
                    && null != batchInstanceIdentifiers && batchInstanceIdentifiers.size() > 0
                    && null != batchInstanceIdentifierSet && batchInstanceIdentifierSet.size() > 0) {
                final Conjunction conjunction = Restrictions.conjunction();
                final Disjunction disjunction = Restrictions.disjunction();
                criteria.createAlias(BATCH_CLASS, BATCH_CLASS);
                disjunction.add(Restrictions.in(BATCH_CLASS_IDENTIFIER, batchClassIdentifiers));
                disjunction.add(Restrictions.in(BATCH_INSTANCE_IDENTIFIER, batchInstanceIdentifiers));
                conjunction.add(Restrictions
                        .not(Restrictions.in(BATCH_INSTANCE_IDENTIFIER, batchInstanceIdentifierSet)));
                conjunction.add(disjunction);
                criteria.add(conjunction);
            } else if (null != batchClassIdentifiers && batchClassIdentifiers.size() > 0
                    && null != batchInstanceIdentifiers && batchInstanceIdentifiers.size() > 0) {
                final Disjunction disjunction = Restrictions.disjunction();
                criteria.createAlias(BATCH_CLASS, BATCH_CLASS);
                disjunction.add(Restrictions.in(BATCH_CLASS_IDENTIFIER, batchClassIdentifiers));
                disjunction.add(Restrictions.in(BATCH_INSTANCE_IDENTIFIER, batchInstanceIdentifiers));
                criteria.add(disjunction);
            } else if (null != batchClassIdentifiers && batchClassIdentifiers.size() > 0
                    && null != batchInstanceIdentifierSet && batchInstanceIdentifierSet.size() > 0) {
                final Conjunction conjunction = Restrictions.conjunction();
                criteria.createAlias(BATCH_CLASS, BATCH_CLASS);
                conjunction.add(Restrictions.in(BATCH_CLASS_IDENTIFIER, batchClassIdentifiers));
                conjunction.add(Restrictions
                        .not(Restrictions.in(BATCH_INSTANCE_IDENTIFIER, batchInstanceIdentifierSet)));
                criteria.add(conjunction);
            } else if (null != batchInstanceIdentifiers && batchInstanceIdentifiers.size() > 0
                    && null != batchInstanceIdentifierSet && batchInstanceIdentifierSet.size() > 0) {
                final Conjunction conjunction = Restrictions.conjunction();
                conjunction.add(Restrictions.in(BATCH_INSTANCE_IDENTIFIER, batchInstanceIdentifiers));
                conjunction.add(Restrictions
                        .not(Restrictions.in(BATCH_INSTANCE_IDENTIFIER, batchInstanceIdentifierSet)));
                criteria.add(conjunction);
            } else if (null != batchInstanceIdentifiers && batchInstanceIdentifiers.size() > 0) {
                criteria.add(Restrictions.in(BATCH_INSTANCE_IDENTIFIER, batchInstanceIdentifiers));
            } else if (null != batchClassIdentifiers && batchClassIdentifiers.size() > 0) {
                criteria.createAlias(BATCH_CLASS, BATCH_CLASS);
                criteria.add(Restrictions.in(BATCH_CLASS_IDENTIFIER, batchClassIdentifiers));
            }

            batchInstanceList = find(criteria, firstResult, maxResults, filters, orders);
        }
        break;
    default:
        if (null != batchClassIdentifiers && batchClassIdentifiers.size() > 0) {
            BatchInstanceFilter[] filters = null;
            if (filterClauseList != null) {
                filters = filterClauseList.toArray(new BatchInstanceFilter[filterClauseList.size()]);
            }
            Order[] orders = null;
            if (orderList != null) {
                orders = orderList.toArray(new Order[orderList.size()]);
            }

            criteria.createAlias(BATCH_CLASS, BATCH_CLASS);
            criteria.add(Restrictions.in(BATCH_CLASS_IDENTIFIER, batchClassIdentifiers));
            batchInstanceList = find(criteria, firstResult, maxResults, filters, orders);
        }
        break;
    }
    return batchInstanceList;
}