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;
}
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);
}
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);
}
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);
}
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());
}
Aggregations