Search in sources :

Example 1 with AbstractObservationDAO

use of org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO in project SOS by 52North.

the class OfferingDAO method getMaxDate4Offering.

/**
 * Get max time from observations for offering
 *
 * @param offering
 *            Offering identifier
 * @param session
 *            Hibernate session Hibernate session
 * @return max time for offering
 * @throws CodedException If an error occurs
 */
public DateTime getMaxDate4Offering(final String offering, final Session session) throws OwsExceptionReport {
    Object maxStart;
    Object maxEnd;
    if (HibernateHelper.isNamedQuerySupported(SQL_QUERY_GET_MAX_DATE_FOR_OFFERING, session)) {
        Query namedQuery = session.getNamedQuery(SQL_QUERY_GET_MAX_DATE_FOR_OFFERING);
        namedQuery.setParameter(OFFERING, offering);
        LOGGER.debug("QUERY getMaxDate4Offering(offering) with NamedQuery: {}", SQL_QUERY_GET_MAX_DATE_FOR_OFFERING);
        maxStart = namedQuery.uniqueResult();
        maxEnd = maxStart;
    } else {
        AbstractObservationDAO observationDAO = getDaoFactory().getObservationDAO();
        Criteria cstart = observationDAO.getDefaultObservationInfoCriteria(session);
        Criteria cend = observationDAO.getDefaultObservationInfoCriteria(session);
        addOfferingRestricionForObservation(cstart, offering);
        addOfferingRestricionForObservation(cend, offering);
        addMinMaxProjection(cstart, MinMax.MAX, DataEntity.PROPERTY_SAMPLING_TIME_START);
        addMinMaxProjection(cend, MinMax.MAX, DataEntity.PROPERTY_SAMPLING_TIME_END);
        LOGGER.debug("QUERY getMaxDate4Offering(offering) start: {}", HibernateHelper.getSqlString(cstart));
        LOGGER.debug("QUERY getMaxDate4Offering(offering) end: {}", HibernateHelper.getSqlString(cend));
        if (HibernateHelper.getSqlString(cstart).equals(HibernateHelper.getSqlString(cend))) {
            maxStart = cstart.uniqueResult();
            maxEnd = maxStart;
            LOGGER.debug("Max time start and end query are identically, only one query is executed!");
        } else {
            maxStart = cstart.uniqueResult();
            maxEnd = cend.uniqueResult();
        }
    }
    if (maxStart == null && maxEnd == null) {
        return null;
    } else {
        final DateTime start = new DateTime(maxStart, DateTimeZone.UTC);
        if (maxEnd != null) {
            final DateTime end = new DateTime(maxEnd, DateTimeZone.UTC);
            if (end.isAfter(start)) {
                return end;
            }
        }
        return start;
    }
}
Also used : Query(org.hibernate.query.Query) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) AbstractObservationDAO(org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO) DateTime(org.joda.time.DateTime)

Example 2 with AbstractObservationDAO

use of org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO in project SOS by 52North.

the class ProcedureDAO method getMaxDate4Procedure.

/**
 * Get max time from observations for procedure
 *
 * @param procedure
 *            ProcedureEntity identifier
 * @param session
 *            Hibernate session
 * @return max time for procedure
 * @throws CodedException
 *             If an error occurs
 */
