Example usage for org.hibernate.criterion Projections max

List of usage examples for org.hibernate.criterion Projections max

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections max.

Prototype

public static AggregateProjection max(String propertyName) 

Source Link

Document

A property maximum value projection

Usage

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

License:Open Source License

/**
 * Query data availability information depending on supported functionality
 * /*from   w w w .  jav  a2  s .  c om*/
 * @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.n52.sos.ds.hibernate.util.HibernateCriteriaQueryUtilities.java

License:Open Source License

/**
 * Get max phenomenon from observations//from  w w  w.j a  va2  s.c o m
 * 
 * @param session
 *            Hibernate session
 * 
 * @return max time
 */
@Deprecated
public static DateTime getMaxResultTime(Session session) {
    Object max = session.createCriteria(Observation.class)
            .setProjection(Projections.max(Observation.RESULT_TIME))
            .add(Restrictions.eq(Observation.DELETED, false)).uniqueResult();
    if (max == null) {
        return null;
    } else {
        return new DateTime(max);
    }
}

From source file:org.n52.sos.ds.hibernate.util.HibernateCriteriaQueryUtilities.java

License:Open Source License

/**
 * @param session/*ww w .  j a v  a  2 s.c  om*/
 *            the session
 * 
 * @return the global getEqualRestiction bounding box over all observations,
 *         or <tt>null</tt>
 */
@Deprecated
public static TimePeriod getGlobalTemporalBoundingBox(Session session) {
    if (session != null) {
        Criteria criteria = session.createCriteria(Observation.class);
        criteria.add(Restrictions.eq(Observation.DELETED, false));
        criteria.setProjection(
                Projections.projectionList().add(Projections.min(Observation.PHENOMENON_TIME_START))
                        .add(Projections.max(Observation.PHENOMENON_TIME_START))
                        .add(Projections.max(Observation.PHENOMENON_TIME_END)));
        Object temporalBoundingBox = criteria.uniqueResult();
        if (temporalBoundingBox instanceof Object[]) {
            Object[] record = (Object[]) temporalBoundingBox;
            TimePeriod bBox = createTimePeriod((Timestamp) record[0], (Timestamp) record[1],
                    (Timestamp) record[2]);
            return bBox;
        }
    }
    return null;
}

From source file:org.n52.sos.ds.hibernate.util.HibernateCriteriaQueryUtilities.java

License:Open Source License

/**
 * @return a Map containing the bounding box for each offering
 *///  w ww.  java 2 s.c o m
@Deprecated
public static Map<String, TimePeriod> getTemporalBoundingBoxesForOfferings(Session session) {
    if (session != null) {
        Criteria criteria = session.createCriteria(Observation.class)
                .add(Restrictions.eq(Observation.DELETED, false));
        criteria.createAlias(Observation.OFFERINGS, "off");
        criteria.setProjection(
                Projections.projectionList().add(Projections.min(Observation.PHENOMENON_TIME_START))
                        .add(Projections.max(Observation.PHENOMENON_TIME_START))
                        .add(Projections.max(Observation.PHENOMENON_TIME_END))
                        .add(Projections.groupProperty("off." + Offering.IDENTIFIER)));

        List<?> temporalBoundingBoxes = criteria.list();
        if (!temporalBoundingBoxes.isEmpty()) {
            HashMap<String, TimePeriod> temporalBBoxMap = new HashMap<String, TimePeriod>(
                    temporalBoundingBoxes.size());
            for (Object recordObj : temporalBoundingBoxes) {
                if (recordObj instanceof Object[]) {
                    Object[] record = (Object[]) recordObj;
                    TimePeriod value = createTimePeriod((Timestamp) record[0], (Timestamp) record[1],
                            (Timestamp) record[2]);
                    temporalBBoxMap.put((String) record[3], value);
                }
            }
            LOGGER.debug(temporalBoundingBoxes.toString());
            return temporalBBoxMap;
        }
    }
    return new HashMap<String, TimePeriod>(0);
}

From source file:org.n52.sos.ds.hibernate.util.HibernateCriteriaQueryUtilities.java

License:Open Source License

/**
 * Get max phenomenon from observations/*from   www  . j  a  va  2s  .  c  o  m*/
 * 
 * @param session
 *            Hibernate session
 * 
 * @return max time
 */
@Deprecated
public static DateTime getMaxPhenomenonTime(Session session) {
    Object maxStart = session.createCriteria(Observation.class)
            .setProjection(Projections.max(Observation.PHENOMENON_TIME_START))
            .add(Restrictions.eq(Observation.DELETED, false)).uniqueResult();
    Object maxEnd = session.createCriteria(Observation.class)
            .setProjection(Projections.max(Observation.PHENOMENON_TIME_END))
            .add(Restrictions.eq(Observation.DELETED, false)).uniqueResult();
    if (maxStart == null && maxEnd == null) {
        return null;
    } else {
        DateTime start = new DateTime(maxStart);
        if (maxEnd != null) {
            DateTime end = new DateTime(maxEnd);
            if (end.isAfter(start)) {
                return end;
            }
        }
        return start;
    }
}

