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;
}
}
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;
}
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;
}
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;
}
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);
}
};
}
Aggregations