package org.surfnet.widgets.synd;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;

import org.surfnet.widgets.model.Widget;

public class SyndFeedBuilder {
  private List<Widget> widgets = Collections.emptyList();
  private String source;
  private int limit;

  public SyndFeedBuilder(List<Widget> widgets) {
    if (widgets != null) {
      this.widgets = widgets;
    }
  }

  public SyndFeedBuilder setSource(String source) {
    this.source = source;
    return this;
  }

  public SyndFeedBuilder limit(int i) {
    this.limit = i;
    return this;
  }

  public SyndFeed build() {
    final SyndFeedImpl syndFeed = new SyndFeedImpl();
    syndFeed.setAuthor(source);
    syndFeed.setFeedType("atom_1.0");
    List<SyndEntry> entries = new ArrayList<SyndEntry>();

    int i=0;
    while (i < limit && i < widgets.size()) {
      Widget w = widgets.get(i++);
      SyndEntry entry = new SyndEntryImpl();
      if (w.getUri() != null) {
        entry.setLink(w.getUri().toString());
        entry.setUri(w.getUri().toString());
      }
      entry.setTitle(w.getName());
      final SyndContentImpl syndContent = new SyndContentImpl();
      syndContent.setValue(w.getDescription());
      entry.setDescription(syndContent);
      entry.setUpdatedDate(w.getUpdated());
      entry.setPublishedDate(w.getCreated());
      entries.add(entry);
    }
    syndFeed.setEntries(entries);
    return syndFeed;
  }
}

@@ -110,16 +110,65 @@
 .List;%0A%0A
+import com.sun.syndication.feed.module.DCModule;%0A
 import c
@@ -1641,32 +1641,390 @@
 .getCreated());%0A
+      entry.setAuthor(w.getAuthor());%0A%0A      /*%0A       Here we use the DC module to set a Rights element.%0A       However, Atom 1.0 also specifies a native Rights element.%0A       We should probably use Rome's pluginManager to support this.%0A      */%0A      DCModule dcModule = (DCModule) entry.getModule(DCModule.URI);%0A      dcModule.setRights(w.getLicense());%0A
       entries.ad

Use author and rights fields from the Widget in the atom feed.