Search in sources :

Example 1 with Identifiable

use of eu.bcvsolutions.idm.core.api.domain.Identifiable 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 Identifiable

use of eu.bcvsolutions.idm.core.api.domain.Identifiable in project CzechIdMng by bcvsolutions.

the class DefaultAttachmentManagerIntegrationTest method testCreateAttachment.

@Test
@Transactional
public void testCreateAttachment() throws IOException {
    Identifiable owner = new TestOwnerEntity(UUID.randomUUID());
    String content = "test data";
    IdmAttachmentDto attachment = prepareAttachment(content);
    // 
    attachmentManager.saveAttachment(owner, attachment);
    List<IdmAttachmentDto> attachments = attachmentManager.getAttachments(owner, null).getContent();
    // 
    Assert.assertEquals(1, attachments.size());
    IdmAttachmentDto createdAttachment = attachments.get(0);
    Assert.assertEquals(attachment.getName(), createdAttachment.getName());
    Assert.assertEquals(attachment.getMimetype(), createdAttachment.getMimetype());
    Assert.assertEquals(owner.getClass().getCanonicalName(), createdAttachment.getOwnerType());
    Assert.assertEquals(owner.getId(), createdAttachment.getOwnerId());
    Assert.assertNotNull(createdAttachment.getContentId());
    Assert.assertNotNull(createdAttachment.getContentPath());
    Assert.assertNull(createdAttachment.getParent());
    Assert.assertNull(createdAttachment.getNextVersion());
    Assert.assertEquals(Integer.valueOf(1), createdAttachment.getVersionNumber());
    Assert.assertEquals("1.0", createdAttachment.getVersionLabel());
    Assert.assertEquals(AttachableEntity.DEFAULT_ENCODING, createdAttachment.getEncoding());
    Assert.assertEquals(Long.valueOf(content.getBytes(AttachableEntity.DEFAULT_CHARSET).length), createdAttachment.getFilesize());
    Assert.assertEquals(1, attachmentManager.getAttachmentVersions(createdAttachment.getId()).size());
    // 
    // get attachment data
    Assert.assertEquals(content, IOUtils.toString(attachmentManager.getAttachmentData(createdAttachment.getId())));
    // 
    // remove owner
    attachmentManager.deleteAttachments(owner);
    // 
    Assert.assertEquals(0, attachmentManager.getAttachments(owner, null).getTotalElements());
}
Also used : IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) Identifiable(eu.bcvsolutions.idm.core.api.domain.Identifiable) Test(org.junit.Test) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Identifiable

use of eu.bcvsolutions.idm.core.api.domain.Identifiable in project CzechIdMng by bcvsolutions.

the class DefaultAttachmentManagerIntegrationTest method testAttachmentVersionsWithSameName.

