use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.
the class DefaultEntityEventManager method createEvent.
private IdmEntityEventDto createEvent(Class<? extends Identifiable> ownerType, UUID ownerId, EntityEvent<? extends Identifiable> originalEvent) {
Assert.notNull(ownerType);
Assert.notNull(ownerId, "Change can be published after entity id is assigned at least.");
//
IdmEntityEventDto savedEvent = new IdmEntityEventDto();
savedEvent.setOwnerId(ownerId);
savedEvent.setOwnerType(getOwnerType(ownerType));
savedEvent.setResult(new OperationResultDto.Builder(OperationState.CREATED).build());
savedEvent.setInstanceId(eventConfiguration.getAsynchronousInstanceId());
//
if (originalEvent != null) {
savedEvent.setEventType(originalEvent.getType().name());
savedEvent.getProperties().putAll(originalEvent.getProperties());
savedEvent.setParent(EntityUtils.toUuid(originalEvent.getProperties().get(EVENT_PROPERTY_EVENT_ID)));
savedEvent.setExecuteDate((DateTime) originalEvent.getProperties().get(EVENT_PROPERTY_EXECUTE_DATE));
savedEvent.setPriority((PriorityType) originalEvent.getProperties().get(EVENT_PROPERTY_PRIORITY));
savedEvent.setParentEventType(originalEvent.getType().name());
savedEvent.setContent(originalEvent.getContent());
savedEvent.setOriginalSource(originalEvent.getOriginalSource());
savedEvent.setClosed(originalEvent.isClosed());
if (savedEvent.isClosed()) {
savedEvent.setResult(new OperationResultDto.Builder(OperationState.EXECUTED).setModel(new DefaultResultModel(CoreResultCode.EVENT_ALREADY_CLOSED)).build());
}
savedEvent.setSuspended(originalEvent.isSuspended());
} else {
// notify as default event type
savedEvent.setEventType(CoreEventType.NOTIFY.name());
}
if (savedEvent.getPriority() == null) {
savedEvent.setPriority(PriorityType.NORMAL);
}
//
return savedEvent;
}
use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.
the class DefaultSysProvisioningOperationService method handleFailed.
/**
* REQUIRES_NEW => we want to have log in queue / archive all time, even original transaction ends with exception.
*/
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public SysProvisioningOperationDto handleFailed(SysProvisioningOperationDto operation, Exception ex) {
ResultModel resultModel;
if (ex instanceof ResultCodeException) {
resultModel = ((ResultCodeException) ex).getError().getError();
} else {
String uid = getByProvisioningOperation(operation).getUid();
resultModel = new DefaultResultModel(AccResultCode.PROVISIONING_FAILED, ImmutableMap.of("name", uid, "system", getSystem(operation).getName(), "operationType", operation.getOperationType(), "objectClass", operation.getProvisioningContext().getConnectorObject().getObjectClass().getType()));
}
LOG.error(resultModel.toString(), ex);
//
operation.increaseAttempt();
operation.setMaxAttempts(provisioningConfiguration.getRetryMaxAttempts());
operation.setResult(new OperationResult.Builder(OperationState.EXCEPTION).setModel(resultModel).setCause(ex).build());
//
if (!operation.isDryRun()) {
operation = save(operation);
// create archive operation for the current attempt
provisioningArchiveService.archive(operation);
//
// calculate next attempt
SysProvisioningOperationDto firstOperation = getFirstOperationByBatchId(operation.getBatch());
if (firstOperation.equals(operation)) {
SysProvisioningBatchDto batch = batchService.get(operation.getBatch());
batch.setNextAttempt(batchService.calculateNextAttempt(operation));
batch = batchService.save(batch);
}
//
if (securityService.getCurrentId() != null) {
// TODO: check account owner
// TODO: notification is moved into console ... simple LOG instead?
notificationManager.send(AccModuleDescriptor.TOPIC_PROVISIONING, new IdmMessageDto.Builder().setModel(resultModel).build());
}
}
return operation;
}
use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.
the class DefaultProvisioningExecutor method execute.
/**
* Next processing is executed outside a transaction
* => operation states has to be saved in new transactions
* => rollback on the target system is not possible anyway.
*/
@Override
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public synchronized OperationResult execute(SysProvisioningBatchDto batch) {
Assert.notNull(batch, "Provisioning batch is required.");
batch = batchService.get(batch.getId());
//
OperationResult result = null;
List<SysProvisioningOperationDto> operations = provisioningOperationService.getByTimelineAndBatchId(batch.getId());
if (operations.isEmpty()) {
// reset next attempt time if is filled
if (batch.getNextAttempt() != null) {
batch.setNextAttempt(null);
batchService.save(batch);
}
return new OperationResult.Builder(OperationState.EXECUTED).build();
}
for (SysProvisioningOperationDto provisioningOperation : operations) {
// operation is already running - not complete or executed manually between
if (provisioningOperation.getResultState() == OperationState.RUNNING) {
LOG.debug("Previous operation [{}] still running, next operations will be executed after previous operation ends (with next retry run)", provisioningOperation.getId());
//
ResultModel resultModel = new DefaultResultModel(AccResultCode.PROVISIONING_IS_IN_QUEUE, ImmutableMap.of("name", provisioningOperation.getSystemEntityUid(), "system", provisioningOperation.getSystem(), "operationType", provisioningOperation.getOperationType(), "objectClass", provisioningOperation.getProvisioningContext().getConnectorObject().getObjectClass()));
result = new OperationResult.Builder(OperationState.NOT_EXECUTED).setModel(resultModel).build();
break;
}
// It not possible to get operation from embedded, because missing request
// not run in transaction
SysProvisioningOperationDto operation = executeInternal(provisioningOperation);
result = operation.getResult();
if (OperationState.EXECUTED != result.getState()) {
// stop processing next requests
return result;
}
}
// last processed request state (previous requests will be OperationState.EXECUTED)
return result;
}
use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.
the class ContractSliceDeleteProcessor method createDeleteDirtyState.
/**
* Create new dirty state for delete of contract slice
*
* @param slice
* @param parameters
* @return
*/
private IdmEntityStateDto createDeleteDirtyState(IdmContractSliceDto slice, Map<String, Serializable> parameters) {
Map<String, Object> transformedMarameters = new HashMap<String, Object>();
transformedMarameters.put("entityId", slice.getId());
// Mark state for delete the slice
transformedMarameters.put(ClearDirtyStateForContractSliceTaskExecutor.TO_DELETE, Boolean.TRUE);
transformedMarameters.putAll(parameters);
DefaultResultModel resultModel = new DefaultResultModel(CoreResultCode.DIRTY_STATE, transformedMarameters);
IdmEntityStateDto dirtyState = new IdmEntityStateDto();
dirtyState.setResult(new OperationResultDto.Builder(OperationState.BLOCKED).setModel(resultModel).build());
return entityStateManager.saveState(slice, dirtyState);
}
use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.
the class DefaultImportManager method executeImportForType.
/**
* Ensures add new and update existed DTOs by given batch.
*
* @param descriptor
* @param context
*/
private void executeImportForType(ExportDescriptorDto descriptor, ImportContext context) {
Class<? extends BaseDto> dtoClass = descriptor.getDtoClass();
Path dtoTypePath = Paths.get(context.getTempDirectory().toString(), dtoClass.getSimpleName());
try {
List<BaseDto> dtos;
try (Stream<Path> paths = Files.walk(dtoTypePath)) {
dtos = //
paths.filter(//
Files::isRegularFile).map(path -> {
BaseDto dto = convertFileToDto(path.toFile(), dtoClass, context);
Assert.notNull(dto, "DTO cannot be null after conversion from the batch!");
return dto;
}).collect(Collectors.toList());
}
if (dtos.isEmpty()) {
return;
}
// Sorts all DTOs for this type (maybe it is tree).
dtos = sortsDTOs(dtoClass, dtos);
int i = 0;
for (BaseDto dto : dtos) {
// Flush Hibernate in batch - performance improving
if (i % 20 == 0 && i > 0) {
// Call hard hibernate session flush and clear
if (getHibernateSession().isOpen()) {
getHibernateSession().flush();
getHibernateSession().clear();
}
}
i++;
// Increase counter and update state of import LRT.
context.getImportTaskExecutor().increaseCounter();
context.getImportTaskExecutor().updateState();
BaseDto parentDto = getParentDtoFromBatch(dto, context);
if (parentDto == null) {
parentDto = dto;
}
BaseDto originalDto = dto;
try {
dto = makeAdvancedPairing(dto, context, dtoClass);
if (dto == null) {
// If DTO after advanced pairing is null, then was not found and is optional ->
// skip.
IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), originalDto, RequestOperationType.ADD, (UUID) parentDto.getId());
ResultModel resultModel = new DefaultResultModel(CoreResultCode.IMPORT_DTO_SKIPPED, ImmutableMap.of("dto", originalDto.toString()));
dtoLog.setResult(new OperationResultDto.Builder(OperationState.CANCELED).setModel(resultModel).build());
importLogService.saveDistinct(dtoLog);
continue;
}
} catch (ResultCodeException ex) {
if (context.isDryRun() && ex.getError() != null && ex.getError().getError() != null && CoreResultCode.IMPORT_ADVANCED_PARING_FAILED_NOT_FOUND.name().equals(ex.getError().getError().getStatusEnum())) {
// Not found DTO we will mark as skipped in dry run mode.
IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), originalDto, RequestOperationType.ADD, (UUID) parentDto.getId());
dtoLog.setResult(new OperationResultDto.Builder(OperationState.EXCEPTION).setException(ex).build());
importLogService.saveDistinct(dtoLog);
continue;
} else if (ex.getError() != null && ex.getError().getError() != null && CoreResultCode.IMPORT_ADVANCED_PARING_NOT_FOUND_OPTIONAL.name().equals(ex.getError().getError().getStatusEnum())) {
// Not found DTO, but optional, we will mark as skipped.
IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), originalDto, RequestOperationType.ADD, (UUID) parentDto.getId());
dtoLog.setResult(new OperationResultDto.Builder(OperationState.CANCELED).setException(ex).build());
importLogService.saveDistinct(dtoLog);
continue;
}
throw ex;
}
Class<? extends BaseDto> serviceDtoClass = dtoClass;
if (dto instanceof IdmFormInstanceDto) {
// Form instance is very special here (doesn't have entity in DB).
IdmFormInstanceDto formInstance = (IdmFormInstanceDto) dto;
IdmFormDefinitionDto definition = formInstance.getFormDefinition();
Assert.notNull(definition, "Definition cannot be null for import!");
CoreEvent<IdmFormInstanceDto> event = new CoreEvent<>(CoreEventType.UPDATE, formInstance);
// Check if owner exist (UPDATE/ADD)
@SuppressWarnings("unchecked") Class<? extends BaseDto> ownerType = (Class<? extends BaseDto>) ((IdmFormInstanceDto) dto).getOwnerType();
UUID ownerId = UUID.fromString((String) ((IdmFormInstanceDto) dto).getOwnerId());
BaseDto ownerDto = this.getDtoService(ownerType).get(ownerId);
IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), dto, ownerDto != null ? RequestOperationType.UPDATE : RequestOperationType.ADD, ownerId);
if (!context.isDryRun()) {
formService.publish(event);
dtoLog.setResult(new OperationResultDto(OperationState.EXECUTED));
} else {
dtoLog.setResult(new OperationResultDto.Builder(OperationState.NOT_EXECUTED).setModel(//
new DefaultResultModel(CoreResultCode.IMPORT_EXECUTED_AS_DRYRUN)).build());
}
importLogService.saveDistinct(dtoLog);
continue;
}
if (dto.getClass().isAnnotationPresent(Inheritable.class)) {
serviceDtoClass = dto.getClass().getAnnotation(Inheritable.class).dtoService();
}
ReadWriteDtoService<BaseDto, ?> dtoService = getDtoService(serviceDtoClass);
BaseDto currentDto = dtoService.get(dto.getId());
if (currentDto != null) {
// DTO with same ID already exists -> update.
IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), dto, RequestOperationType.UPDATE, (UUID) parentDto.getId());
// Resolve excluded fields
dto = this.excludeFields(dto, currentDto, context);
if (!context.isDryRun()) {
dtoService.save(dto);
dtoLog.setResult(new OperationResultDto(OperationState.EXECUTED));
} else {
dtoLog.setResult(new OperationResultDto.Builder(OperationState.NOT_EXECUTED).setModel(//
new DefaultResultModel(CoreResultCode.IMPORT_EXECUTED_AS_DRYRUN)).build());
}
importLogService.saveDistinct(dtoLog);
continue;
}
if (dto instanceof Codeable) {
// We try to find exists DTO by code.
currentDto = lookupService.lookupDto(serviceDtoClass, ((Codeable) dto).getCode());
}
// Find target DTO by example source DTO (typically by more then one filter field).
currentDto = findByExample(dto, null, context);
if (dto instanceof IdmFormDefinitionDto) {
IdmFormDefinitionDto definition = (IdmFormDefinitionDto) dto;
// We try to find exists definition by code and type (IdmFormDefinitionDto is
// not Codeable).
currentDto = formService.getDefinition(definition.getType(), definition.getCode());
}
if (dto instanceof IdmFormAttributeDto) {
IdmFormAttributeDto attribute = (IdmFormAttributeDto) dto;
IdmFormDefinitionDto definition = formService.getDefinition(attribute.getFormDefinition());
if (definition != null) {
// We try to find exists attribute definition by code and form definition.
currentDto = formService.getAttribute(definition, attribute.getCode());
} else {
currentDto = null;
}
}
if (currentDto != null) {
// We found current DTO in IdM.
// Save old and new ID for next DTOs.
context.getReplacedIDs().put((UUID) dto.getId(), (UUID) currentDto.getId());
// We have to change the ID in import DTO.
dto.setId(currentDto.getId());
// Update current DTO by batch DTO.
IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), dto, RequestOperationType.UPDATE, (UUID) parentDto.getId());
if (!context.isDryRun()) {
// Resolve excluded fields
dto = this.excludeFields(dto, currentDto, context);
// Save a DTO.
dtoService.save(dto);
dtoLog.setResult(new OperationResultDto(OperationState.EXECUTED));
} else {
dtoLog.setResult(new OperationResultDto.Builder(OperationState.NOT_EXECUTED).setModel(//
new DefaultResultModel(CoreResultCode.IMPORT_EXECUTED_AS_DRYRUN)).build());
}
importLogService.saveDistinct(dtoLog);
} else {
IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), dto, RequestOperationType.ADD, (UUID) parentDto.getId());
// No current DTO was found -> create.
if (!context.isDryRun()) {
// Resolve excluded fields
dto = this.excludeFields(dto, null, context);
// Save new DTO.
dtoService.save(dto);
dtoLog.setResult(new OperationResultDto(OperationState.EXECUTED));
} else {
dtoLog.setResult(new OperationResultDto.Builder(OperationState.NOT_EXECUTED).setModel(//
new DefaultResultModel(CoreResultCode.IMPORT_EXECUTED_AS_DRYRUN)).build());
}
importLogService.saveDistinct(dtoLog);
}
}
} catch (IOException | IllegalArgumentException e) {
throw new ResultCodeException(CoreResultCode.EXPORT_IMPORT_IO_FAILED, e);
}
}
Aggregations