Search in sources :

Example 1 with Entity

use of com.nexblocks.authguard.service.model.Entity in project AuthGuard by AuthGuard.

the class IdempotencyServiceImpl method performOperation.

@Override
public <T extends Entity> CompletableFuture<T> performOperation(final Supplier<T> operation, final String idempotentKey, final String entityType) {
    return findByKeyAndEntityType(idempotentKey, entityType).thenApplyAsync(record -> {
        if (record.isPresent()) {
            throw new IdempotencyException(record.get());
        }
        return operation.get();
    }).thenApply(result -> {
        final IdempotentRecordBO record = IdempotentRecordBO.builder().id(ID.generate()).entityId(result.getId()).entityType(result.getEntityType()).idempotentKey(idempotentKey).build();
        // we don't have to wait for this to finish
        CompletableFuture.runAsync(() -> create(record));
        return result;
    });
}
Also used : ServiceMapper(com.nexblocks.authguard.service.mappers.ServiceMapper) IdempotencyService(com.nexblocks.authguard.service.IdempotencyService) Entity(com.nexblocks.authguard.service.model.Entity) Inject(com.google.inject.Inject) IdempotentRecordBO(com.nexblocks.authguard.service.model.IdempotentRecordBO) Optional(java.util.Optional) IdempotencyException(com.nexblocks.authguard.service.exceptions.IdempotencyException) CompletableFuture(java.util.concurrent.CompletableFuture) IdempotentRecordsRepository(com.nexblocks.authguard.dal.persistence.IdempotentRecordsRepository) Supplier(java.util.function.Supplier) ID(com.nexblocks.authguard.service.util.ID) IdempotentRecordBO(com.nexblocks.authguard.service.model.IdempotentRecordBO) IdempotencyException(com.nexblocks.authguard.service.exceptions.IdempotencyException)

Example 2 with Entity

use of com.nexblocks.authguard.service.model.Entity in project AuthGuard by AuthGuard.

the class PersistenceService method create.

public BO create(final BO entity) {
    final OffsetDateTime now = OffsetDateTime.now();
    final DO mappedDo = boToDo.apply(entity);
    mappedDo.setId(ID.generate());
    mappedDo.setDeleted(false);
    mappedDo.setCreatedAt(now);
    mappedDo.setLastModified(now);
    return repository.save(mappedDo).thenApply(persisted -> {
        final BO persistedBo = doToBo.apply(persisted);
        messageBus.publish(channel, Messages.created(persistedBo));
        return persistedBo;
    }).join();
}
Also used : AbstractDO(com.nexblocks.authguard.dal.model.AbstractDO) MessageBus(com.nexblocks.authguard.emb.MessageBus) Entity(com.nexblocks.authguard.service.model.Entity) OffsetDateTime(java.time.OffsetDateTime) Optional(java.util.Optional) Function(java.util.function.Function) Repository(com.nexblocks.authguard.dal.repository.Repository) ID(com.nexblocks.authguard.service.util.ID) Messages(com.nexblocks.authguard.emb.Messages) OffsetDateTime(java.time.OffsetDateTime) AbstractDO(com.nexblocks.authguard.dal.model.AbstractDO)

Example 3 with Entity

use of com.nexblocks.authguard.service.model.Entity in project AuthGuard by AuthGuard.

the class PersistenceService method update.

public Optional<BO> update(final BO entity) {
    final OffsetDateTime now = OffsetDateTime.now();
    final DO mappedDo = boToDo.apply(entity);
    mappedDo.setLastModified(now);
    return repository.update(mappedDo).thenApply(opt -> {
        final Optional<BO> boOpt = opt.map(doToBo);
        boOpt.ifPresent(bo -> messageBus.publish(channel, Messages.updated(bo)));
        return boOpt;
    }).join();
}
Also used : AbstractDO(com.nexblocks.authguard.dal.model.AbstractDO) MessageBus(com.nexblocks.authguard.emb.MessageBus) Entity(com.nexblocks.authguard.service.model.Entity) OffsetDateTime(java.time.OffsetDateTime) Optional(java.util.Optional) Function(java.util.function.Function) Repository(com.nexblocks.authguard.dal.repository.Repository) ID(com.nexblocks.authguard.service.util.ID) Messages(com.nexblocks.authguard.emb.Messages) Optional(java.util.Optional) OffsetDateTime(java.time.OffsetDateTime) AbstractDO(com.nexblocks.authguard.dal.model.AbstractDO)

Aggregations

Entity (com.nexblocks.authguard.service.model.Entity)3 ID (com.nexblocks.authguard.service.util.ID)3 Optional (java.util.Optional)3 AbstractDO (com.nexblocks.authguard.dal.model.AbstractDO)2 Repository (com.nexblocks.authguard.dal.repository.Repository)2 MessageBus (com.nexblocks.authguard.emb.MessageBus)2 Messages (com.nexblocks.authguard.emb.Messages)2 OffsetDateTime (java.time.OffsetDateTime)2 Function (java.util.function.Function)2 Inject (com.google.inject.Inject)1 IdempotentRecordsRepository (com.nexblocks.authguard.dal.persistence.IdempotentRecordsRepository)1 IdempotencyService (com.nexblocks.authguard.service.IdempotencyService)1 IdempotencyException (com.nexblocks.authguard.service.exceptions.IdempotencyException)1 ServiceMapper (com.nexblocks.authguard.service.mappers.ServiceMapper)1 IdempotentRecordBO (com.nexblocks.authguard.service.model.IdempotentRecordBO)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Supplier (java.util.function.Supplier)1