Search in sources :

Example 1 with ObservationContext

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

the class EReportingObservationDAO method fillObservationContext.

@Override
protected ObservationContext fillObservationContext(ObservationContext ctx, OmObservation omObservation, Session session) {
    if (ctx instanceof EReportingObservationContext) {
        EReportingObservationContext ectx = (EReportingObservationContext) ctx;
        AqdSamplingPoint samplingPoint = null;
        if (omObservation.isSetParameter()) {
            samplingPoint = new AqdSamplingPoint();
            List<NamedValue<?>> remove = Lists.newArrayList();
            for (NamedValue<?> namedValue : omObservation.getParameter()) {
                if (checkForSamplingPoint(namedValue.getName())) {
                    addSamplingPointParameterValuesToAqdSamplingPoint(samplingPoint, namedValue.getValue());
                    remove.add(namedValue);
                } else if (checkForAssessmentType(namedValue.getName())) {
                    addAssessmentTypeParameterValuesToAqdSamplingPoint(samplingPoint, namedValue.getValue());
                    remove.add(namedValue);
                }
            }
            omObservation.getParameter().removeAll(remove);
            EReportingSamplingPointDAO dao = new EReportingSamplingPointDAO(getDaoFactory());
            ectx.setSamplingPoint(dao.getOrInsert(samplingPoint, session));
        }
        if (samplingPoint == null && omObservation.getObservationConstellation().isSetFeatureOfInterest()) {
            samplingPoint = new AqdSamplingPoint();
            AbstractFeature featureOfInterest = omObservation.getObservationConstellation().getFeatureOfInterest();
            addSamplingPointParameterValuesToAqdSamplingPoint(samplingPoint, new ReferenceValue(new ReferenceType(featureOfInterest.getIdentifier(), featureOfInterest.isSetName() ? featureOfInterest.getFirstName().getValue() : "")));
            EReportingSamplingPointDAO dao = new EReportingSamplingPointDAO(getDaoFactory());
            ectx.setSamplingPoint(dao.getOrInsert(samplingPoint, session));
        }
        if (samplingPoint != null && samplingPoint.getAssessmentType() == null) {
            addAssessmentTypeParameterValuesToAqdSamplingPoint(samplingPoint, new TextValue(AssessmentType.Fixed.name()));
        }
    }
    return ctx;
}
Also used : ReferenceValue(org.n52.shetland.ogc.om.values.ReferenceValue) TextValue(org.n52.shetland.ogc.om.values.TextValue) AbstractFeature(org.n52.shetland.ogc.gml.AbstractFeature) NamedValue(org.n52.shetland.ogc.om.NamedValue) EReportingObservationContext(org.n52.sos.ds.hibernate.dao.ereporting.EReportingObservationContext) AqdSamplingPoint(org.n52.shetland.aqd.AqdSamplingPoint) ReferenceType(org.n52.shetland.ogc.gml.ReferenceType) EReportingSamplingPointDAO(org.n52.sos.ds.hibernate.dao.ereporting.EReportingSamplingPointDAO)

Example 2 with ObservationContext

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

the class ObservationPersister method checkForParameter.