From source file:org.n52.sos.ds.hibernate.util.HibernateCriteriaQueryUtilities.java

License:Open Source License

/**
 * Get max time from observations for procedure
 * //from  www  .ja va  2  s  . c om
 * @param procedure
 *            Procedure identifier
 * @param session
 *            Hibernate session
 * @return max time for procedure
 */
@Deprecated
public static DateTime getMaxDate4Procedure(String procedure, Session session) {
    Criteria cstart = session.createCriteria(Observation.class).add(Restrictions.eq(Observation.DELETED, false))
            .setProjection(Projections.max(Observation.PHENOMENON_TIME_START))
            .createCriteria(Observation.PROCEDURE).add(Restrictions.eq(Procedure.IDENTIFIER, procedure));

    Object maxStart = cstart.uniqueResult();

    Criteria cend = session.createCriteria(Observation.class).add(Restrictions.eq(Observation.DELETED, false))
            .setProjection(Projections.max(Observation.PHENOMENON_TIME_END))
            .createCriteria(Observation.PROCEDURE).add(Restrictions.eq(Procedure.IDENTIFIER, procedure));

    Object maxEnd = cend.uniqueResult();

    if (maxStart == null && maxEnd == null) {
        return null;
    } else {
        DateTime start = new DateTime(maxStart);
        if (maxEnd != null) {
            DateTime end = new DateTime(maxEnd);
            if (end.isAfter(start)) {
                return end;
            }
        }
        return start;
    }
}

From source file:org.n52.sos.ds.hibernate.util.HibernateCriteriaQueryUtilities.java

License:Open Source License

/**
 * Get max result time from observations for offering
 * //ww  w .  j  a va 2s . co  m
 * @param offering
 *            Offering identifier
 * @param session
 *            Hibernate session
 * 
 * @return max result time for offering
 */
@Deprecated
public static DateTime getMaxResultTime4Offering(String offering, Session session) {
    Criteria c = session.createCriteria(Observation.class).add(Restrictions.eq(Observation.DELETED, false))
            .setProjection(Projections.max(Observation.RESULT_TIME));
    c.createCriteria(Observation.OFFERINGS).add(Restrictions.eq(Offering.IDENTIFIER, offering));
    Object maxStart = c.uniqueResult();
    if (maxStart == null) {
        return null;
    } else {
        return new DateTime(maxStart);
    }
}

From source file:org.nema.medical.mint.server.domain.ChangeDAO.java

License:Apache License

@SuppressWarnings("unchecked")
public Change findLastChange(final String studyID) {
    final DetachedCriteria maxChangeIndex = DetachedCriteria.forClass(Change.class)
            .setProjection(Projections.max("changeIndex")).add(Restrictions.eq("studyID", studyID));
    final DetachedCriteria lastChange = DetachedCriteria.forClass(Change.class)
            .add(Restrictions.eq("studyID", studyID)).add(Property.forName("changeIndex").eq(maxChangeIndex));
    final List<Change> list = getHibernateTemplate().findByCriteria(lastChange);
    if (!list.isEmpty()) {
        assert list.size() == 1;
        return list.get(0);
    }/*from w  w w.j a  v  a2  s.  c o  m*/
    return null;
}

From source file:org.openbravo.advpaymentmngt.process.FIN_AddPayment.java

License:Open Source License

/**
 * Returns the date in which last payment for this invoice took place
 *//*from  w  w w  .j a  v  a 2 s.c  om*/
private static Date getFinalSettlementDate(Invoice invoice) {
    final OBCriteria<FIN_PaymentSchedInvV> obc = OBDal.getInstance().createCriteria(FIN_PaymentSchedInvV.class);
    OBContext.setAdminMode();
    try {
        obc.add(Restrictions.eq(FIN_PaymentSchedInvV.PROPERTY_INVOICE, invoice));
        obc.setProjection(Projections.max(FIN_PaymentSchedInvV.PROPERTY_LASTPAYMENT));
        return (Date) obc.uniqueResult();
    } finally {
        OBContext.restorePreviousMode();
    }
}

From source file:org.openbravo.advpaymentmngt.process.FIN_PaymentMonitorProcess.java

License:Open Source License

/**
 * Returns the date in which last payment for this invoice took place
 *///from w  w  w .ja  v  a  2s. com
private static Date getFinalSettlementDate(Invoice invoice) {
    final OBCriteria<FIN_PaymentSchedInvV> obc = OBDal.getInstance().createCriteria(FIN_PaymentSchedInvV.class);
    // For Background process execution at system level
    if (OBContext.getOBContext().isInAdministratorMode()) {
        obc.setFilterOnReadableClients(false);
        obc.setFilterOnReadableOrganization(false);
    }
    obc.add(Restrictions.eq(FIN_PaymentSchedInvV.PROPERTY_INVOICE, invoice));
    obc.setProjection(Projections.max(FIN_PaymentSchedInvV.PROPERTY_LASTPAYMENT));
    Object o = obc.list().get(0);
    if (o != null) {
        return ((Date) o);
    } else {
        return null;
    }
}