Search in sources :

Example 41 with AbstractDto

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;
}
Also used : IdmRequestItemDto(eu.bcvsolutions.idm.core.api.dto.IdmRequestItemDto) Requestable(eu.bcvsolutions.idm.core.api.domain.Requestable) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) IdmRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRequestDto) WorkflowProcessInstanceDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowProcessInstanceDto) WorkflowHistoricProcessInstanceDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowHistoricProcessInstanceDto)

Example 42 with AbstractDto

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);
}
Also used : AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) JsonNode(com.fasterxml.jackson.databind.JsonNode) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) IOException(java.io.IOException)

Example 43 with AbstractDto

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;
}
Also used : ExportDescriptorDto(eu.bcvsolutions.idm.core.api.dto.ExportDescriptorDto) Path(java.nio.file.Path) Embedded(eu.bcvsolutions.idm.core.api.domain.Embedded) ImportContext(eu.bcvsolutions.idm.core.api.dto.ImportContext) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) Autowired(org.springframework.beans.factory.annotation.Autowired) FormService(eu.bcvsolutions.idm.core.eav.api.service.FormService) CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent) IdmImportLogDto(eu.bcvsolutions.idm.core.api.dto.IdmImportLogDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Map(java.util.Map) BasePermission(eu.bcvsolutions.idm.core.security.api.domain.BasePermission) JsonNode(com.fasterxml.jackson.databind.JsonNode) AbstractLongRunningTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.AbstractLongRunningTaskExecutor) AttachmentConfiguration(eu.bcvsolutions.idm.core.ecm.api.config.AttachmentConfiguration) Path(java.nio.file.Path) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ImmutableMap(com.google.common.collect.ImmutableMap) ImportTaskExecutor(eu.bcvsolutions.idm.core.scheduler.task.impl.ImportTaskExecutor) IdmImportLogFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmImportLogFilter) ReadWriteDtoService(eu.bcvsolutions.idm.core.api.service.ReadWriteDtoService) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) Set(java.util.Set) IdmExportImportService(eu.bcvsolutions.idm.core.api.service.IdmExportImportService) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) FileSystemUtils(org.springframework.util.FileSystemUtils) Sets(com.google.common.collect.Sets) IntrospectionException(java.beans.IntrospectionException) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) Inheritable(eu.bcvsolutions.idm.core.api.domain.Inheritable) Objects(java.util.Objects) EmbeddedDto(eu.bcvsolutions.idm.core.api.dto.EmbeddedDto) List(java.util.List) Stream(java.util.stream.Stream) PropertyDescriptor(java.beans.PropertyDescriptor) CollectionUtils(org.springframework.util.CollectionUtils) Entry(java.util.Map.Entry) Strings(org.apache.logging.log4j.util.Strings) ExportImportType(eu.bcvsolutions.idm.core.api.domain.ExportImportType) ZipUtils(eu.bcvsolutions.idm.core.api.utils.ZipUtils) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) RequestOperationType(eu.bcvsolutions.idm.core.api.domain.RequestOperationType) ExportManager(eu.bcvsolutions.idm.core.api.service.ExportManager) ImportManager(eu.bcvsolutions.idm.core.api.service.ImportManager) IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) AttachableEntity(eu.bcvsolutions.idm.core.ecm.api.entity.AttachableEntity) Session(org.hibernate.Session) MessageFormat(java.text.MessageFormat) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) Lists(com.google.common.collect.Lists) LookupService(eu.bcvsolutions.idm.core.api.service.LookupService) IdmImportLogService(eu.bcvsolutions.idm.core.api.service.IdmImportLogService) Service(org.springframework.stereotype.Service) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) EntityUtils(eu.bcvsolutions.idm.core.api.utils.EntityUtils) IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) IdmExportImportDto(eu.bcvsolutions.idm.core.api.dto.IdmExportImportDto) Codeable(eu.bcvsolutions.idm.core.api.domain.Codeable) Files(java.nio.file.Files) LongRunningTaskManager(eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskManager) AttachmentManager(eu.bcvsolutions.idm.core.ecm.api.service.AttachmentManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) Field(java.lang.reflect.Field) File(java.io.File) BaseFilter(eu.bcvsolutions.idm.core.api.dto.filter.BaseFilter) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) ExportDescriptorDto(eu.bcvsolutions.idm.core.api.dto.ExportDescriptorDto) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) Paths(java.nio.file.Paths) CoreEventType(eu.bcvsolutions.idm.core.api.event.CoreEvent.CoreEventType) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) InputStream(java.io.InputStream) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) IOException(java.io.IOException) UUID(java.util.UUID) Files(java.nio.file.Files)

Example 44 with AbstractDto

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());
}
Also used : IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) IdmBulkActionDto(eu.bcvsolutions.idm.core.api.bulk.action.dto.IdmBulkActionDto) HashMap(java.util.HashMap) IdmContractGuaranteeDto(eu.bcvsolutions.idm.core.api.dto.IdmContractGuaranteeDto) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) IdmIdentityContractDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto) AbstractBulkActionTest(eu.bcvsolutions.idm.test.api.AbstractBulkActionTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 45 with AbstractDto

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());
}
Also used : IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) IdmBulkActionDto(eu.bcvsolutions.idm.core.api.bulk.action.dto.IdmBulkActionDto) HashMap(java.util.HashMap) IdmContractGuaranteeDto(eu.bcvsolutions.idm.core.api.dto.IdmContractGuaranteeDto) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) IdmIdentityContractDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto) AbstractBulkActionTest(eu.bcvsolutions.idm.test.api.AbstractBulkActionTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)54 UUID (java.util.UUID)28 Test (org.junit.Test)16 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)15 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)13 HashMap (java.util.HashMap)12 List (java.util.List)11 Autowired (org.springframework.beans.factory.annotation.Autowired)11 Transactional (org.springframework.transaction.annotation.Transactional)11 Assert (org.springframework.util.Assert)11 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)10 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)10 Map (java.util.Map)10 Service (org.springframework.stereotype.Service)10 Embedded (eu.bcvsolutions.idm.core.api.domain.Embedded)9 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)9 IntrospectionException (java.beans.IntrospectionException)9 Field (java.lang.reflect.Field)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 ImmutableMap (com.google.common.collect.ImmutableMap)8