Search in sources :

Example 6 with Document

use of com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document in project aws-sdk-android by aws-amplify.

the class Table method getItem.

private Document getItem(Key key, GetItemOperationConfig config) {
    final GetItemRequest request = new GetItemRequest(tableName, key);
    if (config != null) {
        request.setConsistentRead(config.isConsistentRead());
        if (config.getAttributesToGet() != null && config.getAttributesToGet().size() > 0) {
            request.setAttributesToGet(config.getAttributesToGet());
        }
    }
    Table.appendDynamoDBDocumentUserAgentString(request);
    final GetItemResult response = this.client.getItem(request);
    final Map<String, AttributeValue> item = response.getItem();
    if (item == null || item.size() == 0) {
        return null;
    }
    final Document returnDocument = fromAttributeMap(item);
    returnDocument.commit();
    return returnDocument;
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) GetItemResult(com.amazonaws.services.dynamodbv2.model.GetItemResult) Document(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Example 7 with Document

use of com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document in project aws-sdk-android by aws-amplify.

the class Table method makeKey.

private Key makeKey(Document doc) {
    final Key key = new Key();
    for (final Entry<String, KeyDescription> kvp : this.keys.entrySet()) {
        final String keyName = kvp.getKey();
        final KeyDescription description = kvp.getValue();
        final DynamoDBEntry value = doc.get(keyName);
        if (value == null) {
            throw new IllegalStateException("no value for key " + keyName);
        }
        final Primitive primitive = value.asPrimitive();
        if (primitive == null) {
            throw new IllegalStateException("Key attribute " + keyName + " must be a Primitive type");
        }
        if (primitive.getType() != description.getType()) {
            throw new IllegalStateException("Key attribute " + keyName + " must be of type " + description.getType());
        }
        key.put(keyName, primitive.convertToAttributeValue());
    }
    return key;
}
Also used : DynamoDBEntry(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBEntry) Primitive(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive) KeyDescription(com.amazonaws.mobileconnectors.dynamodbv2.document.internal.KeyDescription) Key(com.amazonaws.mobileconnectors.dynamodbv2.document.internal.Key)

Example 8 with Document

use of com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document in project aws-sdk-android by aws-amplify.

the class Table method putItem.

/**
 * Puts a document into DynamoDB.
 *
 * @param document the {@link Document}.
 * @param config {@link PutItemOperationConfig}.
 * @return the dynamodb {@link Document}.
 */
public Document putItem(Document document, PutItemOperationConfig config) {
    final PutItemRequest request = new PutItemRequest();
    request.setTableName(this.tableName);
    request.setItem(toAttributeMap(document));
    if (config != null && config.getReturnValue().compareTo(ReturnValue.ALL_OLD) == 0) {
        request.setReturnValues(ReturnValue.ALL_OLD);
    }
    Table.appendDynamoDBDocumentUserAgentString(request);
    final PutItemResult result = client.putItem(request);
    Document ret = null;
    if (config != null && config.getReturnValue().compareTo(ReturnValue.ALL_OLD) == 0) {
        final Map<String, AttributeValue> values = result.getAttributes();
        ret = this.fromAttributeMap(values);
        ret.commit();
    }
    return ret;
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) PutItemRequest(com.amazonaws.services.dynamodbv2.model.PutItemRequest) PutItemResult(com.amazonaws.services.dynamodbv2.model.PutItemResult) Document(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document)

Example 9 with Document

use of com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document in project aws-sdk-android by aws-amplify.

the class JsonUtils method writeJson.

private static void writeJson(DynamoDBEntry entry, JsonWriter writer) throws IOException {
    if (entry instanceof Document) {
        writer.beginObject();
        final Document doc = (Document) entry;
        for (final Entry<String, DynamoDBEntry> docEntry : doc.entrySet()) {
            final String key = docEntry.getKey();
            final DynamoDBEntry value = docEntry.getValue();
            writer.name(key);
            writeJson(value, writer);
        }
        writer.endObject();
        return;
    }
    if (entry instanceof Primitive) {
        final Primitive p = (Primitive) entry;
        writePrimitive(p.getType(), writer, p);
        return;
    }
    if (entry instanceof PrimitiveList) {
        final PrimitiveList pl = (PrimitiveList) entry;
        writer.beginArray();
        for (final DynamoDBEntry e : pl.getEntries()) {
            writePrimitive(pl.getType(), writer, e);
        }
        writer.endArray();
        return;
    }
    if (entry instanceof DynamoDBList) {
        final DynamoDBList pl = (DynamoDBList) entry;
        writer.beginArray();
        for (final DynamoDBEntry e : pl.getEntries()) {
            writeJson(e, writer);
        }
        writer.endArray();
        return;
    }
    if (entry instanceof DynamoDBBool) {
        writer.value(((DynamoDBBool) entry).asBoolean());
        return;
    }
    if (entry instanceof DynamoDBNull) {
        writer.nullValue();
        return;
    }
    throw new JsonParseException("unable to convert to json " + entry);
}
Also used : DynamoDBEntry(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBEntry) Primitive(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive) JsonPrimitive(com.google.gson.JsonPrimitive) PrimitiveList(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.PrimitiveList) DynamoDBBool(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBBool) DynamoDBNull(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBNull) Document(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document) JsonParseException(com.google.gson.JsonParseException) DynamoDBList(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBList)

Aggregations

Document (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document)8 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)4 Primitive (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive)3 DynamoDBBool (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBBool)2 DynamoDBEntry (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBEntry)2 DynamoDBList (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBList)2 JsonParseException (com.google.gson.JsonParseException)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 ArrayList (java.util.ArrayList)2 DynamoDBNull (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBNull)1 PrimitiveList (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.PrimitiveList)1 Key (com.amazonaws.mobileconnectors.dynamodbv2.document.internal.Key)1 KeyDescription (com.amazonaws.mobileconnectors.dynamodbv2.document.internal.KeyDescription)1 Condition (com.amazonaws.services.dynamodbv2.model.Condition)1 DeleteItemRequest (com.amazonaws.services.dynamodbv2.model.DeleteItemRequest)1 DeleteItemResult (com.amazonaws.services.dynamodbv2.model.DeleteItemResult)1 GetItemRequest (com.amazonaws.services.dynamodbv2.model.GetItemRequest)1 GetItemResult (com.amazonaws.services.dynamodbv2.model.GetItemResult)1 PutItemRequest (com.amazonaws.services.dynamodbv2.model.PutItemRequest)1 PutItemResult (com.amazonaws.services.dynamodbv2.model.PutItemResult)1