use of com.mongodb.client.model.ValidationOptions in project mongo-java-driver by mongodb.
the class MongoDatabaseImpl method createCollection.
@Override
@SuppressWarnings("deprecation")
public void createCollection(final String collectionName, final CreateCollectionOptions createCollectionOptions, final SingleResultCallback<Void> callback) {
CreateCollectionOperation operation = new CreateCollectionOperation(name, collectionName, writeConcern).capped(createCollectionOptions.isCapped()).sizeInBytes(createCollectionOptions.getSizeInBytes()).autoIndex(createCollectionOptions.isAutoIndex()).maxDocuments(createCollectionOptions.getMaxDocuments()).usePowerOf2Sizes(createCollectionOptions.isUsePowerOf2Sizes()).storageEngineOptions(toBsonDocument(createCollectionOptions.getStorageEngineOptions())).collation(createCollectionOptions.getCollation());
IndexOptionDefaults indexOptionDefaults = createCollectionOptions.getIndexOptionDefaults();
if (indexOptionDefaults.getStorageEngine() != null) {
operation.indexOptionDefaults(new BsonDocument("storageEngine", toBsonDocument(indexOptionDefaults.getStorageEngine())));
}
ValidationOptions validationOptions = createCollectionOptions.getValidationOptions();
if (validationOptions.getValidator() != null) {
operation.validator(toBsonDocument(validationOptions.getValidator()));
}
if (validationOptions.getValidationLevel() != null) {
operation.validationLevel(validationOptions.getValidationLevel());
}
if (validationOptions.getValidationAction() != null) {
operation.validationAction(validationOptions.getValidationAction());
}
executor.execute(operation, callback);
}
use of com.mongodb.client.model.ValidationOptions in project mongo-java-driver by mongodb.
the class CollectionHelper method create.
public void create(final String collectionName, final CreateCollectionOptions options) {
drop(namespace);
CreateCollectionOperation operation = new CreateCollectionOperation(namespace.getDatabaseName(), collectionName, WriteConcern.ACKNOWLEDGED).capped(options.isCapped()).sizeInBytes(options.getSizeInBytes()).autoIndex(options.isAutoIndex()).maxDocuments(options.getMaxDocuments());
IndexOptionDefaults indexOptionDefaults = options.getIndexOptionDefaults();
if (indexOptionDefaults.getStorageEngine() != null) {
operation.indexOptionDefaults(new BsonDocument("storageEngine", toBsonDocument(indexOptionDefaults.getStorageEngine())));
}
ValidationOptions validationOptions = options.getValidationOptions();
if (validationOptions.getValidator() != null) {
operation.validator(toBsonDocument(validationOptions.getValidator()));
}
if (validationOptions.getValidationLevel() != null) {
operation.validationLevel(validationOptions.getValidationLevel());
}
if (validationOptions.getValidationAction() != null) {
operation.validationAction(validationOptions.getValidationAction());
}
operation.execute(getBinding());
}
use of com.mongodb.client.model.ValidationOptions in project mongo-java-driver by mongodb.
the class MongoDatabaseImpl method createCollection.
@Override
@SuppressWarnings("deprecation")
public void createCollection(final String collectionName, final CreateCollectionOptions createCollectionOptions) {
CreateCollectionOperation operation = new CreateCollectionOperation(name, collectionName, writeConcern).collation(createCollectionOptions.getCollation()).capped(createCollectionOptions.isCapped()).sizeInBytes(createCollectionOptions.getSizeInBytes()).autoIndex(createCollectionOptions.isAutoIndex()).maxDocuments(createCollectionOptions.getMaxDocuments()).usePowerOf2Sizes(createCollectionOptions.isUsePowerOf2Sizes()).storageEngineOptions(toBsonDocument(createCollectionOptions.getStorageEngineOptions()));
IndexOptionDefaults indexOptionDefaults = createCollectionOptions.getIndexOptionDefaults();
if (indexOptionDefaults.getStorageEngine() != null) {
operation.indexOptionDefaults(new BsonDocument("storageEngine", toBsonDocument(indexOptionDefaults.getStorageEngine())));
}
ValidationOptions validationOptions = createCollectionOptions.getValidationOptions();
if (validationOptions.getValidator() != null) {
operation.validator(toBsonDocument(validationOptions.getValidator()));
}
if (validationOptions.getValidationLevel() != null) {
operation.validationLevel(validationOptions.getValidationLevel());
}
if (validationOptions.getValidationAction() != null) {
operation.validationAction(validationOptions.getValidationAction());
}
executor.execute(operation);
}
use of com.mongodb.client.model.ValidationOptions in project morphia by mongodb.
the class DatastoreImpl method process.
void process(final MappedClass mc, final Validation validation) {
if (validation != null) {
String collectionName = mc.getCollectionName();
CommandResult result = getDB().command(new BasicDBObject("collMod", collectionName).append("validator", parse(validation.value())).append("validationLevel", validation.level().getValue()).append("validationAction", validation.action().getValue()));
if (!result.ok()) {
if (result.getInt("code") == 26) {
ValidationOptions options = new ValidationOptions().validator(parse(validation.value())).validationLevel(validation.level()).validationAction(validation.action());
getDatabase().createCollection(collectionName, new CreateCollectionOptions().validationOptions(options));
} else {
result.throwOnError();
}
}
}
}
use of com.mongodb.client.model.ValidationOptions in project morphia by mongodb.
the class TestDocumentValidation method addValidation.
private MongoDatabase addValidation(final Document validator, final String collectionName) {
ValidationOptions options = new ValidationOptions().validator(validator).validationLevel(ValidationLevel.MODERATE).validationAction(ValidationAction.ERROR);
MongoDatabase database = getMongoClient().getDatabase(TEST_DB_NAME);
database.getCollection(collectionName).drop();
database.createCollection(collectionName, new CreateCollectionOptions().validationOptions(options));
return database;
}
Aggregations