package foodtruck.geolocation;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.sun.jersey.api.client.WebResource;

import org.codehaus.jettison.json.JSONObject;

import foodtruck.util.ServiceException;

/**
 * Extracting out the resource for easier testing
 * @author aviolette@gmail.com
 * @since 10/18/11
 */
public class YahooResource {
  private final WebResource resource;
  private String yahooId;

  @Inject
  public YahooResource(@YahooEndPoint WebResource resource, @Named("yahoo.app.id") String yahooId) {
    this.yahooId = yahooId;
    this.resource = resource;
  }

  /**
   * Calls the geolocation service and returns the JSON payload
   * @param location the location
   * @return the JSON Object
   * @throws ServiceException if an error occurs calling the service
   */
  public JSONObject findLocation(String location, boolean reverse) throws ServiceException {
    try {
      WebResource r = resource.queryParam("q", location)
          .queryParam("flags", "j")
          .queryParam("appid", yahooId);
      if (reverse) {
        r = r.queryParam("gflags", "R");
      }
      return r.header("Accept", "text/json; application/json")
          .get(JSONObject.class);
    } catch (RuntimeException rte) {
      throw new ServiceException(rte);
    }
  }
}

@@ -62,45 +62,8 @@
 ct;%0A
-import com.google.inject.name.Named;%0A
 impo
@@ -152,16 +152,55 @@
 bject;%0A%0A
+import foodtruck.dao.ConfigurationDAO;%0A
 import f
@@ -417,30 +417,48 @@
 private 
-String yahooId
+final ConfigurationDAO configDAO
 ;%0A%0A  @In
@@ -526,75 +526,68 @@
 ce, 
-@Named(%22yahoo.app.id%22) String yahooId) %7B%0A    this.yahooId = yahooId
+ConfigurationDAO configDAO) %7B%0A    this.configDAO = configDAO
 ;%0A  
@@ -1058,15 +1058,40 @@
 d%22, 
-yahooId
+configDAO.find().getYahooAppId()
 );%0A 

Use configuration setting to determine Yahoo App ID