Search in sources :

Example 46 with DBPProject

use of org.jkiss.dbeaver.model.app.DBPProject in project dbeaver by serge-rider.

the class ProjectExportWizard method exportProjects.

private void exportProjects(DBRProgressMonitor monitor, final ProjectExportData exportData) throws IOException, CoreException, InterruptedException {
    if (!exportData.getOutputFolder().exists()) {
        if (!exportData.getOutputFolder().mkdirs()) {
            // $NON-NLS-2$
            throw new IOException("Cannot create directory '" + exportData.getOutputFolder().getAbsolutePath() + "'");
        }
    }
    String archiveName = exportData.getArchiveFileName() + ExportConstants.ARCHIVE_FILE_EXT;
    File archiveFile = new File(exportData.getOutputFolder(), archiveName);
    FileOutputStream exportStream = new FileOutputStream(archiveFile);
    try {
        ByteArrayOutputStream metaBuffer = new ByteArrayOutputStream(10000);
        ZipOutputStream archiveStream = new ZipOutputStream(exportStream);
        // Start meta
        XMLBuilder meta = new XMLBuilder(metaBuffer, GeneralUtils.UTF8_ENCODING);
        meta.startElement(ExportConstants.TAG_ARCHIVE);
        meta.addAttribute(ExportConstants.ATTR_VERSION, ExportConstants.ARCHIVE_VERSION_CURRENT);
        exportData.initExport(DBWorkbench.getPlatform().getWorkspace(), meta, archiveStream);
        {
            // Export source info
            meta.startElement(ExportConstants.TAG_SOURCE);
            meta.addAttribute(ExportConstants.ATTR_TIME, System.currentTimeMillis());
            meta.addAttribute(ExportConstants.ATTR_ADDRESS, InetAddress.getLocalHost().getHostAddress());
            meta.addAttribute(ExportConstants.ATTR_HOST, InetAddress.getLocalHost().getHostName());
            meta.endElement();
        }
        Map<DBPProject, Integer> resCountMap = new HashMap<>();
        monitor.beginTask(CoreMessages.dialog_project_export_wizard_monitor_collect_info, exportData.getProjectsToExport().size());
        for (DBPProject project : exportData.getProjectsToExport()) {
            // Add used drivers to export data
            final DBPDataSourceRegistry dataSourceRegistry = project.getDataSourceRegistry();
            if (dataSourceRegistry != null) {
                for (DBPDataSourceContainer dataSourceDescriptor : dataSourceRegistry.getDataSources()) {
                    exportData.usedDrivers.add(dataSourceDescriptor.getDriver());
                }
            }
            resCountMap.put(project, getChildCount(exportData, project.getEclipseProject()));
            monitor.worked(1);
        }
        monitor.done();
        {
            // Export projects
            exportData.meta.startElement(ExportConstants.TAG_PROJECTS);
            for (DBPProject project : exportData.getProjectsToExport()) {
                monitor.beginTask(NLS.bind(CoreMessages.dialog_project_export_wizard_monitor_export_project, project.getName()), resCountMap.get(project));
                try {
                    exportProject(monitor, exportData, project.getEclipseProject());
                } finally {
                    monitor.done();
                }
            }
            exportData.meta.endElement();
        }
        if (exportData.isExportDrivers()) {
            // Export driver libraries
            Set<File> libFiles = new HashSet<>();
            Map<String, File> libPathMap = new HashMap<>();
            for (DBPDriver driver : exportData.usedDrivers) {
                for (DBPDriverLibrary fileDescriptor : driver.getDriverLibraries()) {
                    final File libraryFile = fileDescriptor.getLocalFile();
                    if (libraryFile != null && !fileDescriptor.isDisabled() && libraryFile.exists()) {
                        libFiles.add(libraryFile);
                        libPathMap.put(fileDescriptor.getPath(), libraryFile);
                    }
                }
            }
            if (!libFiles.isEmpty()) {
                monitor.beginTask(CoreMessages.dialog_project_export_wizard_monitor_export_libraries, libFiles.size());
                // $NON-NLS-1$
                final ZipEntry driversFolder = new ZipEntry(ExportConstants.DIR_DRIVERS + "/");
                // $NON-NLS-1$
                driversFolder.setComment("Database driver libraries");
                exportData.archiveStream.putNextEntry(driversFolder);
                exportData.archiveStream.closeEntry();
                exportData.meta.startElement(ExportConstants.TAG_LIBRARIES);
                Set<String> libFileNames = new HashSet<>();
                for (String libPath : libPathMap.keySet()) {
                    final File libFile = libPathMap.get(libPath);
                    // Check for file name duplications
                    final String libFileName = libFile.getName();
                    if (libFileNames.contains(libFileName)) {
                        // $NON-NLS-1$
                        log.warn("Duplicate driver library file name: " + libFileName);
                        continue;
                    }
                    libFileNames.add(libFileName);
                    monitor.subTask(libFileName);
                    exportData.meta.startElement(RegistryConstants.TAG_FILE);
                    exportData.meta.addAttribute(ExportConstants.ATTR_PATH, libPath);
                    // $NON-NLS-1$
                    exportData.meta.addAttribute(ExportConstants.ATTR_FILE, "drivers/" + libFileName);
                    exportData.meta.endElement();
                    // $NON-NLS-1$
                    final ZipEntry driverFile = new ZipEntry(ExportConstants.DIR_DRIVERS + "/" + libFileName);
                    // $NON-NLS-1$
                    driverFile.setComment("Driver library");
                    exportData.archiveStream.putNextEntry(driverFile);
                    try (InputStream is = new FileInputStream(libFile)) {
                        IOUtils.copyStream(is, exportData.archiveStream, COPY_BUFFER_SIZE);
                    }
                    exportData.archiveStream.closeEntry();
                    monitor.worked(1);
                }
                exportData.meta.endElement();
                monitor.done();
            }
        }
        // Add meta to archive
        {
            exportData.meta.endElement();
            exportData.meta.flush();
            archiveStream.putNextEntry(new ZipEntry(ExportConstants.META_FILENAME));
            archiveStream.write(metaBuffer.toByteArray());
            archiveStream.closeEntry();
        }
        // Finish archive creation
        archiveStream.finish();
    } finally {
        ContentUtils.close(exportStream);
    }
}
Also used : HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) XMLBuilder(org.jkiss.utils.xml.XMLBuilder) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) ZipOutputStream(java.util.zip.ZipOutputStream) DBPDriverLibrary(org.jkiss.dbeaver.model.connection.DBPDriverLibrary) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry) IFile(org.eclipse.core.resources.IFile) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) HashSet(java.util.HashSet)