public DateTime getMaxDate4Procedure(final String procedure, final Session session) throws OwsExceptionReport {
    Object maxStart = null;
    Object maxEnd = null;
    if (HibernateHelper.isNamedQuerySupported(SQL_QUERY_GET_MAX_DATE_FOR_PROCEDURE, session)) {
        Query namedQuery = session.getNamedQuery(SQL_QUERY_GET_MAX_DATE_FOR_PROCEDURE);
        namedQuery.setParameter(PROCEDURE, procedure);
        LOGGER.trace("QUERY getMaxDate4Procedure(procedure) with NamedQuery: {}", SQL_QUERY_GET_MAX_DATE_FOR_PROCEDURE);
        maxStart = namedQuery.uniqueResult();
        maxEnd = maxStart;
    } else {
        AbstractObservationDAO observationDAO = getDaoFactory().getObservationDAO();
        Criteria cstart = observationDAO.getDefaultObservationInfoCriteria(session);
        Criteria cend = observationDAO.getDefaultObservationInfoCriteria(session);
        if (observationDAO instanceof SeriesObservationDAO) {
            addProcedureRestrictionForSeries(cstart, procedure);
            addProcedureRestrictionForSeries(cend, procedure);
        } else {
            addProcedureRestrictionForObservation(cstart, procedure);
            addProcedureRestrictionForObservation(cend, procedure);
        }
        addMinMaxProjection(cstart, MinMax.MAX, DataEntity.PROPERTY_SAMPLING_TIME_START);
        addMinMaxProjection(cend, MinMax.MAX, DataEntity.PROPERTY_SAMPLING_TIME_END);
        LOGGER.trace("QUERY getMaxDate4Procedure(procedure) start: {}", HibernateHelper.getSqlString(cstart));
        LOGGER.trace("QUERY getMaxDate4Procedure(procedure) end: {}", HibernateHelper.getSqlString(cend));
        if (HibernateHelper.getSqlString(cstart).endsWith(HibernateHelper.getSqlString(cend))) {
            maxStart = cstart.uniqueResult();
            maxEnd = maxStart;
            LOGGER.trace("Max time start and end query are identically, only one query is executed!");
        } else {
            maxStart = cstart.uniqueResult();
            maxEnd = cend.uniqueResult();
        }
    }
    if (maxStart == null && maxEnd == null) {
        return null;
    } else {
        final DateTime start = new DateTime(maxStart, DateTimeZone.UTC);
        if (maxEnd != null) {
            final DateTime end = new DateTime(maxEnd, DateTimeZone.UTC);
            if (end.isAfter(start)) {
                return end;
            }
        }
        return start;
    }
}
Also used : Query(org.hibernate.query.Query) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) AbstractObservationDAO(org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO) SeriesObservationDAO(org.n52.sos.ds.hibernate.dao.observation.series.SeriesObservationDAO) DateTime(org.joda.time.DateTime)

Example 3 with AbstractObservationDAO

use of org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO in project SOS by 52North.

the class ProcedureDAO method getProcedureTimeExtrema.

@SuppressWarnings("unchecked")
public Map<String, TimeExtrema> getProcedureTimeExtrema(Session session) throws OwsExceptionReport {
    List<ProcedureTimeExtrema> results = null;
    if (isAllProcedureTimeExtremaNamedQuerySupported(session)) {
        Query namedQuery = session.getNamedQuery(SQL_QUERY_GET_ALL_PROCEDURE_TIME_EXTREMA);
        LOGGER.trace("QUERY getProcedureTimeExtrema() with NamedQuery '{}': {}", SQL_QUERY_GET_ALL_PROCEDURE_TIME_EXTREMA, namedQuery.getQueryString());
        namedQuery.setResultTransformer(transformer);
        results = namedQuery.list();
    } else {
        AbstractSeriesDAO seriesDAO = getDaoFactory().getSeriesDAO();
        if (seriesDAO != null) {
            Criteria c = seriesDAO.getDefaultSeriesCriteria(session);
            c.createAlias(DatasetEntity.PROPERTY_PROCEDURE, "p");
            c.setProjection(Projections.projectionList().add(Projections.groupProperty(P_PREFIX + ProcedureEntity.IDENTIFIER)).add(Projections.min(DatasetEntity.PROPERTY_FIRST_VALUE_AT)).add(Projections.max(DatasetEntity.PROPERTY_LAST_VALUE_AT)));
            LOGGER.trace(QUERY_TIME_EXTREMA_LOG_TEMPLATE, HibernateHelper.getSqlString(c));
            c.setResultTransformer(transformer);
            results = c.list();
        }
        if (checkHasNoProcedureTimeResult(results)) {
            AbstractObservationDAO observationDAO = getDaoFactory().getObservationDAO();
            Criteria criteria = observationDAO.getDefaultObservationTimeCriteria(session);
            String alias = observationDAO.addProcedureAlias(criteria);
            criteria.setProjection(Projections.projectionList().add(Projections.groupProperty(alias + ProcedureEntity.IDENTIFIER)).add(Projections.min(DataEntity.PROPERTY_SAMPLING_TIME_START)).add(Projections.max(DataEntity.PROPERTY_SAMPLING_TIME_START)).add(Projections.max(DataEntity.PROPERTY_SAMPLING_TIME_START)));
            LOGGER.trace(QUERY_TIME_EXTREMA_LOG_TEMPLATE, HibernateHelper.getSqlString(criteria));
            criteria.setResultTransformer(transformer);
            results = criteria.list();
        }
    }
    Map<String, TimeExtrema> procedureTimeExtrema = Maps.newHashMap();
    if (results != null && !results.isEmpty()) {
        for (ProcedureTimeExtrema pte : results) {
            if (pte != null && pte.isSetProcedure()) {
                procedureTimeExtrema.put(pte.getProcedure(), pte);
            }
        }
    }
    return procedureTimeExtrema;
}
Also used : Query(org.hibernate.query.Query) ProcedureTimeExtrema(org.n52.sos.ds.hibernate.util.ProcedureTimeExtrema) TimeExtrema(org.n52.sos.ds.hibernate.util.TimeExtrema) ProcedureTimeExtrema(org.n52.sos.ds.hibernate.util.ProcedureTimeExtrema) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) AbstractObservationDAO(org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO) AbstractSeriesDAO(org.n52.sos.ds.hibernate.dao.observation.series.AbstractSeriesDAO)