private <T extends DataEntity<?>> T checkForParameter(T observation, ParameterHolder parameterHolder, ObservationContext ctx, Session session) throws OwsExceptionReport {
    if (parameterHolder.isSetParameter()) {
        if (parameterHolder.isSetFromToParameter()) {
            NamedValue<BigDecimal> fromParameter = parameterHolder.getFromParameter();
            NamedValue<BigDecimal> toParameter = parameterHolder.getToParameter();
            if (!(observation instanceof ProfileDataEntity)) {
                observation.setVerticalFrom(fromParameter.getValue().getValue());
                observation.setVerticalTo(toParameter.getValue().getValue());
            }
            // set vertical metadata
            VerticalMetadataEntity verticalMetadata = new VerticalMetadataEntity();
            verticalMetadata.setVerticalFromName(fromParameter.getName().getHref());
            verticalMetadata.setVerticalToName(toParameter.getName().getHref());
            if (fromParameter.getValue().isSetUnit()) {
                verticalMetadata.setVerticalUnit(getUnit(fromParameter.getValue().getUnitObject(), caches.units, session));
            }
            if (parameterHolder.isSetHeightDepthParameter()) {
                if (parameterHolder.isSetDepthParameter() && parameterHolder.isSetHeightParameter()) {
                    verticalMetadata.setOrientation(Integer.valueOf(0).shortValue());
                } else if (parameterHolder.isSetDepthParameter()) {
                    verticalMetadata.setOrientation(Integer.valueOf(-1).shortValue());
                } else {
                    verticalMetadata.setOrientation(Integer.valueOf(1).shortValue());
                }
            }
            ctx.setVertical(verticalMetadata);
            parameterHolder.removeParameter(fromParameter);
            parameterHolder.removeParameter(toParameter);
        } else if (parameterHolder.isSetHeightDepthParameter()) {
            NamedValue<BigDecimal> parameter = parameterHolder.getHeightDepthParameter();
            VerticalMetadataEntity verticalMetadata = new VerticalMetadataEntity();
            if (parameterHolder.isSetDepthParameter()) {
                if (!(observation instanceof ProfileDataEntity)) {
                    observation.setVerticalFrom(parameter.getValue().getValue());
                    observation.setVerticalTo(parameter.getValue().getValue());
                }
                verticalMetadata.setOrientation(Integer.valueOf(-1).shortValue());
            } else {
                if (!(observation instanceof ProfileDataEntity)) {
                    observation.setVerticalFrom(parameter.getValue().getValue());
                    observation.setVerticalTo(parameter.getValue().getValue());
                }
                verticalMetadata.setOrientation(Integer.valueOf(1).shortValue());
            }
            // set vertical metadata
            verticalMetadata.setVerticalFromName(parameter.getName().getHref());
            verticalMetadata.setVerticalToName(parameter.getName().getHref());
            if (parameter.getValue().isSetUnit()) {
                verticalMetadata.setVerticalUnit(getUnit(parameter.getValue().getUnitObject(), caches.units, session));
            }
            ctx.setVertical(verticalMetadata);
            parameterHolder.removeParameter(parameter);
        }
        if (parameterHolder.isSetParameter()) {
            Set<ParameterEntity<?>> insertParameter = daos.parameter().insertParameter(parameterHolder.getParameter(), caches.units, session);
            observation.setParameters(insertParameter);
        }
    }
    return observation;
}
Also used : VerticalMetadataEntity(org.n52.series.db.beans.VerticalMetadataEntity) ParameterEntity(org.n52.series.db.beans.parameter.ParameterEntity) NamedValue(org.n52.shetland.ogc.om.NamedValue) ProfileDataEntity(org.n52.series.db.beans.ProfileDataEntity) BigDecimal(java.math.BigDecimal)

Example 3 with ObservationContext

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

the class ObservationPersister method persist.

