package goplaces.resources;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import org.json.JSONObject;

import goplaces.models.CustomizeRouteQuery;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.memcache.ErrorHandlers;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.appengine.api.taskqueue.*;

@Path("/place_details")
public class PlaceDetailsResource {

	@Context UriInfo uriInfo;
	@Context Request request;
	
	DatastoreService datastore;
	MemcacheService syncCache;
	
	private static final Logger log = Logger.getLogger(SelectWaypointsResource.class.getName());
	
	public PlaceDetailsResource(){
		datastore = DatastoreServiceFactory.getDatastoreService();
		syncCache = MemcacheServiceFactory.getMemcacheService();
		
		syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
	}
}
	// @POST
	// @Produces(MediaType.APPLICATION_JSON)
	// //public String selectWaypointsForRoute(CustomizeRouteQuery customizeRouteQuery, @Context HttpServletResponse servletResponse) {

@@ -1529,24 +1529,55 @@
 %0A%09%7D%0A
-%7D
 %0A%09
-// @POST%0A%09// 
+@GET%0A%09@Consumes(MediaType.APPLICATION_JSON)%0A%09
 @Pro
@@ -1615,13 +1615,8 @@
 N)%0A%09
-// //
 publ
@@ -1629,117 +1629,952 @@
 ing 
-selectWaypointsForRoute(CustomizeRouteQuery customizeRouteQuery, @Context HttpServletResponse servletResponse) %7B%0A
+getPlaceDetails(@QueryParam(%22place_id%22) String place_id, @Context HttpServletResponse servletResponse) %7B%0A%09%09JSONObject answer = new JSONObject();%0A%09%09System.out.println(%22Place id is %22 + place_id);%0A%09%09try%7B%0A%09%09%09String place_details = (String)syncCache.get(%22place-%22+place_id);%0A%09%09%09if(place_details != null)%7B%0A%09%09%09%09System.out.println(%22Place details in memcache (and datastore) for %22 + place_id);%0A%09%09%09%09return answer.put(%22status%22,%22found in memcache%22).toString();%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09Entity place_details_entity = datastore.get(KeyFactory.createKey(%22Place%22, place_id));%0A%09%09%09if(place_details_entity != null)%7B%0A%09%09%09%09System.out.println(%22Place details in datastore for %22 + place_id);%0A%09%09%09%09return answer.put(%22status%22,%22found in datastore%22).toString();%0A%09%09%09%7D%0A%09%09%09return answer.put(%22status%22,%22not found%22).toString();%0A%09%09%7D%0A%09%09catch(Exception e)%7B%0A%09%09%09System.out.println(%22PLACEDETAILSRESOURCE ERROR %22 + e.getMessage());%0A%09%09%09return answer.put(%22status%22,%22error: not found%22).toString();%0A%09%09%7D%0A%09%7D%0A%0A%7D%0A%09

add code to check if given place id exists in datastore. next step: format response