Example 47 with DBPProject

use of org.jkiss.dbeaver.model.app.DBPProject in project dbeaver by serge-rider.

the class EditorUtils method setFileDataSource.

public static void setFileDataSource(@NotNull IFile file, @NotNull DatabaseEditorContext context) {
    DBPProject projectMeta = DBWorkbench.getPlatform().getWorkspace().getProject(file.getProject());
    if (projectMeta == null) {
        return;
    }
    DBPDataSourceContainer dataSourceContainer = context.getDataSourceContainer();
    String dataSourceId = dataSourceContainer == null ? null : dataSourceContainer.getId();
    projectMeta.setResourceProperty(file, PROP_SQL_DATA_SOURCE_ID, dataSourceId);
    if (!isDefaultContextSettings(context)) {
        projectMeta.setResourceProperty(file, PROP_CONTEXT_DEFAULT_DATASOURCE, dataSourceId);
        String catalogName = getDefaultCatalogName(context);
        if (catalogName != null)
            projectMeta.setResourceProperty(file, PROP_CONTEXT_DEFAULT_CATALOG, catalogName);
        String schemaName = getDefaultSchemaName(context);
        if (catalogName != null || schemaName != null)
            projectMeta.setResourceProperty(file, PROP_CONTEXT_DEFAULT_SCHEMA, schemaName);
    }
}
Also used : DBPProject(org.jkiss.dbeaver.model.app.DBPProject) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 48 with DBPProject

use of org.jkiss.dbeaver.model.app.DBPProject in project dbeaver by dbeaver.

the class ProjectExportWizard method exportProjects.

