use of org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class PanacheResourceStorage method createOrUpdateRegistryDeployment.
// *** RegistryDeployment
@Override
public boolean createOrUpdateRegistryDeployment(RegistryDeploymentData deployment) throws RegistryDeploymentStorageConflictException, RegistryDeploymentNotFoundException {
// TODO Is this necessary if using @Valid?
requireNonNull(deployment);
Optional<RegistryDeploymentData> existing = empty();
if (deployment.getId() != null) {
// TODO investigate using locks, such as optimistic locks
existing = deploymentRepository.findByIdOptional(deployment.getId());
if (existing.isEmpty()) {
throw new RegistryDeploymentNotFoundException(deployment.getId().toString());
}
}
try {
final Instant now = Instant.now();
deployment.getStatus().setLastUpdated(now);
if (existing.isEmpty()) {
deploymentRepository.persistAndFlush(deployment);
} else {
em.merge(deployment);
em.flush();
}
} catch (PersistenceException ex) {
if (ex.getCause() instanceof ConstraintViolationException) {
throw new RegistryDeploymentStorageConflictException();
} else {
throw ex;
}
}
return existing.isEmpty();
}
Aggregations