/**
 * Copyright 2012-2013 StackMob
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.stackmob.customcode;

import com.stackmob.core.customcode.CustomCodeMethod;
import com.stackmob.core.rest.ProcessedAPIRequest;
import com.stackmob.core.rest.ResponseToProcess;
import com.stackmob.sdkapi.SDKServiceProvider;

import java.net.HttpURLConnection;
import java.util.*;

import facebook4j.Facebook;
import facebook4j.FacebookException;
import facebook4j.FacebookFactory;
import facebook4j.Friend;
import facebook4j.Post;
import facebook4j.ResponseList;
import facebook4j.conf.ConfigurationBuilder;


import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class HelloWorld implements CustomCodeMethod {

  @Override
  public String getMethodName() {
    return "hello_world";
  }

  @Override
  public List<String> getParams() {
    return Arrays.asList("accessToken");
  }

  @Override
  public ResponseToProcess execute (ProcessedAPIRequest request, SDKServiceProvider serviceProvider) {
      Map<String, Object> map = new HashMap<String, Object>();

      String accessToken = "";
      JSONParser parser = new JSONParser();
      try {
          Object obj = parser.parse(request.getBody());
          JSONObject jsonObject = (JSONObject) obj;
          accessToken = (String) jsonObject.get("accessToken");
      } catch (ParseException pe) {

      }

      ConfigurationBuilder cb = new ConfigurationBuilder();
      cb.setDebugEnabled(true)
              .setOAuthAppId("651101234913565")
              .setOAuthAppSecret("9eb8f1ddb07752885ff07e6d76ad7903")
              .setOAuthAccessToken(accessToken);

      Facebook facebook = new FacebookFactory(cb.build()).getInstance();
      ResponseList<Friend> myFriends = null;
      try {
          myFriends = facebook.getFriends();
      }
      catch (Exception e) {

      }
      map.put("msg", "hello");


      return new ResponseToProcess(HttpURLConnection.HTTP_OK, map);
  }

}

@@ -1643,16 +1643,19 @@
 ;%0A%0A     
+ //
  String 
@@ -1673,24 +1673,27 @@
  = %22%22;%0A     
+ //
  JSONParser 
@@ -1720,24 +1720,27 @@
 ser();%0A     
+ //
  try %7B%0A     
@@ -1735,24 +1735,27 @@
  try %7B%0A     
+ //
      Object 
@@ -1794,24 +1794,27 @@
 dy());%0A     
+ //
      JSONObj
@@ -1849,24 +1849,27 @@
 ) obj;%0A     
+ //
      accessT
@@ -1920,16 +1920,19 @@
 );%0A     
+ //
  %7D catch
@@ -1960,16 +1960,19 @@
 %7B%0A%0A     
+ //
  %7D%0A%0A    
@@ -1972,16 +1972,19 @@
 %7D%0A%0A     
+ //
  Configu
@@ -2035,16 +2035,19 @@
 );%0A     
+ //
  cb.setD
@@ -2065,24 +2065,27 @@
 (true)%0A     
+ //
          .se
@@ -2112,32 +2112,35 @@
 34913565%22)%0A     
+ //
          .setOAu
@@ -2193,24 +2193,26 @@
 )%0A      
+//
         
 .setOAut
@@ -2195,32 +2195,33 @@
       //        
+ 
 .setOAuthAccessT
@@ -2245,16 +2245,19 @@
 ;%0A%0A     
+ //
  Faceboo
@@ -2321,16 +2321,19 @@
 );%0A     
+ //
  Respons
@@ -2369,16 +2369,19 @@
 l;%0A     
+ //
  try %7B%0A 
@@ -2380,24 +2380,27 @@
  try %7B%0A     
+ //
      myFrien
@@ -2432,24 +2432,27 @@
 );%0A     
+ //
  %7D%0A     
  catch (
@@ -2443,16 +2443,19 @@
  %7D%0A     
+ //
  catch (
@@ -2475,16 +2475,19 @@
 %7B%0A%0A     
+ //
  %7D%0A     

comment all method