Java tutorial
/* Cirrostratus: Platform independent SOQL Editor for use with SalesForce and Dreamforce cloud databases Copyright (C) 2011 Steve S Gee Jr <ioexcept@gmail.com> Jim Daly <thejamesdaly@gmail.com> Bryan Leboff <leboff@gmail.com> http://cirrostratus.sourceforge.net/ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.html>. */ package org.geefive.salesforce.soqleditor; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Iterator; import java.util.Vector; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; public class SOQLQueryEngine { private SFDCOAuth sfdcOauth = null; public SOQLQueryEngine(SFDCOAuth oauth) { sfdcOauth = oauth; } public QueryResultContainer executeQuery(String soql) throws Exception { HttpClient restClient = new HttpClient(); restClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109); restClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000); String restfulURLTarget = sfdcOauth.getServerURL() + sfdcOauth.getQueryURI(); GetMethod method = null; QueryResultContainer soqlResults = new QueryResultContainer(); try { method = new GetMethod(restfulURLTarget); // updateStatus("Setting authorization header with SID [" + sfdcOauth.getSecureID() + "]"); method.setRequestHeader("Authorization", "OAuth " + sfdcOauth.getSecureID()); NameValuePair[] params = new NameValuePair[1]; // params[0] = new NameValuePair("q","SELECT Name, Id, Phone, CreatedById from Account LIMIT 100"); params[0] = new NameValuePair("q", soql); method.setQueryString(params); int httpResponseCode = restClient.executeMethod(method); // updateStatus("HTTP_RESPONSE_CODE [" + httpResponseCode + "]"); // updateStatus("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"); // updateStatus("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% EXECUTING QUERY %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"); if (httpResponseCode == HttpStatus.SC_OK) { try { JSONObject response = new JSONObject( new JSONTokener(new InputStreamReader(method.getResponseBodyAsStream()))); JSONArray results = response.getJSONArray("records"); Vector<HashMap<String, String>> resies = getResults(results, new HashMap<String, String>(), new Vector<HashMap<String, String>>(), ""); soqlResults = createOutput(resies); } catch (JSONException jsonex) { throw jsonex; } } } finally { method.releaseConnection(); } return soqlResults; } private QueryResultContainer createOutput(Vector<HashMap<String, String>> resies) { QueryResultContainer soqlResults = new QueryResultContainer(); //create columns for (HashMap<String, String> res : resies) { Iterator<String> keys = res.keySet().iterator(); while (keys.hasNext()) { String column = keys.next(); if (!soqlResults.getColumnVector().contains(column)) { soqlResults.addColumnHeader(column); } } } //create rows for (HashMap<String, String> res : resies) { Vector<String> newRow = new Vector<String>(); for (String column : soqlResults.getColumnVector()) { String value = res.get(column); if (value != null && value.equals("null")) newRow.add(""); else newRow.add(value); } soqlResults.addDataRow(newRow); } return soqlResults; } private Vector<HashMap<String, String>> hashObj(JSONObject results, HashMap<String, String> newRow, Vector<HashMap<String, String>> newRows, String object) throws JSONException { Iterator keys = results.keys(); while (keys.hasNext()) { String column = (String) keys.next(); if (results.get(column).getClass().equals(JSONObject.class) && !column.equalsIgnoreCase("attributes")) { if (results.getJSONObject(column).has("records")) { //its a many child json object treat accordingly newRows = getResults(results.getJSONObject(column).getJSONArray("records"), new HashMap<String, String>(newRow), newRows, column); //get rid of parent row.. only keep the children newRow.clear(); } else { hashObj(results.getJSONObject(column), newRow, new Vector<HashMap<String, String>>(), column); } } else if (!column.equalsIgnoreCase("attributes")) { String pre = object.equals("") ? "" : object + "."; String columnValue = results.getString(column); newRow.put(pre + column, columnValue); } } if (!newRow.isEmpty()) newRows.add(newRow); return newRows; } private Vector<HashMap<String, String>> getResults(JSONArray response, HashMap<String, String> newRow, Vector<HashMap<String, String>> newRows, String object) throws JSONException { for (int i = 0; i < response.length(); i++) { JSONObject subObjs = response.getJSONObject(i); newRows = hashObj(subObjs, newRow, newRows, object); newRow = new HashMap<String, String>(newRow); } return newRows; } }