Search in sources :

Example 1 with FormableEntity

use of eu.bcvsolutions.idm.core.eav.api.entity.FormableEntity in project CzechIdMng by bcvsolutions.

the class DefaultFormService method saveValues.

@Override
@Transactional
public List<IdmFormValueDto> saveValues(Identifiable owner, IdmFormAttributeDto attribute, List<Serializable> persistentValues) {
    Assert.notNull(owner, "Form values owner is required!");
    Assert.notNull(owner.getId(), "Owner id is required!");
    Assert.notNull(attribute, "Form attribute definition is required!");
    FormableEntity ownerEntity = getOwnerEntity(owner);
    IdmFormDefinitionDto formDefinition = formDefinitionService.get(attribute.getFormDefinition());
    // 
    if (persistentValues == null || persistentValues.isEmpty()) {
        // delete previous attributes
        deleteValues(ownerEntity, attribute);
        return null;
    }
    if (!attribute.isMultiple() && persistentValues.size() > 1) {
        throw new IllegalArgumentException(MessageFormat.format("Form attribute [{0}:{1}] does not support multivalue, sent [{2}] values.", formDefinition.getCode(), attribute.getCode(), persistentValues.size()));
    }
    FormValueService<FormableEntity> formValueService = getFormValueService(ownerEntity);
    // get old values
    List<IdmFormValueDto> values = formValueService.getValues(ownerEntity, attribute);
    // size isn't same drop and create
    if (values.size() != persistentValues.size()) {
        deleteValues(owner, attribute);
        // create
        List<IdmFormValueDto> results = new ArrayList<>();
        for (short seq = 0; seq < persistentValues.size(); seq++) {
            IdmFormValueDto value = new IdmFormValueDto();
            value.setOwnerAndAttribute(ownerEntity, attribute);
            // 
            value.setValue(persistentValues.get(seq));
            value.setSeq(seq);
            results.add(formValueService.save(value));
        }
        ;
        // 
        return results;
    }
    // compare values
    List<IdmFormValueDto> results = new ArrayList<>();
    for (IdmFormValueDto value : values) {
        IdmFormValueDto newValue = new IdmFormValueDto();
        newValue.setOwnerAndAttribute(ownerEntity, attribute);
        Serializable serializableValue = persistentValues.get(value.getSeq());
        newValue.setValue(serializableValue);
        if (!value.isEquals(newValue)) {
            value.setValue(serializableValue);
            results.add(formValueService.save(value));
        }
    }
    return results;
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) 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) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with FormableEntity

use of eu.bcvsolutions.idm.core.eav.api.entity.FormableEntity in project CzechIdMng by bcvsolutions.

the class DefaultFormService method getValues.

@Override
@Transactional(readOnly = true)
public List<IdmFormValueDto> getValues(Identifiable owner, IdmFormAttributeDto attribute) {
    Assert.notNull(owner, "Form values owner is required!");
    Assert.notNull(owner.getId(), "Owner id is required!");
    Assert.notNull(attribute, "Form attribute definition is required!");
    // 
    FormableEntity ownerEntity = getOwnerEntity(owner);
    FormValueService<FormableEntity> formValueService = getFormValueService(ownerEntity);
    return formValueService.getValues(ownerEntity, attribute);
}
Also used : FormableEntity(eu.bcvsolutions.idm.core.eav.api.entity.FormableEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with FormableEntity

use of eu.bcvsolutions.idm.core.eav.api.entity.FormableEntity 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 4 with FormableEntity

use of eu.bcvsolutions.idm.core.eav.api.entity.FormableEntity in project CzechIdMng by bcvsolutions.

the class DefaultFormService method getConfidentialPersistentValue.

@Override
public Serializable getConfidentialPersistentValue(IdmFormValueDto guardedValue) {
    Assert.notNull(guardedValue);
    Assert.notNull(guardedValue.getOwnerId());
    // 
    FormableEntity ownerEntity = getOwnerEntity(guardedValue.getOwnerId(), guardedValue.getOwnerType());
    FormValueService<?> formValueService = getFormValueService(ownerEntity);
    return formValueService.getConfidentialPersistentValue(guardedValue);
}
Also used : FormableEntity(eu.bcvsolutions.idm.core.eav.api.entity.FormableEntity)

Example 5 with FormableEntity

use of eu.bcvsolutions.idm.core.eav.api.entity.FormableEntity in project CzechIdMng by bcvsolutions.

the class DefaultFormServiceItegrationTest method testSaveSingleAttributeValues.

@Test
public void testSaveSingleAttributeValues() {
    FormableEntity owner = createTestOwner("test8");
    IdmFormDefinitionDto formDefinition = formService.getDefinition(IdmIdentity.class);
    IdmFormAttributeDto attribute = formDefinition.getMappedAttributeByCode(InitDemoData.FORM_ATTRIBUTE_PHONE);
    // save value
    List<IdmFormValueDto> attributeValues = formService.saveValues(owner, attribute, Lists.newArrayList(FORM_VALUE_ONE));
    assertEquals(1, attributeValues.size());
    assertEquals(FORM_VALUE_ONE, attributeValues.get(0).getValue());
    attributeValues = formService.getValues(owner, attribute);
    assertEquals(1, attributeValues.size());
    assertEquals(FORM_VALUE_ONE, attributeValues.get(0).getValue());
    // 
    identityService.deleteById(owner.getId());
}
Also used : IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) 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

FormableEntity (eu.bcvsolutions.idm.core.eav.api.entity.FormableEntity)22 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)15 Test (org.junit.Test)15 IdmFormDefinitionDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto)14 IdmFormValueDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto)12 IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)9 Transactional (org.springframework.transaction.annotation.Transactional)6 IdmFormInstanceDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto)5 IdmIdentity (eu.bcvsolutions.idm.core.model.entity.IdmIdentity)4 Serializable (java.io.Serializable)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 ConfigurationClass (eu.bcvsolutions.idm.core.api.domain.ConfigurationClass)1 ConfigurationClassProperty (eu.bcvsolutions.idm.core.api.domain.ConfigurationClassProperty)1 CoreResultCode (eu.bcvsolutions.idm.core.api.domain.CoreResultCode)1 Identifiable (eu.bcvsolutions.idm.core.api.domain.Identifiable)1 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)1 CoreEvent (eu.bcvsolutions.idm.core.api.event.CoreEvent)1