Search in sources :

Example 1 with DatabaseTransferProducer

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer in project dbeaver by serge-rider.

the class DataImportHandler method chooseProducer.

@Override
protected IDataTransferProducer chooseProducer(ExecutionEvent event, IDataTransferConsumer consumer) {
    final DBNModel navigatorModel = DBeaverCore.getInstance().getNavigatorModel();
    final DBNNode rootNode = DBeaverCore.getInstance().getLiveProjects().size() == 1 ? navigatorModel.getRoot().getProject(DBeaverCore.getInstance().getProjectRegistry().getActiveProject()) : navigatorModel.getRoot();
    DBNNode node = BrowseObjectDialog.selectObject(HandlerUtil.getActiveShell(event), "Select source container for '" + consumer.getTargetName() + "'", rootNode, null, new Class[] { DBSObjectContainer.class, DBSDataContainer.class }, new Class[] { DBSDataContainer.class });
    if (node instanceof DBNDatabaseNode) {
        DBSObject object = ((DBNDatabaseNode) node).getObject();
        if (object instanceof DBSDataContainer) {
            return new DatabaseTransferProducer((DBSDataContainer) object);
        }
    }
    return null;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DatabaseTransferProducer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) DBNModel(org.jkiss.dbeaver.model.navigator.DBNModel)

Example 2 with DatabaseTransferProducer

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer in project dbeaver by serge-rider.

the class ResultSetViewer method fillContextMenu.

