|
| 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