Search in sources :

Example 1 with IdmFormInstanceDto

use of eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto in project CzechIdMng by bcvsolutions.

the class DefaultFormService method saveFormInstance.

/**
 * {@inheritDoc}
 *
 * TODO: validations by given form definition? I don't think, it will not be
 * useful in synchronization etc. - only FE validations will be enough ...
 */
@Override
@Transactional
public IdmFormInstanceDto saveFormInstance(Identifiable owner, IdmFormDefinitionDto formDefinition, List<IdmFormValueDto> values) {
    FormableEntity ownerEntity = getOwnerEntity(owner);
    Assert.notNull(values, "Form values are required!");
    Assert.notNull(ownerEntity, "Form values owner is required!");
    formDefinition = checkDefaultDefinition(ownerEntity.getClass(), formDefinition);
    // 
    FormValueService<FormableEntity> formValueService = getFormValueService(ownerEntity);
    // 
    Map<UUID, IdmFormValueDto> previousValues = new HashMap<>();
    formValueService.getValues(ownerEntity, formDefinition).forEach(formValue -> {
        previousValues.put(formValue.getId(), formValue);
    });
    // 
    List<IdmFormValueDto> results = new ArrayList<>();
    for (IdmFormValueDto value : values) {
        // value could contant attribute id only
        UUID attributeId = value.getFormAttribute();
        Assert.notNull(attributeId, "Form attribute is required");
        IdmFormAttributeDto attribute = formDefinition.getMappedAttribute(attributeId);
        Assert.notNull(attribute, "Form attribute is required");
        // 
        value.setOwnerAndAttribute(ownerEntity, attribute);
        // 
        IdmFormValueDto previousValue = value.getId() == null ? null : previousValues.get(value.getId());
        if (previousValue != null) {
            // saved values will not be removed
            previousValues.remove(value.getId());
            // confidential value is always updated - only new values are sent from client
            if (value.isConfidential() || !value.isEquals(previousValue)) {
                // update value
                results.add(formValueService.save(value));
                LOG.trace("FormValue [{}:{}] for owner [{}] was updated", attribute.getCode(), value.getId(), ownerEntity);
            }
        } else {
            // create new value
            results.add(formValueService.save(value));
            LOG.trace("FormValue [{}:{}] for owner [{}] was created", attribute.getCode(), value.getId(), ownerEntity);
        }
    }
    // 
    // remove unsaved values by attribute definition (patch method is not
    // implemented now)
    previousValues.values().stream().filter(formValue -> {
        // they could not be sent with form (only changed values)
        return !formValue.isConfidential();
    }).forEach(value -> {
        formValueService.delete(value);
        LOG.trace("FormValue [{}:{}] for owner [{}] was deleted", value.getFormAttribute(), value.getId(), ownerEntity);
    });
    // publish event - eav was saved
    if (lookupService.getDtoLookup(ownerEntity.getClass()) == null) {
        // TODO: remove this branch after all agends will be rewritten to dto usage
        entityEventManager.process(new CoreEvent<>(CoreEventType.EAV_SAVE, ownerEntity));
    } else {
        entityEventManager.process(new CoreEvent<>(CoreEventType.EAV_SAVE, lookupService.lookupDto(ownerEntity.getClass(), ownerEntity.getId())));
    }
    // 
    return new IdmFormInstanceDto(ownerEntity, formDefinition, results);
}
Also used : FormableEntity(eu.bcvsolutions.idm.core.eav.api.entity.FormableEntity) OrderAwarePluginRegistry(org.springframework.plugin.core.OrderAwarePluginRegistry) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) PluginRegistry(org.springframework.plugin.core.PluginRegistry) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) FormService(eu.bcvsolutions.idm.core.eav.api.service.FormService) StringUtils(org.apache.commons.lang3.StringUtils) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) PersistentType(eu.bcvsolutions.idm.core.eav.api.domain.PersistentType) Introspector(java.beans.Introspector) CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent) Lists(com.google.common.collect.Lists) LookupService(eu.bcvsolutions.idm.core.api.service.LookupService) IdmFormValueDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Map(java.util.Map) IdmFormAttributeService(eu.bcvsolutions.idm.core.eav.api.service.IdmFormAttributeService) Pageable(org.springframework.data.domain.Pageable) IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) Method(java.lang.reflect.Method) ConfigurationClass(eu.bcvsolutions.idm.core.api.domain.ConfigurationClass) ImmutableMap(com.google.common.collect.ImmutableMap) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) ConfigurationClassProperty(eu.bcvsolutions.idm.core.api.domain.ConfigurationClassProperty) UUID(java.util.UUID) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) IntrospectionException(java.beans.IntrospectionException) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) List(java.util.List) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) FormValueService(eu.bcvsolutions.idm.core.eav.api.service.FormValueService) IdmFormDefinitionService(eu.bcvsolutions.idm.core.eav.api.service.IdmFormDefinitionService) PropertyDescriptor(java.beans.PropertyDescriptor) CoreEventType(eu.bcvsolutions.idm.core.api.event.CoreEvent.CoreEventType) Identifiable(eu.bcvsolutions.idm.core.api.domain.Identifiable) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) PageImpl(org.springframework.data.domain.PageImpl) EntityEventManager(eu.bcvsolutions.idm.core.api.service.EntityEventManager) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) HashMap(java.util.HashMap) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) ArrayList(java.util.ArrayList) IdmFormValueDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto) UUID(java.util.UUID) FormableEntity(eu.bcvsolutions.idm.core.eav.api.entity.FormableEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with IdmFormInstanceDto

use of eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto in project CzechIdMng by bcvsolutions.

the class IdentityReportExecutor method generateData.

@Override
protected IdmAttachmentDto generateData(RptReportDto report) {
    File temp = null;
    FileOutputStream outputStream = null;
    try {
        // prepare temp file for json stream
        temp = getAttachmentManager().createTempFile();
        outputStream = new FileOutputStream(temp);
        // write into json stream
        JsonGenerator jGenerator = getMapper().getFactory().createGenerator(outputStream, JsonEncoding.UTF8);
        try {
            // json will be array of identities
            jGenerator.writeStartArray();
            // form instance has useful methods to transform form values
            IdmFormInstanceDto formInstance = new IdmFormInstanceDto(report, getFormDefinition(), report.getFilter());
            // initialize filter by given form - transform to multi value map
            // => form attribute defined above will be automaticaly mapped to identity filter
            IdmIdentityFilter filter = new IdmIdentityFilter(formInstance.toMultiValueMap());
            // report extends long running task - show progress by count and counter lrt attributes
            counter = 0L;
            // find a first page of identities
            Pageable pageable = new PageRequest(0, 100, new Sort(Direction.ASC, IdmIdentity_.username.getName()));
            do {
                Page<IdmIdentityDto> identities = identityService.find(filter, pageable, IdmBasePermission.READ);
                if (count == null) {
                    // report extends long running task - show progress by count and counter lrt attributes
                    count = identities.getTotalElements();
                }
                boolean canContinue = true;
                for (Iterator<IdmIdentityDto> i = identities.iterator(); i.hasNext() && canContinue; ) {
                    // write single identity into json
                    getMapper().writeValue(jGenerator, i.next());
                    // 
                    // supports cancel report generating (report extends long running task)
                    ++counter;
                    canContinue = updateState();
                }
                // iterate while next page of identities is available
                pageable = identities.hasNext() && canContinue ? identities.nextPageable() : null;
            } while (pageable != null);
            // 
            // close array of identities
            jGenerator.writeEndArray();
        } finally {
            // close json stream
            jGenerator.close();
        }
        // save create temp file with array of identities in json as attachment
        return createAttachment(report, new FileInputStream(temp));
    } catch (IOException ex) {
        throw new ReportGenerateException(report.getName(), ex);
    } finally {
        // just for sure - jGenerator should close stream itself
        IOUtils.closeQuietly(outputStream);
        FileUtils.deleteQuietly(temp);
    }
}
Also used : IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) IdmIdentityFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmIdentityFilter) IOException(java.io.IOException) ReportGenerateException(eu.bcvsolutions.idm.rpt.api.exception.ReportGenerateException) FileInputStream(java.io.FileInputStream) PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) FileOutputStream(java.io.FileOutputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Sort(org.springframework.data.domain.Sort) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) File(java.io.File)

