Example usage for com.mongodb MongoNamespace MongoNamespace

List of usage examples for com.mongodb MongoNamespace MongoNamespace

Introduction

In this page you can find the example usage for com.mongodb MongoNamespace MongoNamespace.

Prototype

public MongoNamespace(final String fullName) 

Source Link

Document

Construct an instance for the given full name.

Usage

From source file:com.imaginea.mongodb.services.impl.CollectionServiceImpl.java

License:Apache License

/**
 * Creates a collection inside a database in mongo to which user is connected to.
 *
 * @param dbName Name of Database in which to insert a collection
 * @param selectedCollectionName Collection on which the operation is performed
 * @param newCollName Name of Collection to be added/renamed to
 * @param capped Specify if the collection is capped
 * @param size Specify the size of collection
 * @param maxDocs specify maximum no of documents in the collection
 * @return Success if Insertion is successful else throw exception
 * @throws DatabaseException throw super type of UndefinedDatabaseException
 * @throws ValidationException throw super type of
 *         EmptyDatabaseNameException,EmptyCollectionNameException
 * @throws CollectionException throw super type of
 *         DuplicateCollectionException,InsertCollectionException
 *//*from   ww  w . java2s  . c o m*/
public String updateCollection(String dbName, String selectedCollectionName, String newCollName, boolean capped,
        long size, int maxDocs, boolean autoIndexId)
        throws DatabaseException, CollectionException, ValidationException {

    if (dbName == null || dbName.equals("")) {
        throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Invalid Database name");
    }

    if (selectedCollectionName == null || newCollName == null) {
        throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Collection name should be provided");
    }
    if (selectedCollectionName.equals("") || newCollName.equals("")) {
        throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Collection name cannot be empty");
    }
    String result = "No updates were specified!";
    try {
        // if (!databaseService.getDbList().contains(dbName)) {
        //   throw new DatabaseException(ErrorCodes.DB_DOES_NOT_EXISTS,
        //       "Db with name [" + dbName + "] doesn't exist.");
        // }

        boolean convertedToCapped = false, convertedToNormal = false, renamed = false;

        MongoDatabase db = mongoInstance.getDatabase(dbName);
        MongoCollection<Document> selectedCollection = db.getCollection(selectedCollectionName);
        CreateCollectionOptions options = new CreateCollectionOptions();
        options.capped(capped);
        if (capped) {
            options.maxDocuments(maxDocs);
            options.autoIndex(autoIndexId);
            options.sizeInBytes(size);
            createCollection(options, selectedCollection, selectedCollectionName, db);
            convertedToCapped = true;
        } else {
            createCollection(options, selectedCollection, selectedCollectionName, db);
            convertedToNormal = true;
        }

        if (!selectedCollectionName.equals(newCollName)) {
            if (getCollList(dbName).contains(newCollName)) {
                throw new CollectionException(ErrorCodes.COLLECTION_ALREADY_EXISTS,
                        "Collection [" + newCollName + "] already exists in Database [" + dbName + "]");
            }
            selectedCollection = db.getCollection(selectedCollectionName);

            MongoNamespace mongoNamespace = new MongoNamespace(dbName + "." + newCollName);

            selectedCollection.renameCollection(mongoNamespace);
            renamed = true;
        }
        if ((convertedToNormal || convertedToCapped) && renamed) {
            result = "Collection [" + selectedCollectionName + "] was successfully updated.";
        } else if (convertedToCapped) {
            result = "Collection [" + selectedCollectionName
                    + "] was successfully converted to capped collection";
        } else if (convertedToNormal) {
            result = "Capped Collection [" + selectedCollectionName
                    + "] was successfully converted to normal collection";
        } else if (renamed) {
            result = "Collection [" + selectedCollectionName + "] was successfully renamed to '" + newCollName
                    + "'";
        }
    } catch (MongoException m) {
        throw new CollectionException(ErrorCodes.COLLECTION_UPDATE_EXCEPTION, m.getMessage());
    }
    return result;
}