Search in sources :

Example 36 with DBRRunnableWithProgress

use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress in project dbeaver by serge-rider.

the class EditForeignKeyPage method handleRefTableSelect.

private void handleRefTableSelect(ISelection selection) {
    DBNDatabaseNode refTableNode = null;
    if (!selection.isEmpty() && selection instanceof IStructuredSelection && ((IStructuredSelection) selection).size() == 1) {
        final Object element = ((IStructuredSelection) selection).getFirstElement();
        if (element instanceof DBNDatabaseNode && ((DBNDatabaseNode) element).getObject() instanceof DBSTable && ((DBNDatabaseNode) element).getObject().isPersisted()) {
            refTableNode = (DBNDatabaseNode) element;
        }
    }
    if (refTableNode != null) {
        if (refTableNode.getObject() == curRefTable) {
            // The same selection
            return;
        } else {
            curRefTable = (DBSTable) refTableNode.getObject();
        }
    }
    uniqueKeyCombo.removeAll();
    try {
        curConstraints = new ArrayList<>();
        curConstraint = null;
        if (refTableNode != null) {
            final DBSTable refTable = (DBSTable) refTableNode.getObject();
            DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

                @Override
                public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    try {
                        // Cache own table columns
                        ownTable.getAttributes(monitor);
                        // Cache ref table columns
                        refTable.getAttributes(monitor);
                        // Get constraints
                        final Collection<? extends DBSTableConstraint> constraints = refTable.getConstraints(monitor);
                        if (!CommonUtils.isEmpty(constraints)) {
                            for (DBSTableConstraint constraint : constraints) {
                                if (constraint.getConstraintType().isUnique()) {
                                    curConstraints.add(constraint);
                                }
                            }
                        }
                    } catch (DBException e) {
                        throw new InvocationTargetException(e);
                    }
                }
            });
        }
        for (DBSTableConstraint constraint : curConstraints) {
            uniqueKeyCombo.add(constraint.getName());
        }
        uniqueKeyCombo.select(0);
        uniqueKeyCombo.setEnabled(curConstraints.size() > 1);
        if (curConstraints.size() == 1) {
            curConstraint = curConstraints.get(0);
        }
    } catch (InvocationTargetException e) {
        UIUtils.showErrorDialog(getShell(), CoreMessages.dialog_struct_edit_fk_error_load_constraints_title, CoreMessages.dialog_struct_edit_fk_error_load_constraints_message, e.getTargetException());
    } catch (InterruptedException e) {
    // do nothing
    }
    handleUniqueKeySelect();
    updatePageState();
}
Also used : DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) Collection(java.util.Collection) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 37 with DBRRunnableWithProgress

use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress in project dbeaver by serge-rider.

the class DialogUtils method saveToFile.

public static void saveToFile(IValueController controller) {
    if (!(controller.getValue() instanceof DBDContent)) {
        log.error(CoreMessages.model_jdbc_bad_content_value_ + controller.getValue());
        return;
    }
    Shell shell = UIUtils.getShell(controller.getValueSite());
    final File saveFile = selectFileForSave(shell, controller.getValueName());
    if (saveFile == null) {
        return;
    }
    final DBDContent value = (DBDContent) controller.getValue();
    try {
        DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    DBDContentStorage storage = value.getContents(monitor);
                    if (ContentUtils.isTextContent(value)) {
                        try (Reader cr = storage.getContentReader()) {
                            ContentUtils.saveContentToFile(cr, saveFile, GeneralUtils.UTF8_ENCODING, monitor);
                        }
                    } else {
                        try (InputStream cs = storage.getContentStream()) {
                            ContentUtils.saveContentToFile(cs, saveFile, monitor);
                        }
                    }
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        UIUtils.showErrorDialog(shell, CoreMessages.model_jdbc_could_not_save_content, //$NON-NLS-2$
        CoreMessages.model_jdbc_could_not_save_content_to_file_ + saveFile.getAbsolutePath() + "'", e.getTargetException());
    } catch (InterruptedException e) {
    // do nothing
    }
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) InputStream(java.io.InputStream) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) Reader(java.io.Reader) 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 38 with DBRRunnableWithProgress

use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress in project dbeaver by serge-rider.

the class ERDResourceHandler method createDiagram.