Example 3 with IdmFormInstanceDto

use of eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto in project CzechIdMng by bcvsolutions.

the class IdentityReportExecutor method generateData.

@Override
protected IdmAttachmentDto generateData(RptReportDto report) {
    File temp = null;
    FileOutputStream outputStream = null;
    try {
        // prepare temp file for json stream
        temp = getAttachmentManager().createTempFile();
        outputStream = new FileOutputStream(temp);
        // write into json stream
        JsonGenerator jGenerator = getMapper().getFactory().createGenerator(outputStream, JsonEncoding.UTF8);
        try {
            // json will be array of identities
            jGenerator.writeStartArray();
            // form instance has useful methods to transform form values
            IdmFormInstanceDto formInstance = new IdmFormInstanceDto(report, getFormDefinition(), report.getFilter());
            // initialize filter by given form - transform to multi value map
            // => form attribute defined above will be automaticaly mapped to identity filter
            IdmIdentityFilter filter = new IdmIdentityFilter(formInstance.toMultiValueMap());
            // report extends long running task - show progress by count and counter lrt attributes
            counter = 0L;
            // find a first page of identities
            Pageable pageable = new PageRequest(0, 100, new Sort(Direction.ASC, IdmIdentity_.username.getName()));
            do {
                Page<IdmIdentityDto> identities = identityService.find(filter, pageable, IdmBasePermission.READ);
                if (count == null) {
                    // report extends long running task - show progress by count and counter lrt attributes
                    count = identities.getTotalElements();
                }
                boolean canContinue = true;
                for (Iterator<IdmIdentityDto> i = identities.iterator(); i.hasNext() && canContinue; ) {
                    // write single identity into json
                    getMapper().writeValue(jGenerator, i.next());
                    // 
                    // supports cancel report generating (report extends long running task)
                    ++counter;
                    canContinue = updateState();
                }
                // iterate while next page of identities is available
                pageable = identities.hasNext() && canContinue ? identities.nextPageable() : null;
            } while (pageable != null);
            // 
            // close array of identities
            jGenerator.writeEndArray();
        } finally {
            // close json stream
            jGenerator.close();
        }
        // save create temp file with array of identities in json as attachment
        return createAttachment(report, new FileInputStream(temp));
    } catch (IOException ex) {
        throw new ReportGenerateException(report.getName(), ex);
    } finally {
        // just for sure - jGenerator should close stream itself
        IOUtils.closeQuietly(outputStream);
        FileUtils.deleteQuietly(temp);
    }
}
Also used : IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) IdmIdentityFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmIdentityFilter) IOException(java.io.IOException) ReportGenerateException(eu.bcvsolutions.idm.rpt.api.exception.ReportGenerateException) FileInputStream(java.io.FileInputStream) PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) FileOutputStream(java.io.FileOutputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Sort(org.springframework.data.domain.Sort) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) File(java.io.File)

