use of org.n52.series.db.beans.sta.SensorEntity in project sensorweb-server-sta by 52North.
the class SensorService method updateEntity.
@Override
public ProcedureEntity updateEntity(String id, ProcedureEntity entity, HttpMethod method) throws STACRUDException {
checkUpdate(entity);
if (HttpMethod.PATCH.equals(method)) {
synchronized (getLock(id)) {
Optional<ProcedureEntity> existing = getRepository().findByStaIdentifier(id, EntityGraphRepository.FetchGraph.FETCHGRAPH_FORMAT, EntityGraphRepository.FetchGraph.FETCHGRAPH_PROCEDUREHISTORY);
if (existing.isPresent()) {
ProcedureEntity merged = merge(existing.get(), entity);
if (entity instanceof SensorEntity) {
if (((SensorEntity) entity).hasDatastreams()) {
AbstractSensorThingsEntityServiceImpl<?, DatastreamEntity, DatastreamEntity> dsService = getDatastreamService();
for (DatastreamEntity datastreamEntity : ((SensorEntity) entity).getDatastreams()) {
dsService.createOrUpdate(datastreamEntity);
}
}
}
checkFormat(merged);
checkProcedureHistory(merged);
getRepository().save(getAsProcedureEntity(merged));
return merged;
}
}
throw new STACRUDException("Unable to update. Entity not found.", HTTPStatus.NOT_FOUND);
} else if (HttpMethod.PUT.equals(method)) {
throw new STACRUDException("Http PUT is not yet supported!", HTTPStatus.NOT_IMPLEMENTED);
}
throw new STACRUDException("Invalid http method for updating entity!", HTTPStatus.BAD_REQUEST);
}
use of org.n52.series.db.beans.sta.SensorEntity in project sensorweb-server-sta by 52North.
the class SensorService method fetchExpandEntities.
@Override
protected SensorEntity fetchExpandEntities(ProcedureEntity entity, ExpandFilter expandOption) throws STACRUDException, STAInvalidQueryException {
for (ExpandItem expandItem : expandOption.getItems()) {
String expandProperty = expandItem.getPath();
if (SensorEntityDefinition.NAVIGATION_PROPERTIES.contains(expandProperty)) {
Page<DatastreamEntity> observedProps = getDatastreamService().getEntityCollectionByRelatedEntityRaw(entity.getStaIdentifier(), STAEntityDefinition.SENSORS, expandItem.getQueryOptions());
SensorEntity sensor = new SensorEntity(entity);
return sensor.setDatastreams(observedProps.get().collect(Collectors.toSet()));
} else {
throw new STAInvalidQueryException("Invalid expandOption supplied. Cannot find " + expandProperty + " on Entity of type 'Sensor'");
}
}
return new SensorEntity(entity);
}
use of org.n52.series.db.beans.sta.SensorEntity in project sensorweb-server-sta by 52North.
the class MessageBusRepository method getRelatedCollections.
private <S extends T> Map<String, Set<String>> getRelatedCollections(S rawObject) {
Map<String, Set<String>> collections = new HashMap<>();
if (rawObject instanceof ProcedureEntity) {
if (rawObject instanceof SensorEntity) {
SensorEntity entity = (SensorEntity) rawObject;
if (entity.hasDatastreams()) {
collections.put(STAEntityDefinition.DATASTREAMS, entity.getDatastreams().stream().map(DatastreamEntity::getStaIdentifier).collect(Collectors.toSet()));
}
} else {
ProcedureEntity entity = (ProcedureEntity) rawObject;
collections.put(STAEntityDefinition.DATASTREAM, datastreamRepository.findAll(dQs.withSensorStaIdentifier(entity.getStaIdentifier())).stream().map(DatastreamEntity::getStaIdentifier).collect(Collectors.toSet()));
}
} else if (rawObject instanceof LocationEntity) {
LocationEntity entity = (LocationEntity) rawObject;
if (entity.hasHistoricalLocations()) {
collections.put(STAEntityDefinition.HISTORICAL_LOCATIONS, entity.getHistoricalLocations().stream().map(HistoricalLocationEntity::getStaIdentifier).collect(Collectors.toSet()));
}
if (entity.hasThings()) {
collections.put(STAEntityDefinition.THINGS, entity.getThings().stream().map(PlatformEntity::getStaIdentifier).collect(Collectors.toSet()));
}
} else if (rawObject instanceof PlatformEntity) {
PlatformEntity entity = (PlatformEntity) rawObject;
if (entity.hasLocationEntities()) {
collections.put(STAEntityDefinition.LOCATIONS, entity.getLocations().stream().map(LocationEntity::getStaIdentifier).collect(Collectors.toSet()));
}
if (entity.hasHistoricalLocations()) {
collections.put(STAEntityDefinition.HISTORICAL_LOCATIONS, entity.getHistoricalLocations().stream().map(HistoricalLocationEntity::getStaIdentifier).collect(Collectors.toSet()));
}
if (entity.hasDatastreams()) {
collections.put(STAEntityDefinition.DATASTREAMS, entity.getDatastreams().stream().map(DatastreamEntity::getStaIdentifier).collect(Collectors.toSet()));
}
} else if (rawObject instanceof DatastreamEntity) {
DatastreamEntity entity = (DatastreamEntity) rawObject;
if (entity.hasThing()) {
collections.put(STAEntityDefinition.THINGS, Collections.singleton(entity.getThing().getStaIdentifier()));
}
if (entity.hasProcedure()) {
collections.put(STAEntityDefinition.SENSORS, Collections.singleton(entity.getProcedure().getStaIdentifier()));
}
if (entity.hasObservableProperty()) {
collections.put(STAEntityDefinition.OBSERVED_PROPERTIES, Collections.singleton(entity.getObservableProperty().getStaIdentifier()));
}
} else if (rawObject instanceof HistoricalLocationEntity) {
HistoricalLocationEntity entity = (HistoricalLocationEntity) rawObject;
if (entity.hasThing()) {
collections.put(STAEntityDefinition.THINGS, Collections.singleton(entity.getThing().getStaIdentifier()));
}
if (entity.hasLocationEntities()) {
collections.put(STAEntityDefinition.LOCATIONS, entity.getLocations().stream().map(LocationEntity::getStaIdentifier).collect(Collectors.toSet()));
}
} else if (rawObject instanceof ObservationEntity<?>) {
ObservationEntity<?> entity = (ObservationEntity<?>) rawObject;
if (entity.getDataset() != null && entity.getDataset().getFeature() != null) {
collections.put(STAEntityDefinition.FEATURES_OF_INTEREST, Collections.singleton(entity.getDataset().getFeature().getStaIdentifier()));
}
Optional<DatastreamEntity> datastreamEntity = datastreamRepository.findOne(dQs.withObservationStaIdentifier(entity.getStaIdentifier()));
if (datastreamEntity.isPresent()) {
collections.put(STAEntityDefinition.DATASTREAMS, Collections.singleton(datastreamEntity.get().getStaIdentifier()));
} else {
LOGGER.debug("No Datastream associated with this Entity {}", entity.getStaIdentifier());
}
} else if (rawObject instanceof AbstractFeatureEntity) {
return collections;
} else if (rawObject instanceof PhenomenonEntity) {
PhenomenonEntity entity = (PhenomenonEntity) rawObject;
List<DatastreamEntity> observations = datastreamRepository.findAll(dQs.withObservedPropertyStaIdentifier(entity.getStaIdentifier()));
collections.put(STAEntityDefinition.DATASTREAMS, observations.stream().map(DatastreamEntity::getStaIdentifier).collect(Collectors.toSet()));
} else {
LOGGER.error("Error while computing related Collections: Could not identify Entity Type");
}
return collections;
}
use of org.n52.series.db.beans.sta.SensorEntity in project sensorweb-server-sta by 52North.
the class SensorService method createEntity.
@Override
public ProcedureEntity createEntity(ProcedureEntity sensor) throws STACRUDException {
if (sensor.getStaIdentifier() != null && !sensor.isSetName()) {
Optional<ProcedureEntity> optionalEntity = getRepository().findByStaIdentifier(sensor.getStaIdentifier(), EntityGraphRepository.FetchGraph.FETCHGRAPH_FORMAT);
if (optionalEntity.isPresent()) {
return optionalEntity.get();
} else {
throw new STACRUDException("No Sensor with id '" + sensor.getStaIdentifier() + "' found");
}
}
if (sensor.getStaIdentifier() == null) {
if (getRepository().existsByName(sensor.getName())) {
Optional<ProcedureEntity> optional = getRepository().findOne(sQS.withStaIdentifier(sensor.getStaIdentifier()).or(sQS.withName(sensor.getName())));
return optional.isPresent() ? optional.get() : null;
} else {
// Autogenerate Identifier
String uuid = UUID.randomUUID().toString();
sensor.setIdentifier(uuid);
sensor.setStaIdentifier(uuid);
}
}
synchronized (getLock(sensor.getStaIdentifier())) {
if (getRepository().existsByStaIdentifier(sensor.getStaIdentifier())) {
throw new STACRUDException("Identifier already exists!", HTTPStatus.CONFLICT);
}
ProcedureEntity procedure = getAsProcedureEntity(sensor);
checkFormat(procedure);
// Intermediate save to allow DatastreamService->createOrUpdate to use this entity. Does not trigger
// intercept handling (e.g. mqtt). Needed as Datastream<->Procedure connection is not yet set but
// required by interceptors
getRepository().intermediateSave(procedure);
checkProcedureHistory(procedure);
if (sensor instanceof SensorEntity && ((SensorEntity) sensor).hasDatastreams()) {
AbstractSensorThingsEntityServiceImpl<?, DatastreamEntity, DatastreamEntity> dsService = getDatastreamService();
for (DatastreamEntity datastreamEntity : ((SensorEntity) sensor).getDatastreams()) {
try {
dsService.createOrUpdate(datastreamEntity);
} catch (STACRUDException e) {
// Datastream might be currently processing.
}
}
}
// Save with Interception as procedure is now linked to Datastream
getRepository().save(procedure);
return procedure;
}
}
Aggregations