package com.mongodb.operation;
import com.mongodb.MongoClientException;
import com.mongodb.WriteConcern;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.binding.AsyncWriteBinding;
import com.mongodb.binding.WriteBinding;
import com.mongodb.client.model.Collation;
import com.mongodb.connection.AsyncConnection;
import com.mongodb.connection.Connection;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.operation.OperationHelper.AsyncCallableWithConnection;
import com.mongodb.operation.OperationHelper.CallableWithConnection;
import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonString;
import java.util.List;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
import static com.mongodb.operation.CommandOperationHelper.executeCommand;
import static com.mongodb.operation.CommandOperationHelper.executeCommandAsync;
import static com.mongodb.operation.CommandOperationHelper.writeConcernErrorWriteTransformer;
import static com.mongodb.operation.OperationHelper.LOGGER;
import static com.mongodb.operation.OperationHelper.releasingCallback;
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotFour;
import static com.mongodb.operation.OperationHelper.withConnection;
import static com.mongodb.internal.operation.WriteConcernHelper.appendWriteConcernToCommand;
import static com.mongodb.operation.CommandOperationHelper.writeConcernErrorTransformer;
import static com.mongodb.operation.OperationHelper.withAsyncConnection;
@Deprecated
public class CreateViewOperation implements AsyncWriteOperation<Void>, WriteOperation<Void> {
private final String databaseName;
private final String viewName;
private final String viewOn;
private final List<BsonDocument> pipeline;
private final WriteConcern writeConcern;
private Collation collation;
public CreateViewOperation(final String databaseName, final String viewName, final String viewOn, final List<BsonDocument> pipeline,
final WriteConcern writeConcern) {
this.databaseName = notNull("databaseName", databaseName);
this.viewName = notNull("viewName", viewName);
this.viewOn = notNull("viewOn", viewOn);
this.pipeline = notNull("pipeline", pipeline);
this.writeConcern = notNull("writeConcern", writeConcern);
}
public String getDatabaseName() {
return databaseName;
}
public String getViewName() {
return viewName;
}
public String getViewOn() {
return viewOn;
}
public List<BsonDocument> getPipeline() {
return pipeline;
}
public WriteConcern getWriteConcern() {
return writeConcern;
}
public Collation getCollation() {
return collation;
}
public CreateViewOperation collation(final Collation collation) {
this.collation = collation;
return this;
}
@Override
public Void execute(final WriteBinding binding) {
return withConnection(binding, new CallableWithConnection<Void>() {
@Override
public Void call(final Connection connection) {
if (!serverIsAtLeastVersionThreeDotFour(connection.getDescription())) {
throw createExceptionForIncompatibleServerVersion();
}
executeCommand(binding, databaseName, getCommand(connection.getDescription()), writeConcernErrorTransformer());
return null;
}
});
}
@Override
public void executeAsync(final AsyncWriteBinding binding, final SingleResultCallback<Void> callback) {
withAsyncConnection(binding, new AsyncCallableWithConnection() {
@Override
public void call(final AsyncConnection connection, final Throwable t) {
SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
if (t != null) {
errHandlingCallback.onResult(null, t);
}
else {
SingleResultCallback<Void> wrappedCallback = releasingCallback(errHandlingCallback, connection);
if (!serverIsAtLeastVersionThreeDotFour(connection.getDescription())) {
wrappedCallback.onResult(null, createExceptionForIncompatibleServerVersion());
}
executeCommandAsync(binding, databaseName, getCommand(connection.getDescription()),
connection, writeConcernErrorWriteTransformer(), wrappedCallback);
}
}
});
}
private BsonDocument getCommand(final ConnectionDescription description) {
BsonDocument commandDocument = new BsonDocument("create", new BsonString(viewName))
.append("viewOn", new BsonString(viewOn))
.append("pipeline", new BsonArray(pipeline));
if (collation != null) {
commandDocument.put("collation", collation.asDocument());
}
appendWriteConcernToCommand(writeConcern, commandDocument, description);
return commandDocument;
}
private MongoClientException createExceptionForIncompatibleServerVersion() {
return new MongoClientException("Can not create view. The minimum server version that supports view creation is 3.4");
}
}