use of eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto 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;
}
use of eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto 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);
}
use of eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto in project CzechIdMng by bcvsolutions.
the class DefaultSysSystemServiceTest method testFormAttributes.
/**
* Test add and delete extended attributes to owner
*/
@Test
public void testFormAttributes() {
// create owner
SysSystemDto system = new SysSystemDto();
system.setName(SYSTEM_NAME_ONE);
system = systemService.save(system);
SysSystemDto systemOne = systemService.getByCode(SYSTEM_NAME_ONE);
assertEquals(SYSTEM_NAME_ONE, systemOne.getName());
//
// create definition one
IdmFormDefinitionDto formDefinitionOne = new IdmFormDefinitionDto();
formDefinitionOne.setType(SysSystem.class.getCanonicalName());
formDefinitionOne.setCode("v1");
formDefinitionOne = formDefinitionService.save(formDefinitionOne);
IdmFormAttributeDto attributeDefinitionOne = new IdmFormAttributeDto();
attributeDefinitionOne.setFormDefinition(formDefinitionOne.getId());
attributeDefinitionOne.setCode("name_" + System.currentTimeMillis());
attributeDefinitionOne.setName(attributeDefinitionOne.getCode());
attributeDefinitionOne.setPersistentType(PersistentType.TEXT);
attributeDefinitionOne = formAttributeService.save(attributeDefinitionOne);
formDefinitionOne = formDefinitionService.get(formDefinitionOne.getId());
//
// create definition two
IdmFormDefinitionDto formDefinitionTwo = new IdmFormDefinitionDto();
formDefinitionTwo.setType(SysSystem.class.getCanonicalName());
formDefinitionTwo.setCode("v2");
formDefinitionTwo = formDefinitionService.save(formDefinitionTwo);
IdmFormAttributeDto attributeDefinitionTwo = new IdmFormAttributeDto();
attributeDefinitionTwo.setFormDefinition(formDefinitionTwo.getId());
attributeDefinitionTwo.setCode("name_" + System.currentTimeMillis());
attributeDefinitionTwo.setName(attributeDefinitionTwo.getCode());
attributeDefinitionTwo.setPersistentType(PersistentType.TEXT);
attributeDefinitionTwo = formAttributeService.save(attributeDefinitionTwo);
formDefinitionTwo = formDefinitionService.get(formDefinitionTwo.getId());
//
IdmFormValueDto value1 = new IdmFormValueDto(attributeDefinitionOne);
value1.setValue("test1");
IdmFormValueDto value2 = new IdmFormValueDto(attributeDefinitionTwo);
value2.setValue("test2");
formService.saveValues(system, formDefinitionOne, Lists.newArrayList(value1));
formService.saveValues(system, formDefinitionTwo, Lists.newArrayList(value2));
assertEquals("test1", formService.getValues(system, formDefinitionOne).get(0).getStringValue());
assertEquals("test2", formService.getValues(system, formDefinitionTwo).get(0).getStringValue());
assertEquals("test2", formService.getValues(system, formDefinitionTwo, attributeDefinitionTwo.getName()).get(0).getValue());
//
// create second owner
SysSystemDto systemTwo = new SysSystemDto();
systemTwo.setName(SYSTEM_NAME_TWO);
systemTwo = systemService.save(systemTwo);
assertEquals(0, formService.getValues(systemTwo, formDefinitionOne).size());
assertEquals(0, formService.getValues(systemTwo, formDefinitionTwo).size());
assertEquals(1, formService.getValues(system, formDefinitionOne).size());
assertEquals(1, formService.getValues(system, formDefinitionTwo).size());
systemService.delete(systemTwo);
assertEquals(1, formService.getValues(system, formDefinitionOne).size());
assertEquals(1, formService.getValues(system, formDefinitionTwo).size());
formService.deleteValues(system, formDefinitionOne);
assertEquals(0, formService.getValues(system, formDefinitionOne).size());
assertEquals("test2", formService.getValues(system, formDefinitionTwo).get(0).getStringValue());
systemService.delete(system);
}
use of eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto in project CzechIdMng by bcvsolutions.
the class IdentityReportExecutorIntegrationTest method testDisabledIdentity.
@Test
@Transactional
public void testDisabledIdentity() throws IOException {
// prepare test identities
IdmIdentityDto identityOne = helper.createIdentity();
IdmIdentityDto identityDisabled = helper.createIdentity();
identityService.disable(identityDisabled.getId());
//
// prepare report filter
RptReportDto report = new RptReportDto(UUID.randomUUID());
report.setExecutorName(reportExecutor.getName());
IdmFormDto filter = new IdmFormDto();
IdmFormDefinitionDto definition = reportExecutor.getFormDefinition();
IdmFormValueDto disabled = new IdmFormValueDto(definition.getMappedAttributeByCode(IdmIdentityFilter.PARAMETER_DISABLED));
disabled.setValue(false);
filter.getValues().add(disabled);
filter.setFormDefinition(definition.getId());
report.setFilter(filter);
//
// generate report
report = reportExecutor.generate(report);
Assert.assertNotNull(report.getData());
List<IdmIdentityDto> identityRoles = mapper.readValue(attachmentManager.getAttachmentData(report.getData()), new TypeReference<List<IdmIdentityDto>>() {
});
//
// test
Assert.assertTrue(identityRoles.stream().anyMatch(i -> i.equals(identityOne)));
Assert.assertFalse(identityRoles.stream().anyMatch(i -> i.equals(identityDisabled)));
//
attachmentManager.deleteAttachments(report);
}
use of eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto in project CzechIdMng by bcvsolutions.
the class DefaultCommonFormServiceIntegrationTest method createDefinition.
private IdmFormAttributeDto createDefinition() {
IdmFormAttributeDto attributeDefinitionOne = new IdmFormAttributeDto();
attributeDefinitionOne.setCode(helper.createName());
attributeDefinitionOne.setName(attributeDefinitionOne.getCode());
attributeDefinitionOne.setPersistentType(PersistentType.TEXT);
IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class.getCanonicalName(), helper.createName(), Lists.newArrayList(attributeDefinitionOne));
return formDefinitionOne.getMappedAttributeByCode(attributeDefinitionOne.getCode());
}
Aggregations