001/*
002 * (C) Copyright 2008 JBoss and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Original file from org.jboss.seam.rss.ui.UIEntry in jboss-seam-rss
016 *     Thierry Martins
017 */
018package org.nuxeo.ecm.platform.ui.web.component.seam;
019
020import java.io.IOException;
021import java.text.SimpleDateFormat;
022
023import javax.faces.context.FacesContext;
024
025import org.jboss.seam.contexts.Contexts;
026
027import yarfraw.core.datamodel.ChannelFeed;
028import yarfraw.core.datamodel.FeedFormat;
029import yarfraw.core.datamodel.ItemEntry;
030import yarfraw.core.datamodel.Person;
031import yarfraw.core.datamodel.Text;
032
033/**
034 * Override to support date formatting for RSS 2.0.
035 * <p>
036 * Default component only deals with ATOM format.
037 *
038 * @since 5.6
039 */
040public class UIEntry extends org.jboss.seam.rss.ui.UIEntry {
041
042    private static final String COMPONENT_TYPE = UIEntry.class.getName();
043
044    protected static final String RSS20_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";;
045
046    private FeedFormat feedFormat;
047
048    @Override
049    public String getFamily() {
050        return COMPONENT_TYPE;
051    }
052
053    private Text makeText(String textString) {
054        Text text = new Text(getTextFormat());
055        text.setText(textString);
056        return text;
057    }
058
059    @Override
060    public void encodeBegin(FacesContext facesContext) throws IOException {
061        ChannelFeed channelFeed = (ChannelFeed) Contexts.getEventContext().get(FEED_IMPL_KEY);
062
063        ItemEntry itemEntry = new ItemEntry();
064        itemEntry.setUid(getUid());
065        itemEntry.setTitle(makeText(getTitle()));
066        itemEntry.addLink(getLink());
067        String author = getAuthor();
068        if (author != null) {
069            Person authorPerson = new Person();
070            authorPerson.setName(author);
071            itemEntry.addAuthorOrCreator(authorPerson);
072        }
073        itemEntry.setDescriptionOrSummary(makeText(getSummary()));
074        if (getUpdated() != null) {
075            itemEntry.setUpdatedDate(getUpdated(), new SimpleDateFormat(getFeedDateFormat()));
076        }
077        if (getPublished() != null) {
078            itemEntry.setPubDate(getPublished(), new SimpleDateFormat(getFeedDateFormat()));
079        }
080
081        channelFeed.addItem(itemEntry);
082    }
083
084    public FeedFormat getFeedFormat() {
085        return (FeedFormat) valueOf("feedFormat", feedFormat);
086    }
087
088    public void setFeedFormat(FeedFormat feedFormat) {
089        this.feedFormat = feedFormat;
090    }
091
092    public String getFeedDateFormat() {
093        if (FeedFormat.RSS20.equals(getFeedFormat())) {
094            return RSS20_DATE_FORMAT;
095        } else {
096            return ATOM_DATE_FORMAT;
097        }
098    }
099}