package com.mongodb.operation;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.binding.AsyncConnectionSource;
import com.mongodb.binding.AsyncReadBinding;
import com.mongodb.binding.ConnectionSource;
import com.mongodb.binding.ReadBinding;
import com.mongodb.connection.AsyncConnection;
import com.mongodb.connection.Connection;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.connection.ServerDescription;
import com.mongodb.operation.CommandOperationHelper.CommandReadTransformer;
import com.mongodb.operation.CommandOperationHelper.CommandReadTransformerAsync;
import org.bson.BsonDocument;
import org.bson.BsonString;
import org.bson.codecs.BsonDocumentCodec;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
import static com.mongodb.operation.CommandOperationHelper.CommandCreator;
import static com.mongodb.operation.CommandOperationHelper.executeCommand;
import static com.mongodb.operation.CommandOperationHelper.executeCommandAsync;
import static com.mongodb.operation.OperationHelper.CallableWithConnection;
import static com.mongodb.operation.OperationHelper.LOGGER;
import static com.mongodb.operation.OperationHelper.withConnection;
@Deprecated
public class UserExistsOperation implements AsyncReadOperation<Boolean>, ReadOperation<Boolean> {
private final String databaseName;
private final String userName;
private boolean retryReads;
public UserExistsOperation(final String databaseName, final String userName) {
this.databaseName = notNull("databaseName", databaseName);
this.userName = notNull("userName", userName);
}
public UserExistsOperation retryReads(final boolean retryReads) {
this.retryReads = retryReads;
return this;
}
public boolean getRetryReads() {
return retryReads;
}
@Override
public Boolean execute(final ReadBinding binding) {
return withConnection(binding, new CallableWithConnection<Boolean>() {
@Override
public Boolean call(final Connection connection) {
return executeCommand(binding, databaseName, getCommandCreator(), transformer(), retryReads);
}
});
}
@Override
public void executeAsync(final AsyncReadBinding binding, final SingleResultCallback<Boolean> callback) {
executeCommandAsync(binding, databaseName, getCommandCreator(), new BsonDocumentCodec(),
asyncTransformer(), retryReads, errorHandlingCallback(callback, LOGGER));
}
private CommandReadTransformer<BsonDocument, Boolean> transformer() {
return new CommandReadTransformer<BsonDocument, Boolean>() {
@Override
public Boolean apply(final BsonDocument result, final ConnectionSource source, final Connection connection) {
return result.get("users").isArray() && !result.getArray("users").isEmpty();
}
};
}
private CommandReadTransformerAsync<BsonDocument, Boolean> asyncTransformer() {
return new CommandReadTransformerAsync<BsonDocument, Boolean>() {
@Override
public Boolean apply(final BsonDocument result, final AsyncConnectionSource source, final AsyncConnection connection) {
return result.get("users").isArray() && !result.getArray("users").isEmpty();
}
};
}
private CommandCreator getCommandCreator() {
return new CommandCreator() {
@Override
public BsonDocument create(final ServerDescription serverDescription, final ConnectionDescription connectionDescription) {
return getCommand();
}
};
}
private BsonDocument getCommand() {
return new BsonDocument("usersInfo", new BsonString(userName));
}
}