private void exportProjects(DBRProgressMonitor monitor, final ProjectExportData exportData) throws IOException, CoreException, InterruptedException {
    if (!exportData.getOutputFolder().exists()) {
        if (!exportData.getOutputFolder().mkdirs()) {
            // $NON-NLS-2$
            throw new IOException("Cannot create directory '" + exportData.getOutputFolder().getAbsolutePath() + "'");
        }
    }
    String archiveName = exportData.getArchiveFileName() + ExportConstants.ARCHIVE_FILE_EXT;
    File archiveFile = new File(exportData.getOutputFolder(), archiveName);
    FileOutputStream exportStream = new FileOutputStream(archiveFile);
    try {
        ByteArrayOutputStream metaBuffer = new ByteArrayOutputStream(10000);
        ZipOutputStream archiveStream = new ZipOutputStream(exportStream);
        // Start meta
        XMLBuilder meta = new XMLBuilder(metaBuffer, GeneralUtils.UTF8_ENCODING);
        meta.startElement(ExportConstants.TAG_ARCHIVE);
        meta.addAttribute(ExportConstants.ATTR_VERSION, ExportConstants.ARCHIVE_VERSION_CURRENT);
        exportData.initExport(DBWorkbench.getPlatform().getWorkspace(), meta, archiveStream);
        {
            // Export source info
            meta.startElement(ExportConstants.TAG_SOURCE);
            meta.addAttribute(ExportConstants.ATTR_TIME, System.currentTimeMillis());
            meta.addAttribute(ExportConstants.ATTR_ADDRESS, InetAddress.getLocalHost().getHostAddress());
            meta.addAttribute(ExportConstants.ATTR_HOST, InetAddress.getLocalHost().getHostName());
            meta.endElement();
        }
        Map<DBPProject, Integer> resCountMap = new HashMap<>();
        monitor.beginTask(CoreMessages.dialog_project_export_wizard_monitor_collect_info, exportData.getProjectsToExport().size());
        for (DBPProject project : exportData.getProjectsToExport()) {
            // Add used drivers to export data
            final DBPDataSourceRegistry dataSourceRegistry = project.getDataSourceRegistry();
            if (dataSourceRegistry != null) {
                for (DBPDataSourceContainer dataSourceDescriptor : dataSourceRegistry.getDataSources()) {
                    exportData.usedDrivers.add(dataSourceDescriptor.getDriver());
                }
            }
            resCountMap.put(project, getChildCount(exportData, project.getEclipseProject()));
            monitor.worked(1);
        }
        monitor.done();
        {
            // Export projects
            exportData.meta.startElement(ExportConstants.TAG_PROJECTS);
            for (DBPProject project : exportData.getProjectsToExport()) {
                monitor.beginTask(NLS.bind(CoreMessages.dialog_project_export_wizard_monitor_export_project, project.getName()), resCountMap.get(project));
                try {
                    exportProject(monitor, exportData, project.getEclipseProject());
                } finally {
                    monitor.done();
                }
            }
            exportData.meta.endElement();
        }
        if (exportData.isExportDrivers()) {
            // Export driver libraries
            Set<File> libFiles = new HashSet<>();
            Map<String, File> libPathMap = new HashMap<>();
            for (DBPDriver driver : exportData.usedDrivers) {
                for (DBPDriverLibrary fileDescriptor : driver.getDriverLibraries()) {
                    final File libraryFile = fileDescriptor.getLocalFile();
                    if (libraryFile != null && !fileDescriptor.isDisabled() && libraryFile.exists()) {
                        libFiles.add(libraryFile);
                        libPathMap.put(fileDescriptor.getPath(), libraryFile);
                    }
                }
            }
            if (!libFiles.isEmpty()) {
                monitor.beginTask(CoreMessages.dialog_project_export_wizard_monitor_export_libraries, libFiles.size());
                // $NON-NLS-1$
                final ZipEntry driversFolder = new ZipEntry(ExportConstants.DIR_DRIVERS + "/");
                // $NON-NLS-1$
                driversFolder.setComment("Database driver libraries");
                exportData.archiveStream.putNextEntry(driversFolder);
                exportData.archiveStream.closeEntry();
                exportData.meta.startElement(ExportConstants.TAG_LIBRARIES);
                Set<String> libFileNames = new HashSet<>();
                for (String libPath : libPathMap.keySet()) {
                    final File libFile = libPathMap.get(libPath);
                    // Check for file name duplications
                    final String libFileName = libFile.getName();
                    if (libFileNames.contains(libFileName)) {
                        // $NON-NLS-1$
                        log.warn("Duplicate driver library file name: " + libFileName);
                        continue;
                    }
                    libFileNames.add(libFileName);
                    monitor.subTask(libFileName);
                    exportData.meta.startElement(RegistryConstants.TAG_FILE);
                    exportData.meta.addAttribute(ExportConstants.ATTR_PATH, libPath);
                    // $NON-NLS-1$
                    exportData.meta.addAttribute(ExportConstants.ATTR_FILE, "drivers/" + libFileName);
                    exportData.meta.endElement();
                    // $NON-NLS-1$
                    final ZipEntry driverFile = new ZipEntry(ExportConstants.DIR_DRIVERS + "/" + libFileName);
                    // $NON-NLS-1$
                    driverFile.setComment("Driver library");
                    exportData.archiveStream.putNextEntry(driverFile);
                    try (InputStream is = new FileInputStream(libFile)) {
                        IOUtils.copyStream(is, exportData.archiveStream, COPY_BUFFER_SIZE);
                    }
                    exportData.archiveStream.closeEntry();
                    monitor.worked(1);
                }
                exportData.meta.endElement();
                monitor.done();
            }
        }
        // Add meta to archive
        {
            exportData.meta.endElement();
            exportData.meta.flush();
            archiveStream.putNextEntry(new ZipEntry(ExportConstants.META_FILENAME));
            archiveStream.write(metaBuffer.toByteArray());
            archiveStream.closeEntry();
        }
        // Finish archive creation
        archiveStream.finish();
    } finally {
        ContentUtils.close(exportStream);
    }
}
Also used : HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) XMLBuilder(org.jkiss.utils.xml.XMLBuilder) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) ZipOutputStream(java.util.zip.ZipOutputStream) DBPDriverLibrary(org.jkiss.dbeaver.model.connection.DBPDriverLibrary) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry) IFile(org.eclipse.core.resources.IFile) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) HashSet(java.util.HashSet)