Example 4 with AbstractObservationDAO

use of org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO in project SOS by 52North.

the class InsertObservationHandler method insertObservation.

private void insertObservation(OmObservation sosObservation, InsertObservationCache cache, CompositeOwsException exceptions, Session session) throws OwsExceptionReport, CodedException {
    checkSpatialFilteringProfile(sosObservation);
    OmObservationConstellation sosObsConst = sosObservation.getObservationConstellation();
    cache.addOfferings(sosObsConst.getOfferings());
    AbstractFeatureEntity hFeature = null;
    // if (sosObsConst.getOfferings().size() > 1) {
    // 
    // }
    String offeringID = sosObsConst.getOfferings().iterator().next();
    DatasetEntity hDataset = cache.get(sosObsConst, offeringID);
    if (hDataset == null) {
        if (!cache.isChecked(sosObsConst, offeringID)) {
            try {
                hDataset = getDaoFactory().getSeriesDAO().checkSeries(sosObsConst, offeringID, session, Sos2Constants.InsertObservationParams.observationType.name());
                // add to cache table
                cache.putConstellation(sosObsConst, offeringID, hDataset);
            } catch (OwsExceptionReport owse) {
                exceptions.add(owse);
            }
            // mark as checked
            cache.checkConstellation(sosObsConst, offeringID);
        }
    }
    if (hDataset != null) {
        // getFeature feature from local cache or create if necessary
        hFeature = getFeature(sosObsConst.getFeatureOfInterest(), cache, session);
        // AbstractFeature/offering combo
        if (!cache.isChecked(sosObsConst.getFeatureOfInterest(), offeringID)) {
            getDaoFactory().getFeatureOfInterestDAO().checkOrInsertRelatedFeatureRelation(hFeature, hDataset.getOffering(), session);
            cache.checkFeature(sosObsConst.getFeatureOfInterest(), offeringID);
        }
        AbstractObservationDAO observationDAO = getDaoFactory().getObservationDAO();
        DatasetEntity dataset = null;
        if (sosObservation.getValue() instanceof SingleObservationValue) {
            dataset = observationDAO.insertObservationSingleValue(hDataset, hFeature, sosObservation, cache.getCodespaceCache(), cache.getUnitCache(), cache.getFormatCache(), session);
        } else if (sosObservation.getValue() instanceof MultiObservationValues) {
            dataset = observationDAO.insertObservationMultiValue(hDataset, hFeature, sosObservation, cache.getCodespaceCache(), cache.getUnitCache(), cache.getFormatCache(), session);
        }
        if (dataset != null && !cache.get(sosObsConst, offeringID).equals(dataset)) {
            cache.putConstellation(sosObsConst, offeringID, dataset);
        }
    }
}
Also used : SingleObservationValue(org.n52.shetland.ogc.om.SingleObservationValue) AbstractFeatureEntity(org.n52.series.db.beans.AbstractFeatureEntity) DatasetEntity(org.n52.series.db.beans.DatasetEntity) OmObservationConstellation(org.n52.shetland.ogc.om.OmObservationConstellation) AbstractObservationDAO(org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO) OwsExceptionReport(org.n52.shetland.ogc.ows.exception.OwsExceptionReport) MultiObservationValues(org.n52.shetland.ogc.om.MultiObservationValues)

