package org.sagebionetworks.bridge.dynamodb;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.sagebionetworks.bridge.config.BridgeConfig;
import org.sagebionetworks.bridge.config.BridgeConfigFactory;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;

public class DynamoTestUtil {

    private static final AmazonDynamoDB DYNAMO;
    static {
        BridgeConfig config = BridgeConfigFactory.getConfig();
        String awsKey = config.getProperty("aws.key");
        String secretKey = config.getProperty("aws.secret.key");
        DYNAMO = new AmazonDynamoDBClient(new BasicAWSCredentials(awsKey,
                secretKey));
    }

    public static <T extends DynamoTable> void clearTable(Class<T> clazz) {
        List<String> keyAttrs = getKeyAttrs(clazz);
        String tableName = clazz.getAnnotation(DynamoDBTable.class).tableName();
        tableName = DynamoInitializer.getTableName(tableName);
        ScanResult result = DYNAMO.scan((new ScanRequest(tableName)).withAttributesToGet(keyAttrs));
        List<Map<String, AttributeValue>> items = result.getItems();
        do {
            for (Map<String, AttributeValue> item : items) {
                DYNAMO.deleteItem(tableName, item);
            }
            items = null;
            Map<String, AttributeValue> lastKey = result.getLastEvaluatedKey();
            if (lastKey != null) {
                result = DYNAMO.scan(new ScanRequest(tableName)
                        .withAttributesToGet(keyAttrs)
                        .withExclusiveStartKey(lastKey));
                items = result.getItems();
            }
        } while (items != null);
    }

    private static <T extends DynamoTable> List<String> getKeyAttrs(Class<T> clazz) {
        List<String> keyAttrs = new ArrayList<String>();
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            if (method.getAnnotation(DynamoDBHashKey.class) != null 
                    || method.getAnnotation(DynamoDBRangeKey.class) != null) {

                String name = method.getName();
                // Remove 'get', 'set', or 'is'
                if (name.startsWith("get") || name.startsWith("set")) {
                    name = name.substring(3);
                } else if (name.startsWith("is")) {
                    name = name.substring(2);
                }
                // Make sure the first letter is lower case
                char[] chars = name.toCharArray();
                char c = chars[0];
                c = Character.toLowerCase(c);
                chars[0] = c;
                name = new String(chars);
                keyAttrs.add(name);
            }
        }
        return keyAttrs;
    }
}

@@ -2586,17 +2586,16 @@
 null) %7B%0A
-%0A
         
@@ -3051,40 +3051,12 @@
 char
- c = chars%5B0%5D;%0A                c
+s%5B0%5D
  = C
@@ -3080,36 +3080,16 @@
 ase(
-c);%0A                
 chars%5B0%5D
  = c
@@ -3084,20 +3084,17 @@
 chars%5B0%5D
- = c
+)
 ;%0A      
@@ -3103,23 +3103,29 @@
         
-name = 
+keyAttrs.add(
 new Stri
@@ -3137,43 +3137,8 @@
 ars)
-;%0A                keyAttrs.add(name
 );%0A 

Simplify code.