|
27 | 27 | */ |
28 | 28 | public class ContractStatusChangedHandlerFunction { |
29 | 29 |
|
30 | | - Logger logger = LogManager.getLogger(); |
31 | | - |
32 | | - final String TABLE_NAME = System.getenv("CONTRACT_STATUS_TABLE"); |
33 | | - |
34 | | - ObjectMapper objectMapper = new ObjectMapper(); |
35 | | - |
36 | | - DynamoDbClient dynamodbClient = DynamoDbClient.builder() |
37 | | - .build(); |
38 | | - |
39 | | - /** |
40 | | - * |
41 | | - * @param inputStream |
42 | | - * @param outputStream |
43 | | - * @param context |
44 | | - * @return |
45 | | - * @throws IOException |
46 | | - * |
47 | | - */ |
48 | | - @Tracing |
49 | | - @Metrics(captureColdStart = true) |
50 | | - @Logging(logEvent = true) |
51 | | - public void handleRequest(InputStream inputStream, OutputStream outputStream, |
52 | | - Context context) throws IOException { |
53 | | - |
54 | | - // deseralised and save contract status change in dynamodb table |
55 | | - |
56 | | - Event event = Marshaller.unmarshal(inputStream, |
57 | | - Event.class); |
58 | | - // save to database |
59 | | - ContractStatusChanged contractStatusChanged = event.getDetail(); |
60 | | - saveContractStatus(contractStatusChanged.getPropertyId(), contractStatusChanged.getContractStatus(), |
61 | | - contractStatusChanged.getContractId(), |
62 | | - contractStatusChanged.getContractLastModifiedOn()); |
63 | | - |
64 | | - OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); |
65 | | - writer.write(objectMapper.writeValueAsString(event.getDetail())); |
66 | | - writer.close(); |
67 | | - } |
68 | | - |
69 | | - @Tracing |
70 | | - void saveContractStatus(String propertyId, |
71 | | - String contractStatus, String contractId, Long contractLastModifiedOn) { |
72 | | - Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); |
73 | | - AttributeValue keyvalue = AttributeValue.fromS(propertyId); |
74 | | - key.put("property_id", keyvalue); |
75 | | - |
76 | | - Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>(); |
77 | | - expressionAttributeValues.put(":t", AttributeValue.fromS(contractStatus)); |
78 | | - expressionAttributeValues.put(":c", AttributeValue.fromS(contractId)); |
79 | | - expressionAttributeValues.put(":m", AttributeValue |
80 | | - .fromN(String.valueOf(contractLastModifiedOn))); |
81 | | - |
82 | | - UpdateItemRequest updateItemRequest = UpdateItemRequest.builder() |
83 | | - .key(key) |
84 | | - .tableName(TABLE_NAME) |
85 | | - .updateExpression( |
86 | | - "set contract_status=:t, contract_last_modified_on=:m, contract_id=:c") |
87 | | - .expressionAttributeValues(expressionAttributeValues) |
88 | | - .build(); |
89 | | - |
90 | | - dynamodbClient.updateItem(updateItemRequest); |
91 | | - } |
92 | | - |
93 | | - public void setDynamodbClient(DynamoDbClient dynamodbClient) { |
94 | | - this.dynamodbClient = dynamodbClient; |
| 30 | + private static final Logger logger = LogManager.getLogger(); |
| 31 | + private static final String TABLE_NAME = System.getenv("CONTRACT_STATUS_TABLE"); |
| 32 | + private static final ObjectMapper objectMapper = new ObjectMapper(); |
| 33 | + |
| 34 | + private DynamoDbClient dynamodbClient = DynamoDbClient.builder().build(); |
| 35 | + |
| 36 | + /** |
| 37 | + * Handles contract status change events from EventBridge |
| 38 | + * |
| 39 | + * @param inputStream the input stream containing the event |
| 40 | + * @param outputStream the output stream for the response |
| 41 | + * @param context the Lambda context |
| 42 | + * @throws IOException if there's an error processing the event |
| 43 | + */ |
| 44 | + @Tracing |
| 45 | + @Metrics(captureColdStart = true) |
| 46 | + @Logging(logEvent = true) |
| 47 | + public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException { |
| 48 | + Event event = Marshaller.unmarshal(inputStream, Event.class); |
| 49 | + ContractStatusChanged contractStatusChanged = event.getDetail(); |
| 50 | + |
| 51 | + saveContractStatus( |
| 52 | + contractStatusChanged.getPropertyId(), |
| 53 | + contractStatusChanged.getContractStatus(), |
| 54 | + contractStatusChanged.getContractId(), |
| 55 | + contractStatusChanged.getContractLastModifiedOn() |
| 56 | + ); |
| 57 | + |
| 58 | + try (OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) { |
| 59 | + writer.write(objectMapper.writeValueAsString(event.getDetail())); |
95 | 60 | } |
| 61 | + } |
| 62 | + |
| 63 | + @Tracing |
| 64 | + void saveContractStatus(String propertyId, String contractStatus, String contractId, Long contractLastModifiedOn) { |
| 65 | + Map<String, AttributeValue> key = Map.of("property_id", AttributeValue.fromS(propertyId)); |
| 66 | + |
| 67 | + Map<String, AttributeValue> expressionAttributeValues = Map.of( |
| 68 | + ":t", AttributeValue.fromS(contractStatus), |
| 69 | + ":c", AttributeValue.fromS(contractId), |
| 70 | + ":m", AttributeValue.fromN(String.valueOf(contractLastModifiedOn)) |
| 71 | + ); |
| 72 | + |
| 73 | + UpdateItemRequest updateItemRequest = UpdateItemRequest.builder() |
| 74 | + .key(key) |
| 75 | + .tableName(TABLE_NAME) |
| 76 | + .updateExpression("set contract_status=:t, contract_last_modified_on=:m, contract_id=:c") |
| 77 | + .expressionAttributeValues(expressionAttributeValues) |
| 78 | + .build(); |
| 79 | + |
| 80 | + dynamodbClient.updateItem(updateItemRequest); |
| 81 | + } |
| 82 | + |
| 83 | + public void setDynamodbClient(DynamoDbClient dynamodbClient) { |
| 84 | + this.dynamodbClient = dynamodbClient; |
| 85 | + } |
96 | 86 | } |
0 commit comments