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 updatePage(String pageInfo) {
    try {/*from www.  ja  v a  2s  .c o m*/
        MongoCollection<PageDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.PAGES.toString(), PageDAO.class);
        PageDAO pageInformation = new PageDAOBuilder().build(pageInfo);
        if (pageInformation != null) {
            Document selectDocument = new Document();
            selectDocument.put("pageId", pageInformation.getPageId());
            selectDocument.put("referenceId", pageInformation.getReferenceId());
            Document updatedDocument = new Document();
            updatedDocument.put("about", pageInformation.getAbout());
            updatedDocument.put("interestedAgeRange",
                    JSON.parse(pageInformation.getInterestedAgeRange().toString()));
            updatedDocument.put("intertestedGender", pageInformation.getIntertestedGender());
            updatedDocument.put("referenceInfo", JSON.parse(pageInformation.getReferenceInfo().toString()));
            PageDAO result1 = mongoCollection.findOneAndUpdate(selectDocument,
                    new Document("$set", updatedDocument));
            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 addPageMember(String pageId, String memberInfo) {
    try {/*  w  w w .  j  a va  2 s .  co  m*/
        MongoCollection<PageMemberDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.PAGEMEMBERS.toString(), PageMemberDAO.class);
        Document selectDocument = new Document();
        selectDocument.put("pageId", pageId);
        MemberInfo mInfo = MemberInfo.getMemberInfo(memberInfo);
        if (mInfo != null) {
            PageMemberDAO pageInfo = mongoCollection.find(selectDocument).first();
            boolean userIsExist = false;
            if (pageInfo != null) {
                List<MemberInfo> memberList = pageInfo.getMemberList();
                List<MemberInfo> tempMemberList = new ArrayList<>();
                if (memberList.size() > 0) {
                    for (int i = 0; i < memberList.size(); i++) {
                        MemberInfo tempMemberInfo = memberList.get(i);
                        if (tempMemberInfo.getUserId().equals(mInfo.getUserId())) {
                            tempMemberInfo
                                    .setRelationTypeId(PropertyProvider.get("PAGE_MEMBER_STATUS_ID_LIKED"));
                            userIsExist = true;
                        }
                        tempMemberList.add(tempMemberInfo);
                    }
                }
                if (userIsExist != false) {
                    mongoCollection.findOneAndUpdate(selectDocument, new Document("$set",
                            new Document("memberList", JSON.parse(tempMemberList.toString()))));
                } else {
                    mongoCollection.findOneAndUpdate(selectDocument,
                            new Document("$push", new Document("memberList", JSON.parse(mInfo.toString()))));
                }
            } else {
                List<MemberInfo> mList = new ArrayList<MemberInfo>();
                mList.add(mInfo);
                PageMemberDAO pageMember = new PageMemberDAO();
                pageMember.setPageId(pageId);
                pageMember.setMemberList(mList);
                mongoCollection.insertOne(pageMember);
            }
            this.getResultEvent().setResponseCode(PropertyProvider.get("SUCCESSFUL_OPERATION"));
        } else {
            this.getResultEvent().setResponseCode(PropertyProvider.get("NULL_POINTER_EXCEPTION"));
        }
    } catch (Exception ex) {
        this.getResultEvent().setResponseCode(PropertyProvider.get("ERROR_EXCEPTION"));
    }
    return this.resultEvent;

}

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

public ResultEvent leavePageMemberShip(String pageId, String userId) {
    try {/*from w w  w  .j  a v a 2s .  co m*/
        MongoCollection<PageMemberDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.PAGEMEMBERS.toString(), PageMemberDAO.class);
        Document selectionDocument = new Document();
        selectionDocument.put("pageId", pageId);
        selectionDocument.put("memberList.userId", userId);
        Document removedDocument = new Document();
        removedDocument.put("userId", userId);
        mongoCollection.findOneAndUpdate(selectionDocument,
                new Document("$pull", new Document("memberList", removedDocument)));
        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 editAlbumTotalImg(String pageId, String albumId, int totalImgInfo) {
    try {//from  w w  w.  ja v a2 s.  co  m
        MongoCollection<PageAlbumDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.PAGEALBUMS.toString(), PageAlbumDAO.class);
        Document selectionDocument = new Document();
        selectionDocument.put("pageId", pageId);
        selectionDocument.put("albumId", albumId);
        PageAlbumDAO result = mongoCollection.findOneAndUpdate(selectionDocument,
                new Document("$set", new Document("totalImg", totalImgInfo)));
        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 editAlbum(String pageId, String albumId, String albumInfo) {
    try {/*from   w  w  w.j a va 2 s. c o m*/
        MongoCollection<PageAlbumDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.PAGEALBUMS.toString(), PageAlbumDAO.class);

        Document selectionDocument = new Document();
        selectionDocument.put("pageId", pageId);
        selectionDocument.put("albumId", albumId);
        PageAlbumDAO albumInfoObj = new PageAlbumDAOBuilder().build(albumInfo);
        Document modifiedQuery = new Document();
        modifiedQuery.put("photoId", albumInfoObj.getPhotoId());
        modifiedQuery.put("defaultImg", albumInfoObj.getDefaultImg());
        modifiedQuery.put("totalImg", albumInfoObj.getTotalImg());
        modifiedQuery.put("referenceId", albumInfoObj.getReferenceId());
        PageAlbumDAO result = mongoCollection.findOneAndUpdate(selectionDocument,
                new Document("$set", modifiedQuery));
        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 addAlbumLikeByReferenceId(String referenceId, String likeInfo) {
    try {/*  ww w .j ava  2  s  .c om*/
        MongoCollection<PageAlbumDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.PAGEALBUMS.toString(), PageAlbumDAO.class);
        String attrReferenceId = PropertyProvider.get("REFERENCE_ID");
        String attrLike = PropertyProvider.get("LIKE");
        BasicDBObject selectQuery = (BasicDBObject) QueryBuilder.start(attrReferenceId).is(referenceId).get();
        Like albumlikeInfo = Like.getLikeInfo(likeInfo);
        if (albumlikeInfo != null) {
            mongoCollection.findOneAndUpdate(selectQuery,
                    new Document("$push", new Document(attrLike, JSON.parse(albumlikeInfo.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 addAlbumCommentByReferenceId(String referenceId, String commentInfo) {
    try {//  ww  w. j  a v  a  2 s  .  c o  m
        MongoCollection<PagePhotoDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.PAGEALBUMS.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 addPhotoLikeByReferenceId(String referenceId, String likeInfo) {
    try {/*from  w ww . java2 s . c  o  m*/
        MongoCollection<PagePhotoDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.PAGEPHOTOS.toString(), PagePhotoDAO.class);
        String attrReferenceId = PropertyProvider.get("REFERENCE_ID");
        String attrLike = PropertyProvider.get("LIKE");
        BasicDBObject selectQuery = (BasicDBObject) QueryBuilder.start(attrReferenceId).is(referenceId).get();
        Like photoLikeInfo = Like.getLikeInfo(likeInfo);
        if (photoLikeInfo != null) {
            mongoCollection.findOneAndUpdate(selectQuery,
                    new Document("$push", new Document(attrLike, JSON.parse(photoLikeInfo.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 addPhotoLike(String photoId, String likeInfo) {
    try {//ww  w.ja v  a  2 s . co  m
        MongoCollection<PagePhotoDAO> mongoCollection = DBConnection.getInstance().getConnection()
                .getCollection(Collections.PAGEPHOTOS.toString(), PagePhotoDAO.class);
        BasicDBObject selectQuery = (BasicDBObject) QueryBuilder.start("photoId").is(photoId).get();
        Like photoLikeInfo = Like.getLikeInfo(likeInfo);
        if (photoLikeInfo != null) {
            mongoCollection.findOneAndUpdate(selectQuery,
                    new Document("$push", new Document("like", JSON.parse(photoLikeInfo.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 addMPhotoLike(String userId, String photoId, String likeInfo) {
    try {/*from ww  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("photoId").is(photoId).get();
        Like photoLikeInfo = Like.getLikeInfo(likeInfo);
        if (photoLikeInfo != null) {
            mongoCollection.findOneAndUpdate(selectQuery,
                    new Document("$set", new Document("modifiedOn", utility.getCurrentTime())));
            mongoCollection.findOneAndUpdate(selectQuery,
                    new Document("$push", new Document("like", JSON.parse(photoLikeInfo.toString()))));
            UserInfo userInfo = photoLikeInfo.getUserInfo();
            notificationModel.addGeneralNotificationPagePhotoLike(userId, photoId, userInfo.toString());
            this.getResultEvent().setResponseCode(PropertyProvider.get("SUCCESSFUL_OPERATION"));
        }
    } catch (Exception ex) {
        this.getResultEvent().setResponseCode(PropertyProvider.get("ERROR_EXCEPTION"));
    }
    return this.resultEvent;
}