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