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:com.heisenberg.mongo.MongoWorkflowInstanceStore.java

License:Apache License

protected BasicDBObject buildQuery(WorkflowInstanceQueryImpl workflowInstanceQuery) {
    BasicDBObject query = new BasicDBObject();
    if (workflowInstanceQuery.workflowInstanceId != null) {
        query.append(fields._id, new ObjectId(workflowInstanceQuery.workflowInstanceId));
    }/*from  w  w  w  .  j  a v  a2 s . c  o  m*/
    if (workflowInstanceQuery.activityInstanceId != null) {
        query.append(fields.activityInstances + "." + fields._id,
                new ObjectId(workflowInstanceQuery.workflowInstanceId));
    }
    return query;
}

From source file:com.heisenberg.mongo.MongoWorkflowStore.java

License:Apache License

@Override
public List<WorkflowImpl> loadWorkflows(WorkflowQueryImpl query) {
    BasicDBObject q = new BasicDBObject();
    if (query.id != null) {
        q.append(fields._id, new ObjectId(query.id));
    }/* w  w w.j  a  v a 2 s  .  c  o m*/
    if (query.name != null) {
        q.append(fields.name, query.name);
    }
    List<WorkflowImpl> processes = new ArrayList<WorkflowImpl>();
    DBCursor cursor = find(q);
    if (query.limit != null) {
        cursor.limit(query.limit);
    }
    if (query.orderBy != null) {
        cursor.sort(writeOrderBy(query.orderBy));
    }
    while (cursor.hasNext()) {
        BasicDBObject dbProcess = (BasicDBObject) cursor.next();
        WorkflowImpl processDefinition = readProcessDefinition(dbProcess);
        processes.add(processDefinition);
    }
    return processes;
}

From source file:com.heisenberg.mongo.MongoWorkflowStore.java

License:Apache License

public DBObject writeOrderBy(OrderBy orderBy) {
    BasicDBObject dbOrderBy = new BasicDBObject();
    for (OrderByElement element : orderBy.orderByElements) {
        String dbField = getDbField(element.field);
        int dbDirection = (element.direction == OrderByDirection.ASCENDING ? 1 : -1);
        dbOrderBy.append(dbField, dbDirection);
    }//from   ww  w. ja v a2 s.c  o m
    return dbOrderBy;
}

From source file:com.hw3dot2.BlogPostDAO.java

License:Apache License

public String addPost(String title, String body, List 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();

    if (!StringUtils.isEmpty(title) && !StringUtils.isEmpty(body) && !StringUtils.isEmpty(username)
            && (tags.size() > 0)) {
        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<DBObject>());
        post.append("date", new Date());

        postsCollection.insert(post);//from ww  w.ja va2  s .c  om
    }

    // 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;
}

From source file:com.hw3dot2.BlogPostDAO.java

License:Apache License

public void addPostComment(final String name, final String email, final String body, final String permalink) {
    BasicDBObject comment = new BasicDBObject();
    comment.append("author", name);
    if (!StringUtils.isEmpty(email)) {
        comment.append("email", email);
    }//from ww w.  ja va  2  s.  co  m
    comment.append("body", body);
    postsCollection.update(new BasicDBObject("permalink", permalink),
            new BasicDBObject("$push", new BasicDBObject("comments", comment)));

    // XXX HW 3.3, Work Here
    // Hints:
    // - email is optional and may come in NULL. Check for that.
    // - best solution uses an update command to the database and a suitable
    //   operator to append the comment on to any existing list of comments

}

From source file:com.ibm.bluemix.smartveggie.dao.SubOutletVendorAllocationDaoImpl.java

