Example usage for com.mongodb BasicDBObject append

List of usage examples for com.mongodb BasicDBObject append

Introduction

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

Prototype

@Override
public BasicDBObject append(final String key, final Object val) 

Source Link

Document

Add a key/value pair to this object

Usage

From source file:co.edu.uniandes.csw.Arquidalgos.usuario.persistence._UsuarioPersistence.java

License:MIT License

public void deleteUsuarioNoSql(String id) {

    DBCollection coll = null;//from   w  w w  . j  ava  2s .  c  o m
    BasicDBObject doc = new BasicDBObject();

    coll = db.getCollection("UsuarioDTO");

    doc.append("facebookId", id);
    coll.remove(doc);
}

From source file:com.aankam.servlet.MongoCrudServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*  w  w  w  . j  a  v  a  2  s  .c  om*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    MongoClientURI uri = new MongoClientURI("mongodb://Username:Pasword@ds061721.mongolab.com:61721/cs612");
    MongoClient client = new MongoClient(uri);
    DB db = client.getDB(uri.getDatabase());

    response.setContentType("text/html;charset=UTF-8");

    String serialNo = request.getParameter("serialNo");
    String name = request.getParameter("name");
    String emailId = request.getParameter("emailId");
    String operation = request.getParameter("operation");

    BasicDBObject customer;
    BasicDBObject updateQuery;
    BasicDBObject deleteQuery;
    BasicDBObject searchQuery;
    DBCollection customersCollection;
    customersCollection = db.getCollection("Customers");
    DBObject searchResult = null;
    boolean flag = false;
    DBCursor cursor;
    switch (operation) {
    case "create":
        customer = new BasicDBObject();
        customer.put("serialNo", serialNo);
        customer.put("name", name);
        customer.put("emailId", emailId);
        customersCollection.insert(customer);
        break;
    case "update":
        updateQuery = new BasicDBObject();
        updateQuery.append("serialNo", serialNo);
        cursor = customersCollection.find(updateQuery);
        customer = new BasicDBObject();
        customer.put("serialNo", serialNo);
        customer.put("name", name);
        customer.put("emailId", emailId);
        if (cursor.hasNext()) {
            customersCollection.update(updateQuery, customer);
            flag = true;
        }

        break;
    case "delete":
        deleteQuery = new BasicDBObject();
        deleteQuery.append("serialNo", serialNo);
        customersCollection.remove(deleteQuery);
        break;
    case "search":
        searchQuery = new BasicDBObject();
        searchQuery.append("serialNo", serialNo);
        cursor = customersCollection.find(searchQuery);
        while (cursor.hasNext()) {
            searchResult = cursor.next();
        }
        break;
    default:
        break;

    }
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet MongoCrudServlet</title>");
        out.println("</head>");
        out.println("<body>");

        switch (operation) {
        case "create":
            out.println("<h3>Customer was successfully created.</h3>");
            break;
        case "update":
            if (flag == true) {
                out.println("<h3>Customer was successfully updated.</h3>");
            } else {
                out.println("<h3>Customer was not found to update.</h3>");
            }

            break;
        case "delete":
            out.println("<h3>Customer was successfully deleted.</h3>");
            break;
        case "search":
            if (searchResult != null) {
                out.println("<h3>The following customer was found:</h3>");
                out.println("<h4>Serial No :" + searchResult.get("serialNo") + "</h4>");
                out.println("<h4>Name :" + searchResult.get("name") + "</h4>");
                out.println("<h4>Email Id :" + searchResult.get("emailId") + "</h4>");
            } else {
                out.println("<h3>Customer not found.</h3>");
            }
            break;
        default:
            break;
        }
        out.println("<a href=\"index.html\">Back to Main Form</a>");
        out.println("</body>");
        out.println("</html>");
    }
}

From source file:com.affairs.dao.zaffar.CurrentAffairsDAO.java

License:Apache License

public boolean addSayDoProject(String username, Map sayDoProjectMap) {

    boolean insertSuccess = true;
    BasicDBObject projectObj = new BasicDBObject(sayDoProjectMap);
    projectObj.append("isCurrent", true).append("delInd", false);
    projectObj.append("createdBy", username).append("updatedBy", username);
    projectObj.append("createdDate", new Date()).append("updatedDate", new Date());

    WriteResult insertResult = null;/* w  ww  .  j  av  a 2s  . com*/

    try {
        insertResult = this.saydoCollection.insert(projectObj);
    } catch (Exception exp) {
        System.out.println(exp.getMessage());
    }

    if (insertResult.getLastError() != null) {
        System.out.println("Insert did not happen properly");
        insertSuccess = false;
    }

    return insertSuccess;
}

From source file:com.affairs.dao.zaffar.CurrentAffairsDAO.java

License:Apache License

public boolean updateSayDoProject(String username, Map whereConditionMap, Map updateConditionMap) {

    boolean updateSuccess = true;

    BasicDBObject whereConditionObj = new BasicDBObject(whereConditionMap);
    BasicDBObject updateConditionObj = new BasicDBObject(updateConditionMap);

    updateConditionObj.append("updatedBy", username);
    updateConditionObj.append("updatedDate", new Date());

    WriteResult updateResult = null;/*from www  .  j  a  v a  2 s.  com*/

    try {
        updateResult = this.saydoCollection.update(whereConditionObj,
                new BasicDBObject("$set", updateConditionObj));
    } catch (Exception exp) {
        System.out.println(exp.getMessage());
    }

    if (updateResult.getLastError() != null) {
        System.out.println("Update did not happen properly");
        updateSuccess = false;
    }

    return updateSuccess;
}