public static IFile createDiagram(final EntityDiagram copyFrom, final String title, IFolder folder, DBRProgressMonitor monitor) throws DBException {
    if (folder == null) {
        try {
            folder = getDiagramsFolder(DBeaverCore.getInstance().getProjectRegistry().getActiveProject(), true);
        } catch (CoreException e) {
            throw new DBException("Can't obtain folder for diagram", e);
        }
    }
    if (folder == null) {
        throw new DBException("Can't detect folder for diagram");
    }
    final IFile file = ContentUtils.getUniqueFile(folder, CommonUtils.escapeFileName(title), ERD_EXT);
    try {
        DBRRunnableWithProgress runnable = new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    EntityDiagram newDiagram = copyFrom == null ? new EntityDiagram(null, "<Diagram>") : copyFrom.copy();
                    newDiagram.setName(title);
                    newDiagram.setLayoutManualAllowed(true);
                    newDiagram.setLayoutManualDesired(true);
                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                    DiagramLoader.save(monitor, null, newDiagram, false, buffer);
                    InputStream data = new ByteArrayInputStream(buffer.toByteArray());
                    file.create(data, true, RuntimeUtils.getNestedMonitor(monitor));
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        };
        if (monitor == null) {
            DBeaverUI.runInProgressService(runnable);
        } else {
            runnable.run(monitor);
        }
    } catch (InvocationTargetException e) {
        throw new DBException("Error creating diagram", e.getTargetException());
    } catch (InterruptedException e) {
    // interrupted
    }
    return file;
}
Also used : DBException(org.jkiss.dbeaver.DBException) EntityDiagram(org.jkiss.dbeaver.ext.erd.model.EntityDiagram) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 39 with DBRRunnableWithProgress

use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress in project dbeaver by dbeaver.

the class MockDataSettings method init.

