package com.citysearch.webwidget.facade;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.citysearch.webwidget.bean.HouseAd;
import com.citysearch.webwidget.bean.NearbyPlace;
import com.citysearch.webwidget.bean.NearbyPlacesResponse;
import com.citysearch.webwidget.bean.RequestBean;
import com.citysearch.webwidget.exception.CitysearchException;
import com.citysearch.webwidget.exception.InvalidRequestParametersException;
import com.citysearch.webwidget.util.HouseAdsUtil;

public class UrbanSpoonNearbyPlacesFacade extends AbstractNearByPlacesFacade {
    private Logger log = Logger.getLogger(getClass());

    protected UrbanSpoonNearbyPlacesFacade(String contextPath, int displaySize) {
        super(contextPath, displaySize);
    }

    public NearbyPlacesResponse getNearbyPlaces(RequestBean request)
            throws InvalidRequestParametersException, CitysearchException {
        request.validate();
        return findInsiderPagesNearbyPlaces(request);
    }

    private NearbyPlacesResponse findInsiderPagesNearbyPlaces(RequestBean request)
            throws CitysearchException {
        NearbyPlacesResponse response = new NearbyPlacesResponse();

        List<NearbyPlace> nearbyPlaces = null;

        if (!StringUtils.isBlank(request.getLatitude())
                && !StringUtils.isBlank(request.getLongitude())) {
            nearbyPlaces = getAdsFromPFPLocation(request);
        }
        List<NearbyPlace> backfill = null;
        List<HouseAd> houseAds = null;
        List<NearbyPlace> searchResults = null;
        int noOfAdsNeeded = (nearbyPlaces == null || nearbyPlaces.isEmpty()) ? this.displaySize
                : this.displaySize - nearbyPlaces.size();
        if (noOfAdsNeeded > 0) {
            nearbyPlaces = (nearbyPlaces == null) ? new ArrayList<NearbyPlace>() : nearbyPlaces;
            if (!StringUtils.isBlank(request.getWhere())) {
                Set<String> listingIds = new TreeSet<String>();
                for (NearbyPlace nbp : nearbyPlaces)
                    listingIds.add(nbp.getListingId());
                List<NearbyPlace> pfpResults = getAdsFromPFP(request, listingIds, noOfAdsNeeded);
                if (pfpResults != null && !pfpResults.isEmpty()) {
                    pfpResults = (pfpResults.size() > noOfAdsNeeded) ? pfpResults.subList(0,
                            noOfAdsNeeded) : pfpResults;
                    nearbyPlaces.addAll(pfpResults);
                }
            }
            int noOfBackFillNeeded = (nearbyPlaces == null || nearbyPlaces.isEmpty()) ? this.displaySize
                    : this.displaySize - nearbyPlaces.size();
            if (noOfBackFillNeeded > 0) {
                backfill = getNearbyPlacesBackfill(request, noOfBackFillNeeded);
                int noOfSearchResultsNeeded = (backfill == null || backfill.isEmpty()) ? noOfBackFillNeeded
                        : noOfBackFillNeeded - backfill.size();
                if (noOfSearchResultsNeeded > 0) {
                    searchResults = getSearchResults(request, noOfSearchResultsNeeded);
                    int noOfHouseAdsNeeded = (searchResults == null || searchResults.isEmpty()) ? noOfSearchResultsNeeded
                            : noOfSearchResultsNeeded - searchResults.size();
                    if (noOfHouseAdsNeeded > 0) {
                        houseAds = HouseAdsUtil.getHouseAds(getContextPath(),
                                request.getDartClickTrackUrl());
                        houseAds = houseAds.subList(0, noOfHouseAdsNeeded);
                    } else if (noOfHouseAdsNeeded < 0) {
                        searchResults = searchResults.subList(0, noOfSearchResultsNeeded);
                    }
                } else if (noOfSearchResultsNeeded < 0) {
                    backfill = backfill.subList(0, noOfBackFillNeeded);
                }
            }
        }
        response.setNearbyPlaces(nearbyPlaces);
        response.setBackfill(backfill);
        response.setSearchResults(searchResults);
        response.setHouseAds(houseAds);
        return response;
    }

}

@@ -56,134 +56,14 @@
 til.