@Override
public void fillContextMenu(@NotNull IMenuManager manager, @Nullable final DBDAttributeBinding attr, @Nullable final ResultSetRow row) {
    final DBPDataSource dataSource = getDataSource();
    // Custom oldValue items
    final ResultSetValueController valueController;
    final Object value;
    if (attr != null && row != null) {
        valueController = new ResultSetValueController(this, attr, row, IValueController.EditType.NONE, null);
        value = valueController.getValue();
    } else {
        valueController = null;
        value = null;
    }
    {
        {
            // Standard items
            manager.add(ActionUtils.makeCommandContribution(site, IWorkbenchCommandConstants.EDIT_CUT));
            manager.add(ActionUtils.makeCommandContribution(site, IWorkbenchCommandConstants.EDIT_COPY));
            MenuManager extCopyMenu = new MenuManager(ActionUtils.findCommandName(ResultSetCopySpecialHandler.CMD_COPY_SPECIAL));
            extCopyMenu.add(ActionUtils.makeCommandContribution(site, ResultSetCopySpecialHandler.CMD_COPY_SPECIAL));
            extCopyMenu.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_COPY_COLUMN_NAMES));
            if (row != null) {
                extCopyMenu.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_COPY_ROW_NAMES));
            }
            manager.add(extCopyMenu);
            if (valueController != null) {
                manager.add(ActionUtils.makeCommandContribution(site, IWorkbenchCommandConstants.EDIT_PASTE));
                manager.add(ActionUtils.makeCommandContribution(site, CoreCommands.CMD_PASTE_SPECIAL));
                manager.add(ActionUtils.makeCommandContribution(site, IWorkbenchCommandConstants.EDIT_DELETE));
                // Edit items
                manager.add(new Separator());
                manager.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_ROW_EDIT));
                manager.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_ROW_EDIT_INLINE));
                if (!valueController.isReadOnly() && !DBUtils.isNullValue(value)) /* && !attr.isRequired()*/
                {
                    manager.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_CELL_SET_NULL));
                }
            }
            manager.add(new GroupMarker(MENU_GROUP_EDIT));
        }
        if (valueController != null) {
            // Menus from value handler
            try {
                manager.add(new Separator());
                valueController.getValueManager().contributeActions(manager, valueController, null);
            } catch (Exception e) {
                log.error(e);
            }
            if (row.getState() == ResultSetRow.STATE_REMOVED || (row.changes != null && row.changes.containsKey(attr))) {
                manager.insertAfter(IResultSetController.MENU_GROUP_EDIT, ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_CELL_RESET));
            }
        }
    }
    if (dataSource != null && attr != null && model.getVisibleAttributeCount() > 0 && !model.isUpdateInProgress()) {
        // Filters and View
        manager.add(new Separator());
        {
            String filtersShortcut = ActionUtils.findCommandDescription(ResultSetCommandHandler.CMD_FILTER_MENU, getSite(), true);
            String menuName = CoreMessages.controls_resultset_viewer_action_order_filter;
            if (!CommonUtils.isEmpty(filtersShortcut)) {
                menuName += " (" + filtersShortcut + ")";
            }
            MenuManager filtersMenu = new MenuManager(menuName, DBeaverIcons.getImageDescriptor(UIIcon.FILTER), //$NON-NLS-1$
            "filters");
            filtersMenu.setRemoveAllWhenShown(true);
            filtersMenu.addMenuListener(new IMenuListener() {

                @Override
                public void menuAboutToShow(IMenuManager manager) {
                    fillFiltersMenu(attr, manager);
                }
            });
            manager.add(filtersMenu);
        }
        {
            MenuManager viewMenu = new MenuManager("View/Format", null, //$NON-NLS-1$
            "view");
            List<? extends DBDAttributeTransformerDescriptor> transformers = dataSource.getContainer().getPlatform().getValueHandlerRegistry().findTransformers(dataSource, attr, null);
            if (!CommonUtils.isEmpty(transformers)) {
                MenuManager transformersMenu = new MenuManager("View as");
                transformersMenu.setRemoveAllWhenShown(true);
                transformersMenu.addMenuListener(new IMenuListener() {

                    @Override
                    public void menuAboutToShow(IMenuManager manager) {
                        fillAttributeTransformersMenu(manager, attr);
                    }
                });
                viewMenu.add(transformersMenu);
            } else {
                final Action customizeAction = new Action("View as") {
                };
                customizeAction.setEnabled(false);
                viewMenu.add(customizeAction);
            }
            if (getModel().isSingleSource()) {
                if (valueController != null) {
                    viewMenu.add(new SetRowColorAction(attr, valueController.getValue()));
                    if (getModel().hasColorMapping(attr)) {
                        viewMenu.add(new ResetRowColorAction(attr, valueController.getValue()));
                    }
                }
                viewMenu.add(new CustomizeColorsAction(attr, row));
                viewMenu.add(new Separator());
            }
            viewMenu.add(new Action("Data formats ...") {

                @Override
                public void run() {
                    UIUtils.showPreferencesFor(getControl().getShell(), null, PrefPageDataFormat.PAGE_ID);
                }
            });
            manager.add(viewMenu);
        }
        {
            // Navigate
            MenuManager navigateMenu = new MenuManager("Navigate", null, //$NON-NLS-1$
            "navigate");
            if (ActionUtils.isCommandEnabled(ResultSetCommandHandler.CMD_NAVIGATE_LINK, site)) {
                navigateMenu.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_NAVIGATE_LINK));
                navigateMenu.add(new Separator());
            }
            navigateMenu.add(new Separator());
            navigateMenu.add(ActionUtils.makeCommandContribution(site, ITextEditorActionDefinitionIds.LINE_GOTO));
            navigateMenu.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_ROW_FIRST));
            navigateMenu.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_ROW_NEXT));
            navigateMenu.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_ROW_PREVIOUS));
            navigateMenu.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_ROW_LAST));
            navigateMenu.add(new Separator());
            navigateMenu.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_FETCH_PAGE));
            navigateMenu.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_FETCH_ALL));
            if (isHasMoreData() && getDataContainer() != null && (getDataContainer().getSupportedFeatures() & DBSDataContainer.DATA_COUNT) != 0) {
                navigateMenu.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_COUNT));
            }
            manager.add(navigateMenu);
        }
        {
            // Layout
            MenuManager layoutMenu = new MenuManager("Layout", null, //$NON-NLS-1$
            "layout");
            layoutMenu.add(new ToggleModeAction());
            layoutMenu.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_TOGGLE_PANELS));
            layoutMenu.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_SWITCH_PRESENTATION));
            {
                layoutMenu.add(new Separator());
                for (IContributionItem item : fillPanelsMenu()) {
                    layoutMenu.add(item);
                }
            }
            manager.add(layoutMenu);
        }
        manager.add(new Separator());
    }
    // Fill general menu
    final DBSDataContainer dataContainer = getDataContainer();
    if (dataContainer != null && model.hasData()) {
        manager.add(new Action(CoreMessages.controls_resultset_viewer_action_export, DBeaverIcons.getImageDescriptor(UIIcon.EXPORT)) {

            @Override
            public void run() {
                ActiveWizardDialog dialog = new ActiveWizardDialog(site.getWorkbenchWindow(), new DataTransferWizard(new IDataTransferProducer[] { new DatabaseTransferProducer(dataContainer, model.getDataFilter()) }, null), getSelection());
                dialog.open();
            }
        });
    }
    manager.add(new GroupMarker(CoreCommands.GROUP_TOOLS));
    if (dataContainer != null && model.hasData()) {
        manager.add(new Separator());
        manager.add(ActionUtils.makeCommandContribution(site, IWorkbenchCommandConstants.FILE_REFRESH));
    }
    manager.add(new Separator());
    manager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
}
Also used : DataTransferWizard(org.jkiss.dbeaver.tools.transfer.wizard.DataTransferWizard) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) ActiveWizardDialog(org.jkiss.dbeaver.ui.dialogs.ActiveWizardDialog) DatabaseTransferProducer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer) List(java.util.List)

