Search in sources :

Example 1 with FileUpdatedEvent

use of org.obiba.mica.file.event.FileUpdatedEvent 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 2 with FileUpdatedEvent

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

use of org.obiba.mica.file.event.FileUpdatedEvent in project mica2 by obiba.

the class FileSystemService method updateStatus.

/**
 * Update {@link RevisionStatus} of the {@link AttachmentState}.
 *
 * @param state
 * @param status
 */
public void updateStatus(AttachmentState state, RevisionStatus status) {
    state.setRevisionStatus(status);
    attachmentStateRepository.save(state);
    eventBus.post(new FileUpdatedEvent(state));
}
Also used : FileUpdatedEvent(org.obiba.mica.file.event.FileUpdatedEvent)

Aggregations

FileUpdatedEvent (org.obiba.mica.file.event.FileUpdatedEvent)3 ObjectId (org.bson.types.ObjectId)2 Attachment (org.obiba.mica.file.Attachment)2 AttachmentState (org.obiba.mica.file.AttachmentState)2