Search in sources :

Example 1 with ObservationEntity

use of org.n52.series.db.beans.sta.ObservationEntity in project sensorweb-server-sta by 52North.

the class ObservationService method createEntity.

@Override
public ObservationEntity<?> createEntity(ObservationEntity<?> entity) throws STACRUDException {
    synchronized (getLock(entity.getStaIdentifier())) {
        ObservationEntity observation = entity;
        if (!observation.isProcessed()) {
            observation.setProcessed(true);
            check(observation);
            DatastreamEntity datastream = getDatastreamService().createEntity(observation.getDatastream());
            observation.setDatastream(datastream);
            // Fetch with all needed associations
            datastream = datastreamRepository.findByStaIdentifier(datastream.getStaIdentifier(), EntityGraphRepository.FetchGraph.FETCHGRAPH_THINGLOCATION, EntityGraphRepository.FetchGraph.FETCHGRAPH_PROCEDURE, EntityGraphRepository.FetchGraph.FETCHGRAPH_UOM, EntityGraphRepository.FetchGraph.FETCHGRAPH_OBS_TYPE, EntityGraphRepository.FetchGraph.FETCHGRAPH_OBSERVABLE_PROP, EntityGraphRepository.FetchGraph.FETCHGRAPH_DATASETS).orElseThrow(() -> new STACRUDException("Unable to find Datastream!"));
            AbstractFeatureEntity<?> feature = checkFeature(observation, datastream);
            // category (obdProp)
            CategoryEntity category = checkCategory();
            // offering (sensor)
            OfferingEntity offering = checkOffering(datastream);
            // dataset
            DatasetEntity dataset = checkDataset(datastream, feature, category, offering);
            // observation
            ObservationEntity<?> data = checkData(observation, dataset);
            if (data != null) {
                updateDataset(dataset, data);
                updateDatastream(datastream, dataset, data);
            }
            return data;
        }
        return observation;
    }
}
Also used : DatasetEntity(org.n52.series.db.beans.DatasetEntity) DatastreamEntity(org.n52.series.db.beans.sta.DatastreamEntity) CategoryObservationEntity(org.n52.series.db.beans.sta.CategoryObservationEntity) QuantityObservationEntity(org.n52.series.db.beans.sta.QuantityObservationEntity) TextObservationEntity(org.n52.series.db.beans.sta.TextObservationEntity) BooleanObservationEntity(org.n52.series.db.beans.sta.BooleanObservationEntity) ObservationEntity(org.n52.series.db.beans.sta.ObservationEntity) CountObservationEntity(org.n52.series.db.beans.sta.CountObservationEntity) OfferingEntity(org.n52.series.db.beans.OfferingEntity) STACRUDException(org.n52.shetland.ogc.sta.exception.STACRUDException) CategoryEntity(org.n52.series.db.beans.CategoryEntity)

Example 2 with ObservationEntity

use of org.n52.series.db.beans.sta.ObservationEntity in project sensorweb-server-sta by 52North.

the class DatastreamService method fetchExpandEntities.

