Search in sources :

Example 11 with Attachment

use of org.obiba.mica.file.Attachment in project mica2 by obiba.

the class AttachmentDtos method fromDto.

@NotNull
Attachment fromDto(@NotNull Mica.AttachmentDtoOrBuilder dto) {
    Attachment attachment = new Attachment();
    attachment.setId(dto.getId());
    attachment.setName(dto.getFileName());
    if (dto.hasType())
        attachment.setType(dto.getType());
    if (dto.getDescriptionCount() > 0)
        attachment.setDescription(localizedStringDtos.fromDto(dto.getDescriptionList()));
    if (dto.hasLang())
        attachment.setLang(new Locale(dto.getLang()));
    attachment.setSize(dto.getSize());
    if (dto.hasMd5())
        attachment.setMd5(dto.getMd5());
    attachment.setJustUploaded(dto.getJustUploaded());
    if (dto.hasTimestamps())
        TimestampsDtos.fromDto(dto.getTimestamps(), attachment);
    if (dto.getAttributesCount() > 0) {
        dto.getAttributesList().forEach(attributeDto -> attachment.addAttribute(attributeDtos.fromDto(attributeDto)));
    }
    if (dto.hasPath())
        attachment.setPath(dto.getPath());
    return attachment;
}
Also used : Locale(java.util.Locale) Attachment(org.obiba.mica.file.Attachment) NotNull(javax.validation.constraints.NotNull)

Example 12 with Attachment

use of org.obiba.mica.file.Attachment 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 13 with Attachment

use of org.obiba.mica.file.Attachment in project mica2 by obiba.

the class DataAccessRequestResource method getAttachment.

@GET
@Timed
@Path("/attachments/{attachmentId}/_download")
public Response getAttachment(@PathParam("id") String id, @PathParam("attachmentId") String attachmentId) throws IOException {
    subjectAclService.checkPermission("/data-access-request", "VIEW", id);
    DataAccessRequest request = dataAccessRequestService.findById(id);
    Optional<Attachment> r = request.getAttachments().stream().filter(a -> a.getId().equals(attachmentId)).findFirst();
    if (!r.isPresent())
        throw NoSuchEntityException.withId(Attachment.class, attachmentId);
    return Response.ok(fileStoreService.getFile(r.get().getFileReference())).header("Content-Disposition", "attachment; filename=\"" + r.get().getName() + "\"").build();
}
Also used : Comment(org.obiba.mica.core.domain.Comment) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Roles(org.obiba.mica.security.Roles) Path(javax.ws.rs.Path) DataAccessRequestService(org.obiba.mica.access.service.DataAccessRequestService) FileStoreService(org.obiba.mica.file.FileStoreService) Inject(javax.inject.Inject) EventBus(com.google.common.eventbus.EventBus) Strings(com.google.common.base.Strings) ResourceDeletedEvent(org.obiba.mica.security.event.ResourceDeletedEvent) QueryParam(javax.ws.rs.QueryParam) LanguageTag(sun.util.locale.LanguageTag) Map(java.util.Map) JSONUtils(org.obiba.mica.JSONUtils) Mica(org.obiba.mica.web.model.Mica) DataAccessRequest(org.obiba.mica.access.domain.DataAccessRequest) BadRequestException(javax.ws.rs.BadRequestException) DataAccessRequestCommentMailNotification(org.obiba.mica.access.notification.DataAccessRequestCommentMailNotification) Dtos(org.obiba.mica.web.model.Dtos) CommentsService(org.obiba.mica.core.service.CommentsService) DELETE(javax.ws.rs.DELETE) POST(javax.ws.rs.POST) Attachment(org.obiba.mica.file.Attachment) SubjectAclService(org.obiba.mica.security.service.SubjectAclService) ForbiddenException(javax.ws.rs.ForbiddenException) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) NoSuchEntityException(org.obiba.mica.NoSuchEntityException) NoSuchDataAccessRequestException(org.obiba.mica.access.NoSuchDataAccessRequestException) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) Component(org.springframework.stereotype.Component) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) PUT(javax.ws.rs.PUT) SecurityUtils(org.apache.shiro.SecurityUtils) DataAccessRequest(org.obiba.mica.access.domain.DataAccessRequest) Attachment(org.obiba.mica.file.Attachment) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 14 with Attachment

use of org.obiba.mica.file.Attachment in project mica2 by obiba.

the class AbstractPublishedStudyResource method getStudyFileResource.