Example 3 with DatabaseTransferProducer

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer in project dbeaver by dbeaver.

the class OpenSpreadsheetHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IResultSetController resultSet = ResultSetCommandHandler.getActiveResultSet(HandlerUtil.getActivePart(event));
    if (resultSet == null) {
        DBeaverUI.getInstance().showError("Open Excel", "No active results viewer");
        return null;
    }
    DBSDataContainer dataContainer = resultSet.getDataContainer();
    if (dataContainer == null || dataContainer.getDataSource() == null) {
        DBeaverUI.getInstance().showError("Open Excel", "Not connected to a database");
        return null;
    }
    AbstractJob exportJob = new AbstractJob("Open Excel") {

        {
            setUser(true);
            setSystem(false);
        }

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try {
                File tempDir = DBeaverCore.getInstance().getTempFolder(monitor, "office-files");
                File tempFile = new File(tempDir, CommonUtils.escapeFileName(CommonUtils.truncateString(dataContainer.getName(), 32)) + "." + new SimpleDateFormat("yyyyMMdd-HHmmss").format(System.currentTimeMillis()) + ".xlsx");
                DataExporterXLSX exporter = new DataExporterXLSX();
                StreamPOITransferConsumer consumer = new StreamPOITransferConsumer();
                StreamPOIConsumerSettings settings = new StreamPOIConsumerSettings();
                settings.setOutputEncodingBOM(false);
                settings.setOpenFolderOnFinish(false);
                settings.setOutputFolder(tempDir.getAbsolutePath());
                settings.setOutputFilePattern(tempFile.getName());
                Map<Object, Object> properties = DataExporterXLSX.getDefaultProperties();
                consumer.initTransfer(dataContainer, settings, exporter, properties);
                DatabaseTransferProducer producer = new DatabaseTransferProducer(dataContainer);
                DatabaseProducerSettings producerSettings = new DatabaseProducerSettings();
                producerSettings.setExtractType(DatabaseProducerSettings.ExtractType.SINGLE_QUERY);
                producerSettings.setQueryRowCount(false);
                producer.transferData(monitor, consumer, producerSettings);
                consumer.finishTransfer(monitor, false);
                DBeaverUI.asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        if (!UIUtils.launchProgram(tempFile.getAbsolutePath())) {
                            DBeaverUI.getInstance().showError("Open XLSX", "Can't open XLSX file '" + tempFile.getAbsolutePath() + "'");
                        }
                    }
                });
            } catch (Exception e) {
                DBeaverUI.getInstance().showError("Error opening in Excel", null, e);
            }
            return Status.OK_STATUS;
        }
    };
    exportJob.schedule();
    return null;
}
Also used : StreamPOIConsumerSettings(org.jkiss.dbeaver.data.office.export.StreamPOIConsumerSettings) IResultSetController(org.jkiss.dbeaver.ui.controls.resultset.IResultSetController) ExecutionException(org.eclipse.core.commands.ExecutionException) DataExporterXLSX(org.jkiss.dbeaver.data.office.export.DataExporterXLSX) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DatabaseTransferProducer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer) DatabaseProducerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings) StreamPOITransferConsumer(org.jkiss.dbeaver.data.office.export.StreamPOITransferConsumer) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with DatabaseTransferProducer

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer in project dbeaver by dbeaver.

