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.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONTokener; public class SObjectDataLoader extends Thread { private ProgressBarActionInterface reference = null; private Vector<SObjectItem> sObjects = new Vector<SObjectItem>(); private SFDCOAuth sfdcOauth = null; public SObjectDataLoader(SFDCOAuth oauth, ProgressBarActionInterface ref) { sfdcOauth = oauth; reference = ref; } public void run() { try { executeObjectSearch(); } catch (Exception ex) { ex.printStackTrace(); } reference.euthanize(); } public Vector<SObjectItem> getSObjectContainer() { return sObjects; } // Vector<SObjectItem> public void executeObjectSearch() throws Exception { HttpClient restClient = new HttpClient(); restClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109); restClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000); String restfulURLTarget = sfdcOauth.getServerURL() + sfdcOauth.getSobjectURI(); GetMethod method = null; try { method = new GetMethod(restfulURLTarget); method.setRequestHeader("Authorization", "OAuth " + sfdcOauth.getSecureID()); int httpResponseCode = restClient.executeMethod(method); System.out.println("HTTP_RESPONSE_CODE [" + httpResponseCode + "]"); if (httpResponseCode == HttpStatus.SC_OK) { JSONObject response = new JSONObject( new JSONTokener(new InputStreamReader(method.getResponseBodyAsStream()))); // JSONArray resultArray = response.getJSONArray("sobjects"); // JSONObject inner = null; // inner = resultArray.getJSONObject(0); // Iterator keys = inner.keys(); // HashMap columnNames = new HashMap(); // while(keys.hasNext()){ // String column = (String)keys.next(); // if(!column.equalsIgnoreCase("attributes")){ // columnNames.put(column, ""); // } // } JSONArray results = response.getJSONArray("sobjects"); reference.setMaxValue(results.length()); String objectName = null; for (int objID = 0; objID < results.length(); objID++) { try { if (results.getJSONObject(objID).getString("searchable").equals("true")) { objectName = results.getJSONObject(objID).getString("name"); // if(objectName.equals("userchild__c")){ sObjects.add(parseObjectDetails(new SObjectItem(objectName))); // } reference.updateProgressBar(objectName, objID); if (objectName.endsWith("__c")) { reference.incrementCustomObject(); } else { reference.incrementStandardObject(); } } //end if-else } catch (NullPointerException ex) { ex.printStackTrace(); } } //end for } //end success } finally { method.releaseConnection(); } // return sObjects; } private SObjectItem parseObjectDetails(SObjectItem objectField) throws Exception { //System.out.println("xxxxxxxxxxxxxxxxxxxxx " + objectField.getObjectName() + " xxxxxxxxxxxxxxxxxxxxx"); HttpClient restClient = new HttpClient(); restClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109); restClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000); String restfulURLTarget = sfdcOauth.getServerURL() + sfdcOauth.getSobjectURI() + "/" + objectField.getObjectName() + "/describe/"; // System.out.println("RESTful URL Target: " + restfulURLTarget); GetMethod method = null; try { method = new GetMethod(restfulURLTarget); method.setRequestHeader("Authorization", "OAuth " + sfdcOauth.getSecureID()); int httpResponseCode = restClient.executeMethod(method); // System.out.println("HTTP_RESPONSE_CODE [" + httpResponseCode + "]"); if (httpResponseCode == HttpStatus.SC_OK) { JSONObject response = new JSONObject( new JSONTokener(new InputStreamReader(method.getResponseBodyAsStream()))); // System.out.println("RESPONSE [" + response + "]"); JSONArray fieldObject = response.getJSONArray("fields"); try { SObjectColumnDetail object = null; for (int row = 0; row < fieldObject.length(); row++) { object = new SObjectColumnDetail(); // System.out.println("name [" + fieldObject.getJSONObject(row).getString("name") + "]"); object.setFieldName(fieldObject.getJSONObject(row).getString("name")); // System.out.println("length [" + fieldObject.getJSONObject(row).getString("length") + "]"); object.setLength(fieldObject.getJSONObject(row).getString("length")); // System.out.println("digits [" + fieldObject.getJSONObject(row).getString("digits") + "]"); object.setDigits(fieldObject.getJSONObject(row).getString("digits")); // System.out.println("defaultValue [" + fieldObject.getJSONObject(row).getString("defaultValue") + "]"); object.setDefaultValue(fieldObject.getJSONObject(row).getString("defaultValue")); // System.out.println("autoNumber [" + fieldObject.getJSONObject(row).getString("autoNumber") + "]"); object.setAutoNumber(fieldObject.getJSONObject(row).getString("autoNumber")); // System.out.println("calculated [" + fieldObject.getJSONObject(row).getString("calculated") + "]"); object.setCalculated(fieldObject.getJSONObject(row).getString("calculated")); if (fieldObject.getJSONObject(row).getString("calculated").equals("true")) { // System.out.println("calculatedFormula [" + fieldObject.getJSONObject(row).getString("calculatedFormula") + "]"); object.setCalculatedFormula( fieldObject.getJSONObject(row).getString("calculatedFormula")); } objectField.addColumnDetail(object); // System.out.println("..............."); } } catch (Exception ex) { ex.printStackTrace(); } } } finally { method.releaseConnection(); } // System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); return objectField; } }