com.seajas.search.attender.model.searchresult.SearchResult.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.attender.model.searchresult.SearchResult.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.attender.model.searchresult;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEnclosure;
import com.sun.syndication.feed.synd.SyndEnclosureImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;

/**
 * Search result model.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "profile.wsdl.attender.search.seajas.com")
public class SearchResult {
    /**
     * The URL.
     */
    @XmlAttribute
    private String url;

    /**
     * The title.
     */
    @XmlAttribute
    private String title;

    /**
     * The author.
     */
    @XmlAttribute
    private String author;

    /**
     * The source.
     */
    @XmlAttribute
    private String source;

    /**
     * The summary.
     */
    @XmlAttribute
    private String summary;

    /**
     * The publication date.
     */
    @XmlAttribute
    private Date publicationDate;

    /**
     * An (optional) image thumbnail.
     */
    @XmlAttribute
    private String imageThumbnail;

    /**
     * Default constructor.
     */
    public SearchResult() {
        super();
    }

    /**
     * Default constructor.
     * 
     * @param url
     * @param title
     * @param author
     * @param source
     * @param summary
     * @param publicationDate
     * @param imageThumbnail
     */
    public SearchResult(final String url, final String title, final String author, final String source,
            final String summary, final Date publicationDate, final String imageThumbnail) {
        this.url = url;
        this.title = title;
        this.author = author;
        this.source = source;
        this.summary = summary;
        this.publicationDate = publicationDate;
        this.imageThumbnail = imageThumbnail;
    }

    /**
     * Retrieve the url.
     * 
     * @return String
     */
    public String getUrl() {
        return url;
    }

    /**
     * Set the url.
     * 
     * @param url
     */
    public void setUrl(final String url) {
        this.url = url;
    }

    /**
     * Retrieve the title.
     * 
     * @return String
     */
    public String getTitle() {
        return title;
    }

    /**
     * Set the title.
     * 
     * @param title
     */
    public void setTitle(final String title) {
        this.title = title;
    }

    /**
     * Set the author.
     * 
     * @param author
     */
    public void setAuthor(final String author) {
        this.author = author;
    }

    /**
     * Retrieve the author.
     * 
     * @return String
     */
    public String getAuthor() {
        return author;
    }

    /**
     * Set the source.
     * 
     * @param source
     */
    public void setSource(final String source) {
        this.source = source;
    }

    /**
     * Retrieve the source.
     * 
     * @return String
     */
    public String getSource() {
        return source;
    }

    /**
     * Retrieve the summary.
     * 
     * @return String
     */
    public String getSummary() {
        return summary;
    }

    /**
     * Set the summary.
     * 
     * @param summary
     */
    public void setSummary(final String summary) {
        this.summary = summary;
    }

    /**
     * Retrieve the publicationDate.
     * 
     * @return Date
     */
    public Date getPublicationDate() {
        return publicationDate;
    }

    /**
     * Set the publicationDate.
     * 
     * @param publicationDate
     */
    public void setPublicationDate(final Date publicationDate) {
        this.publicationDate = publicationDate;
    }

    /**
     * Retrieve the imageThumbnail.
     * 
     * @return String
     */
    public String getImageThumbnail() {
        return imageThumbnail;
    }

    /**
     * Set the imageThumbnail.
     * 
     * @param imageThumbnail
     */
    public void setImageThumbnail(final String imageThumbnail) {
        this.imageThumbnail = imageThumbnail;
    }

    /**
     * Marshal the document content to a W3C Element node.
     * 
     * @param document
     * @return Element
     */
    public Element toElement(final Document document) {
        Element result = document.createElement("search-result");

        result.setAttribute("url", url);
        result.setAttribute("title", StringEscapeUtils.unescapeHtml(title));
        result.setAttribute("author", StringEscapeUtils.unescapeHtml(author != null ? author : ""));
        result.setAttribute("source", StringEscapeUtils.unescapeHtml(source != null ? source : ""));
        result.setAttribute("publicationDate", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(publicationDate));

        if (!StringUtils.isEmpty(imageThumbnail))
            result.setAttribute("image-thumbnail", imageThumbnail);

        result.appendChild(document.createCDATASection(StringEscapeUtils.unescapeHtml(summary)));

        return result;
    }

    /**
     * Marshal the document content to a syndication feed entry.
     * 
     * @return SyndEntry
     */
    public SyndEntry toSyndEntry() {
        SyndEntry result = new SyndEntryImpl();

        result.setTitle(title);
        result.setLink(url);
        result.setAuthor(author);
        result.setPublishedDate(publicationDate);

        SyndContent entryDescription = new SyndContentImpl();

        entryDescription.setType("text/html");
        entryDescription.setValue(summary);

        result.setDescription(entryDescription);

        if (!StringUtils.isEmpty(imageThumbnail)) {
            SyndEnclosure enclosure = new SyndEnclosureImpl();

            // XXX: We simply try to guess the format based on the extension, or just leave it

            if (imageThumbnail.toLowerCase().endsWith(".jpg") || imageThumbnail.toLowerCase().endsWith(".jpeg"))
                enclosure.setType("image/jpeg");
            else if (imageThumbnail.toLowerCase().endsWith(".gif"))
                enclosure.setType("image/gif");
            else if (imageThumbnail.toLowerCase().endsWith(".png"))
                enclosure.setType("image/png");

            enclosure.setUrl(imageThumbnail);

            result.setEnclosures(Arrays.asList(new SyndEnclosure[] { enclosure }));
        }

        return result;
    }
}