Search in sources :

Example 11 with DataAccessRequest

use of org.obiba.mica.access.domain.DataAccessRequest 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)

Example 12 with DataAccessRequest

use of org.obiba.mica.access.domain.DataAccessRequest in project mica2 by obiba.

the class DataAccessRequestService method updateStatus.

/**
 * Update the status of the {@link DataAccessRequest} matching the identifier.
 *
 * @param id
 * @param status
 * @throws NoSuchDataAccessRequestException
 */
public DataAccessRequest updateStatus(@NotNull String id, @NotNull DataAccessRequest.Status status) throws NoSuchDataAccessRequestException {
    DataAccessRequest request = findById(id);
    setAndLogStatus(request, status);
    save(request);
    return request;
}
Also used : DataAccessRequest(org.obiba.mica.access.domain.DataAccessRequest)

Example 13 with DataAccessRequest

use of org.obiba.mica.access.domain.DataAccessRequest in project mica2 by obiba.

the class DataAccessRequestService method saveAttachments.

public DataAccessRequest saveAttachments(@NotNull DataAccessRequest request) {
    DataAccessRequest saved = findById(request.getId());
    saved.setAttachments(request.getAttachments());
    save(saved);
    sendAttachmentsUpdatedNotificationEmail(request);
    return saved;
}
Also used : DataAccessRequest(org.obiba.mica.access.domain.DataAccessRequest)

Example 14 with DataAccessRequest

use of org.obiba.mica.access.domain.DataAccessRequest in project mica2 by obiba.

the class DataAccessRequestDtos method fromDto.

@NotNull
public DataAccessRequest fromDto(@NotNull Mica.DataAccessRequestDto dto) {
    DataAccessRequest.Builder builder = DataAccessRequest.newBuilder();
    builder.applicant(dto.getApplicant()).status(dto.getStatus());
    if (dto.hasContent())
        builder.content(dto.getContent());
    DataAccessRequest request = builder.build();
    if (dto.hasId())
        request.setId(dto.getId());
    if (dto.getAttachmentsCount() > 0) {
        request.setAttachments(dto.getAttachmentsList().stream().map(attachmentDtos::fromDto).collect(Collectors.toList()));
    }
    TimestampsDtos.fromDto(dto.getTimestamps(), request);
    return request;
}
Also used : DataAccessRequest(org.obiba.mica.access.domain.DataAccessRequest) NotNull(javax.validation.constraints.NotNull)

Example 15 with DataAccessRequest

use of org.obiba.mica.access.domain.DataAccessRequest in project mica2 by obiba.

the class ProjectDtos method asDto.

@NotNull
public Mica.ProjectDto asDto(@NotNull Project project, boolean asDraft) {
    Mica.ProjectDto.Builder builder = Mica.ProjectDto.newBuilder().addAllTitle(// 
    localizedStringDtos.asDto(project.getTitle())).addAllSummary(localizedStringDtos.asDto(project.getSummary()));
    if (project.hasModel())
        builder.setContent(JSONUtils.toJSON(project.getModel()));
    if (project.hasDataAccessRequestId()) {
        try {
            DataAccessRequest request = dataAccessRequestService.findById(project.getDataAccessRequestId());
            Mica.DataAccessRequestSummaryDto.Builder darBuilder = Mica.DataAccessRequestSummaryDto.newBuilder();
            darBuilder.setId(project.getDataAccessRequestId());
            darBuilder.setStatus(request.getStatus().name());
            darBuilder.setApplicant(request.getApplicant());
            darBuilder.setViewable(subjectAclService.isPermitted("/data-access-request", "VIEW", request.getId()));
            builder.setRequest(darBuilder);
        } catch (NoSuchElementException e) {
        // ignore
        }
    }
    if (!project.isNew())
        builder.setId(project.getId());
    Mica.PermissionsDto permissionsDto = permissionsDtos.asDto(project);
    if (asDraft) {
        ProjectState projectState = projectService.getEntityState(project.getId());
        // 
        builder.setTimestamps(TimestampsDtos.asDto(project)).setPublished(// 
        projectState.isPublished()).setExtension(Mica.EntityStateDto.projectState, entityStateDtos.asDto(projectState).setPermissions(permissionsDto).build());
    }
    builder.setPermissions(permissionsDto);
    builder.setTimestamps(TimestampsDtos.asDto(project));
    return builder.build();
}
Also used : DataAccessRequest(org.obiba.mica.access.domain.DataAccessRequest) ProjectState(org.obiba.mica.project.domain.ProjectState) NoSuchElementException(java.util.NoSuchElementException) NotNull(javax.validation.constraints.NotNull)

Aggregations

DataAccessRequest (org.obiba.mica.access.domain.DataAccessRequest)25 Timed (com.codahale.metrics.annotation.Timed)6 DateTime (org.joda.time.DateTime)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 Map (java.util.Map)3 NotNull (javax.validation.constraints.NotNull)3 BadRequestException (javax.ws.rs.BadRequestException)3 Attachment (org.obiba.mica.file.Attachment)3 DataAccessForm (org.obiba.mica.micaConfig.domain.DataAccessForm)3 Strings (com.google.common.base.Strings)2 EventBus (com.google.common.eventbus.EventBus)2 DocumentException (com.itextpdf.text.DocumentException)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 ForbiddenException (javax.ws.rs.ForbiddenException)2 PUT (javax.ws.rs.PUT)2 SecurityUtils (org.apache.shiro.SecurityUtils)2 NoSuchDataAccessRequestException (org.obiba.mica.access.NoSuchDataAccessRequestException)2