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);
}
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));
}
}
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));
}
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);
}
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);
}
Aggregations