-Array
 List;%0A
-import java.util.List;%0Aimport java.util.Set;%0Aimport java.util.TreeSet;%0A%0Aimport org.apache.commons.lang.StringUtils;
 %0Aimp
@@ -942,36 +942,24 @@
  return find
-InsiderPages
 NearbyPlaces
@@ -1017,20 +1017,8 @@
 find
-InsiderPages
 Near
@@ -1156,17 +1156,16 @@
 onse();%0A
-%0A
         
@@ -1201,1258 +1201,187 @@
 s = 
-null;%0A%0A        if (!StringUtils.isBlank(request.getLatitude())%0A                && !StringUtils.isBlank(request.getLongitude())) %7B%0A            nearbyPlaces = getAdsFromPFPLocation(request);%0A        %7D%0A        List%3CNearbyPlace%3E backfill = null;%0A        List%3CHouseAd%3E houseAds = null;%0A        List%3CNearbyPlace%3E searchResults = null;%0A        int noOfAdsNeeded = (nearbyPlaces == null %7C%7C nearbyPlaces.isEmpty()) ? this.displaySize%0A                : this.displaySize - nearbyPlaces.size();%0A        if (noOfAdsNeeded %3E 0) %7B%0A            nearbyPlaces = (nearbyPlaces == null) ? new ArrayList%3CNearbyPlace%3E() : nearbyPlaces;%0A            if (!StringUtils.isBlank(request.getWhere())) %7B%0A                Set%3CString%3E listingIds = new TreeSet%3CString%3E();%0A                for (NearbyPlace nbp : nearbyPlaces)%0A                    listingIds.add(nbp.getListingId());%0A                List%3CNearbyPlace%3E pfpResults = getAdsFromPFP(request, listingIds, noOfAdsNeeded);%0A                if (pfpResults != null && !pfpResults.isEmpty()) %7B%0A                    pfpResults = (pfpResults.size() %3E noOfAdsNeeded) ? pfpResults.subList(0,%0A                            noOfAdsNeeded) : pfpResults;%0A                    nearbyPlaces.addAll(pfpResults);%0A                %7D%0A            %7D%0A    
+getAdsFromPFP(request, null, getDisplaySize());%0A%0A        List%3CNearbyPlace%3E backfill = null;%0A        List%3CHouseAd%3E houseAds = null;%0A        List%3CNearbyPlace%3E searchResults = null;%0A
     
@@ -1493,20 +1493,16 @@
         
-    
 : this.d
@@ -1535,36 +1535,32 @@
 size();%0A        
- 
-   
 if (noOfBackFill
@@ -1565,36 +1565,32 @@
 llNeeded %3E 0) %7B%0A
-    
             back
@@ -1654,36 +1654,32 @@
 d);%0A            
-  
-  
 int noOfSearchRe
@@ -1770,28 +1770,24 @@
             
-    
 : noOfBackFi
@@ -1806,36 +1806,32 @@
 ackfill.size();%0A
-    
             if (
@@ -1869,36 +1869,32 @@
                 
-    
 searchResults = 
@@ -1937,36 +1937,32 @@
 ResultsNeeded);%0A
-    
                 
@@ -2083,28 +2083,24 @@
             
- 
-   
 : noOfSearch
@@ -2145,36 +2145,32 @@
                 
-    
 if (noOfHouseAds
@@ -2195,36 +2195,32 @@
                 
-   
- 
 houseAds = House
@@ -2281,28 +2281,24 @@
             
-    
 request.getD
@@ -2318,20 +2318,16 @@
 Url());%0A
-    
         
@@ -2398,36 +2398,32 @@
                 
-    
 %7D else if (noOfH
@@ -2435,36 +2435,32 @@
 dsNeeded %3C 0) %7B%0A
-    
                 
@@ -2542,34 +2542,26 @@
             
-  
-  %7D%0A    
+%7D%0A
             
@@ -2610,36 +2610,32 @@
                 
-   
- 
 backfill = backf
@@ -2670,34 +2670,16 @@
 eeded);%0A
-                %7D%0A
         
@@ -2686,32 +2686,33 @@
     %7D%0A        %7D%0A
+%0A
         response

Fix to avoid calling PFP location.