@Override
public BasicDBObject allocatedSubOutlet(SubOutletVendorAllocationDTO subOutletVendorAllocationDTO) {

    System.out.println("Allocating Outlet to vendor...");
    DB db = MongodbConnection.getMongoDB();
    DBCollection col = db.getCollection(ICollectionName.COLLECTION_ALLOC_SUBOUTLETS);
    // create a document
    BasicDBObject json = new BasicDBObject();
    json.append("smartCityCode", subOutletVendorAllocationDTO.getSmartCityCode());
    json.append("smartOutletCode", subOutletVendorAllocationDTO.getSmartOutletCode());
    json.append("suboutletCode", subOutletVendorAllocationDTO.getSuboutletCode());
    json.append("vendorLicenseNo", subOutletVendorAllocationDTO.getVendorLicenseNo());
    json.append("vendorUsername", subOutletVendorAllocationDTO.getVendorUsername());
    json.append("suboutletAllocatedFrom", subOutletVendorAllocationDTO.getSuboutletAllocatedFrom());
    json.append("suboutletAllocatedTo", subOutletVendorAllocationDTO.getSuboutletAllocatedTo());

    // insert the document
    col.insert(json);//from   w w  w . j a  va2s.  c  o m
    System.out.println("After allocating outlet..");
    return json;
}

From source file:com.ibm.bluemix.smartveggie.dao.SubOutletVendorAllocationDaoImpl.java

@Override
public BasicDBObject deallocatedSubOutlet(SubOutletVendorAllocationDTO subOutletVendorAllocationDTO) {
    try {/*w  w w  .j  a  v a 2s .  c o  m*/
        System.out.println("starting object delete..");
        DB db = MongodbConnection.getMongoDB();
        BasicDBObject query = new BasicDBObject();
        if (subOutletVendorAllocationDTO != null) {
            if (subOutletVendorAllocationDTO.getVendorUsername() != null
                    && !subOutletVendorAllocationDTO.getVendorUsername().equalsIgnoreCase("")) {
                query.append("vendorUsername", subOutletVendorAllocationDTO.getVendorUsername());
            }
            if (subOutletVendorAllocationDTO.getSmartCityCode() != null
                    && !subOutletVendorAllocationDTO.getSmartCityCode().equalsIgnoreCase("")) {
                query.append("smartCityCode", subOutletVendorAllocationDTO.getSmartCityCode());
            }
            if (subOutletVendorAllocationDTO.getSmartOutletCode() != null
                    && !subOutletVendorAllocationDTO.getSmartOutletCode().equalsIgnoreCase("")) {
                query.append("smartOutletCode", subOutletVendorAllocationDTO.getSmartOutletCode());
            }
            if (subOutletVendorAllocationDTO.getSuboutletCode() != null
                    && !subOutletVendorAllocationDTO.getSuboutletCode().equalsIgnoreCase("")) {
                query.append("suboutletCode", subOutletVendorAllocationDTO.getSuboutletCode());
            }
            if (subOutletVendorAllocationDTO.getSuboutletAllocatedFrom() != null
                    && !subOutletVendorAllocationDTO.getSuboutletAllocatedFrom().equalsIgnoreCase("")) {
                query.append("suboutletAllocatedFrom",
                        subOutletVendorAllocationDTO.getSuboutletAllocatedFrom());
            }
            if (subOutletVendorAllocationDTO.getSuboutletAllocatedTo() != null
                    && !subOutletVendorAllocationDTO.getSuboutletAllocatedTo().equalsIgnoreCase("")) {
                query.append("suboutletAllocatedTo", subOutletVendorAllocationDTO.getSuboutletAllocatedTo());
            }
        }
        System.out.println("Querying for Delete: " + query);
        DBCollection col = db.getCollection(ICollectionName.COLLECTION_ALLOC_SUBOUTLETS);
        DBCursor cursor = col.find(query);
        BasicDBObject obj = null;
        while (cursor.hasNext()) {
            obj = (BasicDBObject) cursor.next();
            System.out.println("Retrieved Allocated Vendor manager outlet: " + obj);
        }
        col.remove(query);
        cursor.close();
        return obj;
    } catch (Exception e) {
        throw e;
    }
}

From source file:com.ibm.bluemix.smartveggie.dao.SubOutletVendorAllocationDaoImpl.java

