Example usage for org.hibernate.criterion ProjectionList add

List of usage examples for org.hibernate.criterion ProjectionList add

Introduction

In this page you can find the example usage for org.hibernate.criterion ProjectionList add.

Prototype

public ProjectionList add(Projection projection) 

Source Link

Document

Add a projection to this list of projections

Usage

From source file:org.n52.sos.ds.hibernate.dao.FeatureOfInterestDAO.java

License:Open Source License

/**
 * Load FOI identifiers and parent ids for use in the cache. Just loading the ids allows us to not load
 * the geometry columns, XML, etc.//from  ww w  .  jav a2s .  c o  m
 *
 * @param session
 * @return Map keyed by FOI identifiers, with value collections of parent FOI identifiers if supported
 */
public Map<String, Collection<String>> getFeatureOfInterestIdentifiersWithParents(final Session session) {
    Criteria criteria = session.createCriteria(FeatureOfInterest.class);
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property(FeatureOfInterest.IDENTIFIER));

    //get parents if transactional profile is active
    boolean tFoiSupported = HibernateHelper.isEntitySupported(TFeatureOfInterest.class);
    if (tFoiSupported) {
        criteria.createAlias(TFeatureOfInterest.PARENTS, "pfoi", JoinType.LEFT_OUTER_JOIN);
        projectionList.add(Projections.property("pfoi." + FeatureOfInterest.IDENTIFIER));
    }
    criteria.setProjection(projectionList);
    //return as List<Object[]> even if there's only one column for consistency
    criteria.setResultTransformer(NoopTransformerAdapter.INSTANCE);

    LOGGER.debug("QUERY getFeatureOfInterestIdentifiersWithParents(): {}",
            HibernateHelper.getSqlString(criteria));
    @SuppressWarnings("unchecked")
    List<Object[]> results = criteria.list();
    Map<String, Collection<String>> foiMap = Maps.newHashMap();
    for (Object[] result : results) {
        String featureIdentifier = (String) result[0];
        String parentFeatureIdentifier = null;
        if (tFoiSupported) {
            parentFeatureIdentifier = (String) result[1];
        }
        if (parentFeatureIdentifier != null) {
            CollectionHelper.addToCollectionMap(featureIdentifier, parentFeatureIdentifier, foiMap);
        } else {
            foiMap.put(featureIdentifier, null);
        }
    }
    return foiMap;
}

From source file:org.n52.sos.ds.hibernate.dao.ProcedureDAO.java

License:Open Source License

/**
 * Get map keyed by undeleted procedure identifiers with
 * collections of parent procedures (if supported) as values
 * @param session//  w  w w . ja  va 2  s .c  o  m
 * @return Map keyed by procedure identifier with values of parent procedure identifier collections
 */
public Map<String, Collection<String>> getProcedureIdentifiers(final Session session) {
    boolean tProcedureSupported = HibernateHelper.isEntitySupported(TProcedure.class);
    Criteria criteria = session.createCriteria(Procedure.class).add(Restrictions.eq(Procedure.DELETED, false));
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property(Procedure.IDENTIFIER));
    if (tProcedureSupported) {
        criteria.createAlias(TProcedure.PARENTS, "pp", JoinType.LEFT_OUTER_JOIN);
        projectionList.add(Projections.property("pp." + Procedure.IDENTIFIER));
    }
    criteria.setProjection(projectionList);
    //return as List<Object[]> even if there's only one column for consistency
    criteria.setResultTransformer(NoopTransformerAdapter.INSTANCE);

    LOGGER.debug("QUERY getProcedureIdentifiers(): {}", HibernateHelper.getSqlString(criteria));
    @SuppressWarnings("unchecked")
    List<Object[]> results = criteria.list();
    Map<String, Collection<String>> map = Maps.newHashMap();
    for (Object[] result : results) {
        String procedureIdentifier = (String) result[0];
        String parentProcedureIdentifier = null;
        if (tProcedureSupported) {
            parentProcedureIdentifier = (String) result[1];
        }
        if (parentProcedureIdentifier == null) {
            map.put(procedureIdentifier, null);
        } else {
            CollectionHelper.addToCollectionMap(procedureIdentifier, parentProcedureIdentifier, map);
        }
    }
    return map;
}

