use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class FileDownloadHelper method initGeneratedColumn.
/**
* Initializes a table column for downloading files.
*
* @param table table displaying some entity
* @param fileProperty property of the entity which is a reference to {@link FileDescriptor}
*/
public static void initGeneratedColumn(final Table table, final String fileProperty) {
final ComponentsFactory componentsFactory = AppBeans.get(ComponentsFactory.NAME);
final ExportDisplay exportDisplay = AppBeans.get(ExportDisplay.NAME);
table.addGeneratedColumn(fileProperty + ".name", new Table.ColumnGenerator() {
@Override
public Component generateCell(final Entity entity) {
final FileDescriptor fd = entity.getValueEx(fileProperty);
if (fd == null) {
return componentsFactory.createComponent(Label.class);
}
if (PersistenceHelper.isNew(fd)) {
Label label = componentsFactory.createComponent(Label.class);
label.setValue(fd.getName());
return label;
} else {
Button button = componentsFactory.createComponent(Button.class);
button.setStyleName("link");
button.setAction(new AbstractAction("download") {
@Override
public void actionPerform(Component component) {
exportDisplay.show(fd);
}
@Override
public String getCaption() {
return fd.getName();
}
});
return button;
}
}
});
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class MultiUploader method init.
@Override
public void init(Map<String, Object> params) {
super.init(params);
multiUpload.setDropZone(new UploadField.DropZone(this));
getDialogOptions().setHeight(themeConstants.get("cuba.gui.multiupload.height")).setResizable(true);
filesDs.refresh();
okBtn.setEnabled(false);
multiUpload.setCaption(getMessage("upload"));
multiUpload.addQueueUploadCompleteListener(() -> {
okBtn.setEnabled(true);
Map<UUID, String> uploads = multiUpload.getUploadsMap();
for (Map.Entry<UUID, String> upload : uploads.entrySet()) {
FileDescriptor fDesc = fileUploading.getFileDescriptor(upload.getKey(), upload.getValue());
tmpFileDescriptors.put(fDesc, upload.getKey());
filesDs.addItem(fDesc);
}
multiUpload.clearUploads();
});
multiUpload.addFileUploadStartListener(e -> okBtn.setEnabled(false));
removeFileAction.setAutocommit(false);
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class FileLoaderImpl method checkIfFileDescriptorExists.
protected void checkIfFileDescriptorExists(FileDescriptor fd) throws FileStorageException {
try (Transaction tx = persistence.getTransaction()) {
FileDescriptor existingFile = persistence.getEntityManager().find(FileDescriptor.class, fd.getId());
if (existingFile == null || entityStates.isDeleted(existingFile)) {
throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fd.getName());
}
tx.commit();
}
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class DesktopImage method createImageResource.
protected Resource createImageResource(Object propertyValue) {
if (propertyValue == null) {
return null;
}
if (propertyValue instanceof FileDescriptor) {
FileDescriptorResource imageResource = createResource(FileDescriptorResource.class);
imageResource.setFileDescriptor((FileDescriptor) propertyValue);
return imageResource;
}
if (propertyValue instanceof byte[]) {
StreamResource imageResource = createResource(StreamResource.class);
Supplier<InputStream> streamSupplier = () -> new ByteArrayDataProvider((byte[]) propertyValue).provide();
imageResource.setStreamSupplier(streamSupplier);
return imageResource;
}
throw new GuiDevelopmentException("The Image component supports only FileDescriptor and byte[] datasource property value binding", getFrame().getId());
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class SendingMessageBrowser method exportFile.
protected void exportFile(SendingAttachment attachment) {
try {
FileDescriptor fd;
if (emailService.isFileStorageUsed() && attachment.getContentFile() != null && fileLoader.fileExists(attachment.getContentFile())) {
fd = attachment.getContentFile();
} else {
fd = getFileDescriptor(attachment);
}
AppConfig.createExportDisplay(this).show(new FileDataProvider(fd), fd.getName(), ExportFormat.OCTET_STREAM);
} catch (FileStorageException e) {
throw new RuntimeException("File export failed", e);
}
}
Aggregations