private <V, T extends DataEntity<V>> T persist(T observation, V value) throws OwsExceptionReport {
    observation.setDeleted(false);
    if (parent == null) {
        daos.observation().addIdentifier(omObservation, observation, session, caches.codespaces);
    } else {
        observation.setParent(parent);
        observation.setStaIdentifier(observation.generateUUID());
    }
    daos.observation().addName(omObservation, observation, session, caches.codespaces);
    daos.observation().addDescription(omObservation, observation);
    daos.observation().addTime(omObservation, observation);
    observation.setValue(value);
    if (samplingGeometry != null) {
        GeometryEntity geometryEntity = new GeometryEntity();
        geometryEntity.setGeometry(samplingGeometry);
        observation.setGeometryEntity(geometryEntity);
        checkUpdateFeatureOfInterestGeometry();
        omObservation.removeSpatialFilteringProfileParameter();
    }
    ObservationContext observationContext = daos.observation().createObservationContext();
    String observationType = ObservationTypeObservationVisitor.getInstance().visit((DataEntity<?>) observation);
    observationContext.setObservationType(getObservationType(observationType, session));
    if (dataset != null) {
        if (!isProfileObservation(dataset) && !isDataArrayObservation(dataset) || isProfileObservation(dataset) && parent == null || isDataArrayObservation(dataset) && parent == null) {
            offerings.add(dataset.getOffering());
            if (!daos.dataset().checkObservationType(dataset, observationType, session)) {
                throw new InvalidParameterValueException().withMessage("The requested observationType (%s) is invalid for procedure = " + "%s, observedProperty = %s and offering = %s! The valid observationType is '%s'!", observationType, observation.getDataset().getProcedure().getIdentifier(), dataset.getObservableProperty().getIdentifier(), dataset.getOffering().getIdentifier(), dataset.getOmObservationType().getFormat());
            }
        }
        observationContext.setPhenomenon(dataset.getObservableProperty());
        observationContext.setProcedure(dataset.getProcedure());
        observationContext.setOffering(dataset.getOffering());
        observationContext.setCategory(dataset.getCategory());
        observationContext.setPlatform(dataset.getPlatform());
        observationContext.setUnit(dataset.getUnit());
        if (!ValueType.not_initialized.equals(dataset.getValueType()) && !observationContext.isSetValueType()) {
            observationContext.setValueType(dataset.getValueType());
        }
    }
    // currently only profiles with one observedProperty are supported
    if (parent != null && !isProfileObservation(dataset)) {
        observationContext.setHiddenChild(true);
    }
    observationContext.setFeatureOfInterest(featureOfInterest);
    if (!observationContext.isSetPlatform()) {
        observationContext.setPlatform(daos.platform().getOrInsertPlatform(featureOfInterest, session));
    }
    daos.observation().fillObservationContext(observationContext, omObservation, session);
    checkForParameter(observation, omObservation.getParameterHolder(), observationContext, session);
    if (observationContext.isSetVertical()) {
        observationContext.setVertical(daos.verticalMetadata().getOrInsertVerticalMetadata(observationContext.getVertical(), session));
    }
    if (dataset != null && dataset.hasVerticalMetadata()) {
        observationContext.setVertical(dataset.getVerticalMetadata());
    }
    DatasetEntity persitedDataset = daos.observation().addObservationContextToObservation(observationContext, observation, session);
    session.save(observation);
    session.flush();
    session.refresh(observation);
    daos.dataset.updateSeriesWithFirstLatestValues(persitedDataset, observation, session);
    return observation;
}
Also used : InvalidParameterValueException(org.n52.shetland.ogc.ows.exception.InvalidParameterValueException) GeometryEntity(org.n52.series.db.beans.GeometryEntity) DatasetEntity(org.n52.series.db.beans.DatasetEntity)

Example 4 with ObservationContext

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

the class AbstractSeriesObservationDAO method addObservationContextToObservation.

@Override
protected DatasetEntity addObservationContextToObservation(ObservationContext ctx, DataEntity<?> observation, Session session) throws OwsExceptionReport {
    AbstractSeriesDAO seriesDAO = getDaoFactory().getSeriesDAO();
    DatasetEntity series = seriesDAO.getOrInsertSeries(ctx, observation, session);
    ((DataEntity) observation).setDataset(series);
    OfferingDAO offeringDAO = getDaoFactory().getOfferingDAO();
    offeringDAO.updateOfferingMetadata(series.getOffering(), observation, session);
    return series;
}
Also used : DatasetEntity(org.n52.series.db.beans.DatasetEntity) DataEntity(org.n52.series.db.beans.DataEntity) OfferingDAO(org.n52.sos.ds.hibernate.dao.OfferingDAO)

Example 5 with ObservationContext

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

the class AbstractSeriesDAO method getOrInsert.

