Example usage for com.amazonaws.services.dynamodbv2.document ScanFilter ScanFilter

List of usage examples for com.amazonaws.services.dynamodbv2.document ScanFilter ScanFilter

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document ScanFilter ScanFilter.

Prototype

public ScanFilter(String attr) 

Source Link

Document

A <a href="http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html#DDB-Scan-request-ScanFilter" >scan filter</a>.

Usage

From source file:com.github.sdmcraft.slingdynamo.impl.DynamoDBResourceProvider.java

License:Open Source License

/**
 * Returns a resource from this resource provider or null if the resource provider cannot find it.
 * The table-name, id and child-ids are parsed out from the path and queried against dynamodb to fetch the specified resource.
 *
 * @param resolver the ResourceResolver to which the returned Resource is attached.
 * @param req the HttpServletRequest made to get this resource
 * @param path the path of the resource. The path is of the format &lt;table-name&gt;/&lt;id&gt;/[&lt;child-id1&gt;/.../&lt;child-idn&gt;]
 * @return the resource at the specified path if it exists else returns null
 *///from   w  w w  . j  a v  a2 s. c  o m
public Resource getResource(ResourceResolver resolver, HttpServletRequest req, String path) {
    Resource resource = null;

    try {
        Map<String, Object> resourceProps = new HashMap<String, Object>();
        ResourceMetadata resourceMetadata = new ResourceMetadata();
        resourceMetadata.setResolutionPath(path);

        if (!path.contains(".")) {
            if (path.length() > root.length()) {
                String subPath = path.substring(root.length() + 1);
                String[] subPathSplits = subPath.split("/");
                String table = subPathSplits[0];
                resourceMetadata.put("table", table);

                Table dbtable = dynamoDB.getTable(table);

                if (subPathSplits.length == 1) {
                    DescribeTableRequest describeTableRequest = new DescribeTableRequest(table);
                    DescribeTableResult describeTableResult = null;

                    describeTableResult = dynamoDBClient.describeTable(describeTableRequest);

                    Date creationDate = describeTableResult.getTable().getCreationDateTime();
                    long itemCount = describeTableResult.getTable().getItemCount();

                    resourceProps.put("creation-date", creationDate);
                    resourceProps.put("record-count", itemCount);
                    resourceProps.put("table-name", table);
                } else if (subPathSplits.length == 2) {
                    int id = Integer.parseInt(subPathSplits[1]);
                    ScanFilter idFilter = new ScanFilter("id").eq(id);
                    ItemCollection<ScanOutcome> items = dbtable.scan(idFilter);
                    Iterator<Item> itemItr = items.iterator();

                    Item item = itemItr.next();
                    resourceProps = itemToMap(item);
                } else if (subPathSplits.length > 2) {
                    int parent = Integer.parseInt(subPathSplits[1]);
                    Item item = null;

                    for (int i = 2; i < subPathSplits.length; i++) {
                        int child = Integer.parseInt(subPathSplits[i]);
                        ScanFilter parentFilter = new ScanFilter("parent").eq(parent);
                        ScanFilter childFilter = new ScanFilter("child_id").eq(child);
                        ItemCollection<ScanOutcome> items = dbtable.scan(parentFilter, childFilter);
                        Iterator<Item> itemItr = items.iterator();
                        item = itemItr.next();
                        parent = item.getInt("id");
                    }

                    resourceProps = itemToMap(item);
                }
            }

            resourceProps.put("hello", "world");

            ModifiableValueMapDecorator valueMap = new ModifiableValueMapDecorator(resourceProps);

            resource = new DynamoDBResource(resolver, resourceMetadata, valueMap, resourceType);
        }
    } catch (Throwable ex) {
        LOGGER.error(ex.getMessage(), ex);
        throw new RuntimeException(ex);
    }

    return resource;
}

From source file:com.github.sdmcraft.slingdynamo.impl.DynamoDBResourceProvider.java

License:Open Source License

/**
 * Gets the children./*  ww  w . j a v a  2  s .co m*/
 *
 * @param dbtable the dbtable
 * @param parent the parent
 * @param childIds the child ids
 * @param path the path
 * @param resolver the resolver
 * @return the children
 */
private List<Resource> getChildren(Table dbtable, int parent, Object[] childIds, String path,
        ResourceResolver resolver) {
    ScanFilter idFilter = new ScanFilter("parent").in(parent);
    ScanFilter childIdsFilter = new ScanFilter("child_id").in(childIds);
    ItemCollection<ScanOutcome> items = dbtable.scan(idFilter, childIdsFilter);
    Iterator<Item> itemItr = items.iterator();
    List<Resource> children = new ArrayList<Resource>();

    while (itemItr.hasNext()) {
        Item item = itemItr.next();
        Iterable<Entry<String, Object>> attributes = item.attributes();
        Iterator<Entry<String, Object>> attributesItr = attributes.iterator();
        Map<String, Object> resourceProps = new HashMap<String, Object>();

        while (attributesItr.hasNext()) {
            Entry<String, Object> attribute = attributesItr.next();
            resourceProps.put(attribute.getKey(), attribute.getValue());
        }

        ModifiableValueMapDecorator valueMap = new ModifiableValueMapDecorator(resourceProps);
        ResourceMetadata resourceMetadata = new ResourceMetadata();
        resourceMetadata.setResolutionPath(path + '/' + resourceProps.get("child_id"));
        resourceMetadata.put("table", dbtable.getTableName());

        children.add(new DynamoDBResource(resolver, resourceMetadata, valueMap, resourceType));
    }

    return children;
}