/*
 * The Gemma project
 * 
 * Copyright (c) 2008 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package ubic.gemma.web.feed;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;

import ubic.gemma.util.ConfigUtils;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.fetcher.FeedFetcher;
import com.sun.syndication.fetcher.impl.FeedFetcherCache;
import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache;
import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher;

/**
 * @spring.bean id="feedReader"
 * @author paul
 * @version $Id$
 */
public class FeedReader implements InitializingBean {

    private static final String GEMMA_HOME_FEEDURL_CONFIG_PARAM = "gemma.home.feedurl";

    private static final String DEFAULT_FEED = "http://www.chibi.ubc.ca/faculty/pavlidis/wiki/spaces/createrssfeed.action?types=blogpost&spaces=gemma&maxResults=1&title=Gemma+News&publicFeed=true&timeSpan=600";

    FeedFetcher feedFetcher;
    String feedUrl;

    /**
     * @return List of news items in HTML format.
     */
    @SuppressWarnings("unchecked")
    public List<NewsItem> getLatestNews() {

        /*
         * reformat the feed.
         */
        Pattern authorP = Pattern.compile( "<p>.*?News Item.*?<b>edited</b>.*?by.*?<a.*?</p>", Pattern.DOTALL
                | Pattern.CASE_INSENSITIVE );
        Pattern footerP = Pattern.compile( "<div style=.?padding: 10px 0;.*?View Online</a>.*?</div>", Pattern.DOTALL
                | Pattern.CASE_INSENSITIVE );
        Pattern borderP = Pattern.compile( "border-top: 1px solid #ddd; border-bottom: 1px solid #ddd;" );
        List<NewsItem> result = new ArrayList<NewsItem>();
        try {
            SyndFeed feed = feedFetcher.retrieveFeed( new URL( feedUrl ) );

            for ( SyndEntry k : ( Collection<SyndEntry> ) feed.getEntries() ) {
                NewsItem n = new NewsItem();

                /*
                 * This code is specific for confluence feeds.
                 */
                String title = k.getTitle();

                String body = k.getDescription().getValue();
                Matcher m = authorP.matcher( body );
                body = m.replaceAll( "" );

                Matcher b = borderP.matcher( body );
                body = b.replaceAll( "" );

                /*
                 * Confluence-specific
                 */
                Matcher footerMatch = footerP.matcher( body );
                body = footerMatch.replaceAll( "" );

                n.setBody( body );
                n.setTitle( title );

                n.setDate( k.getPublishedDate() );

                result.add( n );
            }
        } catch ( Exception e ) {
            NewsItem n = new NewsItem();
            n.setTitle( "No news" );
            n.setBody( "" );
        }
        return result;

    }

    public void afterPropertiesSet() throws Exception {
        FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();
        feedFetcher = new HttpURLFeedFetcher( feedInfoCache );
        this.feedUrl = ConfigUtils.getString( GEMMA_HOME_FEEDURL_CONFIG_PARAM );
        if ( StringUtils.isBlank( feedUrl ) ) {
            this.feedUrl = DEFAULT_FEED;
        }
    }
}

@@ -2050,14 +2050,22 @@
 ?%3Cb%3E
+(
 edited
+%7Cadded)
 %3C/b%3E

remove author name when 'added' or 'edited'.