Search in sources :

Example 1 with HistoricalLocationEntity

use of org.n52.series.db.beans.sta.HistoricalLocationEntity in project SOS by 52North.

the class PlatformDAO method getOrInsertHistoricalLocation.

private HistoricalLocationEntity getOrInsertHistoricalLocation(PlatformEntity platform, LocationEntity location, Session session) {
    HistoricalLocationEntity historicalLocation = new HistoricalLocationEntity();
    historicalLocation.setIdentifier(UUID.randomUUID().toString(), getDaoFactory().isStaSupportsUrls());
    historicalLocation.setThing(platform);
    historicalLocation.setTime(DateTime.now().toDate());
    session.save(historicalLocation);
    session.flush();
    session.refresh(historicalLocation);
    location.addHistoricalLocation(historicalLocation);
    session.saveOrUpdate(location);
    session.flush();
    return historicalLocation;
}
Also used : HistoricalLocationEntity(org.n52.series.db.beans.sta.HistoricalLocationEntity)

Example 2 with HistoricalLocationEntity

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

the class ThingService method generateHistoricalLocation.

private void generateHistoricalLocation(PlatformEntity thing) throws STACRUDException {
    if (thing == null) {
        throw new STACRUDException("Error processing HistoricalLocations. Thing does not exist!");
    }
    // Persist nested HistoricalLocations
    if (thing.hasHistoricalLocations()) {
        Set<HistoricalLocationEntity> historicalLocations = thing.getHistoricalLocations();
        thing.setHistoricalLocations(null);
        for (HistoricalLocationEntity historicalLocation : historicalLocations) {
            // Check if historicalLocation is not already persisted
            if (historicalLocation.getId() == null) {
                getHistoricalLocationService().createOrUpdate(historicalLocation);
            }
        }
    }
    // Create new HistoricalLocation based on current location
    if (thing.hasLocationEntities()) {
        Set<HistoricalLocationEntity> historicalLocations = thing.hasHistoricalLocations() ? new LinkedHashSet<>(thing.getHistoricalLocations()) : new LinkedHashSet<>();
        HistoricalLocationEntity historicalLocation = new HistoricalLocationEntity();
        historicalLocation.setIdentifier(UUID.randomUUID().toString());
        historicalLocation.setThing(thing);
        historicalLocation.setTime(DateTime.now().toDate());
        historicalLocation.setProcessed(true);
        HistoricalLocationEntity createdHistoricalLocation = getHistoricalLocationService().createOrUpdate(historicalLocation);
        if (createdHistoricalLocation != null) {
            historicalLocations.add(createdHistoricalLocation);
        }
        for (LocationEntity location : thing.getLocations()) {
            location.setHistoricalLocations(Collections.singleton(createdHistoricalLocation));
            getLocationService().createOrUpdate(location);
        }
        thing.setHistoricalLocations(historicalLocations);
    }
}
Also used : HistoricalLocationEntity(org.n52.series.db.beans.sta.HistoricalLocationEntity) LocationEntity(org.n52.series.db.beans.sta.LocationEntity) HistoricalLocationEntity(org.n52.series.db.beans.sta.HistoricalLocationEntity) STACRUDException(org.n52.shetland.ogc.sta.exception.STACRUDException)

Example 3 with HistoricalLocationEntity

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

the class HistoricalLocationService method fetchExpandEntities.

@Override
protected HistoricalLocationEntity fetchExpandEntities(HistoricalLocationEntity entity, ExpandFilter expandOption) throws STACRUDException, STAInvalidQueryException {
    for (ExpandItem expandItem : expandOption.getItems()) {
        String expandProperty = expandItem.getPath();
        if (HistoricalLocationEntityDefinition.NAVIGATION_PROPERTIES.contains(expandProperty)) {
            switch(expandProperty) {
                case STAEntityDefinition.LOCATIONS:
                    Page<LocationEntity> locations = getLocationService().getEntityCollectionByRelatedEntityRaw(entity.getStaIdentifier(), STAEntityDefinition.HISTORICAL_LOCATIONS, expandItem.getQueryOptions());
                    entity.setLocations(locations.get().collect(Collectors.toSet()));
                    break;
                case STAEntityDefinition.THING:
                    PlatformEntity things = getThingService().getEntityByRelatedEntityRaw(entity.getStaIdentifier(), STAEntityDefinition.HISTORICAL_LOCATIONS, null, expandItem.getQueryOptions());
                    entity.setThing(things);
                    break;
                default:
                    throw new RuntimeException("This can never happen!");
            }
        } else {
            throw new STAInvalidQueryException("Invalid expandOption supplied. Cannot find " + expandProperty + " on Entity of type 'HistoricalLocation'");
        }
    }
    return entity;
}
Also used : STAInvalidQueryException(org.n52.shetland.ogc.sta.exception.STAInvalidQueryException) PlatformEntity(org.n52.series.db.beans.PlatformEntity) ExpandItem(org.n52.shetland.filter.ExpandItem) HistoricalLocationEntity(org.n52.series.db.beans.sta.HistoricalLocationEntity) LocationEntity(org.n52.series.db.beans.sta.LocationEntity)

Example 4 with HistoricalLocationEntity

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

the class HistoricalLocationService method updateLocations.

private void updateLocations(HistoricalLocationEntity historicalLocation) throws STACRUDException {
    for (LocationEntity location : historicalLocation.getLocations()) {
        location.getHistoricalLocations().remove(historicalLocation);
        getLocationService().updateEntity(location);
    }
}
Also used : HistoricalLocationEntity(org.n52.series.db.beans.sta.HistoricalLocationEntity) LocationEntity(org.n52.series.db.beans.sta.LocationEntity)

Example 5 with HistoricalLocationEntity

use of org.n52.series.db.beans.sta.HistoricalLocationEntity 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)

Aggregations

HistoricalLocationEntity (org.n52.series.db.beans.sta.HistoricalLocationEntity)14 LocationEntity (org.n52.series.db.beans.sta.LocationEntity)9 PlatformEntity (org.n52.series.db.beans.PlatformEntity)6 DatastreamEntity (org.n52.series.db.beans.sta.DatastreamEntity)3 ExpandItem (org.n52.shetland.filter.ExpandItem)3 STACRUDException (org.n52.shetland.ogc.sta.exception.STACRUDException)3 STAInvalidQueryException (org.n52.shetland.ogc.sta.exception.STAInvalidQueryException)3 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Expression (javax.persistence.criteria.Expression)1 Join (javax.persistence.criteria.Join)1 JoinType (javax.persistence.criteria.JoinType)1 Root (javax.persistence.criteria.Root)1 Subquery (javax.persistence.criteria.Subquery)1 AbstractFeatureEntity (org.n52.series.db.beans.AbstractFeatureEntity)1 DescribableEntity (org.n52.series.db.beans.DescribableEntity)1