class ConfigPropertyServiceImpl implements ConfigPropertyService {

	@Override
	public ConfigPropertyRollbackPreview getRollbackPreview(String timestamp) throws Exception {

		ConfigPropertyAuditTrail trail = listAllConfigPropertyAudits(timestamp);
		List<ConfigPropertyRollBackItem> rollBackItemList = new ArrayList<>();

		for (ConfigPropertyAuditItem auditItem : trail.getConfigPropertyAudit()) {
			for (ConfigPropertyDataChange changeItem : auditItem.getConfigPropertyChangeList()) {

				ConfigPropertyRollBackItem existingRollBackItem = getRollBackItemIfExists(rollBackItemList, changeItem);

				if (null == existingRollBackItem) {
					ConfigPropertyRollBackItem rollBackItem = new ConfigPropertyRollBackItem();
					rollBackItem.setApplication(changeItem.getApplication());
					rollBackItem.setConfigKey(changeItem.getConfigKey());
					rollBackItem.setPresentConfigValue(
							changeItem.getNewConfigValue() == null ? "" : changeItem.getNewConfigValue());
					rollBackItem.setFutureConfigValue(
							changeItem.getOldConfigValue() == null ? "" : changeItem.getOldConfigValue());
					rollBackItemList.add(rollBackItem);
				} else {
					existingRollBackItem.setFutureConfigValue(
							changeItem.getOldConfigValue() == null ? "" : changeItem.getOldConfigValue());
				}

			}

		}
		// If any trails got missed out by someone doing manual updates in RDS, the
		// audit trail may not match exactly with the present state of the DB
		List<ConfigPropertyRollBackItem> refinedRollBackItemList = syncWithPresentSystemState(rollBackItemList);

		ConfigPropertyRollbackPreview preview = new ConfigPropertyRollbackPreview();
		preview.setRollbackChangeSet(refinedRollBackItemList);
		preview.setRestoreToTimestamp(timestamp);

		return preview;

	}

}