use of eu.bcvsolutions.idm.core.api.dto.AbstractDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmRequestService method toDto.
@Override
public IdmRequestDto toDto(IdmRequest entity, IdmRequestDto dto) {
IdmRequestDto requestDto = super.toDto(entity, dto);
// Load and add WF process DTO to embedded. Prevents of many requests from FE.
if (requestDto != null && requestDto.getWfProcessId() != null) {
if (RequestState.IN_PROGRESS == requestDto.getState()) {
// Instance of process should exists only in 'IN_PROGRESS' state
WorkflowProcessInstanceDto processInstanceDto = workflowProcessInstanceService.get(requestDto.getWfProcessId());
// size
if (processInstanceDto != null) {
processInstanceDto.setProcessVariables(null);
}
requestDto.getEmbedded().put(AbstractRequestDto.WF_PROCESS_FIELD, processInstanceDto);
} else {
// In others states we need load historic process
WorkflowHistoricProcessInstanceDto processHistDto = workflowHistoricProcessInstanceService.get(requestDto.getWfProcessId());
// size
if (processHistDto != null) {
processHistDto.setProcessVariables(null);
}
requestDto.getEmbedded().put(AbstractRequestDto.WF_PROCESS_FIELD, processHistDto);
}
}
// Load and add owner DTO to embedded. Prevents of many requests from FE.
if (requestDto != null && requestDto.getOwnerId() != null && requestDto.getOwnerType() != null) {
try {
@SuppressWarnings("unchecked") Requestable requestable = requestManager.get(requestDto.getId(), requestDto.getOwnerId(), (Class<Requestable>) Class.forName(requestDto.getOwnerType()));
if (requestable instanceof AbstractDto) {
// If is requestable realized REMOVE, then requestable DTO does not contains
// data (only ID). In this case we don't want send this DTO to FE.
AbstractDto requestableDto = (AbstractDto) requestable;
IdmRequestItemDto itemDto = DtoUtils.getEmbedded(requestableDto, Requestable.REQUEST_ITEM_FIELD, IdmRequestItemDto.class, null);
if (itemDto != null && RequestOperationType.REMOVE == itemDto.getOperation() && itemDto.getState().isTerminatedState()) {
requestable = null;
}
// Minimise response size
requestableDto.setEmbedded(null);
}
if (requestable == null) {
// Entity was not found ... maybe was deleted or not exists yet
LOG.debug(MessageFormat.format("Owner [{0}, {1}] not found for request {2}.", requestDto.getOwnerType(), requestDto.getOwnerId(), requestDto.getId()));
}
requestDto.getEmbedded().put(IdmRequestDto.OWNER_FIELD, requestable);
} catch (ClassNotFoundException e) {
// Only print warning
LOG.warn(MessageFormat.format("Class not found for request {0}.", requestDto.getId()), e);
}
}
return requestDto;
}
use of eu.bcvsolutions.idm.core.api.dto.AbstractDto in project CzechIdMng by bcvsolutions.
the class DefaultImportManager method findByExample.
/**
* Find target DTO by example source DTO (typically by more then one filter field).
*
* @param dto
* @param embeddedDto
* @param context
* @return
*/
@SuppressWarnings("unchecked")
private BaseDto findByExample(BaseDto dto, EmbeddedDto embeddedDto, ImportContext context) throws IOException {
// check, if we can include additional embedded dtos
if (embeddedDto == null) {
// additional embedded dtos not given
return lookupService.lookupByExample(dto);
}
if (!(dto instanceof AbstractDto)) {
// additional embedded dtos supports abstract dto only
return lookupService.lookupByExample(dto);
}
Map<String, JsonNode> jsons = embeddedDto.getEmbedded();
if (CollectionUtils.isEmpty(jsons)) {
// additional embedded dtos given, but empty
return lookupService.lookupByExample(dto);
}
// try to init embedded dtos (~ typed) from embedded json (~ string)
for (Entry<String, JsonNode> entry : jsons.entrySet()) {
AbstractDto abstractDto = (AbstractDto) dto;
String propertyName = entry.getKey();
if (abstractDto.getEmbedded().containsKey(propertyName)) {
// already filled
continue;
}
// get dto type
JsonNode json = entry.getValue();
// by convention
JsonNode jsonDtoType = json.get(AbstractDto.PROPERTY_DTO_TYPE);
if (jsonDtoType == null) {
// dto type is not specified
continue;
}
// resolve dto type as class
String stringDtoType = jsonDtoType.asText();
try {
Class<?> dtoType = Class.forName(stringDtoType);
if (!BaseDto.class.isAssignableFrom(dtoType)) {
// base dto is supported in embedded only
continue;
}
BaseDto baseDto = this.convertStringToDto(json.toString(), (Class<? extends BaseDto>) dtoType, context);
if (baseDto != null) {
abstractDto.getEmbedded().put(propertyName, baseDto);
}
} catch (Exception ex) {
LOG.warn("DTO type [{}] cannot be resolved from json, dto will not be set for find current dto by example.", stringDtoType, ex);
}
}
//
return lookupService.lookupByExample(dto);
}
use of eu.bcvsolutions.idm.core.api.dto.AbstractDto in project CzechIdMng by bcvsolutions.
the class DefaultImportManager method getParentDtoFromBatch.
/**
* Find parent DTO in the batch by parent field in given DTO.
*
* @param dto
* @param context
* @return
* @throws IOException
*/
private BaseDto getParentDtoFromBatch(BaseDto dto, ImportContext context) {
ExportDescriptorDto descriptor = this.getDescriptor(context, dto.getClass());
Assert.notNull(descriptor, "Descriptor cannot be null!");
Set<String> parentFields = descriptor.getParentFields();
if (parentFields.isEmpty()) {
return null;
}
for (String parentField : parentFields) {
try {
UUID parentId = this.getFieldUUIDValue(parentField, dto, dto.getClass());
Class<? extends AbstractDto> parentType = this.getDtoClassFromField(dto.getClass(), parentField);
Assert.notNull(parentType, "Parent type cannot be null!");
if (parentId == null) {
continue;
}
Path dtoTypePath = Paths.get(context.getTempDirectory().toString(), parentType.getSimpleName());
try (Stream<Path> paths = Files.walk(dtoTypePath)) {
BaseDto parentDto = //
paths.filter(//
Files::isRegularFile).map(//
path -> convertFileToDto(path.toFile(), parentType, context)).filter(//
d -> parentId.equals(d.getId())).findFirst().orElse(null);
if (parentDto != null) {
return parentDto;
}
}
} catch (IOException ex) {
throw new ResultCodeException(CoreResultCode.IMPORT_ZIP_EXTRACTION_FAILED, ex);
}
}
return null;
}
use of eu.bcvsolutions.idm.core.api.dto.AbstractDto in project CzechIdMng by bcvsolutions.
the class IdentityAddContractGuaranteeBulkActionTest method withoutPermissionCreateGuarantee.
@Test
@Transactional
public void withoutPermissionCreateGuarantee() {
IdmIdentityDto identityForLogin = getHelper().createIdentity();
IdmRoleDto permissionRole = getHelper().createRole();
getHelper().createBasePolicy(permissionRole.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, IdmBasePermission.READ, IdmBasePermission.COUNT);
getHelper().createIdentityRole(identityForLogin, permissionRole);
loginAsNoAdmin(identityForLogin.getUsername());
List<IdmIdentityDto> guarantees = this.createIdentities(1);
IdmIdentityDto employee = getHelper().createIdentity();
IdmIdentityContractDto contract1 = getHelper().getPrimeContract(employee);
IdmBulkActionDto bulkAction = this.findBulkAction(IdmIdentity.class, IdentityAddContractGuaranteeBulkAction.NAME);
Set<UUID> ids = this.getIdFromList(Arrays.asList(employee));
bulkAction.setIdentifiers(ids);
Map<String, Object> properties = new HashMap<>();
List<String> uuidStrings = guarantees.stream().map(AbstractDto::getId).map(Object::toString).collect(Collectors.toList());
properties.put(IdentityAddContractGuaranteeBulkAction.PROPERTY_NEW_GUARANTEE, uuidStrings);
bulkAction.setProperties(properties);
IdmBulkActionDto processAction = bulkActionManager.processAction(bulkAction);
// no log record is expected
checkResultLrt(processAction, null, 0l, 1l);
// test guarantes on all contracts
List<IdmContractGuaranteeDto> assigned = getGuaranteesForContract(contract1.getId());
Assert.assertEquals(0, assigned.size());
}
use of eu.bcvsolutions.idm.core.api.dto.AbstractDto in project CzechIdMng by bcvsolutions.
the class IdentityRemoveContractGuaranteeBulkActionTest method withoutPermissionDeleteGuarantee.
@Test
@Transactional
public void withoutPermissionDeleteGuarantee() {
List<IdmIdentityDto> guarantees = this.createIdentities(1);
IdmIdentityDto employee = getHelper().createIdentity();
IdmIdentityContractDto contract1 = getHelper().getPrimeContract(employee);
Assert.assertEquals(0, getGuaranteesForContract(contract1.getId()).size());
// init guarantee to delete
createContractGuarantees(contract1, guarantees);
Assert.assertEquals(guarantees.size(), getGuaranteesForContract(contract1.getId()).size());
Assert.assertTrue(isContractGuarantee(contract1, guarantees).isEmpty());
// Log as user without delete permission
IdmIdentityDto identityForLogin = getHelper().createIdentity();
IdmRoleDto permissionRole = getHelper().createRole();
getHelper().createBasePolicy(permissionRole.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, IdmBasePermission.READ, IdmBasePermission.COUNT);
getHelper().createIdentityRole(identityForLogin, permissionRole);
loginAsNoAdmin(identityForLogin.getUsername());
IdmBulkActionDto bulkAction = this.findBulkAction(IdmIdentity.class, IdentityRemoveContractGuaranteeBulkAction.NAME);
Set<UUID> ids = this.getIdFromList(Arrays.asList(employee));
bulkAction.setIdentifiers(ids);
Map<String, Object> properties = new HashMap<>();
List<String> uuidStrings = guarantees.stream().map(AbstractDto::getId).map(Object::toString).collect(Collectors.toList());
properties.put(IdentityAddContractGuaranteeBulkAction.PROPERTY_OLD_GUARANTEE, uuidStrings);
bulkAction.setProperties(properties);
IdmBulkActionDto processAction = bulkActionManager.processAction(bulkAction);
checkResultLrt(processAction, null, 0l, 1l);
// test guarantes on all contracts
List<IdmContractGuaranteeDto> assigned = getGuaranteesForContract(contract1.getId());
Assert.assertEquals(guarantees.size(), assigned.size());
Assert.assertTrue(isContractGuarantee(contract1, guarantees).isEmpty());
}
Aggregations