protected DatasetEntity getOrInsert(ObservationContext ctx, DataEntity<?> observation, Session session) throws OwsExceptionReport {
    Criteria criteria = getDefaultAllSeriesCriteria(session);
    ctx.addIdentifierRestrictionsToCritera(criteria, true, false);
    // criteria.setMaxResults(1);
    // TODO: check for Unit if available!!!
    LOGGER.trace("QUERY getOrInsertSeries(feature, observableProperty, procedure, offering, platform, category): {}", HibernateHelper.getSqlString(criteria));
    List<DatasetEntity> datasets = (List<DatasetEntity>) criteria.list();
    DatasetEntity series = datasets.isEmpty() ? null : datasets.stream().filter(d -> d.getCategory().equals(ctx.getCategory())).findFirst().orElse(datasets.iterator().next());
    if (series == null || series.getDatasetType().equals(DatasetType.not_initialized)) {
        series = preCheckDataset(ctx, observation, series, session);
        if (series != null && series.isMobile()) {
            series.setDatasetType(DatasetType.trajectory);
        }
    }
    if (series == null || series.isSetFeature() && ctx.isSetFeatureOfInterest() && !series.getFeature().getIdentifier().equals(ctx.getFeatureOfInterest().getIdentifier())) {
        series = (DatasetEntity) getDatasetFactory().visit(observation);
        ctx.addValuesToSeries(series);
        series.setDeleted(false);
        series.setPublished(ctx.isPublish());
    } else if (!series.isSetFeature()) {
        ctx.addValuesToSeries(series);
        series.setDeleted(false);
        series.setPublished(ctx.isPublish());
    } else if (!series.hasUnit() && ctx.isSetUnit()) {
        series.setUnit(ctx.getUnit());
        series.setDeleted(false);
        series.setPublished(ctx.isPublish());
    } else if (ctx.isPublish() && !series.isPublished()) {
        series.setPublished(ctx.isPublish());
    } else if (series.isDeleted()) {
        series.setDeleted(false);
    } else {
        return series;
    }
    session.saveOrUpdate(series);
    session.flush();
    session.refresh(series);
    processSta(series, session);
    return series;
}
Also used : InvalidParameterValueException(org.n52.shetland.ogc.ows.exception.InvalidParameterValueException) GetObservationByIdRequest(org.n52.shetland.ogc.sos.request.GetObservationByIdRequest) AbstractFeatureEntity(org.n52.series.db.beans.AbstractFeatureEntity) CodedException(org.n52.shetland.ogc.ows.exception.CodedException) LoggerFactory(org.slf4j.LoggerFactory) DescribableEntity(org.n52.series.db.beans.DescribableEntity) Filter(org.n52.shetland.ogc.filter.Filter) ObservationFactory(org.n52.sos.ds.hibernate.dao.observation.ObservationFactory) Query(org.hibernate.query.Query) ResultFilterRestrictions(org.n52.sos.ds.hibernate.util.ResultFilterRestrictions) DataEntity(org.n52.series.db.beans.DataEntity) Collection(java.util.Collection) SubQueryIdentifier(org.n52.sos.ds.hibernate.util.ResultFilterRestrictions.SubQueryIdentifier) Set(java.util.Set) UUID(java.util.UUID) SpatialFilter(org.n52.shetland.ogc.filter.SpatialFilter) Collectors(java.util.stream.Collectors) GetObservationRequest(org.n52.shetland.ogc.sos.request.GetObservationRequest) FormatEntity(org.n52.series.db.beans.FormatEntity) DatasetType(org.n52.series.db.beans.dataset.DatasetType) List(java.util.List) CollectionHelper(org.n52.shetland.util.CollectionHelper) FetchMode(org.hibernate.FetchMode) HibernateHelper(org.n52.sos.ds.hibernate.util.HibernateHelper) Geometry(org.locationtech.jts.geom.Geometry) PlatformEntity(org.n52.series.db.beans.PlatformEntity) FormatDAO(org.n52.sos.ds.hibernate.dao.FormatDAO) Criteria(org.hibernate.Criteria) ProjectionList(org.hibernate.criterion.ProjectionList) DatastreamEntity(org.n52.series.db.beans.sta.DatastreamEntity) Restrictions(org.hibernate.criterion.Restrictions) OmObservationConstellation(org.n52.shetland.ogc.om.OmObservationConstellation) Session(org.hibernate.Session) DatasetEntity(org.n52.series.db.beans.DatasetEntity) HashSet(java.util.HashSet) OfferingEntity(org.n52.series.db.beans.OfferingEntity) GetResultRequest(org.n52.shetland.ogc.sos.request.GetResultRequest) DateTimeHelper(org.n52.shetland.util.DateTimeHelper) Strings(com.google.common.base.Strings) GetDataAvailabilityRequest(org.n52.shetland.ogc.sos.gda.GetDataAvailabilityRequest) ProcedureEntity(org.n52.series.db.beans.ProcedureEntity) LinkedHashSet(java.util.LinkedHashSet) Criterion(org.hibernate.criterion.Criterion) Sos2Constants(org.n52.shetland.ogc.sos.Sos2Constants) Logger(org.slf4j.Logger) CategoryEntity(org.n52.series.db.beans.CategoryEntity) AbstractIdentifierNameDescriptionDAO(org.n52.sos.ds.hibernate.dao.AbstractIdentifierNameDescriptionDAO) ObservationContext(org.n52.sos.ds.hibernate.dao.observation.ObservationContext) Projections(org.hibernate.criterion.Projections) OwsExceptionReport(org.n52.shetland.ogc.ows.exception.OwsExceptionReport) SpatialRestrictions(org.n52.sos.ds.hibernate.util.SpatialRestrictions) AbstractPhenomenon(org.n52.shetland.ogc.om.AbstractPhenomenon) DaoFactory(org.n52.sos.ds.hibernate.dao.DaoFactory) ComparisonFilter(org.n52.shetland.ogc.filter.ComparisonFilter) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) PhenomenonEntity(org.n52.series.db.beans.PhenomenonEntity) ResultFilterClasses(org.n52.sos.ds.hibernate.util.ResultFilterClasses) TimeExtrema(org.n52.sos.ds.hibernate.util.TimeExtrema) QuantityDataEntity(org.n52.series.db.beans.QuantityDataEntity) ValueType(org.n52.series.db.beans.dataset.ValueType) NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException) Subqueries(org.hibernate.criterion.Subqueries) DatasetEntity(org.n52.series.db.beans.DatasetEntity) List(java.util.List) ProjectionList(org.hibernate.criterion.ProjectionList) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria)

