Skip to content

Instantly share code, notes, and snippets.

@cfalzone
Created December 8, 2014 21:34
Show Gist options
  • Save cfalzone/1da9c26b0e04aef5fdad to your computer and use it in GitHub Desktop.
Save cfalzone/1da9c26b0e04aef5fdad to your computer and use it in GitHub Desktop.
dotCMS Taleo Integration
package com.aquent.aquentcareers;
import java.io.IOException;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.PermissionAPI;
import com.dotmarketing.business.VersionableAPI;
import com.dotmarketing.cache.StructureCache;
import com.dotmarketing.portlets.categories.business.CategoryAPI;
import com.dotmarketing.portlets.categories.model.Category;
import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.structure.model.Relationship;
import com.dotmarketing.portlets.structure.model.Structure;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.UtilMethods;
import com.dotmarketing.viewtools.MailerTool;
import com.liferay.portal.model.User;
// This guy is run on a schedule and pulls the data from taleo into dotCMS Content
// I have a simple Quartz Job that hits this servlet every 4 hours I think
// The reason I made it a servlet was twofold. First, Quartz cannot "see" Classes in the OSGI COntext
// Second, I wanted to be able ot run it on demand as well
public class TaleoImporterServlet extends HttpServlet {
private static final long serialVersionUID = -2834767920632222231L;
private Map<String, Integer> succeeded = new HashMap<String, Integer>();
private Map<String, String> skipped = new HashMap<String, String> ();
private Map<String, String> warnings = new HashMap<String, String> ();
private Map<String, String> errors = new HashMap<String, String> ();
private int totalJobs = 0;
private User user = null;
private Structure structure;
private Host host = null;
private static final String STRUCTURE_VAR_NAME = "AquentCareersJobs";
private static final String HOST_NAME = "aquentcareers.com";
private static final String REPORT_SUBJECT = "Taleo Importer Report";
private static final String REPORT_TO = "[email protected]";
private static final String REPORT_FROM = "[email protected]";
private ContentletAPI conAPI = APILocator.getContentletAPI();
private PermissionAPI perAPI = APILocator.getPermissionAPI();
private CategoryAPI catAPI = APILocator.getCategoryAPI();
private VersionableAPI verAPI = APILocator.getVersionableAPI();
public void init(ServletConfig config) throws ServletException { super.init(config); }
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Logger.info(this, "***** Starting the Taleo Import Job");
succeeded.put("New Jobs Added", 0);
succeeded.put("Jobs Updated", 0);
try {
user = APILocator.getUserAPI().getSystemUser();
} catch(Exception e) {
errors.put("init", "Unable to get the system user: "+e.getMessage());
Logger.error(this, "system user error", e);
return;
}
String days = "2";
if(UtilMethods.isSet(req.getParameter("days"))) {
days = req.getParameter("days");
}
Logger.info(this, "Importing Jobs from the past "+days+" days");
importTaleoJobs(days);
String message = reporting();
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
ServletOutputStream out = resp.getOutputStream();
out.println(message);
Logger.info(this, "***** Finished the Taleo Import Job");
}
private Structure getStructure() {
if(!UtilMethods.isSet(structure)) {
structure = StructureCache.getStructureByVelocityVarName(STRUCTURE_VAR_NAME);
}
return structure;
}
private Host getHost() {
if(host == null) {
try {
host=APILocator.getHostAPI().findByName(HOST_NAME, user, false);
} catch(Exception e) {
Logger.error(this, "Unable to get the host", e);
errors.put("init", "Unable to get the host");
}
}
return host;
}
private void importTaleoJobs(String days) {
// Get the current production taleo site
// This is so I know if this is posted to the production instance in Taleo or test one.
// I just have a field for this in my host structure.
// I guess it could be a plugin property or just hardcoded
Host ACHost = getHost();
if(ACHost == null) return;
String prodSite = ACHost.getStringProperty("taleoProductionSite");
if(! UtilMethods.isSet(prodSite)) {
errors.put("init", "No taleoProductionSite setting found on the Aquent Careers Host");
return;
}
// Pull the jobs
// We generally run 2 days every pull, this works good for the number of jobs we have.
// You might have to play with what schedule works best for you.
// However I do have the ability to call the servlet and have it completely reload all jobs
// Which we have to do from time to time
JSONArray jobs;
try {
if(days.equalsIgnoreCase("ALL")) jobs = TaleoUtil.getAllJobs();
else jobs = TaleoUtil.getLatestJobs(Integer.parseInt(days));
} catch (Exception e) {
errors.put("init", "Unable to pull jobs from Taleo: "+e.getMessage());
Logger.error(this, "Unable to pull jobs from Taleo", e);
return;
}
totalJobs = jobs.length();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for(int i=0; i<jobs.length(); i++) {
Logger.info(this, "Starting Job #"+i+" of "+jobs.length());
JSONObject job;
try {
job = jobs.getJSONObject(i).getJSONObject("requisition");
} catch (JSONException e) {
errors.put("listPos="+i, "Unable to get a job from the list: "+e.getMessage()+"<br />Data: <textarea>"+jobs.toString()+"</textarea>");
Logger.error(this, "json error getting a job from the list", e);
continue;
}
// These are fields we keep in dotCMS.
// I also save the apiresponse so I can parse extra data out of it should we ever need
String jobNumber;
String jobTitle;
String openedDateStr;
String placementType;
String description;
String jobGroup;
String apiResponse;
int division;
int location;
try {
jobNumber = job.getString("reqId");
jobTitle = job.getString("title");
division = job.getInt("division");
openedDateStr = job.getString("openedDate");
placementType = job.getString("EmploymentType");
description = job.getString("description");
jobGroup = job.getString("JobGroup");
location = job.getInt("location");
apiResponse = job.toString(2);
} catch (JSONException e) {
errors.put("listPos="+i, "Unable to get the job details from this job: "+e.getMessage());
Logger.error(this, "json error getting properties from job", e);
continue;
}
// validate some of the fields here
// You'll have to come up with what business rules work well for you here.
if(! UtilMethods.isSet(jobNumber)) {
skipped.put("listPos="+i, "No Job Number for this listing");
continue;
}
if(! UtilMethods.isSet(jobTitle)) {
skipped.put("jobNumber="+jobNumber, "No Job Title for this listing");
continue;
}
if(! UtilMethods.isSet(division)) {
skipped.put("jobNumber="+jobNumber, "No Division for this listing");
continue;
}
if(! UtilMethods.isSet(description)) {
skipped.put("jobNumber="+jobNumber, "No description for this listing");
continue;
}
if(! UtilMethods.isSet(placementType)) {
warnings.put("jobNumber="+jobNumber+" - placementType", "No placement type for this listing");
}
if(! UtilMethods.isSet(openedDateStr)) {
warnings.put("jobNumber="+jobNumber+" - openedDate", "No start date for this listing");
}
if(! UtilMethods.isSet(location)) {
warnings.put("jobNumber="+jobNumber+" - location", "No location for this listing");
}
// See if this job exists
boolean isNew = false;
String query = "+structureName:"+STRUCTURE_VAR_NAME+" +"+STRUCTURE_VAR_NAME+".jobNumber:"+jobNumber;
List<Contentlet> cons;
try {
cons = conAPI.search(query, -1, 0, "modDate desc", user, false);
} catch (Exception e) {
errors.put("jobNumber="+jobNumber, "Unable to search for existing job: "+e.getMessage());
Logger.error(this, "content search error", e);
continue;
}
Contentlet c;
if(cons.size() > 0) {
// The job exists so we are going to update this one
c = cons.get(0);
} else {
// The job does not exist so we have to make a new contentlet
isNew = true;
c = new Contentlet();
c.setIdentifier("");
c.setStructureInode(getStructure().getInode());
c.setHost(getHost().getIdentifier());
c.setLanguageId(1L);
}
// Map division to a category
JSONObject divJson;
try {
divJson = TaleoUtil.getDivision(division);
} catch (Exception e) {
errors.put("jobNumber="+jobNumber, "Unable to get the taleo division for id ("+division+"): "+e.getMessage());
Logger.error(this, "taleo division error", e);
continue;
}
String divName;
try {
divName = divJson.getJSONObject("division").getString("divisionName");
} catch (JSONException e) {
errors.put("jobNumber="+jobNumber, "Unable to get the taleo division for id ("+division+") from the json: "+e.getMessage());
Logger.error(this, "json error getting division name", e);
continue;
}
Category divCat = null;
try {
if(divName.equalsIgnoreCase("Aquent")) {
divCat = catAPI.findByKey("comp_aq", user, false);
} else if(divName.equalsIgnoreCase("VitaminT")) {
divCat = catAPI.findByKey("comp_vt", user, false);
} else {
skipped.put("jobNumber="+jobNumber, "Could not determine the brand category for this job from division name ("+divName+")");
continue;
}
} catch (Exception e) {
errors.put("jobNumber="+jobNumber, "Could not determine the brand category for this job from division name ("+divName+"): "+e.getMessage());
Logger.error(this, "error getting brand category from division name", e);
continue;
}
List<Category> cCats = new ArrayList<Category>();
cCats.add(divCat);
// Get the job's status
String status="Unknown";
boolean isOpen = false;
try {
JSONObject statusJson = TaleoUtil.getJobStatus(Integer.valueOf(jobNumber)).getJSONObject("status");
status = statusJson.getString("text");
if(status.toLowerCase().startsWith("open")) isOpen = true;
} catch (Exception e) {
warnings.put("jobNumber="+jobNumber+" - status", "Could not get status details for this job: "+e.getMessage());
Logger.warn(this, "error getting status details for a job", e);
}
// Get the location fields
String city = "";
String state = "";
String zip = "";
String country = "";
String locName = "";
String locApiResponse = "";
if(UtilMethods.isSet(location)) {
try {
JSONObject locJson = TaleoUtil.getLocation(location).getJSONObject("location");
city = locJson.getString("city");
state = locJson.getString("state");
zip = locJson.getString("zipCode");
country = locJson.getString("countryCode");
locName = locJson.getString("locationName");
locApiResponse = locJson.toString(2);
} catch (Exception e) {
warnings.put("jobNumber="+jobNumber+" - location", "Could not get location details from ("+location+"): "+e.getMessage());
Logger.warn(this, "json error getting location details", e);
}
}
// Determine if this job is posted and when
// This part was a little tricky to come up with
String postedTo = "";
Date postDate = new Date();
Date delistDate = new Date();
boolean isProduction = false;
boolean neverExpires = false;
try {
JSONArray posters = TaleoUtil.getJobPoster(jobNumber);
boolean found = false;
for(int p=0; p<posters.length(); p++) {
JSONObject poster = posters.getJSONObject(p).getJSONObject("poster");
int website = poster.getInt("careerWebSiteId");
JSONObject websiteJson = TaleoUtil.getJobPosterDetails(website);
postedTo = websiteJson.getJSONObject("careerswebsite").getString("name");
if(postedTo.equals(prodSite)) {
found = true;
isProduction = true;
String postDateStr = poster.getString("postDate");
String delistDateStr = poster.getString("delistDate");
postDate = sdf.parse(postDateStr);
if(UtilMethods.isSet(delistDateStr)) {
delistDate = sdf.parse(delistDateStr);
} else {
neverExpires = true;
}
break;
}
}
if(!found) {
warnings.put("jobNumber="+jobNumber+" - posted", "Not posted to a production site");
}
} catch (Exception e) {
warnings.put("jobNumber="+jobNumber+" - posted", "Could not get posting details: "+e.getMessage());
Logger.warn(this, "json error geting posting details", e);
}
// Format the opened date
Date openedDate = new Date();
try {
openedDate = sdf.parse(openedDateStr);
} catch (Exception e) {
warnings.put("jobNumber="+jobNumber+" - openedDate", "Unable to parse the start date from "+openedDateStr+" : "+e.getMessage());
Logger.warn(this, "parse date error", e);
}
// Determine Placement Type
if(UtilMethods.isSet(placementType)) {
String ptquery = "+structureName:AquentCareersPlacementTypes +live:true +deleted:false +AquentCareersPlacementTypes.taleoValue:\""+placementType+"\"";
List<Contentlet> ptcons;
try {
ptcons = conAPI.search(ptquery, -1, 0, "modDate desc", user, false);
} catch (Exception e) {
errors.put("jobNumber="+jobNumber, "Unable to search for placement type from ("+placementType+"): "+e.getMessage());
Logger.error(this, "search for PlacementType error", e);
continue;
}
if(ptcons.size() > 0) {
placementType = ptcons.get(0).getStringProperty("id");
} else {
warnings.put("jobNumber="+jobNumber+" - placementType", "Unable to determine placement type from "+placementType);
placementType = "";
}
}
// Determine Job Group
if(UtilMethods.isSet(jobGroup)) {
String jgquery = "+structureName:AquentCareersJobGroups +live:true +deleted:false +AquentCareersJobGroups.taleoValue:\""+jobGroup+"\"";
List<Contentlet> jgcons;
try {
jgcons = conAPI.search(jgquery, -1, 0, "modDate desc", user, false);
} catch (Exception e) {
errors.put("jobNumber="+jobNumber, "Unable to search for job group from ("+jobGroup+")");
Logger.error(this, "search for jobGroup error", e);
continue;
}
if(jgcons.size() > 0) {
jobGroup = jgcons.get(0).getStringProperty("id");
} else {
warnings.put("jobNumber="+jobNumber+" - jobGroup", "Unable to determine job group from "+jobGroup);
jobGroup = "";
}
}
c.setStringProperty("jobNumber", jobNumber); // Required
c.setStringProperty("jobTitle", jobTitle); // Required
c.setStringProperty("division", divCat.getKey()); // Required
c.setDateProperty("openedDate", openedDate);
c.setStringProperty("placementType", placementType);
c.setStringProperty("description", description); // Required
c.setStringProperty("jobGroup", jobGroup);
c.setStringProperty("status", status);
c.setBoolProperty("isOpen", isOpen);
c.setStringProperty("city", city);
c.setStringProperty("state", state);
c.setStringProperty("zip", zip);
c.setStringProperty("country", country);
c.setStringProperty("locationName", locName);
c.setStringProperty("isProduction", isProduction ? "1" : "0");
c.setBoolProperty("neverExpires", neverExpires);
c.setDateProperty("postDate", postDate);
c.setDateProperty("delistDate", delistDate);
c.setStringProperty("locationApiResponse", locApiResponse);
c.setStringProperty("apiResponse", apiResponse);
try {
if(isNew) {
c = conAPI.checkin(c, new HashMap<Relationship, List<Contentlet>>(), cCats, perAPI.getPermissions(getStructure()), user, false);
verAPI.setLive(c);
int count = succeeded.get("New Jobs Added");
count = count + 1;
succeeded.put("New Jobs Added", count);
} else {
c = conAPI.checkinWithoutVersioning(c, new HashMap<Relationship, List<Contentlet>>(), cCats, perAPI.getPermissions(getStructure()), user, false);
verAPI.setLive(c);
int count = succeeded.get("Jobs Updated");
count = count + 1;
succeeded.put("Jobs Updated", count);
}
} catch (Exception e) {
errors.put("jobNumber="+jobNumber, "Unable to checkin content: "+e.getMessage());
Logger.error(this, "checkin error", e);
continue;
}
Logger.info(this, "Finished Job #"+i+" of "+jobs.length());
}
}
// We send an email with the results to a group email list, works good for us anyway
// I can see if there were errors or why a specific job didn't import
private String reporting() {
StringBuilder buff = new StringBuilder();
for(String key : errors.keySet()) buff.append("\n"+key+" - "+errors.get(key));
StringBuilder buff2 = new StringBuilder();
for(String key : warnings.keySet()) buff2.append("\n"+key+" - "+warnings.get(key));
Logger.info(this, "*** Total Jobs: "+totalJobs);
Logger.info(this, "*** Jobs Added: "+succeeded.get("New Jobs Added"));
Logger.info(this, "*** Jobs Updated: "+succeeded.get("Jobs Updated"));
Logger.info(this, "*** Jobs Skipped: "+skipped.keySet().size());
Logger.info(this, "*** Warnings: "+buff2.toString());
Logger.info(this, "*** Errors: "+buff.toString());
// Send Email
String subject = REPORT_SUBJECT;
StringBuilder message = new StringBuilder();
try {
message.append("<b>Job Ran on</b>:"+InetAddress.getLocalHost().getHostName()+"<br /><br />");
} catch(Exception e) {
// Do Nothing - shouldn't happen
}
message.append("<b>Total Jobs</b>: "+totalJobs+"<br />");
message.append("<b>Jobs Added</b>: "+succeeded.get("New Jobs Added")+"<br />");
message.append("<b>Jobs Updated</b>: "+succeeded.get("Jobs Updated")+"<br />");
message.append("<b>Jobs Skipped</b>: "+skipped.keySet().size()+"<br /><br />");
if(skipped.keySet().size() > 0) {
message.append("<b>Skipped</b>: <ul>");
for(String key : skipped.keySet()) message.append("<li>"+key+" - "+skipped.get(key)+"</li>");
message.append("</ul><br />");
}
if(warnings.keySet().size() > 0) {
message.append("<b>Warnings</b>: <ul>");
for(String key : warnings.keySet()) message.append("<li>"+key+" - "+warnings.get(key)+"</li>");
message.append("</ul><br />");
}
if(errors.keySet().size() > 0) {
subject = "ERROR: "+subject;
message.append("<b>Errors</b>: <ul>");
for(String key : errors.keySet()) message.append("<li>"+key+" - "+errors.get(key)+"</li>");
message.append("</ul><br />");
}
MailerTool mailer = new MailerTool();
String resp = mailer.sendEmail(REPORT_TO, REPORT_FROM, subject, message.toString(), true);
if(UtilMethods.isSet(resp)) {
Logger.error(this, "***** Unable to send report email: "+resp);
}
return message.toString();
}
}
package com.aquent.aquentcareers;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
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.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.json.JSONArray;
import org.json.JSONObject;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.UtilMethods;
public class TaleoUtil {
private static final String taleoUrl = "https://tbe.taleo.net/MANAGER/dispatcher/api/v1/serviceUrl/aquent";
private static final String orgCode = "*****"; // you will need to fill these in
private static final String userName = "*****";
private static final String password = "*****";
private static HttpClient client = null;
private static String endpoint = null;
private static String authToken = null;
public TaleoUtil() { };
private static HttpClient getClient() {
if(client == null) {
client = new HttpClient();
}
return client;
}
// I found some times the connection to taleo's api would stop working and getting a new httpclient helped resolve that
public static void reset() {
client = new HttpClient();
endpoint = null;
authToken = null;
}
// Getting the response from taleo and returning it
private static String fetchTaleoResponse(HttpMethod method) {
try {
int statusCode = getClient().executeMethod(method);
if(statusCode != HttpStatus.SC_OK) {
Logger.error(TaleoUtil.class, "Error Fetching Taleo Response:"+method.getResponseBodyAsString());
return null;
} else {
return method.getResponseBodyAsString();
}
} catch (Exception e) {
Logger.error(TaleoUtil.class, "Exception Fetching Taleo Response", e);
return null;
}
}
// the endpoint it the starting point for all calls
private static String getEndpoint() throws Exception {
if(endpoint == null) {
HttpMethod method = new GetMethod(taleoUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
String taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
// try again after reset
reset();
taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
throw new Exception("Could Not Fetch Taleo Endpoint, see previous errors");
}
}
JSONObject json = new JSONObject(taleoResponse);
boolean success = json.getJSONObject("status").getBoolean("success");
if(success) {
endpoint = json.getJSONObject("response").getString("URL");
} else {
throw new Exception("Fetching Taleo Endpoint did not result in success: "+taleoResponse);
}
}
return endpoint;
}
// Next step get an authentication token from the Taleo API
private static String getAuthToken() throws Exception {
if(authToken == null) {
String loginUrl = getEndpoint()+"/login";
PostMethod method = new PostMethod(loginUrl);
NameValuePair[] loginData = {
new NameValuePair("orgCode", orgCode),
new NameValuePair("userName", userName),
new NameValuePair("password", password)
};
method.setRequestBody(loginData);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
String taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
// Try again after reset
reset();
loginUrl = getEndpoint()+"/login";
method = new PostMethod(loginUrl);
method.setRequestBody(loginData);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
throw new Exception("Could Not Fetch Taleo AuthToken, see previous errors");
}
}
JSONObject json = new JSONObject(taleoResponse);
boolean success = json.getJSONObject("status").getBoolean("success");
if(success) {
authToken = json.getJSONObject("response").getString("authToken");
} else {
throw new Exception("Fetching Taleo AuthToken did not result in success: "+taleoResponse);
}
}
return authToken;
}
// This fetches the last days of job data from Taleo. Note that this method does not handle paging of the data.
// So if you get more than the limit in your delta you will need to modify it some to handle paging, see the next method
public static JSONArray getLatestJobs(int days) throws Exception {
if(days > 0) {
String reqUrl = getEndpoint()+"object/requisition/search?updatedWithin="+days;
HttpMethod method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
String taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
// Try again after reset
reset();
reqUrl = getEndpoint()+"object/requisition/search?updatedWithin="+days;
method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
throw new Exception("Could Not Fetch Jobs, see previous errors");
}
}
JSONObject json = new JSONObject(taleoResponse);
boolean success = json.getJSONObject("status").getBoolean("success");
if(success) {
return json.getJSONObject("response").getJSONArray("searchResults");
} else {
throw new Exception("Fetching Jobs did not result in success: "+taleoResponse);
}
} else {
return null;
}
}
// This tries to get all of the jobs, they are paged so it is not as cut and dry as it sounds.
public static JSONArray getAllJobs() throws Exception {
String reqUrl = getEndpoint()+"object/requisition/search?openedDate_from=2001-01-01";
HttpMethod method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
String taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
// Try again after reset
reset();
reqUrl = getEndpoint()+"object/requisition/search?openedDate_from=2001-01-01";
method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
throw new Exception("Could Not Fetch Jobs, see previous errors");
}
}
JSONObject json = new JSONObject(taleoResponse);
boolean success = json.getJSONObject("status").getBoolean("success");
if(success) {
JSONObject response = json.getJSONObject("response");
JSONArray searchResults = response.getJSONArray("searchResults");
Logger.info(TaleoUtil.class, "Pulled "+searchResults.length()+" jobs");
if(response.has("pagination")) {
JSONObject pagination = response.getJSONObject("pagination");
Logger.info(TaleoUtil.class, "Has More: "+pagination.toString(2));
if(pagination.has("next")) {
boolean hasMore = true;
while(hasMore) {
method = new GetMethod(pagination.getString("next"));
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
reset();
hasMore = false;
} else {
json = new JSONObject(taleoResponse);
success = json.getJSONObject("status").getBoolean("success");
if(success) {
response = json.getJSONObject("response");
JSONArray moreResults = response.getJSONArray("searchResults");
Logger.info(TaleoUtil.class, "Pulled "+moreResults.length()+" more jobs");
for(int i=0; i<moreResults.length(); i++) {
searchResults.put(moreResults.get(i));
}
if(response.has("pagination")) {
pagination = response.getJSONObject("pagination");
Logger.info(TaleoUtil.class, "Has Even More: "+pagination.toString(2));
if(! pagination.has("next")) {
hasMore = false;
}
} else {
hasMore = false;
}
}
}
Logger.info(TaleoUtil.class, "Total Jobs So Far: "+searchResults.length());
}
}
}
return searchResults;
} else {
throw new Exception("Fetching Jobs did not result in success: "+taleoResponse);
}
}
// This gets a specific job's details
public static JSONObject getJob(String jobId) throws Exception {
if(UtilMethods.isSet(jobId)) {
String reqUrl = getEndpoint()+"object/requisition/"+jobId;
HttpMethod method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
String taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
// Try again after reset
reset();
reqUrl = getEndpoint()+"object/requisition/"+jobId;
method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
throw new Exception("Could Not Fetch JobId ("+jobId+"), see previous errors");
}
}
JSONObject json = new JSONObject(taleoResponse);
boolean success = json.getJSONObject("status").getBoolean("success");
if(success) {
return json.getJSONObject("response");
} else {
throw new Exception("Fetching JobId ("+jobId+") did not result in success: "+taleoResponse);
}
} else {
return null;
}
}
// This fetches all of the divisions -- again no paging handling
public static JSONArray getAllDivisions() throws Exception {
String reqUrl = getEndpoint()+"object/division";
HttpMethod method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
String taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
// Try again after reset
reset();
reqUrl = getEndpoint()+"object/division";
method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
throw new Exception("Could Not Fetch Divisions, see previous errors");
}
}
JSONObject json = new JSONObject(taleoResponse);
boolean success = json.getJSONObject("status").getBoolean("success");
if(success) {
return json.getJSONObject("response").getJSONArray("divisions");
} else {
throw new Exception("Fetching divisions did not result in success: "+taleoResponse);
}
}
// This gets a specific division by id
public static JSONObject getDivision(int divId) throws Exception {
if(UtilMethods.isSet(divId)) {
String reqUrl = getEndpoint()+"object/division/"+divId;
HttpMethod method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
String taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
// Try again after reset
reset();
reqUrl = getEndpoint()+"object/division/"+divId;
method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
throw new Exception("Could Not Fetch DivId ("+divId+"), see previous errors");
}
}
JSONObject json = new JSONObject(taleoResponse);
boolean success = json.getJSONObject("status").getBoolean("success");
if(success) {
return json.getJSONObject("response");
} else {
throw new Exception("Fetching DivId ("+divId+") did not result in success: "+taleoResponse);
}
} else {
return null;
}
}
// This gets all locations - again no paging handled
public static JSONArray getAllLocations() throws Exception {
String reqUrl = getEndpoint()+"object/location";
HttpMethod method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
String taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
// Try again after reset
reset();
reqUrl = getEndpoint()+"object/location";
method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
throw new Exception("Could Not Fetch Locations, see previous errors");
}
}
JSONObject json = new JSONObject(taleoResponse);
boolean success = json.getJSONObject("status").getBoolean("success");
if(success) {
return json.getJSONObject("response").getJSONArray("locations");
} else {
throw new Exception("Fetching locations did not result in success: "+taleoResponse);
}
}
// Getting a specific location's details
public static JSONObject getLocation(int locId) throws Exception {
if(UtilMethods.isSet(locId)) {
String reqUrl = getEndpoint()+"object/location/"+locId;
HttpMethod method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
String taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
// Try again after reset
reset();
reqUrl = getEndpoint()+"object/location/"+locId;
method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
throw new Exception("Could Not Fetch LocId ("+locId+"), see previous errors");
}
}
JSONObject json = new JSONObject(taleoResponse);
boolean success = json.getJSONObject("status").getBoolean("success");
if(success) {
return json.getJSONObject("response");
} else {
throw new Exception("Fetching LocId ("+locId+") did not result in success: "+taleoResponse);
}
} else {
return null;
}
}
// Get the job status object
public static JSONObject getJobStatus(int id) throws Exception {
if(UtilMethods.isSet(id)) {
String reqUrl = getEndpoint()+"object/requisition/"+id+"/status";
HttpMethod method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
String taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
// try again after reset
reset();
reqUrl = getEndpoint()+"object/requisition/"+id+"/status";
method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
throw new Exception("Could Not Fetch Status for ID ("+id+"), see previous errors");
}
}
JSONObject json = new JSONObject(taleoResponse);
boolean success = json.getJSONObject("status").getBoolean("success");
if(success) {
return json.getJSONObject("response");
} else {
throw new Exception("Fetching Status for ID ("+id+") did not result in success: "+taleoResponse);
}
} else {
return null;
}
}
// Get's a jobs posters (who posted the job)
public static JSONArray getJobPoster(String id) throws Exception {
if(UtilMethods.isSet(id)) {
String reqUrl = getEndpoint()+"object/requisition/"+id+"/poster";
HttpMethod method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
String taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
// try again after reset
reset();
reqUrl = getEndpoint()+"object/requisition/"+id+"/poster";
method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
throw new Exception("Could Not Fetch Poster for ID ("+id+"), see previous errors");
}
}
JSONObject json = new JSONObject(taleoResponse);
boolean success = json.getJSONObject("status").getBoolean("success");
if(success) {
return json.getJSONObject("response").getJSONArray("posters");
} else {
throw new Exception("Fetching Posters for ID ("+id+") did not result in success: "+taleoResponse);
}
} else {
return null;
}
}
// Gets all of the posters' details (You might need to change the url on this one to match your site within Taleo)
public static JSONObject getAllJobPosterDetails() throws Exception {
String reqUrl = getEndpoint()+"object/careerswebsite/search";
HttpMethod method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
String taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
// try again after reset
reset();
reqUrl = getEndpoint()+"object/careerswebsite/search";
method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
throw new Exception("Could Not Fetch All Poster Details, see previous errors");
}
}
JSONObject json = new JSONObject(taleoResponse);
boolean success = json.getJSONObject("status").getBoolean("success");
if(success) {
return json.getJSONObject("response");
} else {
throw new Exception("Fetching All Poster Details did not result in success: "+taleoResponse);
}
}
// This gets a specific poster's details (You might need to change the url on this one to match your site within Taleo)
public static JSONObject getJobPosterDetails(int id) throws Exception {
if(UtilMethods.isSet(id)) {
String reqUrl = getEndpoint()+"object/careerswebsite/"+id;
HttpMethod method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
String taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
// try again after reset
reset();
reqUrl = getEndpoint()+"object/careerswebsite/"+id;
method = new GetMethod(reqUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.getParams().setIntParameter( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1000000000);
method.setRequestHeader("Cookie", "authToken="+getAuthToken());
taleoResponse = fetchTaleoResponse(method);
if(taleoResponse == null) {
throw new Exception("Could Not Fetch Poster Details for PosterID ("+id+"), see previous errors");
}
}
JSONObject json = new JSONObject(taleoResponse);
boolean success = json.getJSONObject("status").getBoolean("success");
if(success) {
return json.getJSONObject("response");
} else {
throw new Exception("Fetching Poster Details for PosterID ("+id+") did not result in success: "+taleoResponse);
}
} else {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment