/*
   Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
   SPDX-License-Identifier: Apache-2.0
*/
package com.example.dynamodb;

import java.time.Instant;
import java.util.Map;
import java.util.Iterator;
import java.util.HashMap;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.Expression;
import software.amazon.awssdk.enhanced.dynamodb.Key;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;

public class EnhancedQueryRecordsWithSortKey {

    public static void main(String[] args) {

        Region region = Region.US_EAST_1;
        DynamoDbClient ddb = DynamoDbClient.builder()
                .region(region)
                .build();

        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder()
                .dynamoDbClient(ddb)
                .build();

        queryTableSortKeyBetween(enhancedClient);
        ddb.close();
    }

    public static void queryTableSortKeyBetween(DynamoDbEnhancedClient enhancedClient) {

        try {
            DynamoDbTable<Customer> mappedTable =
                    enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));

            // Querying the sort key Name between two values
            Key fromKey = Key.builder().partitionValue("id101").sortValue("S").build();
            Key toKey = Key.builder().partitionValue("id101").sortValue("T").build();

            QueryConditional queryConditional = QueryConditional.sortBetween(fromKey, toKey);

            PageIterable<Customer> customers =
                    mappedTable.query(r -> r.queryConditional(queryConditional));

            customers.stream()
                     .forEach(p -> p.items().forEach(item -> System.out.println(item.getCustName())));

        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        System.out.println("Done");
    }


    @DynamoDbBean
    public static class Customer {

        private String id;
        private String name;
        private String email;
        private Instant regDate;

        @DynamoDbPartitionKey
        public String getId() {
            return this.id;
        };

        public void setId(String id) {

            this.id = id;
        }

        @DynamoDbSortKey
        public String getCustName() {
            return this.name;

        }

        public void setCustName(String name) {

            this.name = name;
        }

        public String getEmail() {
            return this.email;
        }

        public void setEmail(String email) {

            this.email = email;
        }

        public Instant getRegistrationDate() {
            return regDate;
        }
        public void setRegistrationDate(Instant registrationDate) {

            this.regDate = registrationDate;
        }
    }
}

@@ -1,7 +1,338 @@
+//snippet-sourcedescription:%5BEnhancedQueryRecordsWithSortKey.java demonstrates how to query a table with a sort key.%5D%0A//snippet-keyword:%5BSDK for Java v2%5D%0A//snippet-keyword:%5BCode Sample%5D%0A//snippet-service:%5BAmazon DynamoDB%5D%0A//snippet-sourcetype:%5Bfull-example%5D%0A//snippet-sourcedate:%5B12/16/2020%5D%0A//snippet-sourceauthor:%5Bdebito - aws%5D%0A%0A
 /*%0A
-
    C
@@ -500,83 +500,8 @@
 nt;%0A
-import java.util.Map;%0Aimport java.util.Iterator;%0Aimport java.util.HashMap;%0A
 impo
@@ -635,68 +635,8 @@
 le;%0A
-import software.amazon.awssdk.enhanced.dynamodb.Expression;%0A
 impo
@@ -1025,32 +1025,32 @@
 azon.awssdk.
-services
+enhanced
 .dynamodb.mo
@@ -1057,21 +1057,19 @@
 del.
-AttributeValu
+PageIterabl
 e;%0Ai

Fix imports