Example usage for com.mongodb.client MongoCollection findOneAndUpdate

List of usage examples for com.mongodb.client MongoCollection findOneAndUpdate

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection findOneAndUpdate.

Prototype

@Nullable
TDocument findOneAndUpdate(Bson filter, List<? extends Bson> update);

Source Link

Document

Atomically find a document and update it.

Usage

From source file:com.shampan.model.PageModel.java

public ResultEvent addPhotoCommentByReferenceId(String referenceId, String commentInfo) {
    try {/*from w  w w .  j  a  v  a 2 s  .c  o  m*/
        MongoCollection<PagePhotoDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.PAGEPHOTOS.toString(), PagePhotoDAO.class);
        BasicDBObject selectQuery = (BasicDBObject) QueryBuilder.start("referenceId").is(referenceId).get();
        Comment photoCommentInfo = Comment.getCommentInfo(commentInfo);
        if (photoCommentInfo != null) {
            photoCommentInfo.setCreatedOn(utility.getCurrentTime());
            mongoCollection.findOneAndUpdate(selectQuery,
                    new Document("$push", new Document("comment", JSON.parse(photoCommentInfo.toString()))));
            this.getResultEvent().setResponseCode(PropertyProvider.get("SUCCESSFUL_OPERATION"));
        }
    } catch (Exception ex) {
        this.getResultEvent().setResponseCode(PropertyProvider.get("ERROR_EXCEPTION"));
    }
    return this.resultEvent;
}

From source file:com.shampan.model.PageModel.java

public ResultEvent addPhotoComment(String photoId, String commentInfo) {
    try {//from   w w w.j a  v a 2s.  c  om
        MongoCollection<PagePhotoDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.PAGEPHOTOS.toString(), PagePhotoDAO.class);
        BasicDBObject selectQuery = (BasicDBObject) QueryBuilder.start("photoId").is(photoId).get();
        Comment photoCommentInfo = Comment.getCommentInfo(commentInfo);
        if (photoCommentInfo != null) {
            photoCommentInfo.setCreatedOn(utility.getCurrentTime());
            mongoCollection.findOneAndUpdate(selectQuery,
                    new Document("$push", new Document("comment", JSON.parse(photoCommentInfo.toString()))));
            this.getResultEvent().setResponseCode(PropertyProvider.get("SUCCESSFUL_OPERATION"));
        }
    } catch (Exception ex) {
        this.getResultEvent().setResponseCode(PropertyProvider.get("ERROR_EXCEPTION"));
    }
    return this.resultEvent;
}

From source file:com.shampan.model.UserModel.java

public ResultEvent updateLastLogin(String userId) {
    try {//  w  w  w . j  a  va2s  . c o m
        MongoCollection<UserDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.USERS.toString(), UserDAO.class);

        String attrUserId = PropertyProvider.get("USER_ID");
        String attrLastLogin = PropertyProvider.get("LAST_LOGIN");
        Document selectDocument = new Document();
        selectDocument.put(attrUserId, userId);
        Document updateDocument = new Document();
        updateDocument.put(attrLastLogin, utility.getCurrentTime());
        mongoCollection.findOneAndUpdate(selectDocument, new Document("$set", updateDocument));
        this.getResultEvent().setResponseCode(PropertyProvider.get("SUCCESSFUL_OPERATION"));
    } catch (Exception ex) {
        this.getResultEvent().setResponseCode(PropertyProvider.get("ERROR_EXCEPTION"));
    }
    return this.resultEvent;
}

From source file:com.shampan.model.UserModel.java

public ResultEvent rememberUser(String userId, String salt) {
    try {//from  w ww . j  a  va 2 s  . c om
        MongoCollection<UserDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.USERS.toString(), UserDAO.class);
        String attrUserId = PropertyProvider.get("USER_ID");
        String attrRememberCode = PropertyProvider.get("REMEMBER_CODE");
        Document selectDocument = new Document();
        selectDocument.put(attrUserId, userId);
        Document updateDocument = new Document();
        updateDocument.put(attrRememberCode, salt);
        mongoCollection.findOneAndUpdate(selectDocument, new Document("$set", updateDocument));
        this.getResultEvent().setResponseCode(PropertyProvider.get("SUCCESSFUL_OPERATION"));
    } catch (Exception ex) {
        this.getResultEvent().setResponseCode(PropertyProvider.get("ERROR_EXCEPTION"));
    }
    return this.resultEvent;
}

From source file:com.shampan.model.UserModel.java

public ResultEvent activeRegistration(String userId, String activationCode) {
    try {// www .  j  a  v a 2s  .c om
        MongoCollection<UserDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.USERS.toString(), UserDAO.class);
        String attrUserId = PropertyProvider.get("USER_ID");
        String attractiveCode = PropertyProvider.get("ACTIVATION_CODE");
        String attractive = PropertyProvider.get("ACTIVE");
        Document selectDocument = new Document();
        selectDocument.put(attrUserId, userId);
        selectDocument.put(attractiveCode, activationCode);
        Document updateDocument = new Document();
        updateDocument.put(attractiveCode, NULL);
        updateDocument.put(attractive, 1);
        mongoCollection.findOneAndUpdate(selectDocument, new Document("$set", updateDocument));
        this.getResultEvent().setResponseCode(PropertyProvider.get("SUCCESSFUL_OPERATION"));
    } catch (Exception ex) {
        this.getResultEvent().setResponseCode(PropertyProvider.get("ERROR_EXCEPTION"));
    }
    return this.resultEvent;
}

From source file:com.shampan.model.UserModel.java

