@Override
  public Object serialize(Result result) {
    if (result.getStatus().isErrorState()) {
      return serializeError(result.getStatus());
    } else {
      CSVPrinter csvPrinter = null;
      try {
        // A StringBuffer to store the CSV-formatted document while building it.  It may be
        // necessary to use file-based storage if the data set is expected to be really large.
        StringBuffer buffer = new StringBuffer();

        TreeNode<Resource> root = result.getResultTree();

        if (root != null) {
          csvPrinter = new CSVPrinter(buffer, CSVFormat.DEFAULT);

          // TODO: recursively handle tree structure, for now only handle single level of detail
          if ("true".equalsIgnoreCase(root.getStringProperty("isCollection"))) {
            List<String> fieldNameOrder = processHeader(csvPrinter, root);

            Collection<TreeNode<Resource>> children = root.getChildren();
            if (children != null) {
              // Iterate over the child nodes of the collection an add each as a new record in the
              // CSV document.
              for (TreeNode<Resource> child : children) {
                processRecord(csvPrinter, child, fieldNameOrder);
              }
            }
          }
        }

        return buffer.toString();
      } catch (IOException e) {
        //todo: exception handling.  Create ResultStatus 500 and call serializeError
        throw new RuntimeException("Unable to serialize to csv: " + e, e);
      } finally {
        Closeables.closeSilently(csvPrinter);
      }
    }
  }
Serialize the result into a CSV-formatted text document.
<p/>
It is expected that the result set is a collection of flat resources - no sub-resources will be
included in the output.  The root of the tree structure may have a column map (csv_column_map)
and a column order (csv_column_order) property set to indicate the header record and ordering
of the columns.
<p/>
The csv_column_map is a map of resource property names to header descriptive names.  If not
specified, a header record will not be serialized.
<p/>
The csv_column_order is a list of resource property names declaring the order of the columns.
If not specified, the order will be taken from the key order of csv_column_map or the "natural"
ordering of the resource property names, both may be unpredictable.

@param result internal result
@return a String containing the CSV-formatted document
ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/CsvSerializer.java