protected FileResource getStudyFileResource(String id, String fileId) {
    checkAccess(id);
    FileResource fileResource = applicationContext.getBean(FileResource.class);
    BaseStudy study = getStudy(id);
    if (study.hasLogo() && study.getLogo().getId().equals(fileId)) {
        fileResource.setAttachment(study.getLogo());
    } else {
        List<Attachment> attachments = fileSystemService.findAttachments(String.format("^/%s/%s", getStudyPath(id), id), true).stream().filter(a -> a.getId().equals(fileId)).collect(Collectors.toList());
        if (attachments.isEmpty())
            throw NoSuchEntityException.withId(Attachment.class, fileId);
        fileResource.setAttachment(attachments.get(0));
    }
    return fileResource;
}
Also used : FileResource(org.obiba.mica.file.rest.FileResource) Attachment(org.obiba.mica.file.Attachment) SubjectAclService(org.obiba.mica.security.service.SubjectAclService) Logger(org.slf4j.Logger) BaseStudy(org.obiba.mica.study.domain.BaseStudy) LoggerFactory(org.slf4j.LoggerFactory) FileSystemService(org.obiba.mica.file.service.FileSystemService) StudyService(org.obiba.mica.study.service.StudyService) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) NoSuchEntityException(org.obiba.mica.NoSuchEntityException) NoSuchStudyException(org.obiba.mica.study.NoSuchStudyException) PublishedStudyService(org.obiba.mica.study.service.PublishedStudyService) Inject(javax.inject.Inject) List(java.util.List) ModelAwareTranslator(org.obiba.mica.core.ModelAwareTranslator) Dtos(org.obiba.mica.web.model.Dtos) FileResource(org.obiba.mica.file.rest.FileResource) Attachment(org.obiba.mica.file.Attachment) BaseStudy(org.obiba.mica.study.domain.BaseStudy)

Example 15 with Attachment

use of org.obiba.mica.file.Attachment in project mica2 by obiba.

the class DraftIndividualStudyResource method file.

@Path("/file/{fileId}")
public FileResource file(@PathParam("fileId") String fileId, @QueryParam("key") String key) {
    checkPermission("/draft/individual-study", "VIEW", key);
    FileResource fileResource = applicationContext.getBean(FileResource.class);
    Study study = individualStudyService.findDraft(id);
    if (study.hasLogo() && study.getLogo().getId().equals(fileId)) {
        fileResource.setAttachment(study.getLogo());
    } else {
        List<Attachment> attachments = fileSystemService.findAttachments(String.format("^/individual-study/%s", study.getId()), false).stream().filter(a -> a.getId().equals(fileId)).collect(Collectors.toList());
        if (attachments.isEmpty())
            throw NoSuchEntityException.withId(Attachment.class, fileId);
        fileResource.setAttachment(attachments.get(0));
    }
    return fileResource;
}
Also used : RevisionStatus(org.obiba.mica.core.domain.RevisionStatus) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ConstraintException(org.obiba.mica.study.ConstraintException) Path(javax.ws.rs.Path) HashMap(java.util.HashMap) Scope(org.springframework.context.annotation.Scope) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) AbstractGitPersistableResource(org.obiba.mica.AbstractGitPersistableResource) QueryParam(javax.ws.rs.QueryParam) PublishCascadingScope(org.obiba.mica.core.domain.PublishCascadingScope) SubjectAclResource(org.obiba.mica.security.rest.SubjectAclResource) Map(java.util.Map) Mica(org.obiba.mica.web.model.Mica) DefaultValue(javax.ws.rs.DefaultValue) StudyState(org.obiba.mica.study.domain.StudyState) Dtos(org.obiba.mica.web.model.Dtos) Nullable(javax.annotation.Nullable) DELETE(javax.ws.rs.DELETE) FileResource(org.obiba.mica.file.rest.FileResource) Attachment(org.obiba.mica.file.Attachment) FileSystemService(org.obiba.mica.file.service.FileSystemService) IOException(java.io.IOException) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) Maps(com.google.common.collect.Maps) NoSuchEntityException(org.obiba.mica.NoSuchEntityException) IndividualStudyService(org.obiba.mica.study.service.IndividualStudyService) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) Component(org.springframework.stereotype.Component) Study(org.obiba.mica.study.domain.Study) Response(javax.ws.rs.core.Response) PUT(javax.ws.rs.PUT) AbstractGitPersistableService(org.obiba.mica.core.service.AbstractGitPersistableService) Study(org.obiba.mica.study.domain.Study) FileResource(org.obiba.mica.file.rest.FileResource) Attachment(org.obiba.mica.file.Attachment) Path(javax.ws.rs.Path)

Aggregations

Attachment (org.obiba.mica.file.Attachment)16 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 Inject (javax.inject.Inject)7 Dtos (org.obiba.mica.web.model.Dtos)6 IOException (java.io.IOException)5 Map (java.util.Map)5 NotNull (javax.validation.constraints.NotNull)5 NoSuchEntityException (org.obiba.mica.NoSuchEntityException)5 FileSystemService (org.obiba.mica.file.service.FileSystemService)5 Mica (org.obiba.mica.web.model.Mica)5 Strings (com.google.common.base.Strings)4 Maps (com.google.common.collect.Maps)4 Nullable (javax.annotation.Nullable)4 Timed (com.codahale.metrics.annotation.Timed)3 Lists (com.google.common.collect.Lists)3 ObjectId (org.bson.types.ObjectId)3 AttachmentState (org.obiba.mica.file.AttachmentState)3 Throwables (com.google.common.base.Throwables)2 Sets (com.google.common.collect.Sets)2