// populate attribute generators properties map
public void init(MockDataExecuteWizard wizard) throws DBException {
    List<DBSDataManipulator> databaseObjects = wizard.getDatabaseObjects();
    // TODO only the first
    DBSDataManipulator dataManipulator = databaseObjects.iterator().next();
    entity = (DBSEntity) dataManipulator;
    attributes = new ArrayList<>();
    try {
        DBeaverUI.run(wizard.getContainer(), true, true, new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    attributes.addAll(DBUtils.getRealAttributes(entity.getAttributes(monitor)));
                    MockGeneratorRegistry generatorRegistry = MockGeneratorRegistry.getInstance();
                    for (DBSAttributeBase attribute : attributes) {
                        AttributeGeneratorProperties generatorProperties = new AttributeGeneratorProperties(attribute);
                        attributeGenerators.put(attribute.getName(), generatorProperties);
                        // ((JDBCColumnKeyType) attribute).isInUniqueKey()
                        List<DBSEntityReferrer> attributeReferrers = DBUtils.getAttributeReferrers(monitor, (DBSEntityAttribute) attribute);
                        if (!CommonUtils.isEmpty(attributeReferrers)) {
                            MockGeneratorDescriptor generator = generatorRegistry.getGenerator(FK_GENERATOR_ID);
                            putGenerator(generatorProperties, generator);
                        } else {
                            List<MockGeneratorDescriptor> generators = generatorRegistry.findAllGenerators(dataManipulator.getDataSource(), attribute);
                            for (MockGeneratorDescriptor generator : generators) {
                                putGenerator(generatorProperties, generator);
                            }
                        }
                    }
                } catch (DBException e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        DBUserInterface.getInstance().showError("Transfer init failed", "Can't start data transfer", e.getTargetException());
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) MockGeneratorDescriptor(org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor) MockGeneratorRegistry(org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorRegistry) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 40 with DBRRunnableWithProgress

use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress in project dbeaver by dbeaver.

the class CompileHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final IWorkbenchPart activePart = HandlerUtil.getActiveEditor(event);
    final List<OracleSourceObject> objects = getSelectedObjects(event);
    if (!objects.isEmpty()) {
        if (activePart instanceof EntityEditor) {
            // Save editor before compile
            // USe null monitor as entity editor has its own detached job for save
            EntityEditor entityEditor = (EntityEditor) activePart;
            if (entityEditor.isDirty()) {
                NullProgressMonitor monitor = new NullProgressMonitor();
                entityEditor.doSave(monitor);
                if (monitor.isCanceled()) {
                    // Save failed - doesn't make sense to compile
                    return null;
                }
            }
        }
        final Shell activeShell = HandlerUtil.getActiveShell(event);
        if (objects.size() == 1) {
            final OracleSourceObject unit = objects.get(0);
            DBCSourceHost sourceHost = null;
            if (activePart != null) {
                sourceHost = RuntimeUtils.getObjectAdapter(activePart, DBCSourceHost.class);
                if (sourceHost == null) {
                    sourceHost = activePart.getAdapter(DBCSourceHost.class);
                }
            }
            if (sourceHost != null && sourceHost.getSourceObject() != unit) {
                sourceHost = null;
            }
            final DBCCompileLog compileLog = sourceHost == null ? new DBCCompileLogBase() : sourceHost.getCompileLog();
            compileLog.clearLog();
            Throwable error = null;
            try {
                DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

                    @Override
                    public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                        try {
                            compileUnit(monitor, compileLog, unit);
                        } catch (DBCException e) {
                            throw new InvocationTargetException(e);
                        }
                    }
                });
                if (compileLog.getError() != null) {
                    error = compileLog.getError();
                }
            } catch (InvocationTargetException e) {
                error = e.getTargetException();
            } catch (InterruptedException e) {
                return null;
            }
            if (error != null) {
                DBUserInterface.getInstance().showError("Unexpected compilation error", null, error);
            } else if (!CommonUtils.isEmpty(compileLog.getErrorStack())) {
                // Show compile errors
                int line = -1, position = -1;
                StringBuilder fullMessage = new StringBuilder();
                for (DBCCompileError oce : compileLog.getErrorStack()) {
                    fullMessage.append(oce.toString()).append(GeneralUtils.getDefaultLineSeparator());
                    if (line < 0) {
                        line = oce.getLine();
                        position = oce.getPosition();
                    }
                }
                // If compiled object is currently open in editor - try to position on error line
                if (sourceHost != null && sourceHost.getSourceObject() == unit && line > 0 && position > 0) {
                    sourceHost.positionSource(line, position);
                    activePart.getSite().getPage().activate(activePart);
                }
                String errorTitle = unit.getName() + " compilation failed";
                if (sourceHost != null) {
                    sourceHost.setCompileInfo(errorTitle, true);
                    sourceHost.showCompileLog();
                }
                DBUserInterface.getInstance().showError(errorTitle, fullMessage.toString());
            } else {
                String message = unit.getName() + " compiled successfully";
                if (sourceHost != null) {
                    sourceHost.setCompileInfo(message, true);
                }
                UIUtils.showMessageBox(activeShell, "Done", message, SWT.ICON_INFORMATION);
            }
        } else {
            OracleCompilerDialog dialog = new OracleCompilerDialog(activeShell, objects);
            dialog.open();
        }
    }
    return null;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) DBCSourceHost(org.jkiss.dbeaver.model.exec.compile.DBCSourceHost) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBCCompileError(org.jkiss.dbeaver.model.exec.compile.DBCCompileError) InvocationTargetException(java.lang.reflect.InvocationTargetException) OracleCompilerDialog(org.jkiss.dbeaver.ext.oracle.views.OracleCompilerDialog) DBCCompileLogBase(org.jkiss.dbeaver.model.exec.compile.DBCCompileLogBase) Shell(org.eclipse.swt.widgets.Shell) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) DBCCompileLog(org.jkiss.dbeaver.model.exec.compile.DBCCompileLog) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) EntityEditor(org.jkiss.dbeaver.ui.editors.entity.EntityEditor) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) OracleSourceObject(org.jkiss.dbeaver.ext.oracle.model.source.OracleSourceObject)

Aggregations

DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)64 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)60 InvocationTargetException (java.lang.reflect.InvocationTargetException)58 DBException (org.jkiss.dbeaver.DBException)36 ArrayList (java.util.ArrayList)11 IFile (org.eclipse.core.resources.IFile)11 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)11 CoreException (org.eclipse.core.runtime.CoreException)10 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)10 List (java.util.List)9 NotNull (org.jkiss.code.NotNull)9 Nullable (org.jkiss.code.Nullable)9 CommonUtils (org.jkiss.utils.CommonUtils)8 File (java.io.File)7 Collection (java.util.Collection)7 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)7 Log (org.jkiss.dbeaver.Log)6 InputStream (java.io.InputStream)5 IResource (org.eclipse.core.resources.IResource)5 SWT (org.eclipse.swt.SWT)5