Example usage for com.mongodb ErrorCategory DUPLICATE_KEY

List of usage examples for com.mongodb ErrorCategory DUPLICATE_KEY

Introduction

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

Prototype

ErrorCategory DUPLICATE_KEY

To view the source code for com.mongodb ErrorCategory DUPLICATE_KEY.

Click Source Link

Document

A duplicate key error

Usage

From source file:alxpez.blog.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(getRandom().nextInt()));

    Document user = new Document();

    user.append("_id", username).append("password", passwordHash);

    if (email != null && !email.equals("")) {
        // the provided email address
        user.append("email", email);
    }/*from  ww  w .  j a va2s.  c  o m*/

    try {
        usersCollection.insertOne(user);
        return true;
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("Username already in use: " + username);
            return false;
        }
        throw e;
    }
}

From source file:blog.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt()));

    Document user = new Document();

    user.append("_id", username).append("password", passwordHash);

    if (email != null && !email.equals("")) {
        // the provided email address
        user.append("email", email);
    }//from   w ww .  j  a  v  a2s.c  o  m

    try {
        usersCollection.insertOne(user);
        return true;
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("Username already in use: " + username);
            return false;
        }
        throw e;
    }
}

From source file:com.bilko.dao.UserDao.java

License:Apache License

public boolean addUser(final String username, final String password, final String email) {
    final Document user = new Document().append("_id", username).append("password",
            makePasswordHash(password, Integer.toString(random.nextInt())));

    if (StringUtils.isNotEmpty(email)) {
        user.append("email", email);
    }/*from  www. j a v  a 2 s. co m*/

    try {
        collection.insertOne(user);
        return true;
    } catch (final MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("USERNAME ALREADY IN USE: " + username);
            return false;
        }
        throw e;
    }
}

From source file:com.enlightendev.mongodb.blog.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt()));

    // XXX WORK HERE
    // create an object suitable for insertion into the user collection
    // be sure to add username and hashed password to the document. problem instructions
    // will tell you the schema that the documents must follow.
    Document doc = new Document().append("username", username).append("password", passwordHash).append("_id",
            username);/* ww w .ja  v a  2  s.c  om*/

    if (email != null && !email.equals("")) {
        // XXX WORK HERE
        // if there is an email address specified, add it to the document too.
        doc.append("email", email);
    }

    try {
        // XXX WORK HERE
        // insert the document into the user collection here
        usersCollection.insertOne(doc);
        return true;
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("Username already in use: " + username);
            return false;
        }
        throw e;
    }
}

From source file:course.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt()));

    // XXX WORK HERE
    // create an object suitable for insertion into the user collection
    // be sure to add username and hashed password to the document. problem instructions
    // will tell you the schema that the documents must follow.
    Document user = new Document("_id", username).append("password", passwordHash);

    if (email != null && !email.equals("")) {
        // XXX WORK HERE
        // if there is an email address specified, add it to the document too.
        user.append("email", email);
    }//from w  w  w.  ja va 2 s  . co m

    try {
        // XXX WORK HERE
        // insert the document into the user collection here
        usersCollection.insertOne(user);
        return true;
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("Username already in use: " + username);
            return false;
        }
        throw e;
    }
}

From source file:course.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt()));
    Document user = new Document("_id", username).append("password", passwordHash);
    // XXX WORK HERE
    // create an object suitable for insertion into the user collection
    // be sure to add username and hashed password to the document. problem instructions
    // will tell you the schema that the documents must follow.

    if (email != null && !email.equals("")) {
        // XXX WORK HERE
        // if there is an email address specified, add it to the document too.
        user.append("email", email);
    }//from ww w.  j  a v a 2s .co  m

    try {
        // XXX WORK HERE
        // insert the document into the user collection here
        usersCollection.insertOne(user);
        return true;
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("Username already in use: " + username);
            return false;
        }
        throw e;
    }
}

From source file:edu.uniandes.ecos.codeaholics.config.AuthenticationJWT.java

/**
 * * Crea una sesion para un usuario dado su email.
 * /*from  w w w .jav a 2 s. c  o m*/
 * @param pEmail
 *            correo del usuario al que se le crea la sesion
 * @param pUserProfile
 *            perfil del usuario citizen, functionary, etc
 */