From source file:org.n52.sos.ds.hibernate.dao.ProcedureDAO.java

License:Open Source License

/**
 * Query procedure time extrema for the provided procedure identifier
 *
 * @param session//from   w w  w.  ja  v  a  2s.c  o  m
 * @param procedureIdentifier
 * @return ProcedureTimeExtrema
 * @throws CodedException
 */
public TimeExtrema getProcedureTimeExtrema(final Session session, String procedureIdentifier)
        throws OwsExceptionReport {
    Object[] result;
    if (isProcedureTimeExtremaNamedQuerySupported(session)) {
        return getProcedureTimeExtremaFromNamedQuery(session, procedureIdentifier);
    }
    AbstractObservationDAO observationDAO = DaoFactory.getInstance().getObservationDAO();
    Criteria criteria = observationDAO.getDefaultObservationInfoCriteria(session);
    if (observationDAO instanceof AbstractSeriesObservationDAO) {
        criteria.createAlias(SeriesObservationInfo.SERIES, "s");
        criteria.createAlias("s." + Series.PROCEDURE, "p");
    } else {
        criteria.createAlias(ObservationInfo.PROCEDURE, "p");
    }
    criteria.add(Restrictions.eq("p." + Procedure.IDENTIFIER, procedureIdentifier));
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.groupProperty("p." + Procedure.IDENTIFIER));
    projectionList.add(Projections.min(AbstractObservation.PHENOMENON_TIME_START));
    projectionList.add(Projections.max(AbstractObservation.PHENOMENON_TIME_START));
    projectionList.add(Projections.max(AbstractObservation.PHENOMENON_TIME_END));
    criteria.setProjection(projectionList);

    LOGGER.debug("QUERY getProcedureTimeExtrema(procedureIdentifier): {}",
            HibernateHelper.getSqlString(criteria));
    result = (Object[]) criteria.uniqueResult();

    return parseProcedureTimeExtremaResult(result);
}

From source file:org.n52.sos.ds.hibernate.dao.series.AbstractSeriesDAO.java

License:Open Source License

public TimeExtrema getProcedureTimeExtrema(Session session, String procedure) {
    Criteria c = getDefaultSeriesCriteria(session);
    addProcedureToCriteria(c, procedure);
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.min(Series.FIRST_TIME_STAMP));
    projectionList.add(Projections.max(Series.LAST_TIME_STAMP));
    c.setProjection(projectionList);/*from   w  ww.  jav  a 2  s . co m*/
    LOGGER.debug("QUERY getProcedureTimeExtrema(procedureIdentifier): {}", HibernateHelper.getSqlString(c));
    Object[] result = (Object[]) c.uniqueResult();

    TimeExtrema pte = new TimeExtrema();
    if (result != null) {
        pte.setMinTime(DateTimeHelper.makeDateTime(result[0]));
        pte.setMaxTime(DateTimeHelper.makeDateTime(result[1]));
    }
    return pte;
}

From source file:org.n52.sos.ds.hibernate.dao.series.AbstractSeriesValueTimeDAO.java

License:Open Source License

private void addMinMaxTimeProjection(Criteria c) {
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.min(AbstractObservationTime.PHENOMENON_TIME_START));
    projectionList.add(Projections.max(AbstractObservationTime.PHENOMENON_TIME_END));
    projectionList.add(Projections.max(AbstractObservationTime.RESULT_TIME));
    if (HibernateHelper.isColumnSupported(getSeriesValueTimeClass(), AbstractObservationTime.VALID_TIME_START)
            && HibernateHelper.isColumnSupported(getSeriesValueTimeClass(),
                    AbstractObservationTime.VALID_TIME_END)) {
        projectionList.add(Projections.min(AbstractObservationTime.VALID_TIME_START));
        projectionList.add(Projections.max(AbstractObservationTime.VALID_TIME_END));
    }/*from   w w w .ja va2s  .c om*/
    c.setProjection(projectionList);
}

From source file:org.n52.sos.ds.hibernate.GetDataAvailabilityDAO.java

License:Open Source License

