Search in sources :

Example 1 with DataAccessRequestUpdatedEvent

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;
}
Also used : DataAccessForm(org.obiba.mica.micaConfig.domain.DataAccessForm) AttachmentRepository(org.obiba.mica.core.repository.AttachmentRepository) DataAccessRequestDeletedEvent(org.obiba.mica.access.event.DataAccessRequestDeletedEvent) Roles(org.obiba.mica.security.Roles) LoggerFactory(org.slf4j.LoggerFactory) StatusChange(org.obiba.mica.access.domain.StatusChange) FileStoreService(org.obiba.mica.file.FileStoreService) IdentifierGenerator(org.obiba.mica.core.support.IdentifierGenerator) Locale(java.util.Locale) Configuration(com.jayway.jsonpath.Configuration) Map(java.util.Map) SchemaFormContentFileService(org.obiba.mica.core.service.SchemaFormContentFileService) Resource(org.springframework.core.io.Resource) DataAccessRequestRepository(org.obiba.mica.access.DataAccessRequestRepository) Option(com.jayway.jsonpath.Option) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) PdfUtils(org.obiba.mica.PdfUtils) NoSuchDataAccessRequestException(org.obiba.mica.access.NoSuchDataAccessRequestException) List(java.util.List) ByteStreams(com.google.common.io.ByteStreams) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) SecurityUtils(org.apache.shiro.SecurityUtils) BeanUtils(org.springframework.beans.BeanUtils) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Inject(javax.inject.Inject) EventBus(com.google.common.eventbus.EventBus) Value(org.springframework.beans.factory.annotation.Value) Strings(com.google.common.base.Strings) DataAccessFormService(org.obiba.mica.micaConfig.service.DataAccessFormService) Service(org.springframework.stereotype.Service) DataAccessRequest(org.obiba.mica.access.domain.DataAccessRequest) Nullable(javax.annotation.Nullable) OutputStream(java.io.OutputStream) MailService(org.obiba.mica.core.service.MailService) Validated(org.springframework.validation.annotation.Validated) Attachment(org.obiba.mica.file.Attachment) Logger(org.slf4j.Logger) InvalidPathException(com.jayway.jsonpath.InvalidPathException) DataAccessRequestUpdatedEvent(org.obiba.mica.access.event.DataAccessRequestUpdatedEvent) DateTime(org.joda.time.DateTime) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) JsonPath(com.jayway.jsonpath.JsonPath) Maps(com.google.common.collect.Maps) DocumentException(com.itextpdf.text.DocumentException) MicaConfigService(org.obiba.mica.micaConfig.service.MicaConfigService) Configuration.defaultConfiguration(com.jayway.jsonpath.Configuration.defaultConfiguration) DataAccessRequest(org.obiba.mica.access.domain.DataAccessRequest) Attachment(org.obiba.mica.file.Attachment) DataAccessRequestUpdatedEvent(org.obiba.mica.access.event.DataAccessRequestUpdatedEvent)

Aggregations

Strings (com.google.common.base.Strings)1 Throwables (com.google.common.base.Throwables)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 EventBus (com.google.common.eventbus.EventBus)1 ByteStreams (com.google.common.io.ByteStreams)1 DocumentException (com.itextpdf.text.DocumentException)1 Configuration (com.jayway.jsonpath.Configuration)1 Configuration.defaultConfiguration (com.jayway.jsonpath.Configuration.defaultConfiguration)1 InvalidPathException (com.jayway.jsonpath.InvalidPathException)1 JsonPath (com.jayway.jsonpath.JsonPath)1 Option (com.jayway.jsonpath.Option)1 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1