List of usage examples for com.amazonaws.services.dynamodbv2.model PutItemRequest setConditionExpression
public void setConditionExpression(String conditionExpression)
A condition that must be satisfied in order for a conditional PutItem
operation to succeed.
From source file:com.vitembp.embedded.configuration.CloudConfigSync.java
License:Open Source License
/** * Registers the device's ID to the database. * @throws IOException //from w w w . j ava2s . c om */ private static void registerDevice() throws IOException { // get configuration string String config; try { config = SystemConfig.getConfig().writeToString(); } catch (XMLStreamException ex) { throw new IOException("Could not generate configuration string.", ex); } // add holds data to store in table. Map<String, AttributeValue> itemToSet = new HashMap<>(); PutItemRequest pir = new PutItemRequest(); pir.setConditionExpression("attribute_not_exists(ID)"); // add the system ID itemToSet.put("ID", new AttributeValue(SystemConfig.getConfig().getSystemUUID().toString())); itemToSet.put("CONFIG", new AttributeValue(config)); itemToSet.put("UPDATED", new AttributeValue(Boolean.toString(false))); pir.setTableName("DEVICES"); pir.setItem(itemToSet); try { CloudConfigSync.client.putItem(pir); CloudConfigSync.deviceRegistered = true; LOGGER.info("Device successfully registered."); } catch (ConditionalCheckFailedException e) { CloudConfigSync.deviceRegistered = true; LOGGER.debug("Device already registered."); } catch (ResourceNotFoundException e) { LOGGER.error("The database does not contain the device index table.", e); } catch (AmazonServiceException e) { LOGGER.error("Exception occurred writing to database.", e); } }