org.zlogic.voidreader.feed.FeedItem.java Source code

Java tutorial

Introduction

Here is the source code for org.zlogic.voidreader.feed.FeedItem.java

Source

/*
 * Void Reader project.
 * Licensed under Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
 * Author: Dmitry Zolotukhin <zlogic@gmail.com>
 */
package org.zlogic.voidreader.feed;

import com.sun.syndication.feed.synd.SyndEntry;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Objects;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;
import org.apache.commons.io.IOUtils;
import org.stringtemplate.v4.ST;

/**
 * A single feed item representation. If it's just downloaded, contains HTML and
 * text data generated by StringTemplate. If it's loaded from XML, contains the
 * last known item state: sent text, sent PDF, last seen date etc.
 *
 * @author Dmitry Zolotukhin <a
 * href="mailto:zlogic@gmail.com">zlogic@gmail.com</a>
 */
public class FeedItem implements Comparable<FeedItem> {

    /**
     * The state of feed items
     */
    public enum State {

        /**
         * Didn't send any data on this feed
         */
        SENT_NOTHING,
        /**
         * Send the feed contents but not the source PDF
         */
        SENT_ENTRY,
        /**
         * Sent all data, including the source PDF
         */
        SENT_PDF
    };

    /**
     * The feed item unique ID
     */
    @XmlAttribute(name = "uuid")
    private String id;
    /**
     * The date this item was last seen
     */
    @XmlAttribute(name = "lastSeen")
    private Date lastSeen;
    /**
     * The state of this item
     */
    @XmlAttribute(name = "state")
    private State state = State.SENT_NOTHING;
    /**
     * The link for this item
     */
    private String link;
    /**
     * The title of this item
     */
    private String title;
    /**
     * The text contents of this item
     */
    private String itemText;
    /**
     * The HTML contents of this item
     */
    private String itemHtml;
    /**
     * The date this item was originally published
     */
    private Date publishedDate = null;

    /**
     * Empty constructor for JAXB
     */
    private FeedItem() {
    }

    /**
     * Constructs a FeedItem from data downloaded by ROME
     *
     * @param feed the feed
     * @param entry the feed item downloaded by ROME
     * @throws IOException if generating text/HTML contents failed because
     * templates cannot be found
     */
    protected FeedItem(Feed feed, SyndEntry entry) throws IOException {
        this.id = MessageFormat.format("{0}@@{1}@@{2}@@{3}",
                new Object[] { feed.getUrl(), entry.getUri(), entry.getLink(), entry.getTitle() });//Unique ID //NOI18N
        this.link = entry.getLink();
        this.title = entry.getTitle();
        this.lastSeen = new Date();
        this.state = State.SENT_NOTHING;
        publishedDate = entry.getPublishedDate();

        ST textTemplate = new ST(IOUtils.toString(FeedItem.class.getResourceAsStream("templates/FeedItem.txt")),
                '$', '$'); //NOI18N
        textTemplate.add("feed", feed); //NOI18N
        textTemplate.add("entry", entry); //NOI18N
        itemText = textTemplate.render();

        ST htmlTemplate = new ST(IOUtils.toString(FeedItem.class.getResourceAsStream("templates/FeedItem.html")),
                '$', '$'); //NOI18N
        htmlTemplate.add("feed", feed); //NOI18N
        htmlTemplate.add("entry", entry); //NOI18N
        itemHtml = htmlTemplate.render();
        //TODO: extract alt-text from images for comics
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof FeedItem && ((FeedItem) obj).id.equals(id);
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 97 * hash + Objects.hashCode(this.id);
        return hash;
    }

    /**
     * Updates the last seen time to the current time
     */
    public void updateLastSeen() {
        lastSeen = new Date();
    }

    /*
     * Getters
     */
    /**
     * Returns the text contents of this item
     *
     * @return the text contents of this item
     */
    public String getItemText() {
        return itemText;
    }

    /**
     * Returns the HTML contents of this item
     *
     * @return the HTML contents of this item
     */
    public String getItemHtml() {
        return itemHtml;
    }

    /**
     * Returns the link for this item
     *
     * @return the link for this item
     */
    public String getLink() {
        return link;
    }

    /**
     * Returns the title of this item
     *
     * @return the title of this item
     */
    public String getTitle() {
        return title;
    }

    /**
     * Returns the date this item was originally published
     *
     * @return the date this item was originally published
     */
    public Date getPublishedDate() {
        return publishedDate;
    }

    /**
     * Returns the date this item was last seen
     *
     * @return the date this item was last seen
     */
    public Date getLastSeen() {
        return lastSeen;
    }

    /**
     * Returns the state of this item
     *
     * @return the state of this item
     */
    @XmlTransient
    public State getState() {
        return state;
    }

    /**
     * Sets the state of this item
     *
     * @param state the new state of this item
     */
    public void setState(State state) {
        this.state = state;
    }

    @Override
    public int compareTo(FeedItem o) {
        return id.compareTo(o.id);
    }
}