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);
}
}
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);
}
}
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);
}
}
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();
}
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();
}
}
}
Aggregations