/**
 * Query data availability information depending on supported functionality
 * /*from  ww w  .  ja va2  s  . c  o m*/
 * @param req
 *            GetDataAvailability request
 * @param session
 *            Hibernate session
 * @return Data availability information
 * @throws OwsExceptionReport
 *             If an error occurs
 */
private List<?> queryDataAvailabilityValues(GetDataAvailabilityRequest req, Session session)
        throws OwsExceptionReport {
    // check is named queries are supported
    if (checkForNamedQueries(req, session)) {
        return executeNamedQuery(req, session);
    }
    // check if series mapping is supporte
    else if (EntitiyHelper.getInstance().isSeriesSupported()) {
        return querySeriesDataAvailabilities(req, session);
    } else {
        Criteria c = getDefaultObservationInfoCriteria(session);

        if (req.isSetFeaturesOfInterest()) {
            c.createCriteria(ObservationInfo.FEATURE_OF_INTEREST)
                    .add(Restrictions.in(FeatureOfInterest.IDENTIFIER, req.getFeaturesOfInterest()));
        }
        if (req.isSetProcedures()) {
            c.createCriteria(ObservationInfo.PROCEDURE)
                    .add(Restrictions.in(Procedure.IDENTIFIER, req.getProcedures()));

        }
        if (req.isSetObservedProperties()) {
            c.createCriteria(ObservationInfo.OBSERVABLE_PROPERTY)
                    .add(Restrictions.in(ObservableProperty.IDENTIFIER, req.getObservedProperties()));
        }

        if (req.isSetOfferings()) {
            c.createCriteria(ObservationInfo.OFFERINGS)
                    .add(Restrictions.in(Offering.IDENTIFIER, req.getOfferings()));
        }

        ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.groupProperty(ObservationInfo.PROCEDURE))
                .add(Projections.groupProperty(ObservationInfo.OBSERVABLE_PROPERTY))
                .add(Projections.groupProperty(ObservationInfo.FEATURE_OF_INTEREST))
                .add(Projections.min(ObservationInfo.PHENOMENON_TIME_START))
                .add(Projections.max(ObservationInfo.PHENOMENON_TIME_END));
        if (isShowCount(req)) {
            projectionList.add(Projections.rowCount());
        }
        c.setProjection(projectionList);
        c.setResultTransformer(new DataAvailabilityTransformer(session));
        LOGGER.debug("QUERY getDataAvailability(request): {}", HibernateHelper.getSqlString(c));
        List<?> list = c.list();
        if (isIncludeResultTime(req)) {
            for (Object o : list) {
                DataAvailability dataAvailability = (DataAvailability) o;
                dataAvailability.setResultTimes(getResultTimesFromObservation(dataAvailability, req, session));
            }
        }
        return list;
    }
}

From source file:org.onebusaway.transit_data_federation.impl.reporting.UserReportingDaoImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from www .j a  v a  2  s.  co m*/
public List<T2<Object, Integer>> getTripProblemReportSummaries(final TripProblemReportQueryBean query,
        final ETripProblemGroupBy groupBy) {

    List<Object[]> rows = _template.executeFind(new HibernateCallback<List<Object[]>>() {

        @Override
        public List<Object[]> doInHibernate(Session session) throws HibernateException, SQLException {

            Criteria c = session.createCriteria(TripProblemReportRecord.class);

            ProjectionList projections = Projections.projectionList();
            projections.add(Projections.rowCount());
            switch (groupBy) {
            case TRIP:
                projections.add(Projections.groupProperty("tripId.agencyId"));
                projections.add(Projections.groupProperty("tripId.id"));
                break;
            case STATUS:
                projections.add(Projections.groupProperty("status"));
                break;
            case LABEL:
                projections.add(Projections.groupProperty("label"));
                break;
            }
            c.setProjection(projections);

            addQueryToCriteria(query, c);

            return c.list();
        }
    });

    List<T2<Object, Integer>> results = new ArrayList<T2<Object, Integer>>(rows.size());

    for (Object[] row : rows) {
        Integer count = (Integer) row[0];
        Object key = getKeyForTripProblemReportSummariesRow(row, groupBy);
        results.add(Tuples.tuple(key, count.intValue()));
    }

    return results;
}

