package org.xcolab.portlets.feeds.dataProviders;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;
import org.xcolab.commons.beans.SortFilterPage;
import org.xcolab.portlets.feeds.FeedTypeDataProvider;
import org.xcolab.portlets.feeds.FeedsPreferences;
import org.xcolab.portlets.feeds.Helper;
import org.xcolab.portlets.feeds.wrappers.SocialActivityWrapper;

import com.ext.portlet.Activity.ActivityUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.DateUtil;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.social.model.SocialActivity;

public class ActivitiesFeedDataProvider implements FeedTypeDataProvider {

	@Override
	public String populateModel(PortletRequest request,
								PortletResponse response, SortFilterPage sortFilterPage,
								FeedsPreferences feedsPreferences, Model model) throws SystemException, PortalException {

		HttpServletRequest originalRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(request));

		List<SocialActivityWrapper >activities = new ArrayList<SocialActivityWrapper>();
		int lastDaysBetween = -1;
		Date now = new Date();
		int i = 0;
		Map<String, String[]> parameters = request.getParameterMap();
		long filterUserId = 0L;
		User filterUser = null;
		final int pageSize = feedsPreferences.getFeedSize();
		String userIdStr = null;
		if (parameters.containsKey("userId")) {
			userIdStr = parameters.get("userId")[0];
		}
		else if (originalRequest.getParameter("userId") != null){
			userIdStr = originalRequest.getParameter("userId");
		}
		if (userIdStr != null) {
			try {
				filterUserId = Long.parseLong(userIdStr);
				filterUser = UserLocalServiceUtil.getUser(filterUserId);
				model.addAttribute("filterUserId", filterUserId);
				model.addAttribute("filterUser", filterUser);
			}
			catch (Throwable t) {
				// ignore
			}
		}

		for (SocialActivity activity : filterUserId == 0 ?
				ActivityUtil.retrieveWindowedActivities(sortFilterPage.getPage() * pageSize, (sortFilterPage.getPage()+1) * pageSize) :
				ActivityUtil.retrieveWindowedActivities(filterUserId, sortFilterPage.getPage() * pageSize, (sortFilterPage.getPage()+1) * pageSize)) {
			if (SocialActivityWrapper.isEmpty(activity, request)) {
				continue;
			}
			if (!feedsPreferences.getRemoveAdmin() && Helper.isUserAnAdmin(request, activity.getUserId())) {
				continue;
			}

			if (i >= feedsPreferences.getFeedSize()) {
				break;
			}

			int curDaysBetween = DateUtil.getDaysBetween(new Date(activity.getCreateDate()), now, TimeZone.getDefault());
			activities.add(new SocialActivityWrapper(activity, curDaysBetween, lastDaysBetween < curDaysBetween, i % 2 == 1, request, feedsPreferences.getFeedMaxLength()));
			lastDaysBetween = curDaysBetween;
			i++;
		}

		model.addAttribute("activities", activities);


        if (filterUserId == 0) {
            model.addAttribute("isLastPage", ((pageSize * (sortFilterPage.getPage() + 1)) >= ActivityUtil.getAllActivitiesCount()));
        } else {
            model.addAttribute("isLastPage", ((pageSize * (sortFilterPage.getPage() + 1) >= ActivityUtil.getActivitiesCount(filterUserId))));
        }


		return "activities";

	}

}

@@ -54,246 +54,407 @@
 ort 
-java.util.ArrayList;%0Aimport java.util.Date;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.TimeZone;%0A%0Aimport javax.portlet.PortletRequest;%0Aimport javax.portlet.PortletResponse;%0Aimport javax.servlet.http.HttpServletRequest;%0A
+com.ext.portlet.Activity.ActivityUtil;%0Aimport com.liferay.portal.kernel.exception.PortalException;%0Aimport com.liferay.portal.kernel.exception.SystemException;%0Aimport com.liferay.portal.kernel.util.DateUtil;%0Aimport com.liferay.portal.model.User;%0Aimport com.liferay.portal.service.UserLocalServiceUtil;%0Aimport com.liferay.portal.util.PortalUtil;%0Aimport com.liferay.portlet.social.model.SocialActivity;
 %0Aimp
@@ -747,39 +747,37 @@
 rapper;%0A%0Aimport 
-com.ext
+javax
 .portlet.Activit
@@ -773,390 +773,126 @@
 let.
-Activity.ActivityUtil;%0Aimport com.liferay.portal.kernel.exception.PortalException;%0Aimport com.liferay.portal.kernel.exception.SystemException;%0Aimport com.liferay.portal.kernel.util.DateUtil;%0Aimport com.liferay.portal.model.User;%0Aimport com.liferay.portal.service.UserLocalServiceUtil;%0Aimport com.liferay.portal.util.PortalUtil;%0Aimport com.liferay.portlet.social.model.SocialActivity
+PortletRequest;%0Aimport javax.portlet.PortletResponse;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport java.util.*
 ;%0A%0Ap
@@ -2174,106 +2174,97 @@
 %09%09%7D%0A
-%0A%09%09for (SocialActivity activity : filterUserId == 0 ?%0A%09%09%09%09ActivityUtil.retrieveWindowedActivities(
+        %0A        List%3CSocialActivity%3E windowedActivities;%0A        int startRetrievalAt = 
 sort
@@ -2286,33 +2286,62 @@
 age() * pageSize
-,
+;%0A        int endRetrievalAt =
  (sortFilterPage
@@ -2342,33 +2342,35 @@
 erPage.getPage()
-+
+ + 
 1) * pageSize) :
@@ -2370,147 +2370,358 @@
 Size
-) :%0A%09%09%09%09ActivityUtil.retrieveWindowedActivities(filterUserId, sortFilterPage.getPage() * pageSize, (sortFilterPage.getPage()+1) * pageSize)
+;%0A        if (filterUserId == 0) %7B%0A            windowedActivities = ActivityUtil.retrieveWindowedActivities(startRetrievalAt, endRetrievalAt);%0A        %7D else %7B%0A            windowedActivities = ActivityUtil.retrieveWindowedActivities(filterUserId, startRetrievalAt, endRetrievalAt);%0A        %7D%0A%0A        for (SocialActivity activity : windowedActivities
 ) %7B%0A

rephrase for loop so that it becomes more readable and remove redundancies from the retrieval call