@Test
@Transactional
public void testAttachmentVersionsWithSameName() throws IOException {
    Identifiable owner = new TestOwnerEntity(UUID.randomUUID());
    String contentOne = "test data 1";
    IdmAttachmentDto attachmentOne = prepareAttachment(contentOne);
    attachmentOne = attachmentManager.saveAttachment(owner, attachmentOne);
    String contentTwo = "test data 2";
    IdmAttachmentDto attachmentTwo = prepareAttachment(contentTwo);
    attachmentTwo = attachmentManager.saveAttachmentVersion(owner, attachmentTwo, (IdmAttachmentDto) null);
    String contentThree = "test data 3";
    IdmAttachmentDto attachmentThree = prepareAttachment(contentThree);
    attachmentThree = attachmentManager.saveAttachmentVersion(owner, attachmentThree, (IdmAttachmentDto) null);
    // 
    Assert.assertNotNull(attachmentManager.get(attachmentOne));
    Assert.assertNotNull(attachmentManager.get(attachmentTwo));
    Assert.assertNotNull(attachmentManager.get(attachmentThree));
    // 
    // last version only
    List<IdmAttachmentDto> attachments = attachmentManager.getAttachments(owner, null).getContent();
    Assert.assertEquals(1, attachments.size());
    Assert.assertEquals(contentThree, IOUtils.toString(attachmentManager.getAttachmentData(attachments.get(0).getId())));
    // 
    attachments = attachmentManager.getAttachmentVersions(attachmentOne.getId());
    Assert.assertEquals(3, attachments.size());
    // three
    Assert.assertEquals(Integer.valueOf(3), attachments.get(0).getVersionNumber());
    Assert.assertEquals("3.0", attachments.get(0).getVersionLabel());
    Assert.assertNull(attachments.get(0).getNextVersion());
    Assert.assertEquals(attachmentOne.getId(), attachments.get(0).getParent());
    Assert.assertEquals(contentThree, IOUtils.toString(attachmentManager.getAttachmentData(attachments.get(0).getId())));
    // two
    Assert.assertEquals(Integer.valueOf(2), attachments.get(1).getVersionNumber());
    Assert.assertEquals("2.0", attachments.get(1).getVersionLabel());
    Assert.assertEquals(attachmentThree.getId(), attachments.get(1).getNextVersion());
    Assert.assertEquals(attachmentOne.getId(), attachments.get(1).getParent());
    Assert.assertEquals(contentTwo, IOUtils.toString(attachmentManager.getAttachmentData(attachments.get(1).getId())));
    // one
    Assert.assertEquals(Integer.valueOf(1), attachments.get(2).getVersionNumber());
    Assert.assertEquals("1.0", attachments.get(2).getVersionLabel());
    Assert.assertEquals(attachmentTwo.getId(), attachments.get(2).getNextVersion());
    Assert.assertNull(attachments.get(2).getParent());
    Assert.assertEquals(contentOne, IOUtils.toString(attachmentManager.getAttachmentData(attachments.get(2).getId())));
    // 
    attachmentManager.deleteAttachment(attachmentThree);
    // 
    Assert.assertNull(attachmentManager.get(attachmentOne));
    Assert.assertNull(attachmentManager.get(attachmentTwo));
    Assert.assertNull(attachmentManager.get(attachmentThree));
}
Also used : IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) Identifiable(eu.bcvsolutions.idm.core.api.domain.Identifiable) Test(org.junit.Test) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Identifiable

use of eu.bcvsolutions.idm.core.api.domain.Identifiable in project CzechIdMng by bcvsolutions.

the class DefaultAttachmentManagerIntegrationTest method testUpdateAttachment.

@Test
@Transactional
public void testUpdateAttachment() {
    Identifiable owner = new TestOwnerEntity(UUID.randomUUID());
    String contentOne = "test data 1";
    IdmAttachmentDto attachment = prepareAttachment(contentOne);
    attachment = attachmentManager.saveAttachment(owner, attachment);
    // 
    String updatedContent = "update";
    attachment.setInputData(IOUtils.toInputStream(updatedContent));
    attachment.setDescription(updatedContent);
    attachment = attachmentManager.updateAttachment(attachment);
    // 
    List<IdmAttachmentDto> attachments = attachmentManager.getAttachmentVersions(attachment.getId());
    // 
    Assert.assertEquals(1, attachments.size());
    IdmAttachmentDto createdAttachment = attachments.get(0);
    Assert.assertEquals(attachment.getName(), createdAttachment.getName());
    Assert.assertEquals(attachment.getMimetype(), createdAttachment.getMimetype());
    Assert.assertEquals(owner.getClass().getCanonicalName(), createdAttachment.getOwnerType());
    Assert.assertEquals(owner.getId(), createdAttachment.getOwnerId());
    Assert.assertNotNull(createdAttachment.getContentId());
    Assert.assertNotNull(createdAttachment.getContentPath());
    Assert.assertNull(createdAttachment.getParent());
    Assert.assertNull(createdAttachment.getNextVersion());
    Assert.assertEquals(Integer.valueOf(1), createdAttachment.getVersionNumber());
    Assert.assertEquals("1.0", createdAttachment.getVersionLabel());
    Assert.assertEquals(AttachableEntity.DEFAULT_ENCODING, createdAttachment.getEncoding());
    Assert.assertEquals(Long.valueOf(updatedContent.getBytes(AttachableEntity.DEFAULT_CHARSET).length), createdAttachment.getFilesize());
    Assert.assertEquals(updatedContent, createdAttachment.getDescription());
    // 
    attachmentManager.deleteAttachment(attachment);
    // 
    Assert.assertEquals(0, attachmentManager.getAttachmentVersions(attachment.getId()).size());
}
Also used : IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) Identifiable(eu.bcvsolutions.idm.core.api.domain.Identifiable) Test(org.junit.Test) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with Identifiable

