use of eu.bcvsolutions.idm.core.api.domain.Codeable in project CzechIdMng by bcvsolutions.
the class AbstractProvisioningExecutor method createAccountsForAllSystems.
@Override
public void createAccountsForAllSystems(DTO dto) {
SystemEntityType entityType = SystemEntityType.getByClass(dto.getClass());
List<SysSystemMappingDto> systemMappings = findSystemMappingsForEntityType(dto, entityType);
systemMappings.forEach(mapping -> {
SysSchemaObjectClassDto schemaObjectClassDto = schemaObjectClassService.get(mapping.getObjectClass());
UUID systemId = schemaObjectClassDto.getSystem();
UUID accountId = this.getAccountByEntity(dto.getId(), systemId);
if (accountId != null) {
// We already have account for this system -> next
return;
}
SysSystemDto system = DtoUtils.getEmbedded(schemaObjectClassDto, SysSchemaObjectClass_.system, SysSystemDto.class);
List<SysSystemAttributeMappingDto> mappedAttributes = attributeMappingService.findBySystemMapping(mapping);
SysSystemAttributeMappingDto uidAttribute = attributeMappingService.getUidAttribute(mappedAttributes, system);
String uid = attributeMappingService.generateUid(dto, uidAttribute);
// Account management - can be the account created? - execute the script on the system mapping
if (!this.canBeAccountCreated(uid, dto, mapping, system)) {
String entityStr = dto.toString();
if (dto instanceof Codeable) {
entityStr = ((Codeable) dto).getCode();
}
LOG.info(MessageFormat.format("For entity [{0}] and entity type [{1}] cannot be created the account (on system [{2}])," + " because script \"Can be account created\" on the mapping returned \"false\"!", entityStr, entityType, system.getName()));
return;
}
// Create AccAccount and relation between account and entity
createEntityAccount(uid, dto.getId(), systemId);
});
}
use of eu.bcvsolutions.idm.core.api.domain.Codeable in project CzechIdMng by bcvsolutions.
the class AbstractSynchronizationExecutor method doDeleteEntity.
/**
* Delete entity linked with given account
*
* @param account
* @param entityType
* @param log
* @param logItem
* @param actionLogs
*/
protected void doDeleteEntity(AccAccountDto account, SystemEntityType entityType, SysSyncLogDto log, SysSyncItemLogDto logItem, List<SysSyncActionLogDto> actionLogs) {
DTO dto = this.getDtoByAccount(null, account);
if (dto == null) {
addToItemLog(logItem, MessageFormat.format("Warning! - Entity for account [{0}] was not found!", account.getUid()));
initSyncActionLog(SynchronizationActionType.DELETE_ENTITY, OperationResultType.WARNING, logItem, log, actionLogs);
return;
}
String entityIdentification = dto.getId().toString();
if (dto instanceof Codeable) {
entityIdentification = ((Codeable) dto).getCode();
}
logItem.setDisplayName(entityIdentification);
// Delete entity
getService().delete(dto);
}
use of eu.bcvsolutions.idm.core.api.domain.Codeable in project CzechIdMng by bcvsolutions.
the class AbstractSynchronizationExecutor method doCreateLink.
/**
* Create account and relation on him
*
* @param callProvisioning
* @param dto
* @param context
*/
protected void doCreateLink(DTO dto, boolean callProvisioning, SynchronizationContext context) {
String uid = context.getUid();
SystemEntityType entityType = context.getEntityType();
SysSystemDto system = context.getSystem();
SysSyncItemLogDto logItem = context.getLogItem();
SysSystemEntityDto systemEntity = context.getSystemEntity();
String entityIdentification = dto.getId().toString();
if (dto instanceof Codeable) {
entityIdentification = ((Codeable) dto).getCode();
}
logItem.setDisplayName(entityIdentification);
// Generate UID value from mapped attribute marked as UID (Unique ID).
// UID mapped attribute must exist and returned value must be not null
// and must be String
String attributeUid = this.generateUID(context);
AccAccountDto account = doCreateIdmAccount(attributeUid, system);
if (systemEntity != null) {
// If SystemEntity for this account already exist, then we linked
// him to new account
account.setSystemEntity(systemEntity.getId());
}
account = this.applySpecificSettingsBeforeLink(account, dto, context);
if (account == null) {
// Identity account won't be created
addToItemLog(logItem, MessageFormat.format("Link between uid [{0}] and entity [{1}] will not be created due to specific settings of synchronization. " + "Processing of this item is finished.", uid, entityIdentification));
return;
}
account = accountService.save(account);
addToItemLog(logItem, MessageFormat.format("Account with uid [{0}] and id [{1}] was created", uid, account.getId()));
// Create new entity account relation
EntityAccountDto entityAccount = this.createEntityAccount(account, dto, context);
entityAccount = (EntityAccountDto) getEntityAccountService().save(entityAccount);
context.addAccount(account);
// Identity account Created
addToItemLog(logItem, MessageFormat.format("Entity account relation with id [{0}], between account [{1}] and entity [{2}] was created", entityAccount.getId(), uid, entityIdentification));
logItem.setType(entityAccount.getClass().getSimpleName());
logItem.setIdentification(entityAccount.getId().toString());
if (callProvisioning) {
if (this.isProvisioningImplemented(entityType, logItem)) {
// Call provisioning for this entity
callProvisioningForEntity(dto, entityType, logItem);
}
}
}
use of eu.bcvsolutions.idm.core.api.domain.Codeable in project CzechIdMng by bcvsolutions.
the class DefaultImportManager method makeAdvancedPairing.
/**
* Make advanced paring
*
* @param dto
* @param context
* @param dtoClass
* @return
*/
private BaseDto makeAdvancedPairing(BaseDto dto, ImportContext context, Class<? extends BaseDto> dtoClass) {
ExportDescriptorDto descriptor = this.getDescriptor(context, dtoClass);
for (String advancedParingField : descriptor.getAdvancedParingFields()) {
try {
Class<? extends AbstractDto> dtoClassField = this.getDtoClassFromField(dtoClass, advancedParingField);
if (dtoClassField == null) {
throw new ResultCodeException(CoreResultCode.IMPORT_FIELD_EMBEDDED_ANNOTATION_MISSING, ImmutableMap.of("field", advancedParingField));
}
UUID id = this.getFieldUUIDValue(advancedParingField, dto, dtoClass);
if (id == null) {
// ID is null -> no relation exists -> continues
continue;
}
// Get service for DTO in field.
Class<? extends BaseDto> serviceDtoClass = dtoClassField;
if (dtoClassField.isAnnotationPresent(Inheritable.class)) {
serviceDtoClass = dto.getClass().getAnnotation(Inheritable.class).dtoService();
}
ReadWriteDtoService<BaseDto, ?> dtoServiceForField = getDtoService(serviceDtoClass);
BaseDto currentFieldDto = dtoServiceForField.get(id);
if (currentFieldDto != null) {
// DTO exists -> no change is necessary.
continue;
}
// DTO not found by ID, we will try find it by code.
if (dto instanceof AbstractDto) {
Path dtoPath = Paths.get(context.getTempDirectory().toString(), dtoClass.getSimpleName(), MessageFormat.format("{0}.{1}", dto.getId().toString(), ExportManager.EXTENSION_JSON));
EmbeddedDto embeddedDto = (EmbeddedDto) this.convertFileToDto(dtoPath.toFile(), EmbeddedDto.class, context);
JsonNode batchFieldDtoAsString = embeddedDto.getEmbedded().get(advancedParingField);
if (batchFieldDtoAsString == null) {
if (descriptor.isOptional()) {
return null;
} else {
Assert.notNull(batchFieldDtoAsString, MessageFormat.format("Embedded map must contains DTO for advanced paring field [{0}]", advancedParingField));
}
}
BaseDto batchFieldDto;
try {
batchFieldDto = this.convertStringToDto(batchFieldDtoAsString.toString(), dtoClassField, context);
String code = null;
if (batchFieldDto instanceof Codeable) {
code = ((Codeable) batchFieldDto).getCode();
if (Strings.isNotEmpty(code)) {
currentFieldDto = lookupService.lookupDto(serviceDtoClass, code);
}
} else if (batchFieldDto != null) {
// Find target DTO by example source DTO (typically by more then one filter field).
code = batchFieldDto.toString();
EmbeddedDto fieldEmbeddedDto = (EmbeddedDto) this.convertStringToDto(batchFieldDtoAsString.toString(), EmbeddedDto.class, context);
currentFieldDto = this.findByExample(batchFieldDto, fieldEmbeddedDto, context);
}
if (currentFieldDto != null) {
// DTO for given code exists -> replace ID by this new in given DTO.
//
new PropertyDescriptor(advancedParingField, dtoClass).getWriteMethod().invoke(dto, currentFieldDto.getId());
// Save old and new ID for next DTOs.
context.getReplacedIDs().put(id, (UUID) currentFieldDto.getId());
continue;
} else {
// If is DTO set as optional, we will only skip this DTO.
if (descriptor.isOptional()) {
throw new ResultCodeException(CoreResultCode.IMPORT_ADVANCED_PARING_NOT_FOUND_OPTIONAL, ImmutableMap.of("field", advancedParingField, "dto", dto.toString(), "notFoundDto", batchFieldDto.toString(), "code", code));
}
throw new ResultCodeException(CoreResultCode.IMPORT_ADVANCED_PARING_FAILED_NOT_FOUND, ImmutableMap.of("field", advancedParingField, "dto", dto.toString(), "notFoundDto", batchFieldDto.toString(), "code", code));
}
} catch (IOException e) {
throw new ResultCodeException(CoreResultCode.IMPORT_CONVERT_TO_DTO_FAILED, ImmutableMap.of("file", "Converted from String.", "dto", dtoClass), e);
}
}
// (skip this DTO).
if (descriptor.isOptional()) {
return null;
}
} catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
throw new ResultCodeException(CoreResultCode.EXPORT_IMPORT_REFLECTION_FAILED, e);
}
}
return dto;
}
use of eu.bcvsolutions.idm.core.api.domain.Codeable 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