Search in sources :

Example 16 with FileDescriptor

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;
            }
        }
    });
}
Also used : ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) Entity(com.haulmont.cuba.core.entity.Entity) ExportDisplay(com.haulmont.cuba.gui.export.ExportDisplay) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 17 with FileDescriptor

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);
}
Also used : UploadField(com.haulmont.cuba.gui.components.UploadField) FileMultiUploadField(com.haulmont.cuba.gui.components.FileMultiUploadField) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 18 with FileDescriptor

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();
    }
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 19 with FileDescriptor

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());
}
Also used : ByteArrayDataProvider(com.haulmont.cuba.gui.export.ByteArrayDataProvider) InputStream(java.io.InputStream) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 20 with FileDescriptor

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);
    }
}
Also used : FileDataProvider(com.haulmont.cuba.gui.export.FileDataProvider) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Aggregations

FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)47 FileStorageException (com.haulmont.cuba.core.global.FileStorageException)12 UUID (java.util.UUID)9 EntityManager (com.haulmont.cuba.core.EntityManager)5 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)5 UserSession (com.haulmont.cuba.security.global.UserSession)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 Transaction (com.haulmont.cuba.core.Transaction)3 ExportDisplay (com.haulmont.cuba.gui.export.ExportDisplay)3 FileDataProvider (com.haulmont.cuba.gui.export.FileDataProvider)3 RestAPIException (com.haulmont.restapi.exception.RestAPIException)3 File (java.io.File)3 FileStorageAPI (com.haulmont.cuba.core.app.FileStorageAPI)2 FileStorageService (com.haulmont.cuba.core.app.FileStorageService)2 LoadContext (com.haulmont.cuba.core.global.LoadContext)2 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)2 TaskLifeCycle (com.haulmont.cuba.gui.executors.TaskLifeCycle)2 ByteArrayDataProvider (com.haulmont.cuba.gui.export.ByteArrayDataProvider)2