Search in sources :

Example 16 with Attachment

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

the class FileSystemService method copy.

/**
 * Make a copy of the latest {@link Attachment} (and associated raw file) and optionally delete
 * the {@link AttachmentState} source.
 *
 * @param state
 * @param newPath
 * @param newName
 * @param delete
 */
public void copy(AttachmentState state, String newPath, String newName, boolean delete) {
    if (state.getPath().equals(newPath) && state.getName().equals(newName))
        return;
    if (hasAttachmentState(newPath, newName, false))
        throw new IllegalArgumentException("A file with name '" + newName + "' already exists at path: " + newPath);
    Attachment attachment = state.getAttachment();
    Attachment newAttachment = new Attachment();
    BeanUtils.copyProperties(attachment, newAttachment, "id", "version", "createdBy", "createdDate", "lastModifiedBy", "lastModifiedDate");
    newAttachment.setPath(newPath);
    newAttachment.setName(newName);
    save(newAttachment);
    fileStoreService.save(newAttachment.getFileReference(), fileStoreService.getFile(attachment.getFileReference()));
    if (delete)
        updateStatus(state, RevisionStatus.DELETED);
}
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