use of eu.bcvsolutions.idm.core.api.dto.ExportDescriptorDto in project CzechIdMng by bcvsolutions.
the class DefaultSysSyncConfigService method export.
@Override
public void export(UUID id, IdmExportImportDto batch) {
super.export(id, batch);
AbstractSysSyncConfigDto dto = this.get(id);
if (dto == null) {
return;
}
ExportDescriptorDto descriptorDto = getExportManager().getDescriptor(batch, dto.getClass());
if (descriptorDto != null) {
descriptorDto.setOptional(true);
// Token will be excluded for the export. It means a token on the target IdM will be not changed.
// Or will be sets to the null, if sync does not exist yet.
descriptorDto.getExcludedFields().add(SysSyncConfig_.token.getName());
if (dto instanceof SysSyncContractConfigDto) {
// Tree-type will be searching by code (advanced paring by treeType field)
descriptorDto.getAdvancedParingFields().add(SysSyncContractConfig_.defaultTreeType.getName());
// Tree-type will be searching by code (advanced paring by treeNode field)
descriptorDto.getAdvancedParingFields().add(SysSyncContractConfig_.defaultTreeNode.getName());
}
}
}
use of eu.bcvsolutions.idm.core.api.dto.ExportDescriptorDto 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.dto.ExportDescriptorDto in project CzechIdMng by bcvsolutions.
the class DefaultImportManager method removeRedundant.
/**
* Ensures delete redundant entities in target IdM. Deletes DTOs are within
* golden DTO type (super parent = typically system, role, ...).
*
* @param descriptor
* @param context
*/
private void removeRedundant(ExportDescriptorDto descriptor, ImportContext context) {
Class<? extends BaseDto> dtoClass = descriptor.getDtoClass();
// Does this DTO support the authoritative mode?
boolean supportsAuthoritativeMode = descriptor.isSupportsAuthoritativeMode();
if (!supportsAuthoritativeMode) {
return;
}
String superParentFilterProperty = descriptor.getSuperParentFilterProperty();
Assert.notNull(superParentFilterProperty, "For authoritative mode must be superParentFilterProperty defined!");
// Find super parent (gold) DTO (typically it is DTO for system, role ...)
Class<? extends AbstractDto> superParentDtoClass = getSuperParentDtoClass(descriptor, context);
Assert.notNull(superParentDtoClass, "Supper parent cannot be null here!");
Path superParentDtoTypePath = Paths.get(context.getTempDirectory().toString(), superParentDtoClass.getSimpleName());
try {
// Find all super parent IDs for this DTO type in batch.
Set<UUID> superParentIdsInBatch;
try (Stream<Path> paths = Files.walk(superParentDtoTypePath)) {
superParentIdsInBatch = //
paths.filter(//
Files::isRegularFile).map(path -> {
BaseDto dto = convertFileToDto(path.toFile(), superParentDtoClass, context);
return (UUID) dto.getId();
}).collect(Collectors.toSet());
}
Set<Class<? extends BaseDto>> inheritedClasses = getInheritedClasses(dtoClass, context.getManifest());
// Find all IDs for all children classes
Set<Serializable> childrenIdsInBatch = Sets.newHashSet();
for (Class<? extends BaseDto> inheritedClass : inheritedClasses) {
// Find all IDs for this DTO type in batch.
Path dtoTypePath = Paths.get(context.getTempDirectory().toString(), inheritedClass.getSimpleName());
try (Stream<Path> paths = Files.walk(dtoTypePath)) {
Set<Serializable> childrenIds = //
paths.filter(//
Files::isRegularFile).map(//
path -> convertFileToDto(path.toFile(), inheritedClass, context)).map(dto -> {
// If ID has been replaced, then we need to also replace it.
if (context.getReplacedIDs().containsKey((UUID) dto.getId())) {
return context.getReplacedIDs().get((UUID) dto.getId());
}
return dto.getId();
}).collect(Collectors.toSet());
childrenIdsInBatch.addAll(childrenIds);
}
}
superParentIdsInBatch.forEach(superParentId -> {
try {
Class<? extends BaseDto> serviceDtoClass = dtoClass;
if (dtoClass.isAnnotationPresent(Inheritable.class)) {
serviceDtoClass = dtoClass.getAnnotation(Inheritable.class).dtoService();
}
ReadWriteDtoService<BaseDto, BaseFilter> dtoService = getDtoService(serviceDtoClass);
BaseFilter filterBase = dtoService.getFilterClass().getDeclaredConstructor().newInstance();
// Fill super-parent-property by superParentId (call setter = check if filter is
// implemented).
new PropertyDescriptor(superParentFilterProperty, dtoService.getFilterClass()).getWriteMethod().invoke(filterBase, superParentId);
// Load all IDs in IdM for this parent ID.
List<UUID> childrenIdsInIdM = //
dtoService.find(filterBase, null).getContent().stream().map(//
childDto -> ((AbstractDto) childDto).getId()).collect(Collectors.toList());
// IDs to delete = entities missing in the batch.
Set<UUID> idsToDelete = //
childrenIdsInIdM.stream().filter(//
idmId -> !childrenIdsInBatch.contains(idmId)).collect(Collectors.toSet());
idsToDelete.forEach(id -> {
BaseDto baseDto = dtoService.get(id);
IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), baseDto, RequestOperationType.REMOVE, superParentId);
if (!context.isDryRun()) {
dtoService.delete(baseDto);
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 (ReflectiveOperationException | IllegalArgumentException | IntrospectionException e) {
throw new ResultCodeException(CoreResultCode.EXPORT_IMPORT_REFLECTION_FAILED, e);
}
});
} catch (IOException e) {
throw new ResultCodeException(CoreResultCode.EXPORT_IMPORT_IO_FAILED, e);
}
}
use of eu.bcvsolutions.idm.core.api.dto.ExportDescriptorDto in project CzechIdMng by bcvsolutions.
the class DefaultFormService method export.
@Override
public void export(IdmFormInstanceDto formInstanceDto, IdmExportImportDto batch) {
Assert.notNull(batch, "Export batch must exist!");
Assert.notNull(formInstanceDto, "Instance of cannot be null for export!");
// All confidential values will be removed from the export. We don't want change
// confidential value on a target IdM.
List<IdmFormValueDto> valuesWithoutConfidential = //
formInstanceDto.getValues().stream().filter(//
formValue -> !formValue.isConfidential()).collect(Collectors.toList());
formInstanceDto.setValues(valuesWithoutConfidential);
batch.getExportedDtos().add(formInstanceDto);
ExportDescriptorDto exportDescriptorDto = new ExportDescriptorDto(formInstanceDto.getClass());
if (!batch.getExportOrder().contains(exportDescriptorDto)) {
batch.getExportOrder().add(exportDescriptorDto);
}
}
use of eu.bcvsolutions.idm.core.api.dto.ExportDescriptorDto in project CzechIdMng by bcvsolutions.
the class DefaultExportManager method setAuthoritativeMode.
@Override
public void setAuthoritativeMode(Set<String> parentFields, String superParentFilterProperty, Class<? extends BaseDto> dtoClass, IdmExportImportDto batch) {
ExportDescriptorDto descriptorDto = this.getDescriptor(batch, dtoClass);
if (descriptorDto != null) {
descriptorDto.getParentFields().addAll(parentFields);
descriptorDto.setSupportsAuthoritativeMode(true);
descriptorDto.setSuperParentFilterProperty(superParentFilterProperty);
}
}
Aggregations