Search in sources :

Example 1 with DBDContentStorage

use of org.jkiss.dbeaver.model.data.DBDContentStorage in project dbeaver by serge-rider.

the class DialogUtils method loadFromFile.

public static boolean loadFromFile(final IValueController controller) {
    if (!(controller.getValue() instanceof DBDContent)) {
        log.error(CoreMessages.model_jdbc_bad_content_value_ + controller.getValue());
        return false;
    }
    Shell shell = UIUtils.getShell(controller.getValueSite());
    final File openFile = openFile(shell);
    if (openFile == null) {
        return false;
    }
    final DBDContent value = (DBDContent) controller.getValue();
    DBeaverUI.runInUI(PlatformUI.getWorkbench().getActiveWorkbenchWindow(), new DBRRunnableWithProgress() {

        @Override
        public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                DBDContentStorage storage;
                if (ContentUtils.isTextContent(value)) {
                    storage = new ExternalContentStorage(DBeaverCore.getInstance(), openFile, GeneralUtils.UTF8_ENCODING);
                } else {
                    storage = new ExternalContentStorage(DBeaverCore.getInstance(), openFile);
                }
                value.updateContents(monitor, storage);
                controller.updateValue(value, true);
            } catch (Exception e) {
                throw new InvocationTargetException(e);
            }
        }
    });
    return true;
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) ExternalContentStorage(org.jkiss.dbeaver.model.impl.ExternalContentStorage) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with DBDContentStorage

use of org.jkiss.dbeaver.model.data.DBDContentStorage in project dbeaver by serge-rider.

the class BinaryPanelEditor method primeEditorValue.

@Override
public void primeEditorValue(@NotNull DBRProgressMonitor monitor, @NotNull HexEditControl control, @NotNull DBDContent value) throws DBException {
    monitor.beginTask("Prime content value", 1);
    try {
        DBDContentStorage data = value.getContents(monitor);
        String charset = null;
        monitor.subTask("Read binary value");
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        if (data != null) {
            try (InputStream contentStream = data.getContentStream()) {
                ContentUtils.copyStreams(contentStream, -1, buffer, monitor);
            }
            charset = data.getCharset();
        } else {
            charset = DBValueFormatting.getDefaultBinaryFileEncoding(value.getDataSource());
        }
        control.setContent(buffer.toByteArray(), charset);
    } catch (IOException e) {
        throw new DBException("Error reading stream value", e);
    } finally {
        monitor.done();
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Example 3 with DBDContentStorage

use of org.jkiss.dbeaver.model.data.DBDContentStorage in project dbeaver by serge-rider.

the class ImagePanelEditor method primeEditorValue.

@Override
public void primeEditorValue(@NotNull DBRProgressMonitor monitor, @NotNull ImageViewer control, @NotNull DBDContent value) throws DBException {
    monitor.subTask("Read image value");
    DBDContentStorage data = value.getContents(monitor);
    if (data != null) {
        try (InputStream contentStream = data.getContentStream()) {
            if (!control.loadImage(contentStream)) {
                throw new DBException("Can't load image: " + control.getLastError().getMessage());
            }
        } catch (IOException e) {
            throw new DBException("Error reading stream value", e);
        }
    } else {
        control.clearImage();
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) InputStream(java.io.InputStream) IOException(java.io.IOException) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Example 4 with DBDContentStorage

use of org.jkiss.dbeaver.model.data.DBDContentStorage in project dbeaver by serge-rider.

the class StreamTransferConsumer method saveContentToFile.

private File saveContentToFile(DBRProgressMonitor monitor, DBDContent content) throws IOException, DBCException {
    DBDContentStorage contents = content.getContents(monitor);
    if (contents == null) {
        log.warn("Null value content");
        return null;
    }
    if (lobDirectory == null) {
        lobDirectory = new File(settings.getOutputFolder(), LOB_DIRECTORY_NAME);
        if (!lobDirectory.exists()) {
            if (!lobDirectory.mkdir()) {
                throw new IOException("Can't create directory for CONTENT files: " + lobDirectory.getAbsolutePath());
            }
        }
    }
    lobCount++;
    Boolean extractImages = (Boolean) processorProperties.get(StreamConsumerSettings.PROP_EXTRACT_IMAGES);
    String fileExt = (extractImages != null && extractImages) ? ".jpg" : ".data";
    //$NON-NLS-1$ //$NON-NLS-2$
    File lobFile = new File(lobDirectory, outputFile.getName() + "-" + lobCount + fileExt);
    try (InputStream cs = contents.getContentStream()) {
        ContentUtils.saveContentToFile(cs, lobFile, monitor);
    }
    return lobFile;
}
Also used : DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Example 5 with DBDContentStorage

use of org.jkiss.dbeaver.model.data.DBDContentStorage in project dbeaver by serge-rider.

the class DataExporterHTML method exportRow.

@Override
public void exportRow(DBCSession session, Object[] row) throws DBException, IOException {
    out.write("<tr" + (rowCount++ % 2 == 0 ? " class=\"odd\"" : "") + ">");
    for (int i = 0; i < row.length; i++) {
        DBDAttributeBinding column = columns.get(i);
        if (DBUtils.isNullValue(row[i])) {
            writeTextCell(null, false);
        } else if (row[i] instanceof DBDContent) {
            // Content
            // Inline textual content and handle binaries in some special way
            DBDContent content = (DBDContent) row[i];
            try {
                DBDContentStorage cs = content.getContents(session.getProgressMonitor());
                out.write("<td>");
                if (cs != null) {
                    if (ContentUtils.isTextContent(content)) {
                        writeCellValue(cs.getContentReader());
                    } else {
                        getSite().writeBinaryData(cs);
                    }
                }
                out.write("</td>");
            } finally {
                content.release();
            }
        } else {
            String stringValue = super.getValueDisplayString(column, row[i]);
            boolean isImage = row[i] instanceof File && stringValue != null && stringValue.endsWith(".jpg");
            if (isImage) {
                writeImageCell((File) row[i]);
            } else {
                writeTextCell(stringValue, false);
            }
        }
    }
    out.write("</tr>\n");
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) File(java.io.File) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Aggregations

DBDContentStorage (org.jkiss.dbeaver.model.data.DBDContentStorage)15 DBDContent (org.jkiss.dbeaver.model.data.DBDContent)7 DBException (org.jkiss.dbeaver.DBException)4 File (java.io.File)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Reader (java.io.Reader)3 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)3 DBDContentCached (org.jkiss.dbeaver.model.data.DBDContentCached)3 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 NotNull (org.jkiss.code.NotNull)2 DBDContentStorageLocal (org.jkiss.dbeaver.model.data.DBDContentStorageLocal)2 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Date (java.util.Date)1 ExternalContentStorage (org.jkiss.dbeaver.model.impl.ExternalContentStorage)1 StringContentStorage (org.jkiss.dbeaver.model.impl.StringContentStorage)1 TemporaryContentStorage (org.jkiss.dbeaver.model.impl.TemporaryContentStorage)1