package org.adaptlab.chpir.android.activerecordcloudsync;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.net.Uri;
import android.util.Log;

public class HttpFetchr {
    private static final String TAG = "HttpFetchr";
    private static final String ENDPOINT = "endpointapiurl";
    private ReceiveTable mReceiveTable;
    
    public HttpFetchr(ReceiveTable receiveTable) {
        mReceiveTable = receiveTable;
    }
    
    public String getUrl(String urlSpec) throws IOException {
        return new String(getUrlBytes(urlSpec));
    }
    
    public ArrayList<ReceiveTable> fetch() {
        ArrayList<ReceiveTable> items = new ArrayList<ReceiveTable>();
        
        try {
        String url = Uri.parse(ENDPOINT).buildUpon()
            .appendQueryParameter("last_id", mReceiveTable.lastId().toString())
            .build().toString();
        String jsonString = getUrl(url);

        } catch (IOException ioe) {
            Log.e(TAG, "Failed to fetch items", ioe);
        }
        return items;
    }
    
    private byte[] getUrlBytes(String urlSpec) throws IOException {
        URL url = new URL(urlSpec);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            InputStream in = connection.getInputStream();
            
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return null;
            }
            
            int bytesRead = 0;
            byte[] buffer = new byte[1024];
            while ((bytesRead = in.read(buffer)) > 0) {
                out.write(buffer, 0, bytesRead);
            }
            out.close();
            return out.toByteArray();
        } finally {
            connection.disconnect();
        }
    }
}

@@ -227,24 +227,51 @@
 ArrayList;%0A%0A
+import org.json.JSONArray;%0A
 import org.j
@@ -507,16 +507,142 @@
 piurl%22;%0A
+    private static final String LAST_ID_API_PARAM = %22last_id%22;%0A    private static final String RESULTS_API_PARAM = %22results%22;%0A
     priv
@@ -1043,24 +1043,28 @@
 y %7B%0A        
+    
 String url =
@@ -1108,16 +1108,24 @@
         
+        
 .appendQ
@@ -1138,25 +1138,33 @@
 rameter(
-%22last_id%22
+LAST_ID_API_PARAM
 , mRecei
@@ -1204,16 +1204,24 @@
         
+        
 .build()
@@ -1237,24 +1237,28 @@
 ();%0A        
+ 
+   
 String jsonS
@@ -1282,98 +1282,520 @@
 l);%0A
-%0A        %7D catch (IOException ioe) %7B%0A            Log.e(TAG, %22Failed to fetch items%22, ioe);
+            JSONObject jsonResult = new JSONObject(jsonString);%0A            JSONArray jsonArray = jsonResult.getJSONArray(RESULTS_API_PARAM);%0A            %0A            for (int i = 0; i %3C jsonArray.length(); i++) %7B%0A                mReceiveTable.createObjectFromJSON(jsonArray.getJSONObject(i));%0A            %7D%0A            %0A        %7D catch (IOException ioe) %7B%0A            Log.e(TAG, %22Failed to fetch items%22, ioe);%0A        %7D catch (JSONException je) %7B%0A            Log.e(TAG, %22Failed to parse items%22, je);            
 %0A   

Create receivetable object from json