public ResultEvent deactivateRegistration(String userId, String activationCode) {
    try {//from www .  j a v  a 2 s .c  o  m
        MongoCollection<UserDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.USERS.toString(), UserDAO.class);
        String attrUserId = PropertyProvider.get("USER_ID");
        String attractiveCode = PropertyProvider.get("ACTIVATION_CODE");
        String attractive = PropertyProvider.get("ACTIVE");
        Document selectDocument = new Document();
        selectDocument.put(attrUserId, userId);
        Document updateDocument = new Document();
        updateDocument.put(attractiveCode, activationCode);
        updateDocument.put(attractive, 0);
        mongoCollection.findOneAndUpdate(selectDocument, new Document("$set", updateDocument));
        this.getResultEvent().setResponseCode(PropertyProvider.get("SUCCESSFUL_OPERATION"));
    } catch (Exception ex) {
        this.getResultEvent().setResponseCode(PropertyProvider.get("ERROR_EXCEPTION"));
    }
    return this.resultEvent;
}

From source file:com.shampan.model.UserModel.java

public ResultEvent changePassword(String identity, String oldPassword, String newPassword) {
    try {/*from   ww w.  j ava 2  s  .c  om*/
        MongoCollection<UserDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.USERS.toString(), UserDAO.class);
        String attrEmail = PropertyProvider.get("EMAIL");
        String attrPassword = PropertyProvider.get("PASSWORD");
        String attrRememberCode = PropertyProvider.get("REMEMBER_CODE");
        Document selectDocument = new Document();
        selectDocument.put(attrEmail, identity);
        selectDocument.put(attrPassword, oldPassword);
        Document updateDocument = new Document();
        updateDocument.put(attrPassword, newPassword);
        updateDocument.put(attrRememberCode, NULL);
        mongoCollection.findOneAndUpdate(selectDocument, new Document("$set", updateDocument));
        this.getResultEvent().setResponseCode(PropertyProvider.get("SUCCESSFUL_OPERATION"));
    } catch (Exception ex) {
        this.getResultEvent().setResponseCode(PropertyProvider.get("ERROR_EXCEPTION"));
    }
    return this.resultEvent;
}

From source file:com.shampan.model.UserModel.java

public ResultEvent clearForgottenPasswordCode(String forgottenPasswordCode) {
    try {//from  w w w .j av a 2  s.  com
        MongoCollection<UserDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.USERS.toString(), UserDAO.class);

        Document selectDocument = new Document();
        selectDocument.put("forgottenPasswordCode", forgottenPasswordCode);
        Document updateDocument = new Document();
        updateDocument.put("forgottenPasswordCode", NULL);
        updateDocument.put("forgottenPasswordTime", NULL);
        mongoCollection.findOneAndUpdate(selectDocument, new Document("$set", updateDocument));
        this.getResultEvent().setResponseCode(PropertyProvider.get("SUCCESSFUL_OPERATION"));
    } catch (Exception ex) {
        this.getResultEvent().setResponseCode(PropertyProvider.get("ERROR_EXCEPTION"));
    }
    return this.resultEvent;
}

From source file:com.shampan.model.UserModel.java

public ResultEvent resetPassword(String identity, String newPassword) {
    try {/*  w w w  . j  av a2s .c  o  m*/
        MongoCollection<UserDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.USERS.toString(), UserDAO.class);
        String attrEmail = PropertyProvider.get("EMAIL");
        String attrPassword = PropertyProvider.get("PASSWORD");
        String attrForgottenPasswordCode = PropertyProvider.get("FORGOTTEN_PASSWORD_CODE");
        String attrForgottenPasswordTime = PropertyProvider.get("FORGOTTEN_PASSWORD_TIME");
        String attrRememberCode = PropertyProvider.get("REMEMBER_CODE");
        Document selectDocument = new Document();
        selectDocument.put(attrEmail, identity);
        Document updateDocument = new Document();
        updateDocument.put(attrPassword, newPassword);
        updateDocument.put(attrForgottenPasswordCode, NULL);
        updateDocument.put(attrForgottenPasswordTime, NULL);
        updateDocument.put(attrRememberCode, NULL);
        mongoCollection.findOneAndUpdate(selectDocument, new Document("$set", updateDocument));

        this.getResultEvent().setResponseCode(PropertyProvider.get("SUCCESSFUL_OPERATION"));
    } catch (Exception ex) {
        this.getResultEvent().setResponseCode(PropertyProvider.get("ERROR_EXCEPTION"));
    }
    return this.resultEvent;
}

From source file:com.shampan.model.UserModel.java

public ResultEvent forgottenPassword(String identity) {
    try {//from  w  ww  .  j  a  va  2s.c  o m
        MongoCollection<UserDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.USERS.toString(), UserDAO.class);
        String attrEmail = PropertyProvider.get("EMAIL");
        String attrForgottenPasswordCode = PropertyProvider.get("FORGOTTEN_PASSWORD_CODE");
        String attrForgottenPasswordTime = PropertyProvider.get("FORGOTTEN_PASSWORD_TIME");
        Document selectDocument = new Document();
        selectDocument.put(attrEmail, identity);
        Document updateDocument = new Document();
        updateDocument.put(attrForgottenPasswordCode, NULL);
        updateDocument.put(attrForgottenPasswordTime, utility.getCurrentTime());
        mongoCollection.findOneAndUpdate(selectDocument, new Document("$set", updateDocument));
        this.getResultEvent().setResponseCode(PropertyProvider.get("SUCCESSFUL_OPERATION"));
    } catch (Exception ex) {
        this.getResultEvent().setResponseCode(PropertyProvider.get("ERROR_EXCEPTION"));
    }
    return this.resultEvent;
}