Example 49 with DBPProject

use of org.jkiss.dbeaver.model.app.DBPProject in project dbeaver by dbeaver.

the class PrefPageConnectionTypes method performOk.

@Override
public boolean performOk() {
    typeId.setEnabled(false);
    DataSourceProviderRegistry registry = DataSourceProviderRegistry.getInstance();
    List<DBPConnectionType> toRemove = new ArrayList<>();
    for (DBPConnectionType type : registry.getConnectionTypes()) {
        if (!changedInfo.values().contains(type)) {
            // Remove
            toRemove.add(type);
        }
    }
    Set<DBPConnectionType> changedSet = new HashSet<>();
    for (DBPConnectionType connectionType : toRemove) {
        registry.removeConnectionType(connectionType);
        changedSet.add(connectionType);
    }
    for (Map.Entry<DBPConnectionType, DBPConnectionType> entry : changedInfo.entrySet()) {
        boolean hasChanges = false;
        DBPConnectionType changed = entry.getKey();
        DBPConnectionType source = entry.getValue();
        if (source == changed) {
            // New type
            if (CommonUtils.isEmpty(changed.getId())) {
                changed.setId(SecurityUtils.generateUniqueId());
            }
            for (DBPConnectionType type : changedInfo.keySet()) {
                if (type != changed && type.getId().equals(changed.getId())) {
                    changed.setId(SecurityUtils.generateUniqueId());
                    break;
                }
            }
            entry.setValue(new DBPConnectionType(source));
            registry.addConnectionType(changed);
            hasChanges = true;
        } else if (!source.equals(changed)) {
            // Changed type
            source.setId(changed.getId());
            source.setName(changed.getName());
            source.setDescription(changed.getDescription());
            source.setAutocommit(changed.isAutocommit());
            source.setConfirmExecute(changed.isConfirmExecute());
            source.setConfirmDataChange(changed.isConfirmDataChange());
            source.setColor(changed.getColor());
            source.setModifyPermissions(changed.getModifyPermission());
            hasChanges = true;
        }
        if (hasChanges) {
            changedSet.add(source);
        }
    }
    if (!changedSet.isEmpty()) {
        registry.saveConnectionTypes();
        // Flush projects configs (as they cache connection type information)
        for (DBPProject project : DBWorkbench.getPlatform().getWorkspace().getProjects()) {
            DBPDataSourceRegistry projectRegistry = project.getDataSourceRegistry();
            for (DBPDataSourceContainer ds : projectRegistry.getDataSources()) {
                if (changedSet.contains(ds.getConnectionConfiguration().getConnectionType())) {
                    projectRegistry.flushConfig();
                    break;
                }
            }
        }
    }
    return super.performOk();
}
Also used : DBPConnectionType(org.jkiss.dbeaver.model.connection.DBPConnectionType) DataSourceProviderRegistry(org.jkiss.dbeaver.registry.DataSourceProviderRegistry) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 50 with DBPProject

