Search in sources :

Example 1 with WorkItem

use of com.example.entities.WorkItem in project aws-doc-sdk-examples by awsdocs.

the class MongoDBService method getListItems.

// Retrieves all items from MongoDB
public String getListItems() {
    MongoClient mongoClient = getConnection();
    // Get the database name
    DB database = mongoClient.getDB("local");
    DBCursor cur = database.getCollection("items").find();
    DBObject dbo = null;
    ArrayList<WorkItem> itemList = new ArrayList();
    WorkItem item = null;
    int index = 0;
    while (cur.hasNext()) {
        index = 0;
        item = new WorkItem();
        dbo = cur.next();
        Set<String> keys = dbo.keySet();
        for (String key : keys) {
            String value = (String) dbo.get(key).toString();
            if (key.compareTo("_id") == 0)
                item.setId(value);
            else if (key.compareTo("archive") == 0)
                item.setArc(value);
            else if (key.compareTo("date") == 0)
                item.setDate(value);
            else if (key.compareTo("description") == 0)
                item.setDescription(value);
            else if (key.compareTo("guide") == 0)
                item.setGuide(value);
            else if (key.compareTo("status") == 0)
                item.setStatus(value);
            else if (key.compareTo("username") == 0) {
                item.setName(value);
                // last item read
                itemList.add(item);
            }
        }
    }
    return convertToString(toXml(itemList));
}
Also used : MongoClient(com.mongodb.MongoClient) DBCursor(com.mongodb.DBCursor) ArrayList(java.util.ArrayList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) WorkItem(com.example.entities.WorkItem) DB(com.mongodb.DB)

Example 2 with WorkItem

use of com.example.entities.WorkItem in project aws-doc-sdk-examples by awsdocs.

the class MongoDBService method findDocumentById.

public String findDocumentById(String id) {
    try {
        MongoClient mongoClient = getConnection();
        // Get the database name
        DB database = mongoClient.getDB("local");
        DBCollection collection = database.getCollection("items");
        // Return an item based on the ID -- need this for Item Tracker
        DBObject ob1 = collection.findOne(id);
        Set<String> keys = ob1.keySet();
        Iterator iterator = keys.iterator();
        ArrayList itemList = new ArrayList();
        WorkItem item = null;
        item = new WorkItem();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            String value = (String) ob1.get(key).toString();
            if (key.compareTo("_id") == 0)
                item.setId(value);
            else if (key.compareTo("archive") == 0)
                item.setArc(value);
            else if (key.compareTo("date") == 0)
                item.setDate(value);
            else if (key.compareTo("description") == 0)
                item.setDescription(value);
            else if (key.compareTo("guide") == 0)
                item.setGuide(value);
            else if (key.compareTo("status") == 0)
                item.setStatus(value);
            else if (key.compareTo("username") == 0) {
                item.setName(value);
                // last item read
                itemList.add(item);
            }
        }
        return convertToString(toXml(itemList));
    } catch (MongoException e) {
        System.out.println(e.getMessage());
    }
    return "";
}
Also used : MongoClient(com.mongodb.MongoClient) DBCollection(com.mongodb.DBCollection) MongoException(com.mongodb.MongoException) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) WorkItem(com.example.entities.WorkItem) DB(com.mongodb.DB)

Example 3 with WorkItem

use of com.example.entities.WorkItem in project aws-doc-sdk-examples by awsdocs.

the class MainController method addItems.

// Adds a new item to the DynamoDB database.
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
String addItems(HttpServletRequest request, HttpServletResponse response) {
    // Get the logged-in user.
    String name = getLoggedUser();
    String guide = request.getParameter("guide");
    String description = request.getParameter("description");
    String status = request.getParameter("status");
    // Create a Work Item object to pass to the injectNewSubmission method.
    WorkItem myWork = new WorkItem();
    myWork.setGuide(guide);
    myWork.setDescription(description);
    myWork.setStatus(status);
    myWork.setName(name);
    dbService.putRecord(myWork);
    return "Item added";
}
Also used : WorkItem(com.example.entities.WorkItem) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with WorkItem

use of com.example.entities.WorkItem in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBService method toXml.

// Convert Work item data into XML to pass back to the view.
private Document toXml(List<WorkItem> itemList) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();
        // Start building the XML.
        Element root = doc.createElement("Items");
        doc.appendChild(root);
        // Get the elements from the collection.
        int custCount = itemList.size();
        // Iterate through the collection.
        for (int index = 0; index < custCount; index++) {
            // Get the WorkItem object from the collection.
            WorkItem myItem = itemList.get(index);
            Element item = doc.createElement("Item");
            root.appendChild(item);
            // Set Id.
            Element id = doc.createElement("Id");
            id.appendChild(doc.createTextNode(myItem.getId()));
            item.appendChild(id);
            // Set Name.
            Element name = doc.createElement("Name");
            name.appendChild(doc.createTextNode(myItem.getName()));
            item.appendChild(name);
            // Set Date.
            Element date = doc.createElement("Date");
            date.appendChild(doc.createTextNode(myItem.getDate()));
            item.appendChild(date);
            // Set Description.
            Element desc = doc.createElement("Description");
            desc.appendChild(doc.createTextNode(myItem.getDescription()));
            item.appendChild(desc);
            // Set Guide.
            Element guide = doc.createElement("Guide");
            guide.appendChild(doc.createTextNode(myItem.getGuide()));
            item.appendChild(guide);
            // Set Status.
            Element status = doc.createElement("Status");
            status.appendChild(doc.createTextNode(myItem.getStatus()));
            item.appendChild(status);
        }
        return doc;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) WorkItem(com.example.entities.WorkItem)

Example 5 with WorkItem

use of com.example.entities.WorkItem in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBService method getClosedItems.

// Get Closed Items from the DynamoDB table.
public String getClosedItems() {
    // Create a DynamoDbEnhancedClient.
    DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(getClient()).build();
    try {
        // Create a DynamoDbTable object.
        DynamoDbTable<Work> table = enhancedClient.table("Work", TableSchema.fromBean(Work.class));
        AttributeValue attr = AttributeValue.builder().s("Closed").build();
        Map<String, AttributeValue> myMap = new HashMap<>();
        myMap.put(":val1", attr);
        Map<String, String> myExMap = new HashMap<>();
        myExMap.put("#archive", "archive");
        // Set the Expression so only Closed items are queried from the Work table.
        Expression expression = Expression.builder().expressionValues(myMap).expressionNames(myExMap).expression("#archive = :val1").build();
        ScanEnhancedRequest enhancedRequest = ScanEnhancedRequest.builder().filterExpression(expression).limit(15).build();
        // Get items.
        Iterator<Work> results = table.scan(enhancedRequest).items().iterator();
        WorkItem workItem;
        ArrayList<WorkItem> itemList = new ArrayList();
        while (results.hasNext()) {
            // Populate a WorkItem.
            workItem = new WorkItem();
            Work work = results.next();
            workItem.setName(work.getName());
            workItem.setGuide(work.getGuide());
            workItem.setDescription(work.getDescription());
            workItem.setStatus(work.getStatus());
            workItem.setDate(work.getDate());
            workItem.setId(work.getId());
            // Push the workItem to the list.
            itemList.add(workItem);
        }
        return convertToString(toXml(itemList));
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("Done");
    return "";
}
Also used : ScanEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.ScanEnhancedRequest) WorkItem(com.example.entities.WorkItem)

Aggregations

WorkItem (com.example.entities.WorkItem)10 BasicDBObject (com.mongodb.BasicDBObject)3 DB (com.mongodb.DB)3 DBObject (com.mongodb.DBObject)3 MongoClient (com.mongodb.MongoClient)3 ArrayList (java.util.ArrayList)3 DBCursor (com.mongodb.DBCursor)2 Iterator (java.util.Iterator)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 ScanEnhancedRequest (software.amazon.awssdk.enhanced.dynamodb.model.ScanEnhancedRequest)2 DBCollection (com.mongodb.DBCollection)1 MongoException (com.mongodb.MongoException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1