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