use of org.jkiss.dbeaver.model.app.DBPProject in project dbeaver by dbeaver.

the class DatabaseConsumerPageMapping method mapExistingTable.

private void mapExistingTable(DatabaseMappingContainer mapping) {
    final DatabaseConsumerSettings settings = getDatabaseConsumerSettings();
    DBPProject activeProject = DBWorkbench.getPlatform().getWorkspace().getActiveProject();
    if (activeProject != null) {
        DBNNode rootNode = settings.getContainerNode();
        if (rootNode == null) {
            rootNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot().getProjectNode(activeProject).getDatabases();
        }
        DBNNode selectedNode = rootNode;
        if (mapping.getTarget() != null) {
            selectedNode = DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(mapping.getTarget());
        }
        DBNNode node = DBWorkbench.getPlatformUI().selectObject(getShell(), DTUIMessages.database_consumer_page_mapping_node_title, rootNode, selectedNode, new Class[] { DBSObjectContainer.class, DBSDataManipulator.class }, new Class[] { DBSDataManipulator.class }, null);
        if (node != null && node instanceof DBSWrapper) {
            DBSObject object = ((DBSWrapper) node).getObject();
            try {
                if (object instanceof DBSDataManipulator) {
                    mapping.setTarget((DBSDataManipulator) object);
                    mapping.refreshMappingType(getWizard().getRunnableContext(), DatabaseMappingType.existing);
                    mapColumns(mapping);
                } else {
                    mapping.setTarget(null);
                    mapping.refreshMappingType(getWizard().getRunnableContext(), DatabaseMappingType.unspecified);
                }
            } catch (DBException e) {
                DBWorkbench.getPlatformUI().showError(DTUIMessages.database_consumer_page_mapping_title_error_mapping_table, DTUIMessages.database_consumer_page_mapping_message_error_mapping_existing_table, e);
            }
            updateMappingsAndButtons();
            updatePageCompletion();
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBPProject(org.jkiss.dbeaver.model.app.DBPProject)

Aggregations

DBPProject (org.jkiss.dbeaver.model.app.DBPProject)90 IFile (org.eclipse.core.resources.IFile)20 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)18 DBException (org.jkiss.dbeaver.DBException)14 DBPDataSourceRegistry (org.jkiss.dbeaver.model.app.DBPDataSourceRegistry)14 IFolder (org.eclipse.core.resources.IFolder)8 DBPWorkspace (org.jkiss.dbeaver.model.app.DBPWorkspace)8 IResource (org.eclipse.core.resources.IResource)6 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)6 SelectionEvent (org.eclipse.swt.events.SelectionEvent)6 IEditorPart (org.eclipse.ui.IEditorPart)6 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)6 GridData (org.eclipse.swt.layout.GridData)5 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ArrayList (java.util.ArrayList)4 CoreException (org.eclipse.core.runtime.CoreException)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 DBPImage (org.jkiss.dbeaver.model.DBPImage)4 Document (org.w3c.dom.Document)4