/**
  * Need to do this because the responses that tally returns don't actually link users with their
  * response values. In order to do an export under the legacy code, we have to explicitly handle
  * the bucketing system.
  *
  * @param resource - the resource being exported
  * @return NestedBucketStorageSystem
  */
 protected static NestedBucketStorageSystem getBucketSystem(final Resource resource) {
   if (null == resource) {
     return null;
   }
   NestedBucketStorageSystem bucketSystem;
   bucketSystem = resource.adaptTo(NestedBucketStorageSystem.class);
   if (bucketSystem != null) {
     bucketSystem.setBucketPostfix(NestedBucketStorageSystem.DEFAULT_BUCKET_POSTFIX);
   }
   return bucketSystem;
 }
  protected static Map<String, Map<Long, ResponseValue>> getTallyResponses(Resource tallyResource)
      throws JSONException {
    Map<String, Map<Long, ResponseValue>> returnValue =
        new HashMap<String, Map<Long, ResponseValue>>();
    final ResourceResolver resolver = tallyResource.getResourceResolver();
    final String tallyPath = tallyResource.getPath();
    if (!tallyPath.startsWith("/content/usergenerated")) {
      tallyResource = resolver.resolve("/content/usergenerated" + tallyPath);
    }
    final Resource responsesNode = tallyResource.getChild(TallyConstants.RESPONSES_PATH);
    if (responsesNode == null) {
      return null;
    }
    NestedBucketStorageSystem bucketSystem = getBucketSystem(responsesNode);
    if (null == bucketSystem) {
      return null;
    }

    final Iterator<Resource> buckets = bucketSystem.listBuckets();
    while (buckets.hasNext()) {
      final Resource bucketResource = buckets.next();
      final Node bucketNode = bucketResource.adaptTo(Node.class);
      try {
        final NodeIterator userNodesInBucket = bucketNode.getNodes();
        while (userNodesInBucket.hasNext()) {
          final Node userNode = userNodesInBucket.nextNode();
          final NestedBucketStorageSystem userBucketSystem =
              getBucketSystem(resolver.getResource(userNode.getPath()));
          final Iterator<Resource> userBuckets = userBucketSystem.listBuckets();
          final Map<Long, ResponseValue> userReturnValue = new HashMap<Long, ResponseValue>();
          while (userBuckets.hasNext()) {
            final NodeIterator userResponses = userBuckets.next().adaptTo(Node.class).getNodes();
            while (userResponses.hasNext()) {
              final Node responseNode = userResponses.nextNode();
              final Long responseTimestamp = responseNode.getProperty(TIMESTAMP_PROPERTY).getLong();
              userReturnValue.put(
                  responseTimestamp,
                  new PollResponse(responseNode.getProperty(RESPONSE_PROPERTY).getString()));
            }
          }
          returnValue.put(userNode.getName(), userReturnValue);
        }
      } catch (final RepositoryException e) {
        throw new JSONException(
            "Error trying to read user responses from bucket in " + bucketResource.getPath(), e);
      }
    }
    return returnValue;
  }