Aggregations

DatasetEntity (org.n52.series.db.beans.DatasetEntity)5 FormatEntity (org.n52.series.db.beans.FormatEntity)3 OfferingEntity (org.n52.series.db.beans.OfferingEntity)3 ProcedureEntity (org.n52.series.db.beans.ProcedureEntity)3 AbstractFeature (org.n52.shetland.ogc.gml.AbstractFeature)3 InvalidParameterValueException (org.n52.shetland.ogc.ows.exception.InvalidParameterValueException)3 NoApplicableCodeException (org.n52.shetland.ogc.ows.exception.NoApplicableCodeException)3 ObservationContext (org.n52.sos.ds.hibernate.dao.observation.ObservationContext)3 BigDecimal (java.math.BigDecimal)2 Criteria (org.hibernate.Criteria)2 Session (org.hibernate.Session)2 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)2 DateTime (org.joda.time.DateTime)2 CategoryEntity (org.n52.series.db.beans.CategoryEntity)2 DataEntity (org.n52.series.db.beans.DataEntity)2 PhenomenonEntity (org.n52.series.db.beans.PhenomenonEntity)2 UnitEntity (org.n52.series.db.beans.UnitEntity)2 OmObservationConstellation (org.n52.shetland.ogc.om.OmObservationConstellation)2 SosOffering (org.n52.shetland.ogc.sos.SosOffering)2 FormatDAO (org.n52.sos.ds.hibernate.dao.FormatDAO)2