@Override
public BasicDBObject getVendorForSubOutlet(String vendorUserName) {
    try {/*from w ww  . j a  v a 2  s . c o m*/
        System.out.println("starting object retrieve..");
        DB db = MongodbConnection.getMongoDB();
        BasicDBObject query = new BasicDBObject();
        query.append("vendorUsername", vendorUserName);
        System.out.println("Querying for getting vendor suboutlet: " + query);
        DBCollection col = db.getCollection(ICollectionName.COLLECTION_ALLOC_SUBOUTLETS);
        DBCursor cursor = col.find(query);
        BasicDBObject obj = null;
        while (cursor.hasNext()) {
            obj = (BasicDBObject) cursor.next();
            //Check the date
            Date currentDate = Calendar.getInstance().getTime();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            String allocationTillString = (String) obj.get("suboutletAllocatedTo");
            if (allocationTillString != null) {
                Date allocEndDate = null;
                try {
                    allocEndDate = dateFormat.parse(allocationTillString);
                    if (allocEndDate.before(currentDate)) {
                        System.out.println("Suboutlet Allocation already ended....");
                        //subOutletAvailableList.add(allocation);
                    } else {
                        break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("Retrieved Allocated Vendor suboutlet: " + obj);
            }
        }
        cursor.close();
        return obj;
    } catch (Exception e) {
        throw e;
    }
}

From source file:com.ibm.bluemix.smartveggie.dao.UserDaoImpl.java

@Override
public BasicDBObject getUser(String userName, String password) {
    DB db = MongodbConnection.getMongoDB();
    DBCollection col = db.getCollection(ICollectionName.COLLECTION_USERS);
    BasicDBObject obj = null;//from   w ww  .ja  v a  2s  .co  m
    try {
        System.out.println("starting object read..");
        BasicDBObject query = new BasicDBObject();
        if ((userName != null) && (userName != ""))
            query.append("userName", userName);
        if ((password != null) && (password != ""))
            query.append("password", password);
        System.out.println("Querying for: " + query);
        DBCursor cursor = col.find(query);
        while (cursor.hasNext()) {
            obj = (BasicDBObject) cursor.next();
            System.out.println("Retrieved: " + obj);
        }
        cursor.close();
    } catch (Exception e) {
        throw e;
    }
    return obj;
}

From source file:com.ibm.bluemix.smartveggie.dao.UserDaoImpl.java

@Override
public BasicDBObject createUser(UserDTO userDTO) {

    System.out.println("Creating User...");
    DB db = MongodbConnection.getMongoDB();
    DBCollection col = db.getCollection(ICollectionName.COLLECTION_USERS);
    // create a document
    BasicDBObject json = new BasicDBObject();
    json.append("firstName", userDTO.getFirstName());
    json.append("lastName", userDTO.getLastName());
    json.append("addressLine1", userDTO.getAddressLine1());
    json.append("addressLine2", userDTO.getAddressLine2());
    json.append("sex", userDTO.getSex());
    json.append("age", userDTO.getAge());
    json.append("city", userDTO.getCity());
    json.append("pin", userDTO.getPinCode());
    json.append("userType", userDTO.getUserTypeCode());
    json.append("userName", userDTO.getUserName());
    json.append("password", userDTO.getPassword());

    if (userDTO.getUserTypeCode() != null && userDTO.getUserTypeCode().equals("vendor")) {

        json.append("licenseNo", userDTO.getLicenseNo());

        //Process the date field
        SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy");
        String validFrom = userDTO.getValidFrom();
        String validTo = userDTO.getValidTo();

        try {//from   w w w. jav  a  2  s  .  c  o m

            Date validFromDate = formatter.parse(validFrom);
            Date validToDate = formatter.parse(validTo);
            System.out.println(validFromDate);
            json.append("validFrom", validFromDate);
            json.append("validTo", validToDate);
            //System.out.println(formatter.format(validFromDate));

        } catch (Exception e) {
            e.printStackTrace();
        }
        // insert the document
    } else if (userDTO.getUserTypeCode() != null && userDTO.getUserTypeCode().equalsIgnoreCase("regulator")) {
        json.append("regulatingCityCode", userDTO.getRegulatingCityCode());
        json.append("regulatingCityName", userDTO.getRegulatingCityName());
    }
    col.insert(json);
    System.out.println("after insert");
    return json;
}