private static void createSession(String pEmail, String pProfile, String pSalt) {

    if (pProfile.equals(Constants.CITIZEN_USER_PROFILE)) {

        //TODO: this is not safe
        Document filter = new Document();
        filter.append("email", pEmail);
        ArrayList<Document> citizen = DataBaseUtil.find(filter, Constants.CITIZEN_COLLECTION);

        String citizenName = citizen.get(0).get("name").toString();
        String citizenLast = citizen.get(0).get("lastName1").toString();

        token = createJWT(pEmail, pProfile, pSalt, citizenName, citizenLast);

    } else {

        log.info("User is functionary: creating token for functionary");
        //TODO: this is not safe
        Document filter = new Document();
        filter.append("email", pEmail);
        ArrayList<Document> functionary = DataBaseUtil.find(filter, Constants.FUNCTIONARY_COLLECTION);

        String citizenName = functionary.get(0).get("name").toString();
        String citizenLast = functionary.get(0).get("lastName1").toString();
        String citizenMayoralty = functionary.get(0).get("mayoralty").toString();

        token = createJWT(pEmail, pProfile, pSalt, citizenName, citizenLast, citizenMayoralty);

    }

    Document session = new Document();
    session.append("email", pEmail);
    session.append("user-profile", pProfile);
    session.append("token", token);
    session.append("salt", pSalt);
    log.info("Creating Session...");

    try {

        hasSession(pEmail);

        DataBaseUtil.save(session, Constants.SESSION_COLLECTION);

    } catch (SessionAlreadyExistsException ssEx) {

        log.info(ssEx.getMessage() + " : " + pEmail);
        closeSession(pEmail);
        DataBaseUtil.save(session, Constants.SESSION_COLLECTION);

    } catch (MongoWriteException e) {

        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            log.info("Already exist session for user: " + pEmail);
        }
        throw e;
    }
}

From source file:ELK.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt()));

    // XXX WORK HERE
    // create an object suitable for insertion into the user collection
    // be sure to add username and hashed password to the document. problem instructions
    // will tell you the schema that the documents must follow.
    Document user = new Document("_id", username).append("password", passwordHash);

    if (email != null && !email.equals("")) {
        // XXX WORK HERE
        // if there is an email address specified, add it to the document too.
        user.append("email", email);
    }/*from w w  w . j  a  v  a 2  s  .co  m*/

    try {
        // XXX WORK HERE
        // insert the document into the user collection here
        usersCollection.insertOne(user);
        System.out.println("Added New User : username");
        return true;
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("Username already in use: " + username);
            return false;
        }
        throw e;
    }
}

From source file:io.lumeer.storage.mongodb.MongoDbStorage.java

License:Open Source License

@Override
public void createOldDocument(final String collectionName, final DataDocument dataDocument,
        final String documentId, final int version) throws UnsuccessfulOperationException {
    Document doc = new Document(dataDocument);
    doc.put(DOCUMENT_ID, new BasicDBObject(DOCUMENT_ID, new ObjectId(documentId)));
    try {/*  w  ww  .j  a v  a 2 s  .  c o  m*/
        database.getCollection(collectionName).insertOne(doc);
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            throw new UnsuccessfulOperationException(e.getMessage(), e.getCause());
        } else {
            throw e;
        }
    }
}

From source file:org.apache.rya.indexing.entity.storage.mongo.MongoEntityStorage.java

License:Apache License

@Override
public void create(final Entity entity) throws EntityStorageException {
    requireNonNull(entity);//from w w w  .  ja  v  a2  s.co  m

    try {
        final boolean hasDuplicate = detectDuplicates(entity);

        if (!hasDuplicate) {
            mongo.getDatabase(ryaInstanceName).getCollection(COLLECTION_NAME)
                    .insertOne(ENTITY_CONVERTER.toDocument(entity));
        } else {
            throw new EntityNearDuplicateException(
                    "Duplicate data found and will not be inserted for Entity with Subject: " + entity);
        }
    } catch (final MongoException e) {
        final ErrorCategory category = ErrorCategory.fromErrorCode(e.getCode());
        if (category == ErrorCategory.DUPLICATE_KEY) {
            throw new EntityAlreadyExistsException(
                    "Failed to create Entity with Subject '" + entity.getSubject().getData() + "'.", e);
        }
        throw new EntityStorageException(
                "Failed to create Entity with Subject '" + entity.getSubject().getData() + "'.", e);
    }
}