Example 4 with IdmFormInstanceDto

use of eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto in project CzechIdMng by bcvsolutions.

the class TestFilterReportExecutor method generateData.

@Override
protected IdmAttachmentDto generateData(RptReportDto report) {
    try {
        IdmFormInstanceDto formInstance = new IdmFormInstanceDto(report, getFormDefinition(), report.getFilter());
        String username = (String) formInstance.toSinglePersistentValue(IdmIdentity_.username.getName());
        List<IdmIdentityDto> results = TestReportExecutor.identities.stream().filter(identity -> {
            return StringUtils.isEmpty(username) || username.equals(identity.getUsername());
        }).collect(Collectors.toList());
        return createAttachment(report, IOUtils.toInputStream(getMapper().writeValueAsString(results)));
    } catch (FileNotFoundException | JsonProcessingException ex) {
        throw new ReportGenerateException(report.getName(), ex);
    }
}
Also used : IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) Description(org.springframework.context.annotation.Description) IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) AbstractReportExecutor(eu.bcvsolutions.idm.rpt.api.executor.AbstractReportExecutor) FileNotFoundException(java.io.FileNotFoundException) PersistentType(eu.bcvsolutions.idm.core.eav.api.domain.PersistentType) RptReportDto(eu.bcvsolutions.idm.rpt.api.dto.RptReportDto) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Component(org.springframework.stereotype.Component) Lists(com.google.common.collect.Lists) ReportGenerateException(eu.bcvsolutions.idm.rpt.api.exception.ReportGenerateException) IdmIdentity_(eu.bcvsolutions.idm.core.model.entity.IdmIdentity_) IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) FileNotFoundException(java.io.FileNotFoundException) ReportGenerateException(eu.bcvsolutions.idm.rpt.api.exception.ReportGenerateException) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 5 with IdmFormInstanceDto

use of eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto in project CzechIdMng by bcvsolutions.

the class DefaultFormServiceItegrationTest method testReadConfidentialFormValue.

@Test
public void testReadConfidentialFormValue() {
    FormableEntity owner = createTestOwner("test6");
    IdmFormDefinitionDto formDefinition = formService.getDefinition(IdmIdentity.class);
    // save password
    IdmFormValueDto value1 = new IdmFormValueDto(formDefinition.getMappedAttributeByCode(InitDemoData.FORM_ATTRIBUTE_PASSWORD));
    value1.setValue(FORM_VALUE_ONE);
    formService.saveValues(owner, formDefinition, Lists.newArrayList(value1));
    IdmFormInstanceDto savedValues = formService.getFormInstance(owner);
    assertEquals(1, savedValues.getValues().size());
    assertEquals(GuardedString.SECRED_PROXY_STRING, savedValues.toSinglePersistentValue(InitDemoData.FORM_ATTRIBUTE_PASSWORD));
    // 
    identityService.deleteById(owner.getId());
}
Also used : IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) IdmFormValueDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto) FormableEntity(eu.bcvsolutions.idm.core.eav.api.entity.FormableEntity) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Aggregations

IdmFormInstanceDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto)9 IdmFormDefinitionDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto)5 IdmFormValueDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto)5 FormableEntity (eu.bcvsolutions.idm.core.eav.api.entity.FormableEntity)5 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)3 IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)3 ReportGenerateException (eu.bcvsolutions.idm.rpt.api.exception.ReportGenerateException)3 List (java.util.List)3 Pageable (org.springframework.data.domain.Pageable)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 Lists (com.google.common.collect.Lists)2 IdmIdentityFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmIdentityFilter)2 PersistentType (eu.bcvsolutions.idm.core.eav.api.domain.PersistentType)2 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 Collectors (java.util.stream.Collectors)2 StringUtils (org.apache.commons.lang3.StringUtils)2