|
| 1 | +/* |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.microsoft.azure.toolkit.intellij.sqlserver; |
| 7 | + |
| 8 | +import com.intellij.openapi.project.Project; |
| 9 | +import com.microsoft.azure.toolkit.intellij.common.IntellijDatasourceService; |
| 10 | +import com.microsoft.azure.toolkit.lib.common.operation.AzureOperation; |
| 11 | +import com.microsoft.azure.toolkit.lib.common.utils.JdbcUrl; |
| 12 | +import com.microsoft.azure.toolkit.lib.sqlserver.model.SqlServerEntity; |
| 13 | +import com.microsoft.azuretools.ActionConstants; |
| 14 | +import com.microsoft.azuretools.authmanage.AuthMethodManager; |
| 15 | +import com.microsoft.intellij.AzurePlugin; |
| 16 | +import com.microsoft.intellij.actions.AzureSignInAction; |
| 17 | +import com.microsoft.intellij.util.AzureLoginHelper; |
| 18 | +import com.microsoft.tooling.msservices.components.DefaultLoader; |
| 19 | +import com.microsoft.tooling.msservices.helpers.Name; |
| 20 | +import com.microsoft.tooling.msservices.serviceexplorer.AzureIconSymbol; |
| 21 | +import com.microsoft.tooling.msservices.serviceexplorer.NodeActionEvent; |
| 22 | +import com.microsoft.tooling.msservices.serviceexplorer.NodeActionListener; |
| 23 | +import com.microsoft.tooling.msservices.serviceexplorer.azure.sqlserver.SqlServerNode; |
| 24 | + |
| 25 | +import static com.microsoft.intellij.ui.messages.AzureBundle.message; |
| 26 | + |
| 27 | +@Name(OpenSqlServerByToolsAction.ACTION_NAME) |
| 28 | +public class OpenSqlServerByToolsAction extends NodeActionListener { |
| 29 | + |
| 30 | + public static final String ACTION_NAME = "Open by Database Tools"; |
| 31 | + private static final String NAME_PREFIX = "SQL Server - %s"; |
| 32 | + private static final String DEFAULT_DRIVER_CLASS_NAME = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; |
| 33 | + |
| 34 | + private final SqlServerNode node; |
| 35 | + private final Project project; |
| 36 | + |
| 37 | + public OpenSqlServerByToolsAction(SqlServerNode node) { |
| 38 | + super(); |
| 39 | + this.node = node; |
| 40 | + this.project = (Project) node.getProject(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public AzureIconSymbol getIconSymbol() { |
| 45 | + return AzureIconSymbol.SqlServer.CONNECT_TO_SERVER; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void actionPerformed(NodeActionEvent e) { |
| 50 | + AzureSignInAction.doSignIn(AuthMethodManager.getInstance(), project).subscribe((isSuccess) -> { |
| 51 | + this.doActionPerformed(e, isSuccess, project); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected String getServiceName(NodeActionEvent event) { |
| 57 | + return ActionConstants.parse(ActionConstants.SqlServer.CONNECT_TO_SERVER).getServiceName(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected String getOperationName(NodeActionEvent event) { |
| 62 | + return ActionConstants.parse(ActionConstants.SqlServer.CONNECT_TO_SERVER).getOperationName(); |
| 63 | + } |
| 64 | + |
| 65 | + @AzureOperation(name = ActionConstants.SqlServer.CONNECT_TO_SERVER, type = AzureOperation.Type.ACTION) |
| 66 | + private void doActionPerformed(NodeActionEvent e, boolean isLoggedIn, Project project) { |
| 67 | + try { |
| 68 | + if (!isLoggedIn || |
| 69 | + !AzureLoginHelper.isAzureSubsAvailableOrReportError(message("common.error.signIn"))) { |
| 70 | + return; |
| 71 | + } |
| 72 | + } catch (final Exception ex) { |
| 73 | + AzurePlugin.log(message("common.error.signIn"), ex); |
| 74 | + DefaultLoader.getUIHelper().showException(message("common.error.signIn"), ex, message("common.error.signIn"), false, true); |
| 75 | + } |
| 76 | + SqlServerEntity entity = node.getServer().entity(); |
| 77 | + IntellijDatasourceService.DatasourceProperties properties = IntellijDatasourceService.DatasourceProperties.builder() |
| 78 | + .name(String.format(NAME_PREFIX, entity.getName())) |
| 79 | + .driverClassName(DEFAULT_DRIVER_CLASS_NAME) |
| 80 | + .url(JdbcUrl.sqlserver(entity.getFullyQualifiedDomainName()).toString()) |
| 81 | + .username(entity.getAdministratorLoginName() + "@" + entity.getName()) |
| 82 | + .build(); |
| 83 | + IntellijDatasourceService.getInstance().openDataSourceManagerDialog(project, properties); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments