DynamoDBService.java - AWS Code Sample

DynamoDBService.java

/** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * http://aws.amazon.com/apache2.0/ * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. * */ package com.example.services; import com.example.entities.WorkItem; import org.w3c.dom.Document; import org.w3c.dom.Element; import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; import software.amazon.awssdk.enhanced.dynamodb.TableSchema; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.*; import software.amazon.awssdk.enhanced.dynamodb.model.ScanEnhancedRequest; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import software.amazon.awssdk.enhanced.dynamodb.Expression; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.StringWriter; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.*; import org.springframework.stereotype.Component; /* Before running this code example, create a table named Work with a PK named id */ @Component public class DynamoDBService { private DynamoDbClient getClient() { // Create a DynamoDbClient object Region region = Region.US_EAST_1; DynamoDbClient ddb = DynamoDbClient.builder() .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) .region(region) .build(); return ddb; } // Get a single item from the Work table based on the Key public String getItem(String idValue) { DynamoDbClient ddb = getClient(); String status = ""; String description = ""; HashMap<String, AttributeValue> keyToGet = new HashMap<String,AttributeValue>(); keyToGet.put("id", AttributeValue.builder() .s(idValue) .build()); // Create a GetItemRequest object GetItemRequest request = GetItemRequest.builder() .key(keyToGet) .tableName("Work") .build(); try { Map<String,AttributeValue> returnedItem = ddb.getItem(request).item(); // Get keys and values and get description and status for (Map.Entry<String,AttributeValue > entry : returnedItem.entrySet()) { String k = entry.getKey(); AttributeValue v = entry.getValue(); if (k.compareTo("description") == 0) { description = v.s(); } else if (k.compareTo("status") == 0) { status = v.s(); } } return convertToString(toXmlItem(idValue,description,status)); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } // Retrieves items from the DynamoDB table public ArrayList<WorkItem> getListItems() { // Create a DynamoDbEnhancedClient DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder() .dynamoDbClient(getClient()) .build(); try{ // Create a DynamoDbTable object DynamoDbTable<Work> custTable = enhancedClient.table("Work", TableSchema.fromBean(Work.class)); // Get items in the Work table Iterator<Work> results = custTable.scan().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 itemList; } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Done"); return null ; } // Archives an item based on the key public String archiveItem(String id){ DynamoDbClient ddb = getClient(); HashMap<String,AttributeValue> itemKey = new HashMap<String,AttributeValue>(); itemKey.put("id", AttributeValue.builder() .s(id) .build()); HashMap<String, AttributeValueUpdate> updatedValues = new HashMap<String,AttributeValueUpdate>(); // Update the column specified by name with updatedVal updatedValues.put("archive", AttributeValueUpdate.builder() .value(AttributeValue.builder() .s("Closed").build()) .action(AttributeAction.PUT) .build()); UpdateItemRequest request = UpdateItemRequest.builder() .tableName("Work") .key(itemKey) .attributeUpdates(updatedValues) .build(); try { ddb.updateItem(request); return"The item was successfully archived"; } catch (ResourceNotFoundException e) { System.err.println(e.getMessage()); System.exit(1); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } // Updates items in the Work Table public String UpdateItem(String id, String status){ DynamoDbClient ddb = getClient(); HashMap<String,AttributeValue> itemKey = new HashMap<String,AttributeValue>(); itemKey.put("id", AttributeValue.builder() .s(id) .build()); HashMap<String, AttributeValueUpdate> updatedValues = new HashMap<String,AttributeValueUpdate>(); // Update the column specified by name with updatedVal updatedValues.put("status", AttributeValueUpdate.builder() .value(AttributeValue.builder() .s(status).build()) .action(AttributeAction.PUT) .build()); UpdateItemRequest request = UpdateItemRequest.builder() .tableName("Work") .key(itemKey) .attributeUpdates(updatedValues) .build(); try { ddb.updateItem(request); return"The Status for the the item was successfully updated"; } catch (ResourceNotFoundException e) { System.err.println(e.getMessage()); System.exit(1); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } // Retrieves items from the DynamoDB table // Get Open items from the DynamoDB table public String getOpenItems() { // 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("Open") .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(); // Scan 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 "" ; } // Get Closed Items // Retrieves 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 in the Record table and write out the ID values 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 "" ; } public void setItem(WorkItem item) { // Create a DynamoDbEnhancedClient DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder() .dynamoDbClient(getClient()) .build(); putRecord(enhancedClient, item) ; } // Put an item into a DynamoDB table public void putRecord(DynamoDbEnhancedClient enhancedClient, WorkItem item) { try { // Create a DynamoDbTable object DynamoDbTable<Work> workTable = enhancedClient.table("Work", TableSchema.fromBean(Work.class)); // Create an Instant object LocalDate localDate = LocalDate.parse("2020-04-07"); LocalDateTime localDateTime = localDate.atStartOfDay(); Instant instant = localDateTime.toInstant(ZoneOffset.UTC); String myGuid = java.util.UUID.randomUUID().toString(); // Populate the table Work record = new Work(); record.setUsername(item.getName()); record.setId(myGuid); record.setDescription(item.getDescription()); record.setDate(now()) ; record.setStatus(item.getStatus()); record.setArchive("Open"); record.setGuide(item.getGuide()); // Put the customer data into a DynamoDB table workTable.putItem(record); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } } // Convert Work 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; } private String convertToString(Document xml) { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(xml); transformer.transform(source, result); return result.getWriter().toString(); } catch(TransformerException ex) { ex.printStackTrace(); } return null; } private String now() { String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); return sdf.format(cal.getTime()); } // Convert Work data into an XML schema to pass back to client private Document toXmlItem(String id2, String desc2, String status2) { 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 ); Element item = doc.createElement( "Item" ); root.appendChild( item ); //Set Id Element id = doc.createElement( "Id" ); id.appendChild( doc.createTextNode(id2 ) ); item.appendChild( id ); //Set Description Element desc = doc.createElement( "Description" ); desc.appendChild( doc.createTextNode(desc2 ) ); item.appendChild( desc ); //Set Status Element status = doc.createElement( "Status" ); status.appendChild( doc.createTextNode(status2 ) ); item.appendChild( status ); return doc; } catch(ParserConfigurationException e) { e.printStackTrace(); } return null; } }