From source file:org.openbravo.advpaymentmngt.ad_reports.ReportReconciliation.java

License:Open Source License

/**
 * Calculates the sum of outstanding payments/deposits applying the following filters:
 * //w w  w . j a  v  a2s. c om
 * - They belong to the financial account of the reconciliation.
 * 
 * - The transaction date must be lower than the ending date of the reconciliation.
 * 
 * - They do not belong to any reconciliation.
 * 
 * @param recon
 *          Reconciliation
 * @return List with 2 values. The first one is the sum of outstanding payments (transactions) and
 *         the second is the sum of outstanding deposits (transactions).
 */
private List<BigDecimal> getOutstandingPaymentAndDepositTotal(FIN_Reconciliation recon) {
    List<BigDecimal> outList = new ArrayList<BigDecimal>();
    OBContext.setAdminMode(true);
    try {
        OBCriteria<FIN_FinaccTransaction> obcTrans = OBDal.getInstance()
                .createCriteria(FIN_FinaccTransaction.class);
        obcTrans.add(Restrictions.eq(FIN_FinaccTransaction.PROPERTY_ACCOUNT, recon.getAccount()));
        obcTrans.add(Restrictions.eq(FIN_FinaccTransaction.PROPERTY_PROCESSED, true));
        obcTrans.add(Restrictions.le(FIN_FinaccTransaction.PROPERTY_TRANSACTIONDATE, recon.getEndingDate()));
        List<FIN_Reconciliation> afterReconciliations = MatchTransactionDao
                .getReconciliationListAfterDate(recon);
        if (afterReconciliations.size() > 0) {
            obcTrans.add(Restrictions.or(Restrictions.isNull(FIN_FinaccTransaction.PROPERTY_RECONCILIATION),
                    Restrictions.in(FIN_FinaccTransaction.PROPERTY_RECONCILIATION, afterReconciliations)));
        } else {
            obcTrans.add(Restrictions.isNull(FIN_FinaccTransaction.PROPERTY_RECONCILIATION));
        }

        ProjectionList projections = Projections.projectionList();
        projections.add(Projections.sum(FIN_FinaccTransaction.PROPERTY_PAYMENTAMOUNT));
        projections.add(Projections.sum(FIN_FinaccTransaction.PROPERTY_DEPOSITAMOUNT));
        obcTrans.setProjection(projections);

        if (obcTrans.list() != null && obcTrans.list().size() > 0) {
            @SuppressWarnings("rawtypes")
            List o = obcTrans.list();
            Object[] resultSet = (Object[]) o.get(0);
            BigDecimal paymentAmt = (resultSet[0] != null) ? (BigDecimal) resultSet[0] : BigDecimal.ZERO;
            BigDecimal depositAmt = (resultSet[1] != null) ? (BigDecimal) resultSet[1] : BigDecimal.ZERO;
            outList.add(paymentAmt);
            outList.add(depositAmt);
        }

    } finally {
        OBContext.restorePreviousMode();
    }

    return outList;
}

From source file:org.openbravo.advpaymentmngt.ad_reports.ReportReconciliation.java

License:Open Source License

/**
 * Calculates the sum of un-reconciled bank statement lines applying the following filters:
 * //from   w w w.  j  a  va2 s.c o  m
 * - They belong to the financial account of the reconciliation.
 * 
 * - The transaction date must be lower than the ending date of the reconciliation.
 * 
 * - They are not matched with any transaction.
 * 
 * @param recon
 *          Reconciliation
 * @return Sum of the un-reconciled bank statement lines.
 */