@Override
protected DatastreamEntity fetchExpandEntities(DatastreamEntity entity, ExpandFilter expandOption) throws STACRUDException, STAInvalidQueryException {
    for (ExpandItem expandItem : expandOption.getItems()) {
        String expandProperty = expandItem.getPath();
        if (DatastreamEntityDefinition.NAVIGATION_PROPERTIES.contains(expandProperty)) {
            switch(expandProperty) {
                case STAEntityDefinition.SENSOR:
                    ProcedureEntity sensor = getSensorService().getEntityByRelatedEntityRaw(entity.getStaIdentifier(), STAEntityDefinition.DATASTREAMS, null, expandItem.getQueryOptions());
                    entity.setProcedure(sensor);
                    break;
                case STAEntityDefinition.THING:
                    PlatformEntity thing = getThingService().getEntityByRelatedEntityRaw(entity.getStaIdentifier(), STAEntityDefinition.DATASTREAMS, null, expandItem.getQueryOptions());
                    entity.setThing(thing);
                    break;
                case STAEntityDefinition.OBSERVED_PROPERTY:
                    PhenomenonEntity obsProp = getObservedPropertyService().getEntityByRelatedEntityRaw(entity.getStaIdentifier(), STAEntityDefinition.DATASTREAMS, null, expandItem.getQueryOptions());
                    entity.setObservableProperty(obsProp);
                    break;
                case STAEntityDefinition.OBSERVATIONS:
                    Page<ObservationEntity<?>> observations = getObservationService().getEntityCollectionByRelatedEntityRaw(entity.getStaIdentifier(), STAEntityDefinition.DATASTREAMS, expandItem.getQueryOptions());
                    entity.setObservations(observations.get().collect(Collectors.toSet()));
                    break;
                default:
                    logger.error("Trying to expand unrelated Entity!");
                    throw new RuntimeException("This can never happen!");
            }
        } else {
            throw new STAInvalidQueryException("Invalid expandOption supplied. Cannot find " + expandProperty + " on Entity of type 'Datastream'");
        }
    }
    return entity;
}
Also used : ProcedureEntity(org.n52.series.db.beans.ProcedureEntity) STAInvalidQueryException(org.n52.shetland.ogc.sta.exception.STAInvalidQueryException) PlatformEntity(org.n52.series.db.beans.PlatformEntity) ExpandItem(org.n52.shetland.filter.ExpandItem) PhenomenonEntity(org.n52.series.db.beans.PhenomenonEntity) AbstractObservationEntity(org.n52.series.db.beans.sta.AbstractObservationEntity) ObservationEntity(org.n52.series.db.beans.sta.ObservationEntity)

Example 3 with ObservationEntity

use of org.n52.series.db.beans.sta.ObservationEntity in project sensorweb-server-sta by 52North.

the class JSONObservation method createPatchEntity.

private ObservationEntity createPatchEntity() {
    self.setIdentifier(identifier);
    self.setStaIdentifier(identifier);
    // parameters
    handleParameters(parameters);
    // phenomenonTime
    if (phenomenonTime != null) {
        Time time = parseTime(phenomenonTime);
        if (time instanceof TimeInstant) {
            self.setSamplingTimeStart(((TimeInstant) time).getValue().toDate());
            self.setSamplingTimeEnd(((TimeInstant) time).getValue().toDate());
        } else if (time instanceof TimePeriod) {
            self.setSamplingTimeStart(((TimePeriod) time).getStart().toDate());
            self.setSamplingTimeEnd(((TimePeriod) time).getEnd().toDate());
        }
    }
    // Set resultTime only when supplied
    if (resultTime != null) {
        // resultTime
        self.setResultTime(((TimeInstant) parseTime(resultTime)).getValue().toDate());
    }
    // validTime
    if (validTime != null) {
        Time time = parseTime(validTime);
        if (time instanceof TimeInstant) {
            self.setValidTimeStart(((TimeInstant) time).getValue().toDate());
            self.setValidTimeEnd(((TimeInstant) time).getValue().toDate());
        } else if (time instanceof TimePeriod) {
            self.setValidTimeStart(((TimePeriod) time).getStart().toDate());
            self.setValidTimeEnd(((TimePeriod) time).getEnd().toDate());
        }
    }
    self.setValue(result);
    // Link to Datastream
    if (Datastream != null) {
        self.setDatastream(Datastream.toEntity(JSONBase.EntityType.REFERENCE));
    }
    // Link to FOI
    if (FeatureOfInterest != null) {
        self.setFeature(FeatureOfInterest.toEntity(JSONBase.EntityType.REFERENCE));
    }
    return self;
}
Also used : TimePeriod(org.n52.shetland.ogc.gml.time.TimePeriod) DateTime(org.joda.time.DateTime) Time(org.n52.shetland.ogc.gml.time.Time) TimeInstant(org.n52.shetland.ogc.gml.time.TimeInstant)

Example 4 with ObservationEntity

use of org.n52.series.db.beans.sta.ObservationEntity in project sensorweb-server-sta by 52North.

the class JSONObservation method createPostEntity.

