Java tutorial
/* * Copyright 2010-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package com.trk.aboutme.DynamoDB; import com.trk.aboutme.AboutMeApp; import com.trk.aboutme.Shelf; import com.trk.aboutme.DynamoDB.DynamoDBManagerBooks.Pages; import java.io.IOException; import java.util.ArrayList; import java.util.Set; import android.content.Context; import android.util.Log; import com.amazonaws.AmazonServiceException; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey; import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBAutoGeneratedKey; //import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper; import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapperConfig; import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBQueryExpression; import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBScanExpression; import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBVersionAttribute; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable; import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.PaginatedScanList; import com.amazonaws.services.dynamodbv2.model.AttributeDefinition; import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; import com.amazonaws.services.dynamodbv2.model.DeleteTableRequest; import com.amazonaws.services.dynamodbv2.model.DescribeTableRequest; import com.amazonaws.services.dynamodbv2.model.DescribeTableResult; import com.amazonaws.services.dynamodbv2.model.KeySchemaElement; import com.amazonaws.services.dynamodbv2.model.KeyType; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; import com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException; import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType; import com.amazonaws.services.dynamodbv2.model.ScanRequest; public class DynamoDBManagerUsers { private static final String TAG = "DynamoDBManager"; /* * Creates a table with the following attributes: * * Table name: testTableName Hash key: userNo type N Read Capacity Units: 10 * Write Capacity Units: 5 */ public static void createTable() { Log.d(TAG, "Create table called"); AmazonDynamoDBClient ddb = Shelf.clientManager.ddb(); KeySchemaElement kse = new KeySchemaElement().withAttributeName("userNo").withKeyType(KeyType.HASH); AttributeDefinition ad = new AttributeDefinition().withAttributeName("userNo") .withAttributeType(ScalarAttributeType.N); ProvisionedThroughput pt = new ProvisionedThroughput().withReadCapacityUnits(10l) .withWriteCapacityUnits(5l); CreateTableRequest request = new CreateTableRequest().withTableName(Constants.TEST_TABLE_NAME) .withKeySchema(kse).withAttributeDefinitions(ad).withProvisionedThroughput(pt); try { Log.d(TAG, "Sending Create table request"); ddb.createTable(request); Log.d(TAG, "Create request response successfully recieved"); } catch (AmazonServiceException ex) { Log.e(TAG, "Error sending create table request", ex); Shelf.clientManager.wipeCredentialsOnAuthError(ex); } } /* * Retrieves the table description and returns the table status as a string. */ public static String getTestTableStatus() { try { AmazonDynamoDBClient ddb = Shelf.clientManager.ddb(); DescribeTableRequest request = new DescribeTableRequest().withTableName(Constants.TEST_TABLE_NAME); DescribeTableResult result = ddb.describeTable(request); String status = result.getTable().getTableStatus(); return status == null ? "" : status; } catch (ResourceNotFoundException e) { } catch (AmazonServiceException ex) { Shelf.clientManager.wipeCredentialsOnAuthError(ex); } return ""; } /* * Scans the table and returns the list of users. */ public static ArrayList<UserPreference> getUserList() { AmazonDynamoDBClient ddb = Shelf.clientManager.ddb(); DynamoDBMapper mapper = new DynamoDBMapper(ddb); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); try { PaginatedScanList<UserPreference> result = mapper.scan(UserPreference.class, scanExpression); ArrayList<UserPreference> resultList = new ArrayList<UserPreference>(); for (UserPreference up : result) { resultList.add(up); } return resultList; } catch (AmazonServiceException ex) { Shelf.clientManager.wipeCredentialsOnAuthError(ex); } return null; } /* * Retrieves all of the attribute/value pairs for the specified user. */ public static UserPreference getUserPreference(int userNo) { AmazonDynamoDBClient ddb = Shelf.clientManager.ddb(); DynamoDBMapper mapper = new DynamoDBMapper(ddb); try { UserPreference userPreference = mapper.load(UserPreference.class, userNo); return userPreference; } catch (AmazonServiceException ex) { Shelf.clientManager.wipeCredentialsOnAuthError(ex); } return null; } /* * Updates one attribute/value pair for the specified user. */ public static String UpdateOwnLoginPref(UserPreference updateUserPreference) { String result = "true"; AmazonDynamoDBClient ddb = Shelf.clientManager.ddb(); DynamoDBMapper mapper = new DynamoDBMapper(ddb); try { String id = AboutMeApp.getPreferences().getUserId(); if ((id.equals(updateUserPreference.getId())) == false) mapper.save(updateUserPreference); AboutMeApp.getPreferences().setUserId(updateUserPreference.getId()); AboutMeApp.getPreferences().setUserDisplayname(updateUserPreference.getMdisplayName()); AboutMeApp.getPreferences().setUserProfilephoto(updateUserPreference.getMprofilePhoto()); return result; } catch (AmazonServiceException ex) { Shelf.clientManager.wipeCredentialsOnAuthError(ex); return result; } } public static void ThreadUpdateUser(UserPreference pref, ResultListener<String> resultListener, Context context, boolean showProgressBar) { new DynoDBAsyncTask<Pages, Void, String>(new DynamoDBManagerUsers.UpdateUser(pref), resultListener, context, showProgressBar).execute(); } private static class UpdateUser implements Command<String> { private UserPreference m_pref; public UpdateUser(UserPreference pref) { m_pref = pref; } @Override public String execute() throws IOException, IllegalStateException { return UpdateOwnLoginPref(m_pref); } } /* * Deletes the specified user and all of its attribute/value pairs. */ public static void deleteUser(UserPreference deleteUserPreference) { AmazonDynamoDBClient ddb = Shelf.clientManager.ddb(); DynamoDBMapper mapper = new DynamoDBMapper(ddb); try { mapper.delete(deleteUserPreference); } catch (AmazonServiceException ex) { Shelf.clientManager.wipeCredentialsOnAuthError(ex); } } /* * Deletes the test table and all of its users and their attribute/value * pairs. */ public static void cleanUp() { AmazonDynamoDBClient ddb = Shelf.clientManager.ddb(); DeleteTableRequest request = new DeleteTableRequest().withTableName(Constants.TEST_TABLE_NAME); try { ddb.deleteTable(request); } catch (AmazonServiceException ex) { Shelf.clientManager.wipeCredentialsOnAuthError(ex); } } @DynamoDBTable(tableName = "TestUserPreference") public static class UserPreference { @DynamoDBHashKey private String id; // Google ID which will be unique @DynamoDBAttribute(attributeName = "profilephoto") private String mprofilePhoto; @DynamoDBAttribute(attributeName = "displayname") private String mdisplayName; @DynamoDBAttribute(attributeName = "personalbook") private Set<String> mpersonalBook; @DynamoDBAttribute(attributeName = "datejoined") private String m_dateJoined; @DynamoDBAttribute(attributeName = "favcatogories") private Set<String> m_favCatagories; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getMprofilePhoto() { return mprofilePhoto; } public void setMprofilePhoto(String mprofilePhoto) { this.mprofilePhoto = mprofilePhoto; } public String getMdisplayName() { return mdisplayName; } public void setMdisplayName(String mdisplayName) { this.mdisplayName = mdisplayName; } public Set<String> getMpersonalBook() { return mpersonalBook; } public void setMpersonalBook(Set<String> mpersonalBook) { this.mpersonalBook = mpersonalBook; } public String getM_dateJoined() { return m_dateJoined; } public void setM_dateJoined(String m_dateJoined) { this.m_dateJoined = m_dateJoined; } public Set<String> getM_favCatagories() { return m_favCatagories; } public void setM_favCatagories(Set<String> m_favCatagories) { this.m_favCatagories = m_favCatagories; } } }