use of eu.bcvsolutions.idm.core.api.domain.Identifiable in project CzechIdMng by bcvsolutions.

the class FormValueDeleteBulkActionIntegrationTest method prevalidationBulkActionByIds.

@Test
public void prevalidationBulkActionByIds() {
    Identifiable owner = getHelper().createIdentity((GuardedString) null);
    // 
    // create definition with attribute
    IdmFormAttributeDto attribute = new IdmFormAttributeDto();
    String attributeName = getHelper().createName();
    attribute.setCode(attributeName);
    attribute.setName(attribute.getCode());
    attribute.setMultiple(true);
    attribute.setPersistentType(PersistentType.SHORTTEXT);
    IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class, getHelper().createName(), Lists.newArrayList(attribute));
    attribute = formDefinitionOne.getMappedAttributeByCode(attribute.getCode());
    // 
    // fill values
    formService.saveValues(owner, attribute, Lists.newArrayList(FORM_VALUE_ONE, FORM_VALUE_TWO));
    Map<String, List<IdmFormValueDto>> m = formService.getFormInstance(owner, formDefinitionOne).toValueMap();
    // 
    // check value and persistent type
    Assert.assertEquals(2, m.get(attributeName).size());
    // Running prevalidation which is supposed to return no validation errors
    // because this mapping is not used in any synchronization settings.
    IdmBulkActionDto bulkAction = findBulkAction(IdmFormValue.class, FormValueDeleteBulkAction.NAME);
    Set<UUID> ids = new HashSet<>();
    ids.add(m.get(attributeName).stream().filter(v -> v.getValue().equals(FORM_VALUE_ONE)).findFirst().get().getId());
    ids.add(m.get(attributeName).stream().filter(v -> v.getValue().equals(FORM_VALUE_TWO)).findFirst().get().getId());
    bulkAction.setIdentifiers(ids);
    // 
    ResultModels resultModels = bulkActionManager.prevalidate(bulkAction);
    // 
    Assert.assertTrue(resultModels.getInfos().isEmpty());
    // 
    // create attribute required
    attribute.setRequired(true);
    formService.saveAttribute(attribute);
    // 
    resultModels = bulkActionManager.prevalidate(bulkAction);
    Assert.assertEquals(1, resultModels.getInfos().size());
    Assert.assertEquals(2L, resultModels.getInfos().get(0).getParameters().get("count"));
    Assert.assertEquals(attribute.getCode(), resultModels.getInfos().get(0).getParameters().get("attribute"));
}
Also used : IdmBulkActionDto(eu.bcvsolutions.idm.core.api.bulk.action.dto.IdmBulkActionDto) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) List(java.util.List) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) UUID(java.util.UUID) Identifiable(eu.bcvsolutions.idm.core.api.domain.Identifiable) HashSet(java.util.HashSet) ResultModels(eu.bcvsolutions.idm.core.api.dto.ResultModels) AbstractBulkActionTest(eu.bcvsolutions.idm.test.api.AbstractBulkActionTest) Test(org.junit.Test)

Aggregations

Identifiable (eu.bcvsolutions.idm.core.api.domain.Identifiable)51 Test (org.junit.Test)37 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)32 IdmFormDefinitionDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto)31 IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)27 IdmFormValueDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto)24 DefaultAttachmentManagerIntegrationTest (eu.bcvsolutions.idm.core.ecm.service.impl.DefaultAttachmentManagerIntegrationTest)24 List (java.util.List)19 IdmIdentity (eu.bcvsolutions.idm.core.model.entity.IdmIdentity)17 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)16 UUID (java.util.UUID)16 ArrayList (java.util.ArrayList)14 IdmAttachmentDto (eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto)12 Autowired (org.springframework.beans.factory.annotation.Autowired)11 Transactional (org.springframework.transaction.annotation.Transactional)11 Lists (com.google.common.collect.Lists)10 Serializable (java.io.Serializable)10 HashSet (java.util.HashSet)10 Map (java.util.Map)10 PersistentType (eu.bcvsolutions.idm.core.eav.api.domain.PersistentType)9