Skip to content

Commit a23b397

Browse files
authored
Merge pull request #5235 from microsoft/qianjin-sqlserver-04-showproperties
[SQL Server] (No.6) refactor mysql open by database tools action.
2 parents 4c01f61 + 64f6022 commit a23b397

File tree

12 files changed

+792
-142
lines changed

12 files changed

+792
-142
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/mysql/action/MySQLConnectToServerAction.java

Lines changed: 0 additions & 138 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.mysql.action;
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.azuretools.ActionConstants;
13+
import com.microsoft.azuretools.authmanage.AuthMethodManager;
14+
import com.microsoft.intellij.AzurePlugin;
15+
import com.microsoft.intellij.actions.AzureSignInAction;
16+
import com.microsoft.intellij.util.AzureLoginHelper;
17+
import com.microsoft.tooling.msservices.components.DefaultLoader;
18+
import com.microsoft.tooling.msservices.helpers.Name;
19+
import com.microsoft.tooling.msservices.serviceexplorer.AzureIconSymbol;
20+
import com.microsoft.tooling.msservices.serviceexplorer.NodeActionEvent;
21+
import com.microsoft.tooling.msservices.serviceexplorer.NodeActionListener;
22+
import com.microsoft.tooling.msservices.serviceexplorer.azure.mysql.MySQLNode;
23+
24+
import static com.microsoft.intellij.ui.messages.AzureBundle.message;
25+
26+
@Name(OpenMySQLByToolsAction.ACTION_NAME)
27+
public class OpenMySQLByToolsAction extends NodeActionListener {
28+
29+
public static final String ACTION_NAME = "Open by Database Tools";
30+
private static final String MYSQL_PATTERN_NAME = "Azure Database for MySQL - %s";
31+
private static final String MYSQL_DEFAULT_DRIVER = "com.mysql.cj.jdbc.Driver";
32+
33+
private final MySQLNode node;
34+
private final Project project;
35+
36+
public OpenMySQLByToolsAction(MySQLNode node) {
37+
super();
38+
this.node = node;
39+
this.project = (Project) node.getProject();
40+
}
41+
42+
@Override
43+
public AzureIconSymbol getIconSymbol() {
44+
return AzureIconSymbol.MySQL.CONNECT_TO_SERVER;
45+
}
46+
47+
@Override
48+
public void actionPerformed(NodeActionEvent e) {
49+
AzureSignInAction.doSignIn(AuthMethodManager.getInstance(), project).subscribe((isSuccess) -> {
50+
this.doActionPerformed(e, isSuccess, project);
51+
});
52+
}
53+
54+
@Override
55+
protected String getServiceName(NodeActionEvent event) {
56+
return ActionConstants.parse(ActionConstants.MySQL.CONNECT_TO_SERVER).getServiceName();
57+
}
58+
59+
@Override
60+
protected String getOperationName(NodeActionEvent event) {
61+
return ActionConstants.parse(ActionConstants.MySQL.CONNECT_TO_SERVER).getOperationName();
62+
}
63+
64+
@AzureOperation(name = ActionConstants.MySQL.CONNECT_TO_SERVER, type = AzureOperation.Type.ACTION)
65+
private void doActionPerformed(NodeActionEvent e, boolean isLoggedIn, Project project) {
66+
try {
67+
if (!isLoggedIn ||
68+
!AzureLoginHelper.isAzureSubsAvailableOrReportError(message("common.error.signIn"))) {
69+
return;
70+
}
71+
} catch (final Exception ex) {
72+
AzurePlugin.log(message("common.error.signIn"), ex);
73+
DefaultLoader.getUIHelper().showException(message("common.error.signIn"), ex, message("common.error.signIn"), false, true);
74+
}
75+
IntellijDatasourceService.DatasourceProperties properties = IntellijDatasourceService.DatasourceProperties.builder()
76+
.name(String.format(MYSQL_PATTERN_NAME, node.getServer().name()))
77+
.driverClassName(MYSQL_DEFAULT_DRIVER)
78+
.url(JdbcUrl.mysql(node.getServer().fullyQualifiedDomainName()).toString())
79+
.username(node.getServer().administratorLogin() + "@" + node.getServer().name())
80+
.build();
81+
IntellijDatasourceService.getInstance().openDataSourceManagerDialog(project, properties);
82+
}
83+
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.progress.ProgressIndicator;
9+
import com.intellij.openapi.progress.ProgressManager;
10+
import com.intellij.openapi.project.Project;
11+
import com.microsoft.azure.toolkit.intellij.sqlserver.creation.SqlServerCreationDialog;
12+
import com.microsoft.azure.toolkit.lib.common.operation.AzureOperation;
13+
import com.microsoft.azure.toolkit.lib.common.task.AzureTask;
14+
import com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager;
15+
import com.microsoft.azure.toolkit.lib.sqlserver.SqlServerConfig;
16+
import com.microsoft.azure.toolkit.lib.sqlserver.SqlServerService;
17+
import com.microsoft.azure.toolkit.lib.sqlserver.service.ISqlServer;
18+
import com.microsoft.azuretools.authmanage.AuthMethodManager;
19+
import com.microsoft.azuretools.utils.AzureUIRefreshCore;
20+
import com.microsoft.azuretools.utils.AzureUIRefreshEvent;
21+
import com.microsoft.intellij.AzurePlugin;
22+
import com.microsoft.intellij.actions.AzureSignInAction;
23+
import com.microsoft.intellij.util.AzureLoginHelper;
24+
import com.microsoft.tooling.msservices.components.DefaultLoader;
25+
import com.microsoft.tooling.msservices.helpers.Name;
26+
import com.microsoft.tooling.msservices.serviceexplorer.AzureActionEnum;
27+
import com.microsoft.tooling.msservices.serviceexplorer.Node;
28+
import com.microsoft.tooling.msservices.serviceexplorer.NodeActionEvent;
29+
import com.microsoft.tooling.msservices.serviceexplorer.NodeActionListener;
30+
import com.microsoft.tooling.msservices.serviceexplorer.azure.sqlserver.SqlServerModule;
31+
32+
import static com.microsoft.intellij.ui.messages.AzureBundle.message;
33+
34+
@Name("Create")
35+
public class CreateSqlServerAction extends NodeActionListener {
36+
37+
private final SqlServerModule model;
38+
39+
public CreateSqlServerAction(SqlServerModule model) {
40+
super();
41+
this.model = model;
42+
}
43+
44+
@Override
45+
public AzureActionEnum getAction() {
46+
return AzureActionEnum.CREATE;
47+
}
48+
49+
@Override
50+
public void actionPerformed(NodeActionEvent e) {
51+
final Project project = (Project) model.getProject();
52+
AzureSignInAction.doSignIn(AuthMethodManager.getInstance(), project).subscribe((isSuccess) -> {
53+
this.doActionPerformed(e, isSuccess, project);
54+
});
55+
}
56+
57+
private void doActionPerformed(NodeActionEvent e, boolean isLoggedIn, Project project) {
58+
try {
59+
if (!isLoggedIn ||
60+
!AzureLoginHelper.isAzureSubsAvailableOrReportError(message("common.error.signIn"))) {
61+
return;
62+
}
63+
} catch (final Exception ex) {
64+
AzurePlugin.log(message("common.error.signIn"), ex);
65+
DefaultLoader.getUIHelper().showException(message("common.error.signIn"), ex, message("common.error.signIn"), false, true);
66+
}
67+
final SqlServerCreationDialog dialog = new SqlServerCreationDialog(project);
68+
dialog.setOkActionListener((data) -> this.createSqlServer(data, dialog));
69+
dialog.show();
70+
}
71+
72+
private void createSqlServer(final SqlServerConfig config, SqlServerCreationDialog dialog) {
73+
final Runnable runnable = () -> {
74+
final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
75+
indicator.setIndeterminate(true);
76+
DefaultLoader.getIdeHelper().invokeLater(dialog::close);
77+
ISqlServer server = SqlServerService.getInstance().create(config);
78+
refreshAzureExplorer(server);
79+
};
80+
String progressMessage = Node.getProgressMessage(AzureActionEnum.CREATE.getDoingName(), SqlServerModule.MODULE_NAME, config.getServerName());
81+
final AzureTask task = new AzureTask(null, progressMessage, false, runnable);
82+
AzureTaskManager.getInstance().runInBackground(task);
83+
}
84+
85+
@AzureOperation(name = "common|explorer.refresh", type = AzureOperation.Type.TASK)
86+
private void refreshAzureExplorer(ISqlServer server) {
87+
AzureTaskManager.getInstance().runLater(() -> {
88+
if (AzureUIRefreshCore.listeners != null) {
89+
AzureUIRefreshCore.execute(new AzureUIRefreshEvent(AzureUIRefreshEvent.EventType.REFRESH, server));
90+
}
91+
});
92+
}
93+
}

0 commit comments

Comments
 (0)