private ObservationEntity createPostEntity() {
    self.setIdentifier(identifier);
    self.setStaIdentifier(identifier);
    // phenomenonTime
    if (phenomenonTime != null) {
        Time time = parseTime(phenomenonTime);
        if (time instanceof TimeInstant) {
            self.setSamplingTimeStart(((TimeInstant) time).getValue().toDate());
            self.setSamplingTimeEnd(((TimeInstant) time).getValue().toDate());
        } else if (time instanceof TimePeriod) {
            self.setSamplingTimeStart(((TimePeriod) time).getStart().toDate());
            self.setSamplingTimeEnd(((TimePeriod) time).getEnd().toDate());
        }
    } else {
        // Use time of POST Request as fallback
        Date date = DateTime.now().toDate();
        self.setSamplingTimeStart(date);
        self.setSamplingTimeEnd(date);
    }
    // Set resultTime only when supplied
    if (resultTime != null) {
        // resultTime
        self.setResultTime(((TimeInstant) parseTime(resultTime)).getValue().toDate());
    }
    // validTime
    if (validTime != null) {
        Time time = parseTime(validTime);
        if (time instanceof TimeInstant) {
            self.setValidTimeStart(((TimeInstant) time).getValue().toDate());
            self.setValidTimeEnd(((TimeInstant) time).getValue().toDate());
        } else if (time instanceof TimePeriod) {
            self.setValidTimeStart(((TimePeriod) time).getStart().toDate());
            self.setValidTimeEnd(((TimePeriod) time).getEnd().toDate());
        }
    }
    // parameters
    handleParameters(parameters);
    // result
    self.setValue(result);
    // Link to Datastream
    if (Datastream != null) {
        self.setDatastream(Datastream.toEntity(JSONBase.EntityType.FULL, JSONBase.EntityType.REFERENCE));
    } else if (backReference instanceof JSONDatastream) {
        self.setDatastream(((JSONDatastream) backReference).getEntity());
    } else {
        Assert.notNull(null, INVALID_INLINE_ENTITY_MISSING + "Datastream");
    }
    // Link to FOI
    if (FeatureOfInterest != null) {
        self.setFeature(FeatureOfInterest.toEntity(JSONBase.EntityType.FULL, JSONBase.EntityType.REFERENCE));
    } else if (backReference instanceof JSONFeatureOfInterest) {
        self.setFeature(((JSONFeatureOfInterest) backReference).getEntity());
    }
    return self;
}
Also used : TimePeriod(org.n52.shetland.ogc.gml.time.TimePeriod) DateTime(org.joda.time.DateTime) Time(org.n52.shetland.ogc.gml.time.Time) TimeInstant(org.n52.shetland.ogc.gml.time.TimeInstant) Date(java.util.Date)

Example 5 with ObservationEntity

use of org.n52.series.db.beans.sta.ObservationEntity in project sensorweb-server-sta by 52North.

the class DatastreamQuerySpecifications method handleRelatedPropertyFilter.

/**
 * Handles filtering of properties in related Entities.
 *
 * @param propertyName  Name of property
 * @param propertyValue Supposed value of Property
 * @return BooleanExpression evaluating to true if Entity is not filtered out
 */