the class DataImportHandler method chooseProducer.

@Override
protected IDataTransferProducer chooseProducer(ExecutionEvent event, IDataTransferConsumer consumer) {
    final DBNModel navigatorModel = DBeaverCore.getInstance().getNavigatorModel();
    final DBNNode rootNode = DBeaverCore.getInstance().getLiveProjects().size() == 1 ? navigatorModel.getRoot().getProject(DBeaverCore.getInstance().getProjectRegistry().getActiveProject()) : navigatorModel.getRoot();
    DBNNode node = BrowseObjectDialog.selectObject(HandlerUtil.getActiveShell(event), "Select source container for '" + consumer.getTargetName() + "'", rootNode, null, new Class[] { DBSObjectContainer.class, DBSDataContainer.class }, new Class[] { DBSDataContainer.class });
    if (node instanceof DBNDatabaseNode) {
        DBSObject object = ((DBNDatabaseNode) node).getObject();
        if (object instanceof DBSDataContainer) {
            return new DatabaseTransferProducer((DBSDataContainer) object);
        }
    }
    return null;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DatabaseTransferProducer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) DBNModel(org.jkiss.dbeaver.model.navigator.DBNModel)

Example 5 with DatabaseTransferProducer

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer in project dbeaver by serge-rider.

the class DatabaseProducerPageInputObjects method autoAssignMappings.

private void autoAssignMappings(List<DBSObject> containerObjects) {
    boolean chooseConsumer = getWizard().getSettings().isConsumerOptional();
    for (TableItem item : mappingTable.getItems()) {
        DataTransferPipe pipe = (DataTransferPipe) item.getData();
        if ((chooseConsumer && (pipe.getConsumer() == null || pipe.getConsumer().getDatabaseObject() == null)) || (!chooseConsumer && (pipe.getProducer() == null || pipe.getProducer().getDatabaseObject() == null))) {
            DBSObject objectToMap = chooseConsumer ? pipe.getProducer().getDatabaseObject() : pipe.getConsumer().getDatabaseObject();
            if (objectToMap == null) {
                continue;
            }
            DBSObject object = DBUtils.findObject(containerObjects, objectToMap.getName());
            if (object != null) {
                if (chooseConsumer) {
                    if (object instanceof DBSDataManipulator) {
                        pipe.setConsumer(new DatabaseTransferConsumer((DBSDataManipulator) object));
                    }
                } else {
                    if (object instanceof DBSDataContainer) {
                        pipe.setProducer(new DatabaseTransferProducer((DBSDataContainer) object));
                    }
                }
                updateItemData(item, pipe);
            }
        }
    }
    updatePageCompletion();
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DatabaseTransferConsumer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferConsumer) DBSDataManipulator(org.jkiss.dbeaver.model.struct.DBSDataManipulator) DatabaseTransferProducer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer) DataTransferPipe(org.jkiss.dbeaver.tools.transfer.DataTransferPipe)

Aggregations

DatabaseTransferProducer (org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer)11 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)6 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)6 ExecutionException (org.eclipse.core.commands.ExecutionException)5 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)5 DBSDataContainer (org.jkiss.dbeaver.model.struct.DBSDataContainer)4 File (java.io.File)3 SimpleDateFormat (java.text.SimpleDateFormat)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 DBException (org.jkiss.dbeaver.DBException)3 DBDDataFilter (org.jkiss.dbeaver.model.data.DBDDataFilter)3 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)3 DatabaseProducerSettings (org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)2 IDialogSettings (org.eclipse.jface.dialogs.IDialogSettings)2 IInputValidator (org.eclipse.jface.dialogs.IInputValidator)2 FontDescriptor (org.eclipse.jface.resource.FontDescriptor)2 FontRegistry (org.eclipse.jface.resource.FontRegistry)2