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