Search in sources :

Example 1 with AttachmentState

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

the class FileSystemService method updateStatus.

/**
 * Update {@link RevisionStatus} of all files at a given path (and children).
 *
 * @param path
 * @param status
 */
public void updateStatus(String path, RevisionStatus status) {
    List<AttachmentState> states = findAttachmentStates(String.format("^%s$", path), false);
    AttachmentState state = states.stream().filter(s -> DIR_NAME.equals(s.getName())).findFirst().orElseThrow(() -> NoSuchEntityException.withPath(AttachmentState.class, path));
    RevisionStatus currentStatus = state.getRevisionStatus();
    states.addAll(findAttachmentStates(String.format("^%s/", path), false));
    states.forEach(s -> updateStatus(s, status));
    filePublicationFlowNotification.send(path, currentStatus, status);
}
Also used : RevisionStatus(org.obiba.mica.core.domain.RevisionStatus) AttachmentState(org.obiba.mica.file.AttachmentState)

Example 2 with AttachmentState

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

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

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

the class FileSystemService method publishDirs.

// 
// Private methods
// 
/**
 * When publishing a file, in order to be able to browse to this file through the parent folders, publish all the
 * parent folders.
 *
 * @param path
 */
private synchronized void publishDirs(String path, Map<String, AttachmentState> statesToProcess) {
    if (Strings.isNullOrEmpty(path))
        return;
    List<AttachmentState> states = attachmentStateRepository.findByPathAndName(path, DIR_NAME);
    if (states.isEmpty())
        return;
    if (path.lastIndexOf('/') > 0)
        publishDirs(path.substring(0, path.lastIndexOf('/')), statesToProcess);
    else if (path.lastIndexOf('/') == 0 && !"/".equals(path))
        publishDirs("/", statesToProcess);
    AttachmentState state = states.get(0);
    if (state.isPublished())
        return;
    statesToProcess.put(state.getFullPath(), state);
}
Also used : AttachmentState(org.obiba.mica.file.AttachmentState)

Example 5 with AttachmentState

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

the class FileSystemService method rename.

/**
 * Rename a specific file at the same path.
 *
 * @param path
 * @param name
 * @param newName
 */
public void rename(String path, String name, String newName) {
    validateFileName(newName);
    AttachmentState state = getAttachmentState(path, name, false);
    move(state, state.getPath(), newName);
}
Also used : AttachmentState(org.obiba.mica.file.AttachmentState)

Aggregations

AttachmentState (org.obiba.mica.file.AttachmentState)12 Attachment (org.obiba.mica.file.Attachment)3 ObjectId (org.bson.types.ObjectId)2 RevisionStatus (org.obiba.mica.core.domain.RevisionStatus)2 FileUpdatedEvent (org.obiba.mica.file.event.FileUpdatedEvent)2 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 Subscribe (com.google.common.eventbus.Subscribe)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 Collectors (java.util.stream.Collectors)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 Nullable (javax.annotation.Nullable)1 Inject (javax.inject.Inject)1 NotNull (javax.validation.constraints.NotNull)1 Pair (org.apache.commons.math3.util.Pair)1