Skip to content

Commit 439d18c

Browse files
fix behavior of server/account combobox in datasource panel.
1 parent ed08c05 commit 439d18c

File tree

5 files changed

+45
-56
lines changed

5 files changed

+45
-56
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-cosmos/src/main/java/com/microsoft/azure/toolkit/intellij/cosmos/dbtools/AzureCosmosDbAccountConnectionInterceptor.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,15 @@
1919
import java.util.Map;
2020
import java.util.concurrent.CompletionStage;
2121

22-
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.OPERATION_NAME;
23-
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.OP_NAME;
24-
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.OP_TYPE;
25-
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.SERVICE_NAME;
22+
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.*;
2623

2724
@SuppressWarnings("UnstableApiUsage")
2825
public class AzureCosmosDbAccountConnectionInterceptor implements DatabaseConnectionInterceptor {
2926
@Nullable
3027
public CompletionStage<ProtoConnection> intercept(@NotNull DatabaseConnectionInterceptor.ProtoConnection proto, boolean silent) {
3128
final DatabaseConnectionPoint point = proto.getConnectionPoint();
3229
final String accountId = point.getAdditionalProperty(AzureCosmosDbAccountParamEditor.KEY_COSMOS_ACCOUNT_ID);
33-
if (StringUtils.isNotBlank(accountId)) {
30+
if (StringUtils.isNotBlank(accountId) && !StringUtils.equalsIgnoreCase(accountId, AzureCosmosDbAccountParamEditor.NONE)) {
3431
final Map<String, String> properties = new HashMap<>();
3532
final ResourceId id = ResourceId.fromString(accountId);
3633
properties.put("subscriptionId", id.subscriptionId());

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-cosmos/src/main/java/com/microsoft/azure/toolkit/intellij/cosmos/dbtools/AzureCosmosDbAccountParamEditor.java

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,28 @@
6363

6464
public class AzureCosmosDbAccountParamEditor extends ParamEditorBase<AzureCosmosDbAccountParamEditor.CosmosDbAccountComboBox> {
6565
public static final String KEY_COSMOS_ACCOUNT_ID = "AZURE_COSMOS_ACCOUNT";
66+
public static final String NONE = "<NONE>";
6667
public static final String KEY_FROM_AZURE_EXPLORER = "FROM_EXPLORER";
6768
public static final String NO_ACCOUNT_TIPS_TEMPLATE = "<html>No Azure Cosmos DB accounts (%s). You can <a href=''>create one</a> first.</html>";
6869
public static final String NOT_SIGNIN_TIPS = "<html><a href=\"\">Sign in</a> to select an existing Azure Cosmos DB account.</html>";
6970
private final DatabaseAccountKind kind;
7071
@Getter
7172
@Setter
7273
private String text = "";
73-
private String accountId;
74+
@Nullable
7475
private CosmosDBAccountConnectionString connectionString;
7576
private boolean updating;
7677

7778
public AzureCosmosDbAccountParamEditor(@Nonnull DatabaseAccountKind kind, @Nonnull String label, @Nonnull DataInterchange interchange) {
7879
super(new CosmosDbAccountComboBox(kind), interchange, FieldSize.LARGE, label);
7980
this.kind = kind;
80-
final String accountId = interchange.getProperty(KEY_COSMOS_ACCOUNT_ID);
81-
// inputs will be fill with values from account if `this.accountId` is null;
82-
// fill inputs with values from account if it is from azure explorer
83-
// don't change values of inputs if it's modifying an existing data source
84-
if (Objects.isNull(interchange.getProperty(KEY_FROM_AZURE_EXPLORER)) && Objects.nonNull(accountId)) {
85-
this.accountId = accountId;
86-
}
81+
final LocalDataSource dataSource = getDataSourceConfigurable().getDataSource();
8782
final CosmosDbAccountComboBox combox = this.getEditorComponent();
8883
combox.addValueChangedListener(this::setAccount);
8984
interchange.addPersistentProperty(KEY_COSMOS_ACCOUNT_ID);
90-
if (StringUtils.isNotBlank(accountId)) {
91-
interchange.putProperty(KEY_COSMOS_ACCOUNT_ID, null);
85+
final String accountId = interchange.getProperty(KEY_COSMOS_ACCOUNT_ID);
86+
final boolean isModifying = StringUtils.isNotBlank(dataSource.getUsername());
87+
if (isModifying && StringUtils.isNotBlank(accountId)) {
9288
combox.setValue(new AzureComboBox.ItemReference<>(i -> i.getId().equals(accountId)));
9389
}
9490

@@ -162,9 +158,10 @@ private void createAccountInIde(InputEvent e) {
162158
}
163159

164160
private void onPropertiesChanged(String propertyName, Object newValue) {
165-
if (!this.updating && Objects.nonNull(this.connectionString) && StringUtils.isNotEmpty((String) newValue)) {
166-
if (StringUtils.equals(propertyName, "host") && !Objects.equals(this.connectionString.getHost(), newValue) ||
167-
StringUtils.equals(propertyName, "port") && !Objects.equals(this.connectionString.getPort() + "", newValue)) {
161+
if (!this.updating && StringUtils.isNotEmpty((String) newValue) && StringUtils.equals(propertyName, "host") && Objects.nonNull(this.connectionString)) {
162+
final AzureCosmosDbAccountParamEditor.CosmosDbAccountComboBox combox = this.getEditorComponent();
163+
final CosmosDBAccount account = combox.getValue();
164+
if (Objects.nonNull(account) && !Objects.equals(this.connectionString.getHost(), newValue)) {
168165
this.getEditorComponent().setValue((CosmosDBAccount) null);
169166
this.setAccount(null);
170167
}
@@ -180,35 +177,33 @@ private void setAccount(@Nullable CosmosDBAccount account) {
180177
});
181178

182179
final DataInterchange interchange = this.getInterchange();
180+
final String oldAccountId = interchange.getProperty(KEY_COSMOS_ACCOUNT_ID);
183181
final String newAccountId = Optional.ofNullable(account).map(AbstractAzResource::getId).orElse(null);
184-
interchange.putProperty(KEY_COSMOS_ACCOUNT_ID, newAccountId);
185-
if (this.updating || Objects.isNull(newAccountId) || StringUtils.equalsIgnoreCase(newAccountId, this.accountId)) {
186-
return;
187-
}
188-
this.connectionString = null;
189-
this.accountId = newAccountId;
182+
final AzureTaskManager manager = AzureTaskManager.getInstance();
190183
this.updating = true;
191-
// AzureActionManager.getInstance().getAction(Action.REQUIRE_AUTH).handle(combox::reloadItems);
192-
AzureTaskManager.getInstance().runOnPooledThread(() -> {
193-
this.connectionString = account.getCosmosDBAccountPrimaryConnectionString();
194-
final LocalDataSource dataSource = interchange.getDataSource();
195-
final String host = connectionString.getHost();
196-
final String port = String.valueOf(connectionString.getPort());
197-
final String user = connectionString.getUsername();
198-
final String password = String.valueOf(connectionString.getPassword());
199-
this.text = connectionString.getConnectionString();
200-
AzureTaskManager.getInstance().runLater(() -> {
184+
manager.runOnPooledThread(() -> {
185+
this.connectionString = Optional.ofNullable(account).map(CosmosDBAccount::getCosmosDBAccountPrimaryConnectionString).orElse(null);
186+
manager.runLater(() -> {
187+
interchange.putProperty(KEY_COSMOS_ACCOUNT_ID, Optional.ofNullable(newAccountId).orElse(NONE));
188+
if (Objects.isNull(account) || Objects.isNull(connectionString) || StringUtils.equalsIgnoreCase(oldAccountId, newAccountId)) {
189+
this.updating = false;
190+
return;
191+
}
192+
final LocalDataSource dataSource = interchange.getDataSource();
193+
final String host = connectionString.getHost();
194+
final String port = String.valueOf(connectionString.getPort());
195+
final String user = connectionString.getUsername();
196+
final String password = String.valueOf(connectionString.getPassword());
201197
LocalDataSource.setUsername(dataSource, user);
202198
interchange.getCredentials().storePassword(dataSource, new OneTimeString(password));
203199
this.setUseSsl(true);
204200
interchange.putProperties(consumer -> {
205-
consumer.consume(KEY_COSMOS_ACCOUNT_ID, account.getId());
206201
consumer.consume("host", host);
207202
consumer.consume("user", user);
208203
consumer.consume("port", port);
209-
this.updating = false;
210204
});
211205
this.setUsername(user);
206+
this.updating = false;
212207
}, AzureTask.Modality.ANY);
213208
});
214209
}

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-database/src/main/java/com/microsoft/azure/toolkit/intellij/database/dbtools/DatabaseServerConnectionInterceptor.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,15 @@
1919
import java.util.Map;
2020
import java.util.concurrent.CompletionStage;
2121

22-
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.OPERATION_NAME;
23-
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.OP_NAME;
24-
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.OP_TYPE;
25-
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.SERVICE_NAME;
22+
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.*;
2623

2724
@SuppressWarnings("UnstableApiUsage")
2825
public class DatabaseServerConnectionInterceptor implements DatabaseConnectionInterceptor {
2926
@Nullable
3027
public CompletionStage<ProtoConnection> intercept(@Nonnull DatabaseConnectionInterceptor.ProtoConnection proto, boolean silent) {
3128
final DatabaseConnectionPoint point = proto.getConnectionPoint();
3229
final String accountId = point.getAdditionalProperty(DatabaseServerParamEditor.KEY_DB_SERVER_ID);
33-
if (StringUtils.isNotBlank(accountId)) {
30+
if (StringUtils.isNotBlank(accountId) && !StringUtils.equalsIgnoreCase(accountId, DatabaseServerParamEditor.NONE)) {
3431
final Map<String, String> properties = new HashMap<>();
3532
final ResourceId id = ResourceId.fromString(accountId);
3633
properties.put("subscriptionId", id.subscriptionId());

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-database/src/main/java/com/microsoft/azure/toolkit/intellij/database/dbtools/DatabaseServerParamEditor.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import com.microsoft.azure.toolkit.lib.common.operation.OperationContext;
4141
import com.microsoft.azure.toolkit.lib.common.task.AzureTask;
4242
import com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager;
43-
import com.microsoft.azure.toolkit.lib.database.JdbcUrl;
4443
import com.microsoft.azure.toolkit.lib.database.entity.IDatabaseServer;
4544
import com.microsoft.azure.toolkit.lib.mysql.AzureMySql;
4645
import com.microsoft.azure.toolkit.lib.mysql.MySqlServer;
@@ -70,28 +69,26 @@
7069

7170
public class DatabaseServerParamEditor extends ParamEditorBase<DatabaseServerParamEditor.SqlDbServerComboBox> {
7271
public static final String KEY_DB_SERVER_ID = "AZURE_SQL_DB_SERVER";
72+
public static final String NONE = "<NONE>";
7373
public static final String NO_SERVERS_TIPS = "<html>No existing %s servers in Azure. You can <a href=''>create one</a> first.</html>";
7474
public static final String NOT_SIGNIN_TIPS = "<html><a href=\"\">Sign in</a> to select an existing %s server in Azure.</html>";
7575
private final Class<? extends IDatabaseServer<?>> clazz;
7676
@Getter
7777
@Setter
7878
private String text = "";
79-
@Nullable
80-
private JdbcUrl jdbcUrl;
8179
private boolean updating;
8280

8381
public DatabaseServerParamEditor(@Nonnull Class<? extends IDatabaseServer<?>> clazz, @Nonnull String label, @Nonnull DataInterchange interchange) {
8482
super(new SqlDbServerComboBox(clazz), interchange, FieldSize.LARGE, label);
8583
this.clazz = clazz;
8684
final LocalDataSource dataSource = getDataSourceConfigurable().getDataSource();
87-
this.jdbcUrl = Optional.ofNullable(dataSource.getUrl()).filter(StringUtils::isNotBlank).map(JdbcUrl::from).orElse(null);
8885
final SqlDbServerComboBox combox = this.getEditorComponent();
8986
combox.addValueChangedListener(this::setServer);
9087
interchange.addPersistentProperty(KEY_DB_SERVER_ID);
88+
final String serverId = interchange.getProperty(KEY_DB_SERVER_ID);
9189
final boolean isModifying = StringUtils.isNotBlank(dataSource.getUsername());
92-
if (isModifying && Objects.nonNull(this.jdbcUrl)) {
93-
final JdbcUrl url = this.jdbcUrl;
94-
combox.setValue(new AzureComboBox.ItemReference<>(i -> i.getJdbcUrl().getServerHost().equals(url.getServerHost())));
90+
if (isModifying && Objects.nonNull(serverId)) {
91+
combox.setValue(new AzureComboBox.ItemReference<>(i -> i.getId().equals(serverId)));
9592
}
9693

9794
interchange.addPropertyChangeListener((evt -> onPropertiesChanged(evt.getPropertyName(), evt.getNewValue())), this);
@@ -189,18 +186,20 @@ private void setServer(@Nullable IDatabaseServer<?> server) {
189186
OperationContext.action().setTelemetryProperty("resourceType", ((AzResource) a).getFullResourceType());
190187
});
191188
final DataInterchange interchange = this.getInterchange();
192-
interchange.putProperty(KEY_DB_SERVER_ID, Optional.ofNullable(server).map(IDatabaseServer::getId).orElse(null));
193-
final JdbcUrl newUrl = Optional.ofNullable(server).map(IDatabaseServer::getJdbcUrl).orElse(null);
194-
if (this.updating || Objects.isNull(newUrl) || Objects.nonNull(jdbcUrl) && Objects.equals(jdbcUrl.getServerHost(), newUrl.getServerHost())) {
195-
return;
196-
}
197-
this.jdbcUrl = newUrl;
189+
final String oldServerId = interchange.getProperty(KEY_DB_SERVER_ID);
190+
final String newServerId = Optional.ofNullable(server).map(IDatabaseServer::getId).orElse(null);
198191
this.updating = true;
199-
final String user = String.format("%s@%s", server.getAdminName(), server.getName());
200192
AzureTaskManager.getInstance().runLater(() -> {
193+
// null will not be saved at com/intellij/database/dataSource/url/ui/DynamicJdbcUrlEditor#storeProperties
194+
interchange.putProperty(KEY_DB_SERVER_ID, Optional.ofNullable(newServerId).orElse(NONE));
195+
if (Objects.isNull(server) || Objects.equals(oldServerId, newServerId)) {
196+
this.updating = false;
197+
return;
198+
}
199+
final String user = String.format("%s@%s", server.getAdminName(), server.getName());
201200
LocalDataSource.setUsername(interchange.getDataSource(), user);
202201
this.setUsername(user);
203-
this.setJdbcUrl(jdbcUrl.toString());
202+
this.setJdbcUrl(server.getJdbcUrl().toString());
204203
this.updating = false;
205204
}, AzureTask.Modality.ANY);
206205
}

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-database/src/main/java/com/microsoft/azure/toolkit/intellij/database/dbtools/OpenWithDatabaseToolsAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static void openDataSourceManagerDialog(IDatabaseServer<?> server, Projec
2525
final DataSourceDetector.Builder builder = registry.getBuilder()
2626
.withDriverClass(server.getJdbcUrl().getDefaultDriverClass())
2727
.withUrl(server.getJdbcUrl().toString())
28+
.withJdbcAdditionalProperty(DatabaseServerParamEditor.KEY_DB_SERVER_ID, server.getId())
2829
.withUser(String.format("%s@%s", server.getAdminName(), server.getName()))
2930
.withDbms(getDbms(server))
3031
.commit();

0 commit comments

Comments
 (0)