Coverage Report - net.sourceforge.pebble.web.view.impl.ResponseFeedView
 
Classes in this File Line Coverage Branch Coverage Complexity
ResponseFeedView
0%
0/80
0%
0/10
2
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  *
 8  
  *   - Redistributions of source code must retain the above copyright
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 package net.sourceforge.pebble.web.view.impl;
 33  
 
 34  
 import com.sun.syndication.feed.synd.*;
 35  
 import net.sourceforge.pebble.Constants;
 36  
 import net.sourceforge.pebble.api.decorator.FeedDecorator;
 37  
 import net.sourceforge.pebble.domain.*;
 38  
 import net.sourceforge.pebble.security.PebbleUserDetails;
 39  
 
 40  
 import java.util.*;
 41  
 
 42  
 /**
 43  
  * The view for the feed responses
 44  
  *
 45  
  * @author James Roper
 46  
  */
 47  
 public class ResponseFeedView extends AbstractRomeFeedView {
 48  
 
 49  
   public ResponseFeedView(FeedType feedType) {
 50  0
     super(feedType);
 51  0
   }
 52  
 
 53  
   @SuppressWarnings("unchecked")
 54  
   protected SyndFeed getFeed() {
 55  0
     Blog blog = (Blog) getModel().get(Constants.BLOG_KEY);
 56  0
     BlogEntry blogEntry = (BlogEntry) getModel().get(Constants.BLOG_ENTRY_KEY);
 57  0
     List<Response> responses = (List<Response>) getModel().get(Constants.RESPONSES);
 58  
 
 59  0
     Collection<FeedDecorator> feedDecorators = blog.getFeedDecorators();
 60  
 
 61  0
     SyndFeed syndFeed = new SyndFeedImpl();
 62  0
     syndFeed.setUri(generateId(blog, null, "responses"));
 63  
 
 64  0
     if (blogEntry == null) {
 65  0
       populateFeedInfo(blog, syndFeed);
 66  
     } else {
 67  0
       populateFeedInfo(blogEntry, syndFeed);
 68  
     }
 69  
 
 70  0
     if (blog.getImage() != null) {
 71  0
       SyndImage syndImage = new SyndImageImpl();
 72  0
       syndImage.setUrl(blog.getImage());
 73  0
       syndImage.setTitle(blog.getName());
 74  0
       syndImage.setUrl(blog.getUrl());
 75  0
       syndFeed.setImage(syndImage);
 76  
       // Unfortunately, ROME doesn't support the logo attribute for Atom in a feed agnostic way
 77  
     }
 78  0
     syndFeed.setLanguage(blog.getLanguage());
 79  
 
 80  0
     List<SyndEntry> feedEntries = new ArrayList<SyndEntry>();
 81  
 
 82  0
     for (Response response : responses) {
 83  0
       SyndEntry feedEntry = convertResponse(response);
 84  0
       for (FeedDecorator feedDecorator : feedDecorators) {
 85  0
         feedDecorator.decorate(feedEntry, blog, response);
 86  
       }
 87  0
       feedEntries.add(feedEntry);
 88  0
     }
 89  0
     syndFeed.setEntries(feedEntries);
 90  0
     for (FeedDecorator feedDecorator : feedDecorators) {
 91  0
       feedDecorator.decorate(syndFeed, blog, blogEntry);
 92  
     }
 93  0
     return syndFeed;
 94  
   }
 95  
 
 96  
   private SyndEntry convertResponse(Response response) {
 97  
 
 98  0
     SyndEntry feedEntry = new SyndEntryImpl();
 99  0
     feedEntry.setUri(generateId(response.getBlogEntry().getBlog(), response.getBlogEntry().getDate(), response.getGuid()));
 100  0
     feedEntry.setTitle(response.getTitle());
 101  0
     feedEntry.setLink(response.getPermalink());
 102  
 
 103  0
     SyndPerson entryAuthor = new SyndPersonImpl();
 104  0
     entryAuthor.setName(response.getSourceName());
 105  0
     entryAuthor.setUri(response.getSourceLink());
 106  0
     feedEntry.setAuthors(Collections.singletonList(entryAuthor));
 107  
 
 108  0
     feedEntry.setUpdatedDate(response.getDate());
 109  0
     feedEntry.setPublishedDate(response.getDate());
 110  
 
 111  0
     SyndContent content = new SyndContentImpl();
 112  0
     content.setType("text/html");
 113  0
     content.setValue(response.getContent());
 114  0
     feedEntry.setContents(Collections.singletonList(content));
 115  
 
 116  0
     return feedEntry;
 117  
   }
 118  
 
 119  
   private void populateFeedInfo(BlogEntry blogEntry, SyndFeed syndFeed) {
 120  0
     syndFeed.setTitle(blogEntry.getBlog().getName() + " - " + blogEntry.getTitle());
 121  
 
 122  0
     syndFeed.setLink(blogEntry.getPermalink());
 123  0
     SyndLink alternate = new SyndLinkImpl();
 124  0
     alternate.setHref(blogEntry.getPermalink());
 125  0
     alternate.setRel("alternate");
 126  0
     alternate.setType("text/html");
 127  0
     SyndLink self = new SyndLinkImpl();
 128  0
     self.setHref(blogEntry.getBlog().getUrl() + "responses/atom.xml?entry=" + blogEntry.getId());
 129  0
     self.setRel("self");
 130  0
     self.setType(getFeedType().getContentType());
 131  0
     syndFeed.setLinks(Arrays.asList(alternate, self));
 132  
 
 133  0
     syndFeed.setPublishedDate(blogEntry.getLastModified());
 134  
 
 135  0
     PebbleUserDetails userDetails = blogEntry.getUser();
 136  0
     SyndPerson author = new SyndPersonImpl();
 137  0
     author.setName(userDetails.getName());
 138  0
     author.setUri(userDetails.getWebsite());
 139  0
     syndFeed.setAuthors(Collections.singletonList(author));
 140  
 
 141  0
     syndFeed.setDescription(blogEntry.getTruncatedContent());
 142  0
     syndFeed.setCopyright(userDetails.getName());
 143  0
   }
 144  
 
 145  
   private void populateFeedInfo(Blog blog, SyndFeed syndFeed) {
 146  0
     syndFeed.setTitle(blog.getName() + " - Responses");
 147  
 
 148  0
     syndFeed.setLink(blog.getUrl());
 149  0
     SyndLink alternate = new SyndLinkImpl();
 150  0
     alternate.setHref(blog.getUrl());
 151  0
     alternate.setRel("alternate");
 152  0
     alternate.setType("text/html");
 153  0
     SyndLink self = new SyndLinkImpl();
 154  0
     self.setHref(blog.getUrl() + "responses/atom.xml");
 155  0
     self.setRel("self");
 156  0
     self.setType(getFeedType().getContentType());
 157  0
     syndFeed.setLinks(Arrays.asList(alternate, self));
 158  
 
 159  0
     syndFeed.setPublishedDate(blog.getDateOfLastResponse());
 160  
 
 161  0
     syndFeed.setAuthor(blog.getAuthor());
 162  0
     syndFeed.setCopyright(blog.getAuthor());
 163  0
     syndFeed.setDescription(blog.getDescription());
 164  0
   }
 165  
 
 166  
 }