com.seajas.search.attender.service.feed.FeedService.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.attender.service.feed.FeedService.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.service.feed;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import com.seajas.search.attender.model.profile.Profile;
import com.seajas.search.attender.model.profile.ProfileSubscriber.NotificationType;
import com.seajas.search.attender.model.searchresult.SearchResult;
import com.seajas.search.attender.service.search.SearchService;
import com.seajas.search.utilities.ui.InterfaceQueryUtils;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;

/**
 * Feed service implementation.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
@Service
public class FeedService {
    /**
     * The search service.
     */
    @Autowired
    private SearchService searchService;

    /**
     * The default number of results.
     */
    @Value("${attender.project.feed.default.results}")
    private Integer defaultResults;

    /**
     * The maximum number of results.
     */
    @Value("${attender.project.feed.max.results}")
    private Integer maximumResults;

    /**
     * The message source.
     */
    @Autowired
    private MessageSource messageSource;

    /**
     * Create a feed from the given profile and base URL.
     * 
     * @param profile
     * @param notificationType
     * @param totalResults
     * @param baseUrl
     * @param locale
     * @return SyndFeed
     */
    public SyndFeed createFeed(final Profile profile, final NotificationType notificationType,
            final Integer totalResults, final String baseUrl, final Locale locale) {
        // Round up the search parameters and taxonomies into a concise parameter list

        Map<String, String> searchParameters = InterfaceQueryUtils.combineParametersAndTaxonomies(
                profile.getSearchParametersMap(), profile.getTaxonomyIdentifierNumbers());

        // Determine the start date on the notification type

        Calendar calendar = new GregorianCalendar();

        Date endDate = calendar.getTime();

        calendar.add(Calendar.DATE, notificationType.equals(NotificationType.Weekly) ? -7 : -1);

        Date startDate = calendar.getTime();

        // Determine the start date depending on the user's last notification date, and retrieve the results

        Integer actualResults = totalResults != null
                ? (maximumResults == 0 ? totalResults : Math.min(totalResults, maximumResults))
                : defaultResults;

        List<SearchResult> searchResults = searchService.performSearch(profile.getQuery(), startDate, endDate,
                searchParameters, actualResults, locale.getLanguage());

        // Create the actual feed

        SyndFeed feed = new SyndFeedImpl();

        feed.setAuthor(messageSource.getMessage("feed.author", new String[] {}, locale));
        feed.setTitle(messageSource.getMessage("feed.title", new String[] {}, locale));
        feed.setDescription(
                messageSource.getMessage("feed.description", new String[] { String.valueOf(searchResults.size()),
                        profile.getQuery() != null ? profile.getQuery() : "" }, locale));
        feed.setLink(baseUrl);

        // Add the actual entries

        List<SyndEntry> entries = new ArrayList<SyndEntry>(searchResults.size());

        for (SearchResult searchResult : searchResults)
            entries.add(searchResult.toSyndEntry());

        feed.setEntries(entries);

        return feed;
    }
}