package foodtruck.server.resources.json;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import com.google.inject.Inject;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import foodtruck.dao.LocationDAO;
import foodtruck.model.Location;
import foodtruck.server.resources.BadRequestException;

/**
 * @author aviolette@gmail.com
 * @since 4/19/12
 */

@Provider
public class LocationWriter implements JSONWriter<Location>, MessageBodyWriter<Location> {

  private final LocationDAO locationDAO;

  @Inject
  public LocationWriter(LocationDAO locationDAO) {
    this.locationDAO = locationDAO;
  }

  @Override public JSONObject asJSON(Location location) throws JSONException {
    return writeLocation(location, 0, false);
  }

  public JSONObject writeLocation(Location location, int id, boolean fullOptions)
      throws JSONException {
    // this is kind of a hack
    if (location.getKey() == null) {
      Location loc = locationDAO.findByAddress(location.getName());
      if (loc != null) {
        location = Location.builder(loc).key(loc.getKey()).build();
      }
    }
    JSONObject obj = new JSONObject()
        .put("latitude", location.getLatitude())
        .put("longitude", location.getLongitude())
        .put("url", location.getUrl())
        .put("radius", location.getRadius())
        .put("radiateTo", location.getRadiateTo())
        .put("description", location.getDescription())
        .put("name", location.getName())
        .put("designatedStop", location.isDesignatedStop())
        .putOpt("twitterHandle", location.getTwitterHandle())
        .put("key", location.getKey());
    if (fullOptions) {
      obj.put("popular", location.isPopular());
      obj.put("hasBooze", location.isHasBooze());
      obj.put("alias", location.getAlias());
      obj.put("ownedBy", location.getOwnedBy());
      obj.put("autocomplete", location.isAutocomplete());
      obj.put("valid", location.isValid());
    }
    return (id != 0) ? obj.put("id", id) : obj;
  }

  @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
    return type.equals(Location.class);
  }

  @Override public long getSize(Location location, Class<?> type, Type genericType, Annotation[] annotations,
      MediaType mediaType) {
    return -1;
  }

  @Override
  public void writeTo(Location location, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
      MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
      throws IOException, WebApplicationException {
    try {
      JSONSerializer.writeJSON(asJSON(location), entityStream);
    } catch (JSONException e) {
      throw new BadRequestException(e, MediaType.APPLICATION_JSON_TYPE);
    }
  }
}
@@ -1764,24 +1764,118 @@
 .getName())%0A
+        .put(%22hasBooze%22, location.isHasBooze())%0A        .put(%22popular%22, location.isPopular())%0A
         .put
@@ -2051,106 +2051,8 @@
 ) %7B%0A
-      obj.put(%22popular%22, location.isPopular());%0A      obj.put(%22hasBooze%22, location.isHasBooze());%0A
     

Make popular and boozy out of hidden attributes