Search in sources :

Example 6 with PlatformEntity

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

the class ThingService method delete.

@Override
public void delete(String identifier) throws STACRUDException {
    synchronized (getLock(identifier)) {
        if (getRepository().existsByStaIdentifier(identifier)) {
            PlatformEntity thing = getRepository().findByStaIdentifier(identifier, EntityGraphRepository.FetchGraph.FETCHGRAPH_DATASTREAMS, EntityGraphRepository.FetchGraph.FETCHGRAPH_HIST_LOCATION).get();
            // delete datastreams
            thing.getDatastreams().forEach(d -> {
                try {
                    getDatastreamService().delete(d);
                } catch (STACRUDException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            });
            // delete historicalLocation
            thing.getHistoricalLocations().forEach(hl -> {
                try {
                    getHistoricalLocationService().delete(hl);
                } catch (STACRUDException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            });
            getRepository().deleteByStaIdentifier(identifier);
        } else {
            throw new STACRUDException("Unable to delete. Entity not found.", HTTPStatus.NOT_FOUND);
        }
    }
}
Also used : PlatformEntity(org.n52.series.db.beans.PlatformEntity) STACRUDException(org.n52.shetland.ogc.sta.exception.STACRUDException)

Example 7 with PlatformEntity

use of org.n52.series.db.beans.PlatformEntity 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 8 with PlatformEntity

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

the class ThingService method createEntity.

@Override
public PlatformEntity createEntity(PlatformEntity newThing) throws STACRUDException {
    PlatformEntity thing = newThing;
    if (!thing.isProcessed()) {
        if (thing.getStaIdentifier() != null && !thing.isSetName()) {
            Optional<PlatformEntity> optionalEntity = getRepository().findByStaIdentifier(thing.getStaIdentifier());
            if (optionalEntity.isPresent()) {
                return optionalEntity.get();
            } else {
                throw new STACRUDException("No Thing with id '" + thing.getStaIdentifier() + "' found");
            }
        }
        if (thing.getStaIdentifier() == null) {
            if (getRepository().existsByName(thing.getName())) {
                return getRepository().findByName(thing.getName()).orElse(null);
            } else {
                // Autogenerate Identifier
                String uuid = UUID.randomUUID().toString();
                thing.setIdentifier(uuid);
                thing.setStaIdentifier(uuid);
            }
        }
        synchronized (getLock(thing.getStaIdentifier())) {
            if (getRepository().existsByStaIdentifier(thing.getStaIdentifier())) {
                throw new STACRUDException("Identifier already exists!", HTTPStatus.CONFLICT);
            } else {
                thing.setProcessed(true);
                boolean locationChanged = processLocations(thing, thing.getLocations());
                thing = getRepository().intermediateSave(thing);
                boolean hasUnpersistedHLocs = thing.hasHistoricalLocations() && thing.getHistoricalLocations().stream().anyMatch(p -> p.getId() == null);
                if (locationChanged || hasUnpersistedHLocs) {
                    generateHistoricalLocation(thing);
                }
                processDatastreams(thing);
                thing = getRepository().save(thing);
            }
        }
    }
    return thing;
}
Also used : DatastreamEntity(org.n52.series.db.beans.sta.DatastreamEntity) STAEntityDefinition(org.n52.shetland.ogc.sta.model.STAEntityDefinition) LoggerFactory(org.slf4j.LoggerFactory) DependsOn(org.springframework.context.annotation.DependsOn) STAInvalidQueryException(org.n52.shetland.ogc.sta.exception.STAInvalidQueryException) HistoricalLocationEntity(org.n52.series.db.beans.sta.HistoricalLocationEntity) HTTPStatus(org.n52.janmayen.http.HTTPStatus) HashSet(java.util.HashSet) ThingQuerySpecifications(org.n52.sta.data.query.ThingQuerySpecifications) EntityGraphRepository(org.n52.sta.data.repositories.EntityGraphRepository) LocationEntity(org.n52.series.db.beans.sta.LocationEntity) ExpandItem(org.n52.shetland.filter.ExpandItem) STACRUDException(org.n52.shetland.ogc.sta.exception.STACRUDException) ExpandFilter(org.n52.shetland.filter.ExpandFilter) LinkedHashSet(java.util.LinkedHashSet) IdentifierRepository(org.n52.sta.data.repositories.IdentifierRepository) Logger(org.slf4j.Logger) DateTime(org.joda.time.DateTime) HttpMethod(org.springframework.http.HttpMethod) ThingEntityDefinition(org.n52.shetland.ogc.sta.model.ThingEntityDefinition) Set(java.util.Set) UUID(java.util.UUID) ThingRepository(org.n52.sta.data.repositories.ThingRepository) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Component(org.springframework.stereotype.Component) Specification(org.springframework.data.jpa.domain.Specification) Optional(java.util.Optional) PlatformEntity(org.n52.series.db.beans.PlatformEntity) Collections(java.util.Collections) EntityTypes(org.n52.sta.data.service.EntityServiceRepository.EntityTypes) Transactional(org.springframework.transaction.annotation.Transactional) PlatformEntity(org.n52.series.db.beans.PlatformEntity) STACRUDException(org.n52.shetland.ogc.sta.exception.STACRUDException)

Example 9 with PlatformEntity

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

the class ThingService method processDatastreams.

private void processDatastreams(PlatformEntity thing) throws STACRUDException {
    if (thing.hasDatastreams()) {
        Set<DatastreamEntity> datastreams = new LinkedHashSet<>();
        for (DatastreamEntity datastream : thing.getDatastreams()) {
            datastream.setThing(thing);
            DatastreamEntity optionalDatastream = getDatastreamService().createEntity(datastream);
            datastreams.add(optionalDatastream != null ? optionalDatastream : datastream);
        }
        thing.setDatastreams(datastreams);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DatastreamEntity(org.n52.series.db.beans.sta.DatastreamEntity)

Example 10 with PlatformEntity

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

Aggregations

PlatformEntity (org.n52.series.db.beans.PlatformEntity)18 HistoricalLocationEntity (org.n52.series.db.beans.sta.HistoricalLocationEntity)13 LocationEntity (org.n52.series.db.beans.sta.LocationEntity)10 DatastreamEntity (org.n52.series.db.beans.sta.DatastreamEntity)6 STACRUDException (org.n52.shetland.ogc.sta.exception.STACRUDException)6 ExpandItem (org.n52.shetland.filter.ExpandItem)5 STAInvalidQueryException (org.n52.shetland.ogc.sta.exception.STAInvalidQueryException)5 HashSet (java.util.HashSet)4 LinkedHashSet (java.util.LinkedHashSet)4 PhenomenonEntity (org.n52.series.db.beans.PhenomenonEntity)4 ProcedureEntity (org.n52.series.db.beans.ProcedureEntity)4 ObservationEntity (org.n52.series.db.beans.sta.ObservationEntity)3 Specification (org.springframework.data.jpa.domain.Specification)3 Optional (java.util.Optional)2 Set (java.util.Set)2 Expression (javax.persistence.criteria.Expression)2 Join (javax.persistence.criteria.Join)2 JoinType (javax.persistence.criteria.JoinType)2 Root (javax.persistence.criteria.Root)2 Subquery (javax.persistence.criteria.Subquery)2