From source file:com.affairs.dao.zaffar.CurrentAffairsDAO.java

License:Apache License

public boolean upsertSayDoProject(String username, Map whereConditionMap, Map updateConditionMap) {

    boolean upsertSuccess = true;
    BasicDBObject whereConditionObj = new BasicDBObject(whereConditionMap);
    BasicDBObject updateConditionObj = new BasicDBObject(updateConditionMap);

    BasicDBObject updateCurrentObj = new BasicDBObject("isCurrent", false);
    updateCurrentObj.append("updatedBy", username);
    updateCurrentObj.append("updatedDate", new Date());

    BasicDBObject currentProjectObj = (BasicDBObject) findOneObjectByCondition(whereConditionObj);

    WriteResult updateResult = null;/* w  ww  . java  2  s.  c  om*/

    try {
        updateResult = this.saydoCollection.update(whereConditionObj,
                new BasicDBObject("$set", updateCurrentObj));
    } catch (Exception exp) {
        System.out.println(exp.getMessage());
    }

    if (updateResult.getLastError() != null) {
        System.out.println("Update did not happen properly");
        upsertSuccess = false;
    } else {
        currentProjectObj.putAll(updateConditionMap);
        upsertSuccess = addSayDoProject(username, currentProjectObj);
    }

    return upsertSuccess;
}

From source file:com.affairs.dao.zaffar.EntityDAO.java

License:Apache License

public boolean addEntity(String username, Map entityMap) {

    boolean insertSuccess = true;
    BasicDBObject entityObj = new BasicDBObject(entityMap);
    entityObj.append("isCurrent", true).append("delInd", false);
    entityObj.append("createdBy", username).append("updatedBy", username);
    entityObj.append("createdDate", new Date()).append("updatedDate", new Date());

    WriteResult insertResult = null;/*from  w ww. ja  v a2  s. c om*/

    try {
        insertResult = this.entityCollection.insert(entityObj);
    } catch (Exception exp) {
        System.out.println(exp.getMessage());
    }

    if (insertResult.getLastError() != null) {
        System.out.println("Insert did not happen properly");
        insertSuccess = false;
    }

    return insertSuccess;
}

From source file:com.affairs.dao.zaffar.EntityDAO.java

License:Apache License

public boolean updateEntity(String username, Map whereConditionMap, Map updateConditionMap) {

    boolean updateSuccess = true;
    BasicDBObject whereConditionObj = new BasicDBObject(whereConditionMap);
    BasicDBObject updateConditionObj = new BasicDBObject(updateConditionMap);

    updateConditionObj.append("updatedBy", username);
    updateConditionObj.append("updatedDate", new Date());

    WriteResult updateResult = null;/*from w  ww  . j  a v a  2  s.  c o  m*/

    try {
        updateResult = this.entityCollection.update(whereConditionObj,
                new BasicDBObject("$set", updateConditionObj));
    } catch (Exception exp) {
        System.out.println(exp.getMessage());
    }

    if (updateResult.getLastError() != null) {
        System.out.println("Update did not happen properly");
        updateSuccess = false;
    }

    return updateSuccess;
}

From source file:com.andiandy.m101j.week2.hw3.SessionDAO.java

License:Apache License

public String startSession(String username) {

    // get 32 byte random number. that's a lot of bits.
    SecureRandom generator = new SecureRandom();
    byte randomBytes[] = new byte[32];
    generator.nextBytes(randomBytes);//from  w w w . ja  va2s  . co  m

    String sessionID = Base64.encodeBase64(randomBytes).toString();

    // build the BSON object
    BasicDBObject session = new BasicDBObject("username", username);

    session.append("_id", sessionID);

    sessionsCollection.insert(session);

    return session.getString("_id");
}

From source file:com.andiandy.m101j.week2.hw3.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.
    BasicDBObject user = new BasicDBObject("_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.  jav  a  2 s .  com*/

    try {
        // XXX WORK HERE
        // insert the document into the user collection here
        usersCollection.insert(user);
        return true;
    } catch (MongoException.DuplicateKey e) {
        System.out.println("Username already in use: " + username);
        return false;
    }
}

From source file:com.andiandy.m101j.week3.hw2_3.BlogPostDAO.java

License:Apache License

public String addPost(String title, String body, List<String> tags, String username) {

    System.out.println("inserting blog entry " + title + " " + body);

    String permalink = title.replaceAll("\\s", "_"); // whitespace becomes _
    permalink = permalink.replaceAll("\\W", ""); // get rid of non alphanumeric
    permalink = permalink.toLowerCase();

    BasicDBObject post = new BasicDBObject();
    post.append("title", title);
    post.append("author", username);
    post.append("body", body);
    post.append("permalink", permalink);
    post.append("tags", tags);
    post.append("comments", new ArrayList<String>());
    post.append("date", new Date());

    postsCollection.insert(post);//  w  w w  .  j  a v a 2  s  . c o m
    // XXX HW 3.2, Work Here
    // Remember that a valid post has the following keys:
    // author, body, permalink, tags, comments, date
    //
    // A few hints:
    // - Don't forget to create an empty list of comments
    // - for the value of the date key, today's datetime is fine.
    // - tags are already in list form that implements suitable interface.
    // - we created the permalink for you above.

    // Build the post object and insert it

    return permalink;
}