Search in sources :

Example 1 with Attachment

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

the class DataAccessRequestService method getTemplate.

private byte[] getTemplate(Locale locale) throws IOException {
    DataAccessForm dataAccessForm = dataAccessFormService.find().get();
    Attachment pdfTemplate = dataAccessForm.getPdfTemplates().get(locale);
    if (pdfTemplate == null) {
        Map<Locale, Attachment> pdfTemplates = dataAccessForm.getPdfTemplates();
        if (!pdfTemplates.isEmpty()) {
            pdfTemplate = dataAccessForm.getPdfTemplates().get(Locale.ENGLISH);
            if (pdfTemplate == null)
                pdfTemplate = dataAccessForm.getPdfTemplates().values().stream().findFirst().get();
        }
    }
    return pdfTemplate != null ? ByteStreams.toByteArray(fileStoreService.getFile(pdfTemplate.getFileReference())) : ByteStreams.toByteArray(defaultTemplateResource.getInputStream());
}
Also used : Locale(java.util.Locale) Attachment(org.obiba.mica.file.Attachment) DataAccessForm(org.obiba.mica.micaConfig.domain.DataAccessForm)

Example 2 with Attachment

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

the class DataAccessRequestService method delete.

/**
 * Delete the {@link DataAccessRequest} matching the identifier.
 *
 * @param id
 * @throws NoSuchDataAccessRequestException
 */
public void delete(@NotNull String id) throws NoSuchDataAccessRequestException {
    DataAccessRequest dataAccessRequest = findById(id);
    List<Attachment> attachments = dataAccessRequest.getAttachments();
    dataAccessRequestRepository.deleteWithReferences(dataAccessRequest);
    schemaFormContentFileService.deleteFiles(dataAccessRequest);
    attachments.forEach(a -> fileStoreService.delete(a.getId()));
    eventBus.post(new DataAccessRequestDeletedEvent(dataAccessRequest));
}
Also used : DataAccessRequest(org.obiba.mica.access.domain.DataAccessRequest) Attachment(org.obiba.mica.file.Attachment) DataAccessRequestDeletedEvent(org.obiba.mica.access.event.DataAccessRequestDeletedEvent)

Example 3 with Attachment

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

the class FileSystemService method mkdirs.

/**
 * Make sure there are {@link AttachmentState}s representing the directory and its parents at path.
 *
 * @param path
 */
public synchronized void mkdirs(String path) {
    if (Strings.isNullOrEmpty(path))
        return;
    if (attachmentStateRepository.countByPathAndName(String.format("^%s$", normalizeRegex(path)), DIR_NAME) == 0) {
        // make sure parent exists
        if (path.lastIndexOf('/') > 0)
            mkdirs(path.substring(0, path.lastIndexOf('/')));
        else if (path.lastIndexOf('/') == 0 && !"/".equals(path))
            mkdirs("/");
        Attachment attachment = new Attachment();
        attachment.setId(new ObjectId().toString());
        attachment.setName(DIR_NAME);
        attachment.setPath(path);
        attachment.setLastModifiedDate(DateTime.now());
        attachment.setLastModifiedBy(getCurrentUsername());
        attachmentRepository.save(attachment);
        AttachmentState state = new AttachmentState();
        state.setName(DIR_NAME);
        state.setPath(path);
        state.setAttachment(attachment);
        state.setLastModifiedDate(DateTime.now());
        state.setLastModifiedBy(getCurrentUsername());
        attachmentStateRepository.save(state);
        eventBus.post(new FileUpdatedEvent(state));
    }
}
Also used : AttachmentState(org.obiba.mica.file.AttachmentState) ObjectId(org.bson.types.ObjectId) FileUpdatedEvent(org.obiba.mica.file.event.FileUpdatedEvent) Attachment(org.obiba.mica.file.Attachment)

Example 4 with Attachment

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

the class FileSystemService method save.

// 
// Persistence
// 
public void save(Attachment attachment) {
    Attachment saved = attachment;
    validateFileName(attachment.getName());
    List<AttachmentState> states = attachmentStateRepository.findByPathAndName(saved.getPath(), saved.getName());
    AttachmentState state = states.isEmpty() ? new AttachmentState() : states.get(0);
    if (attachment.isNew()) {
        attachment.setId(new ObjectId().toString());
    } else {
        saved = attachmentRepository.findOne(attachment.getId());
        if (saved == null || attachment.isJustUploaded()) {
            saved = attachment;
        } else if (state.isPublished() && state.getPublishedAttachment().getId().equals(attachment.getId())) {
            // about to update a published attachment, so make a soft copy of it
            attachment.setFileReference(saved.getFileReference());
            attachment.setCreatedDate(DateTime.now());
            attachment.setId(new ObjectId().toString());
            saved = attachment;
        } else {
            BeanUtils.copyProperties(attachment, saved, "id", "version", "createdBy", "createdDate", "lastModifiedBy", "lastModifiedDate", "fileReference");
        }
        saved.setLastModifiedDate(DateTime.now());
        saved.setLastModifiedBy(getCurrentUsername());
    }
    if (saved.isJustUploaded()) {
        if (attachmentRepository.exists(saved.getId())) {
            // replace already existing attachment
            fileStoreService.delete(saved.getId());
            attachmentRepository.delete(saved.getId());
        }
        fileStoreService.save(saved.getId());
        saved.setJustUploaded(false);
    }
    attachmentRepository.save(saved);
    state.setAttachment(saved);
    state.setLastModifiedDate(DateTime.now());
    state.setLastModifiedBy(getCurrentUsername());
    if (state.isNew()) {
        if (FileUtils.isDirectory(state)) {
            mkdirs(FileUtils.getParentPath(saved.getPath()));
        } else {
            mkdirs(saved.getPath());
        }
    }
    attachmentStateRepository.save(state);
    eventBus.post(new FileUpdatedEvent(state));
}
Also used : AttachmentState(org.obiba.mica.file.AttachmentState) ObjectId(org.bson.types.ObjectId) FileUpdatedEvent(org.obiba.mica.file.event.FileUpdatedEvent) Attachment(org.obiba.mica.file.Attachment)

Example 5 with Attachment

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

the class FileSystemService method reinstate.

/**
 * Reinstate an existing attachment by copying it as a new one, thus generating a new revision
 *
 * @param attachment
 */
public void reinstate(Attachment attachment) {
    Attachment newAttachment = new Attachment();
    BeanUtils.copyProperties(attachment, newAttachment, "id", "version", "createdBy", "createdDate", "lastModifiedBy", "lastModifiedDate");
    newAttachment.setLastModifiedDate(DateTime.now());
    newAttachment.setLastModifiedBy(getCurrentUsername());
    save(newAttachment);
}
Also used : Attachment(org.obiba.mica.file.Attachment)

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