Example 5 with AbstractObservationDAO

use of org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO in project SOS by 52North.

the class HibernateObservationBuilder method getSeries.

protected DatasetEntity getSeries(OfferingEntity offering, DataEntity o) throws OwsExceptionReport {
    AbstractObservationDAO observationDAO = daoFactory.getObservationDAO();
    SeriesObservationFactory observationFactory = (SeriesObservationFactory) observationDAO.getObservationFactory();
    Criteria criteria = session.createCriteria(observationFactory.seriesClass()).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).add(Restrictions.eq(DatasetEntity.PROPERTY_FEATURE, getFeatureOfInterest())).add(Restrictions.eq(DatasetEntity.PROPERTY_PHENOMENON, getObservableProperty())).add(Restrictions.eq(DatasetEntity.PROPERTY_PROCEDURE, getProcedure())).add(Restrictions.eq(DatasetEntity.PROPERTY_OFFERING, offering));
    DatasetEntity series = (DatasetEntity) criteria.uniqueResult();
    if (series == null) {
        series = (DatasetEntity) daoFactory.getSeriesDAO().getDatasetFactory().visit(o);
        series.setObservableProperty(getObservableProperty());
        series.setProcedure(getProcedure());
        series.setCategory(getCategory());
        series.setPlatform(getPlatform());
        series.setFeature(getFeatureOfInterest());
        series.setOffering(offering);
        series.setDeleted(false);
        series.setPublished(true);
        if (series.hasEreportingProfile()) {
            series.getEreportingProfile().setSamplingPoint(getEReportingSamplingPoint());
        }
        session.save(series);
        session.flush();
        session.refresh(series);
    } else if (series.isDeleted()) {
        series.setDeleted(false);
        session.update(series);
        session.flush();
        session.refresh(series);
    }
    return series;
}
Also used : DatasetEntity(org.n52.series.db.beans.DatasetEntity) SeriesObservationFactory(org.n52.sos.ds.hibernate.dao.observation.series.SeriesObservationFactory) Criteria(org.hibernate.Criteria) AbstractObservationDAO(org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO)

Aggregations

AbstractObservationDAO (org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO)10 Criteria (org.hibernate.Criteria)7 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)5 Query (org.hibernate.query.Query)4 DateTime (org.joda.time.DateTime)3 DatasetEntity (org.n52.series.db.beans.DatasetEntity)3 AbstractFeatureEntity (org.n52.series.db.beans.AbstractFeatureEntity)2 MultiObservationValues (org.n52.shetland.ogc.om.MultiObservationValues)2 OmObservationConstellation (org.n52.shetland.ogc.om.OmObservationConstellation)2 SingleObservationValue (org.n52.shetland.ogc.om.SingleObservationValue)2 AbstractSeriesDAO (org.n52.sos.ds.hibernate.dao.observation.series.AbstractSeriesDAO)2 SeriesObservationDAO (org.n52.sos.ds.hibernate.dao.observation.series.SeriesObservationDAO)2 SeriesObservationFactory (org.n52.sos.ds.hibernate.dao.observation.series.SeriesObservationFactory)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 HashMap (java.util.HashMap)1 PersistenceException (javax.persistence.PersistenceException)1 HibernateException (org.hibernate.HibernateException)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1 ProjectionList (org.hibernate.criterion.ProjectionList)1