Search in sources :

Example 1 with SensorEntity

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);
}
Also used : ProcedureEntity(org.n52.series.db.beans.ProcedureEntity) SensorEntity(org.n52.series.db.beans.sta.SensorEntity) DatastreamEntity(org.n52.series.db.beans.sta.DatastreamEntity) STACRUDException(org.n52.shetland.ogc.sta.exception.STACRUDException)

Example 2 with SensorEntity

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);
}
Also used : STAInvalidQueryException(org.n52.shetland.ogc.sta.exception.STAInvalidQueryException) ExpandItem(org.n52.shetland.filter.ExpandItem) SensorEntity(org.n52.series.db.beans.sta.SensorEntity) DatastreamEntity(org.n52.series.db.beans.sta.DatastreamEntity)

Example 3 with SensorEntity

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;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Optional(java.util.Optional) HashMap(java.util.HashMap) AbstractFeatureEntity(org.n52.series.db.beans.AbstractFeatureEntity) DatastreamEntity(org.n52.series.db.beans.sta.DatastreamEntity) PhenomenonEntity(org.n52.series.db.beans.PhenomenonEntity) ObservationEntity(org.n52.series.db.beans.sta.ObservationEntity) ProcedureEntity(org.n52.series.db.beans.ProcedureEntity) PlatformEntity(org.n52.series.db.beans.PlatformEntity) HistoricalLocationEntity(org.n52.series.db.beans.sta.HistoricalLocationEntity) LocationEntity(org.n52.series.db.beans.sta.LocationEntity) SensorEntity(org.n52.series.db.beans.sta.SensorEntity) List(java.util.List) HistoricalLocationEntity(org.n52.series.db.beans.sta.HistoricalLocationEntity)

Example 4 with SensorEntity

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;
    }
}
Also used : ProcedureEntity(org.n52.series.db.beans.ProcedureEntity) SensorEntity(org.n52.series.db.beans.sta.SensorEntity) DatastreamEntity(org.n52.series.db.beans.sta.DatastreamEntity) STACRUDException(org.n52.shetland.ogc.sta.exception.STACRUDException)

Aggregations

DatastreamEntity (org.n52.series.db.beans.sta.DatastreamEntity)4 SensorEntity (org.n52.series.db.beans.sta.SensorEntity)4 ProcedureEntity (org.n52.series.db.beans.ProcedureEntity)3 STACRUDException (org.n52.shetland.ogc.sta.exception.STACRUDException)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 AbstractFeatureEntity (org.n52.series.db.beans.AbstractFeatureEntity)1 PhenomenonEntity (org.n52.series.db.beans.PhenomenonEntity)1 PlatformEntity (org.n52.series.db.beans.PlatformEntity)1 HistoricalLocationEntity (org.n52.series.db.beans.sta.HistoricalLocationEntity)1 LocationEntity (org.n52.series.db.beans.sta.LocationEntity)1 ObservationEntity (org.n52.series.db.beans.sta.ObservationEntity)1 ExpandItem (org.n52.shetland.filter.ExpandItem)1 STAInvalidQueryException (org.n52.shetland.ogc.sta.exception.STAInvalidQueryException)1