package org.springframework.data.simpledb.core;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.simpledb.AmazonSimpleDB;
import com.amazonaws.services.simpledb.AmazonSimpleDBClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.simpledb.repository.support.entityinformation.SimpleDbEntityInformation;

import java.io.Serializable;
import java.util.List;
import org.springframework.util.Assert;

/**
 *
 */
public class SimpleDbOperationsImpl<T, ID extends Serializable> implements SimpleDbOperations {

    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleDbOperationsImpl.class);
    private final DDL ddl;
    private final AmazonSimpleDB sdb;
    private final String accessID;
    private final String secretKey;

    public enum DDL {

        drop_create,
        update,
        nothing
    }

    public SimpleDbOperationsImpl(String accessID, String secretKey) {
        this(accessID, secretKey, DDL.nothing.name());
    }

    public SimpleDbOperationsImpl(final String accessID, final String secretKey, String ddl) {
        Assert.notNull(accessID);
        Assert.notNull(secretKey);
        sdb = new AmazonSimpleDBClient(new AWSCredentials() {
            @Override
            public String getAWSAccessKeyId() {
                return accessID;
            }

            @Override
            public String getAWSSecretKey() {
                return secretKey;
            }
        });
        this.accessID = accessID;
        this.secretKey = secretKey;
        this.ddl = DDL.valueOf(ddl);
    }

    public String getAccessID() {
        return accessID;
    }

    public String getSecretKey() {
        return secretKey;
    }

    @Override
    public void delete(SimpleDbEntity sdbEntity) {
        logOperation("Delete", sdbEntity);
    }

    @Override
    public Object addItem(SimpleDbEntity entity) {
        logOperation("Add  ", entity);
        return entity.getItem();
    }

    @Override
    public Object updateItem(SimpleDbEntity entity) {
        logOperation("Update", entity);
        return entity.getItem();
    }

    @Override
    public Object findOne(SimpleDbEntityInformation entityInformation, Serializable id) {
        LOGGER.info("FindOne ItemName \"{}\"\"", id);
//        entityInformation.getIdType();
        return null;
    }

    @Override
    public boolean exists(SimpleDbEntityInformation entityInformation, Serializable id) {
        LOGGER.info("Exists ItemName \"{}\"\"", id);
        return false;
    }

    @Override
    public List findAll(SimpleDbEntityInformation entityInformation, Iterable ids) {
        LOGGER.info("Find All Domain \"{}\"\"", entityInformation.getDomain());
        return null;
    }

    private void logOperation(String operation, SimpleDbEntity<T, ID> entity) {
        LOGGER.info(operation + " \"{}\" ItemName \"{}\"\"", entity.getDomain(), entity.getItemName());
    }
}

@@ -200,16 +200,150 @@
 Client;%0A
+import com.amazonaws.services.simpledb.model.PutAttributesRequest;%0Aimport com.amazonaws.services.simpledb.model.ReplaceableAttribute;%0A
 import o
@@ -526,16 +526,44 @@
 izable;%0A
+import java.util.ArrayList;%0A
 import j
@@ -573,24 +573,46 @@
 .util.List;%0A
+import java.util.Map;%0A
 import org.s
@@ -1946,19 +1946,22 @@
 lic 
-void dele
+Object addI
 te
+m
 (Sim
@@ -1972,20 +1972,17 @@
 bEntity 
-sdbE
+e
 ntity) %7B
@@ -2008,26 +2008,365 @@
 on(%22
-Delete%22, sdbEntity
+Add  %22, entity);%0A        Assert.notNull(entity.getDomain(), %22Domain name should not be null%22);%0A        Assert.notNull(entity.getItemName(), %22Item name should not be null%22);%0A        sdb.putAttributes(new PutAttributesRequest(entity.getDomain(), entity.getItemName(), toReplaceableAttributeList(entity.getAttributes(), false)));%0A        return entity.getItem(
 );%0A 
@@ -2395,35 +2395,38 @@
   public Object 
-add
+update
 Item(SimpleDbEnt
@@ -2457,38 +2457,348 @@
 gOperation(%22
-Add  %22, entity
+Update%22, entity);%0A        Assert.notNull(entity.getDomain(), %22Domain name should not be null%22);%0A        Assert.notNull(entity.getItemName(), %22Item name should not be null%22);%0A        sdb.putAttributes(new PutAttributesRequest(entity.getDomain(), entity.getItemName(), toReplaceableAttributeList(entity.getAttributes(), true))
 );%0A        r
@@ -2853,33 +2853,27 @@
  public 
-Object updateI
+void dele
 te
-m
 (SimpleD
@@ -2904,36 +2904,36 @@
   logOperation(%22
-Upda
+Dele
 te%22, entity);%0A  
@@ -2938,38 +2938,161 @@
         
-return entity.getItem(
+Assert.notNull(entity.getDomain(), %22Domain name should not be null%22);%0A        Assert.notNull(entity.getItemName(), %22Item name should not be null%22
 );%0A    %7D
@@ -3709,24 +3709,378 @@
 ull;%0A    %7D%0A%0A
+    private List%3CReplaceableAttribute%3E toReplaceableAttributeList(Map%3CString, String%3E attributes, boolean replace) %7B%0A        List%3CReplaceableAttribute%3E result = new ArrayList%3C%3E();%0A        for (String key : attributes.keySet()) %7B%0A            result.add(new ReplaceableAttribute(key, attributes.get(key), replace));%0A        %7D%0A        return result;%0A    %7D%0A%0A
     private 

add & update operations