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