private BigDecimal getUnreconciledBankStatmentLinesTotal(FIN_Reconciliation recon) {
    BigDecimal total = BigDecimal.ZERO;
    OBContext.setAdminMode(true);
    try {
        OBCriteria<FIN_BankStatementLine> obcBsl = OBDal.getInstance()
                .createCriteria(FIN_BankStatementLine.class);
        obcBsl.createAlias(FIN_BankStatementLine.PROPERTY_BANKSTATEMENT, "bs");
        obcBsl.createAlias(FIN_BankStatementLine.PROPERTY_FINANCIALACCOUNTTRANSACTION, "tr",
                OBCriteria.LEFT_JOIN);
        obcBsl.add(Restrictions.le(FIN_BankStatementLine.PROPERTY_TRANSACTIONDATE, recon.getEndingDate()));
        List<FIN_Reconciliation> afterReconciliations = MatchTransactionDao
                .getReconciliationListAfterDate(recon);
        if (afterReconciliations.size() > 0) {
            obcBsl.add(Restrictions.or(
                    Restrictions.isNull(FIN_BankStatementLine.PROPERTY_FINANCIALACCOUNTTRANSACTION),
                    Restrictions.in("tr." + FIN_FinaccTransaction.PROPERTY_RECONCILIATION,
                            afterReconciliations)));
        } else {
            obcBsl.add(Restrictions.isNull(FIN_BankStatementLine.PROPERTY_FINANCIALACCOUNTTRANSACTION));
        }
        obcBsl.add(Restrictions.eq("bs." + FIN_BankStatement.PROPERTY_ACCOUNT, recon.getAccount()));
        obcBsl.add(Restrictions.eq("bs." + FIN_BankStatement.PROPERTY_PROCESSED, true));
        ProjectionList projections = Projections.projectionList();
        projections.add(Projections.sum(FIN_BankStatementLine.PROPERTY_CRAMOUNT));
        projections.add(Projections.sum(FIN_BankStatementLine.PROPERTY_DRAMOUNT));
        obcBsl.setProjection(projections);

        if (obcBsl.list() != null && obcBsl.list().size() > 0) {
            @SuppressWarnings("rawtypes")
            List o = obcBsl.list();
            Object[] resultSet = (Object[]) o.get(0);
            BigDecimal credit = (resultSet[0] != null) ? (BigDecimal) resultSet[0] : BigDecimal.ZERO;
            BigDecimal debit = (resultSet[1] != null) ? (BigDecimal) resultSet[1] : BigDecimal.ZERO;
            total = credit.subtract(debit);
        }

    } finally {
        OBContext.restorePreviousMode();
    }

    return total;
}

From source file:org.openbravo.advpaymentmngt.ad_reports.ReportReconciliation.java

License:Open Source License

/**
 * Calculates the sum of all the transactions in a higher date than the end date of the given
 * reconciliation./*from   ww w . j  a  v  a 2 s .co m*/
 * 
 * @param recon
 *          Reconciliation.
 * @return Sum of all the transactions in a higher date than the end date of the given
 *         reconciliation.
 */
private BigDecimal getTransactionsTotalAfterReconciliationEndDate(FIN_Reconciliation recon) {
    BigDecimal balance = BigDecimal.ZERO;
    OBContext.setAdminMode(true);
    try {
        OBCriteria<FIN_FinaccTransaction> obcTrans = OBDal.getInstance()
                .createCriteria(FIN_FinaccTransaction.class);
        obcTrans.add(Restrictions.eq(FIN_FinaccTransaction.PROPERTY_ACCOUNT, recon.getAccount()));
        obcTrans.add(Restrictions.eq(FIN_FinaccTransaction.PROPERTY_PROCESSED, true));
        obcTrans.add(Restrictions.gt(FIN_FinaccTransaction.PROPERTY_TRANSACTIONDATE, recon.getEndingDate()));
        ProjectionList projections = Projections.projectionList();
        projections.add(Projections.sum(FIN_FinaccTransaction.PROPERTY_PAYMENTAMOUNT));
        projections.add(Projections.sum(FIN_FinaccTransaction.PROPERTY_DEPOSITAMOUNT));
        obcTrans.setProjection(projections);

        if (obcTrans.list() != null && obcTrans.list().size() > 0) {
            @SuppressWarnings("rawtypes")
            List o = obcTrans.list();
            Object[] resultSet = (Object[]) o.get(0);
            BigDecimal paymentAmt = (resultSet[0] != null) ? (BigDecimal) resultSet[0] : BigDecimal.ZERO;
            BigDecimal depositAmt = (resultSet[1] != null) ? (BigDecimal) resultSet[1] : BigDecimal.ZERO;
            balance = depositAmt.subtract(paymentAmt);
        }

    } finally {
        OBContext.restorePreviousMode();
    }

    return balance;
}