use of org.obiba.mica.access.event.DataAccessRequestUpdatedEvent in project mica2 by obiba.
the class DataAccessRequestService method save.
public DataAccessRequest save(@NotNull DataAccessRequest request) {
DataAccessRequest saved = request;
DataAccessRequest.Status from = null;
Iterable<Attachment> attachmentsToDelete = null;
Iterable<Attachment> attachmentsToSave = null;
if (request.isNew()) {
setAndLogStatus(saved, DataAccessRequest.Status.OPENED);
saved.setId(generateId());
attachmentsToSave = saved.getAttachments();
} else {
saved = dataAccessRequestRepository.findOne(request.getId());
if (saved != null) {
attachmentsToDelete = Sets.difference(Sets.newHashSet(saved.getAttachments()), Sets.newHashSet(request.getAttachments()));
attachmentsToSave = Sets.difference(Sets.newHashSet(request.getAttachments()), Sets.newHashSet(saved.getAttachments()));
from = saved.getStatus();
// validate the status
dataAccessRequestUtilService.checkStatusTransition(saved, request.getStatus());
saved.setStatus(request.getStatus());
if (request.hasStatusChangeHistory())
saved.setStatusChangeHistory(request.getStatusChangeHistory());
// merge beans
BeanUtils.copyProperties(request, saved, "id", "version", "createdBy", "createdDate", "lastModifiedBy", "lastModifiedDate", "statusChangeHistory");
} else {
saved = request;
setAndLogStatus(saved, DataAccessRequest.Status.OPENED);
}
}
schemaFormContentFileService.save(saved, dataAccessRequestRepository.findOne(request.getId()), String.format("/data-access-request/%s", saved.getId()));
if (attachmentsToSave != null)
attachmentsToSave.forEach(a -> {
fileStoreService.save(a.getId());
a.setJustUploaded(false);
attachmentRepository.save(a);
});
saved.setLastModifiedDate(DateTime.now());
dataAccessRequestRepository.saveWithReferences(saved);
if (attachmentsToDelete != null)
attachmentsToDelete.forEach(a -> fileStoreService.delete(a.getId()));
eventBus.post(new DataAccessRequestUpdatedEvent(saved));
sendNotificationEmails(saved, from);
return saved;
}
Aggregations