use of org.obiba.mica.file.Attachment in project mica2 by obiba.
the class AttachmentDtos method fromDto.
@NotNull
Attachment fromDto(@NotNull Mica.AttachmentDtoOrBuilder dto) {
Attachment attachment = new Attachment();
attachment.setId(dto.getId());
attachment.setName(dto.getFileName());
if (dto.hasType())
attachment.setType(dto.getType());
if (dto.getDescriptionCount() > 0)
attachment.setDescription(localizedStringDtos.fromDto(dto.getDescriptionList()));
if (dto.hasLang())
attachment.setLang(new Locale(dto.getLang()));
attachment.setSize(dto.getSize());
if (dto.hasMd5())
attachment.setMd5(dto.getMd5());
attachment.setJustUploaded(dto.getJustUploaded());
if (dto.hasTimestamps())
TimestampsDtos.fromDto(dto.getTimestamps(), attachment);
if (dto.getAttributesCount() > 0) {
dto.getAttributesList().forEach(attributeDto -> attachment.addAttribute(attributeDtos.fromDto(attributeDto)));
}
if (dto.hasPath())
attachment.setPath(dto.getPath());
return attachment;
}
use of org.obiba.mica.file.Attachment in project mica2 by obiba.
the class DataAccessRequestService method save.
public DataAccessRequest save(@NotNull DataAccessRequest request) {
DataAccessRequest saved = request;
DataAccessRequest.Status from = null;
Iterable<Attachment> attachmentsToDelete = null;
Iterable<Attachment> attachmentsToSave = null;
if (request.isNew()) {
setAndLogStatus(saved, DataAccessRequest.Status.OPENED);
saved.setId(generateId());
attachmentsToSave = saved.getAttachments();
} else {
saved = dataAccessRequestRepository.findOne(request.getId());
if (saved != null) {
attachmentsToDelete = Sets.difference(Sets.newHashSet(saved.getAttachments()), Sets.newHashSet(request.getAttachments()));
attachmentsToSave = Sets.difference(Sets.newHashSet(request.getAttachments()), Sets.newHashSet(saved.getAttachments()));
from = saved.getStatus();
// validate the status
dataAccessRequestUtilService.checkStatusTransition(saved, request.getStatus());
saved.setStatus(request.getStatus());
if (request.hasStatusChangeHistory())
saved.setStatusChangeHistory(request.getStatusChangeHistory());
// merge beans
BeanUtils.copyProperties(request, saved, "id", "version", "createdBy", "createdDate", "lastModifiedBy", "lastModifiedDate", "statusChangeHistory");
} else {
saved = request;
setAndLogStatus(saved, DataAccessRequest.Status.OPENED);
}
}
schemaFormContentFileService.save(saved, dataAccessRequestRepository.findOne(request.getId()), String.format("/data-access-request/%s", saved.getId()));
if (attachmentsToSave != null)
attachmentsToSave.forEach(a -> {
fileStoreService.save(a.getId());
a.setJustUploaded(false);
attachmentRepository.save(a);
});
saved.setLastModifiedDate(DateTime.now());
dataAccessRequestRepository.saveWithReferences(saved);
if (attachmentsToDelete != null)
attachmentsToDelete.forEach(a -> fileStoreService.delete(a.getId()));
eventBus.post(new DataAccessRequestUpdatedEvent(saved));
sendNotificationEmails(saved, from);
return saved;
}
use of org.obiba.mica.file.Attachment in project mica2 by obiba.
the class DataAccessRequestResource method getAttachment.
@GET
@Timed
@Path("/attachments/{attachmentId}/_download")
public Response getAttachment(@PathParam("id") String id, @PathParam("attachmentId") String attachmentId) throws IOException {
subjectAclService.checkPermission("/data-access-request", "VIEW", id);
DataAccessRequest request = dataAccessRequestService.findById(id);
Optional<Attachment> r = request.getAttachments().stream().filter(a -> a.getId().equals(attachmentId)).findFirst();
if (!r.isPresent())
throw NoSuchEntityException.withId(Attachment.class, attachmentId);
return Response.ok(fileStoreService.getFile(r.get().getFileReference())).header("Content-Disposition", "attachment; filename=\"" + r.get().getName() + "\"").build();
}
use of org.obiba.mica.file.Attachment in project mica2 by obiba.
the class AbstractPublishedStudyResource method getStudyFileResource.
protected FileResource getStudyFileResource(String id, String fileId) {
checkAccess(id);
FileResource fileResource = applicationContext.getBean(FileResource.class);
BaseStudy study = getStudy(id);
if (study.hasLogo() && study.getLogo().getId().equals(fileId)) {
fileResource.setAttachment(study.getLogo());
} else {
List<Attachment> attachments = fileSystemService.findAttachments(String.format("^/%s/%s", getStudyPath(id), id), true).stream().filter(a -> a.getId().equals(fileId)).collect(Collectors.toList());
if (attachments.isEmpty())
throw NoSuchEntityException.withId(Attachment.class, fileId);
fileResource.setAttachment(attachments.get(0));
}
return fileResource;
}
use of org.obiba.mica.file.Attachment in project mica2 by obiba.
the class DraftIndividualStudyResource method file.
@Path("/file/{fileId}")
public FileResource file(@PathParam("fileId") String fileId, @QueryParam("key") String key) {
checkPermission("/draft/individual-study", "VIEW", key);
FileResource fileResource = applicationContext.getBean(FileResource.class);
Study study = individualStudyService.findDraft(id);
if (study.hasLogo() && study.getLogo().getId().equals(fileId)) {
fileResource.setAttachment(study.getLogo());
} else {
List<Attachment> attachments = fileSystemService.findAttachments(String.format("^/individual-study/%s", study.getId()), false).stream().filter(a -> a.getId().equals(fileId)).collect(Collectors.toList());
if (attachments.isEmpty())
throw NoSuchEntityException.withId(Attachment.class, fileId);
fileResource.setAttachment(attachments.get(0));
}
return fileResource;
}
Aggregations