@Override
protected Specification<DatastreamEntity> handleRelatedPropertyFilter(String propertyName, Specification<?> propertyValue) {
    return (root, query, builder) -> {
        try {
            switch(propertyName) {
                case SENSOR:
                    {
                        Subquery<DatastreamEntity> sq = query.subquery(DatastreamEntity.class);
                        Root<ProcedureEntity> sensor = sq.from(ProcedureEntity.class);
                        sq.select(sensor.get(DescribableEntity.PROPERTY_ID)).where(((Specification<ProcedureEntity>) propertyValue).toPredicate(sensor, query, builder));
                        return builder.in(root.get(DatastreamEntity.PROPERTY_SENSOR)).value(sq);
                    }
                case OBSERVED_PROPERTY:
                    {
                        Subquery<DatastreamEntity> sq = query.subquery(DatastreamEntity.class);
                        Root<PhenomenonEntity> observedProperty = sq.from(PhenomenonEntity.class);
                        sq.select(observedProperty.get(DescribableEntity.PROPERTY_ID)).where(((Specification<PhenomenonEntity>) propertyValue).toPredicate(observedProperty, query, builder));
                        return builder.in(root.get(DatastreamEntity.PROPERTY_OBSERVABLE_PROPERTY)).value(sq);
                    }
                case THING:
                    {
                        Subquery<DatastreamEntity> sq = query.subquery(DatastreamEntity.class);
                        Root<PlatformEntity> thing = sq.from(PlatformEntity.class);
                        sq.select(thing.get(DescribableEntity.PROPERTY_ID)).where(((Specification<PlatformEntity>) propertyValue).toPredicate(thing, query, builder));
                        return builder.in(root.get(DatastreamEntity.PROPERTY_THING)).value(sq);
                    }
                case OBSERVATIONS:
                    {
                        Subquery<DatasetEntity> sq = query.subquery(DatasetEntity.class);
                        Join<DatastreamEntity, DatasetEntity> join = root.join(DatastreamEntity.PROPERTY_DATASETS);
                        Root<ObservationEntity> data = sq.from(ObservationEntity.class);
                        sq.select(data.get(ObservationEntity.PROPERTY_DATASET)).where(((Specification<ObservationEntity>) propertyValue).toPredicate(data, query, builder));
                        return builder.in(join.get(DatasetEntity.PROPERTY_ID)).value(sq);
                    }
                default:
                    throw new STAInvalidFilterExpressionException("Could not find related property: " + propertyName);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    };
}
Also used : Join(javax.persistence.criteria.Join) DatastreamEntity(org.n52.series.db.beans.sta.DatastreamEntity) STAInvalidFilterExpressionException(org.n52.shetland.ogc.sta.exception.STAInvalidFilterExpressionException) Collection(java.util.Collection) DatasetEntity(org.n52.series.db.beans.DatasetEntity) FormatEntity(org.n52.series.db.beans.FormatEntity) DescribableEntity(org.n52.series.db.beans.DescribableEntity) Specification(org.springframework.data.jpa.domain.Specification) PhenomenonEntity(org.n52.series.db.beans.PhenomenonEntity) ObservationEntity(org.n52.series.db.beans.sta.ObservationEntity) FilterConstants(org.n52.shetland.ogc.filter.FilterConstants) JoinType(javax.persistence.criteria.JoinType) Subquery(javax.persistence.criteria.Subquery) PlatformEntity(org.n52.series.db.beans.PlatformEntity) Expression(javax.persistence.criteria.Expression) ProcedureEntity(org.n52.series.db.beans.ProcedureEntity) Root(javax.persistence.criteria.Root) DatasetEntity(org.n52.series.db.beans.DatasetEntity) DatastreamEntity(org.n52.series.db.beans.sta.DatastreamEntity) PhenomenonEntity(org.n52.series.db.beans.PhenomenonEntity) ObservationEntity(org.n52.series.db.beans.sta.ObservationEntity) STAInvalidFilterExpressionException(org.n52.shetland.ogc.sta.exception.STAInvalidFilterExpressionException) ProcedureEntity(org.n52.series.db.beans.ProcedureEntity) PlatformEntity(org.n52.series.db.beans.PlatformEntity) STAInvalidFilterExpressionException(org.n52.shetland.ogc.sta.exception.STAInvalidFilterExpressionException)

Aggregations

ObservationEntity (org.n52.series.db.beans.sta.ObservationEntity)9 BooleanObservationEntity (org.n52.series.db.beans.sta.BooleanObservationEntity)7 CategoryObservationEntity (org.n52.series.db.beans.sta.CategoryObservationEntity)7 CountObservationEntity (org.n52.series.db.beans.sta.CountObservationEntity)7 DatastreamEntity (org.n52.series.db.beans.sta.DatastreamEntity)7 QuantityObservationEntity (org.n52.series.db.beans.sta.QuantityObservationEntity)7 TextObservationEntity (org.n52.series.db.beans.sta.TextObservationEntity)7 STACRUDException (org.n52.shetland.ogc.sta.exception.STACRUDException)7 DatasetEntity (org.n52.series.db.beans.DatasetEntity)4 ProcedureEntity (org.n52.series.db.beans.ProcedureEntity)4 OffsetLimitBasedPageRequest (org.n52.sta.data.OffsetLimitBasedPageRequest)4 CollectionWrapper (org.n52.sta.data.service.util.CollectionWrapper)4 PhenomenonEntity (org.n52.series.db.beans.PhenomenonEntity)3 PlatformEntity (org.n52.series.db.beans.PlatformEntity)3 LocationEntity (org.n52.series.db.beans.sta.LocationEntity)3 ExpandItem (org.n52.shetland.filter.ExpandItem)3 Date (java.util.Date)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Optional (java.util.Optional)2