List of usage examples for com.amazonaws.services.dynamodbv2.document Item hasAttribute
public boolean hasAttribute(String attrName)
From source file:com.exorath.service.lastserver.dynamodb.DynamoDBService.java
License:Apache License
@Override public GetResult getLastServer(UUID playerId) { GetItemSpec spec = new GetItemSpec().withPrimaryKey(PRIM_KEY, playerId.toString()); Item item = table.getItem(spec); logger.info("Retrieved the following last server data for player " + playerId + ": " + item); if (item == null || !item.hasAttribute(GAMEID_ATTR)) { return new GetResult(null, null, null); } else {/*from ww w . j a va 2 s. c o m*/ String gameId = null; String mapId = null; String flavorId = null; if (item.hasAttribute(GAMEID_ATTR)) { gameId = item.getString(GAMEID_ATTR); } if (item.hasAttribute(MAPID_ATTR)) { mapId = item.getString(MAPID_ATTR); } if (item.hasAttribute(FLAVORID_ATTR)) { flavorId = item.getString(FLAVORID_ATTR); } return new GetResult(gameId, mapId, flavorId); } }
From source file:com.exorath.service.lobbymsg.impl.DynamoDBService.java
License:Apache License
@Override public Map<String, Message> fetchMessagesByGameId() throws Exception { Item item = db.getTable(tableName).getItem(getMapMessagesSpec()); if (item == null || !item.hasAttribute(MSGS_FIELD)) return new HashMap<>(); Map<String, Map<String, Object>> messages = item.getMap(MSGS_FIELD); return mapMessages(messages); }
From source file:com.exorath.service.party.service.DynamoDatabaseProvider.java
License:Apache License
/** * Convert an item containing information about a party into a party object * * @param item The item containing the information * @return A party object containing all possible information from the item *///from w w w .ja v a2 s. com private Party getPartyFromItem(Item item) { String owner_uuid = item.hasAttribute(OWNER_UUID) ? item.getString(OWNER_UUID) : null; String party_uuid = item.hasAttribute(PARTY_UUID) ? item.getString(PARTY_UUID) : null; String serverId = item.hasAttribute(SERVER_ID) ? item.getString(SERVER_ID) : null; ArrayList<String> members = item.hasAttribute(MEMBERS) ? new ArrayList<>(item.getStringSet(MEMBERS)) : null; Long expiry = item.hasAttribute(EXPIRE) ? item.getLong(EXPIRE) : null; return new Party(party_uuid, owner_uuid, serverId, members, expiry); }
From source file:com.exorath.service.treasurehunt.dynamodb.DynamoDBService.java
License:Apache License
@Override public GetResult getTreasures(UUID playerId) { GetItemSpec spec = new GetItemSpec().withPrimaryKey(PRIM_KEY, playerId.toString()); Item item = table.getItem(spec); logger.info("Retrieved the following treasures for player " + playerId + ": " + item); if (item == null || !item.hasAttribute(TREASURES_FIELD)) { return new GetResult(new Treasure[0]); } else {/*w w w.j a va 2 s . c om*/ List list = item.getList(TREASURES_FIELD); Treasure[] treasures = new Treasure[list.size()]; int i = 0; for (Object treasure : list) { treasures[i++] = new Treasure(treasure.toString()); } return new GetResult(treasures); } }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecRemove.java
License:LGPL
@Test public void test_remove_singlePath() throws Exception { // setup//w ww . ja va2 s .c o m table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE) .put("a", ImmutableMap.of("a", 2, "b", true)).build())); // setup String patchExpression = "[ { \"op\": \"remove\", \"path\": \"/a\" } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder actual = jsonPatch.get(); UpdateItemExpressionSpec actualSpec = actual.buildForUpdate(); table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, actualSpec); // verify Item item = table.getItem(PK); Assert.assertTrue(item.hasAttribute("key")); Assert.assertEquals(item.getString("key"), "keyValue"); Assert.assertFalse(item.hasAttribute("a")); }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecRemove.java
License:LGPL
@Test public void test_remove_nestedPath() throws Exception { // setup/*w w w .j a va2s. co m*/ table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE) .put("a", ImmutableMap.of("a", 2, "b", true)).build())); // setup String patchExpression = "[ { \"op\": \"remove\", \"path\": \"/a/a\" } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder actual = jsonPatch.get(); UpdateItemExpressionSpec actualSpec = actual.buildForUpdate(); table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, actualSpec); // verify Item item = table.getItem(PK); Assert.assertTrue(item.hasAttribute("key")); Assert.assertEquals(item.getString("key"), "keyValue"); Assert.assertTrue(item.hasAttribute("a")); Assert.assertTrue(item.getRawMap("a").containsKey("b")); Assert.assertEquals(item.getRawMap("a").get("b"), true); Assert.assertFalse(item.getRawMap("a").containsKey("a")); }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecRemove.java
License:LGPL
@Test public void test_remove_absentPath() throws Exception { // setup// w w w. j a v a2 s . c om table.putItem(Item.fromMap( ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE).put("a", "b").build())); // setup String patchExpression = "[ { \"op\": \"remove\", \"path\": \"/c\" } ]"; // $.c does not exist in target JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder actual = jsonPatch.get(); UpdateItemExpressionSpec actualSpec = actual.buildForUpdate(); table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, actualSpec); // verify Item item = table.getItem(PK); Assert.assertTrue(item.hasAttribute("key")); Assert.assertEquals(item.getString("key"), "keyValue"); Assert.assertTrue(item.hasAttribute("a")); Assert.assertEquals(item.getString("a"), "b"); }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java
License:LGPL
@Test public void testReplaceSinglePathNumberExtant() throws Exception { // setup//from w ww .j a va 2 s . c o m table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE) .put("a", "peekaboo").build())); String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a\", \"value\": 1 } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder builder = jsonPatch.get(); UpdateItemExpressionSpec spec = builder.buildForUpdate(); UpdateItemOutcome out = table.updateItem(new UpdateItemSpec().withPrimaryKey(KEY_ATTRIBUTE_NAME, VALUE) .withExpressionSpec(spec).withReturnValues(ReturnValue.ALL_OLD)); Item oldItem = Item.fromMap(InternalUtils.toSimpleMapValue(out.getUpdateItemResult().getAttributes())); Assert.assertTrue(oldItem.hasAttribute("a")); Assert.assertEquals(oldItem.getString("a"), "peekaboo"); // verify Item item = table.getItem(PK); Assert.assertTrue(item.hasAttribute("key")); Assert.assertEquals(item.getString("key"), "keyValue"); Assert.assertTrue(item.hasAttribute("a")); Assert.assertEquals(item.getNumber("a").longValue(), 1L); }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java
License:LGPL
@Test public void test_replace_existingNestedPath_string() throws Exception { // setup/* w ww. j av a 2 s . c o m*/ table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE) .put("a", ImmutableMap.of("a", 2L, "b", true)).build())); String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a/b\", \"value\": \"bar\" } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder builder = jsonPatch.get(); UpdateItemExpressionSpec spec = builder.buildForUpdate(); table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, spec); // verify Item item = table.getItem(PK); Assert.assertTrue(item.hasAttribute("key")); Assert.assertEquals(item.getString("key"), "keyValue"); Assert.assertTrue(item.hasAttribute("a")); Assert.assertTrue(item.getRawMap("a").containsKey("a")); Assert.assertEquals(((BigDecimal) item.getRawMap("a").get("a")).longValue(), 2L); Assert.assertTrue(item.getRawMap("a").containsKey("b")); Assert.assertEquals(item.getRawMap("a").get("b"), "bar"); }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java
License:LGPL
@Test public void test_replace_singlePath_stringSet() throws Exception { // setup//from www . j a va 2 s . com table.putItem(Item.fromMap( ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE).put("a", 1L).build())); String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a\", \"value\": [\"foo\",\"bar\"] } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder builder = jsonPatch.get(); UpdateItemExpressionSpec spec = builder.buildForUpdate(); table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, spec); // verify Item item = table.getItem(PK); Assert.assertTrue(item.hasAttribute("key")); Assert.assertEquals(item.getString("key"), "keyValue"); Assert.assertTrue(item.hasAttribute("a")); Assert.assertTrue(item.getList("a").contains("foo")); Assert.assertTrue(item.getList("a").contains("bar")); }