fork of SalesforceCaseGenerator customized for support personnel database changes

Fork of SalesforceCaseGenerator by Doug Anson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SalesForceCaseGenerator.cpp Source File

SalesForceCaseGenerator.cpp

00001 /* Copyright C2014 ARM, MIT License
00002  *
00003  * Author: Doug Anson (doug.anson@arm.com)
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00006  * and associated documentation files the "Software", to deal in the Software without restriction,
00007  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00008  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in all copies or
00012  * substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00015  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00016  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00017  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00018  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00019  */
00020  
00021  #include "SalesForceCaseGenerator.h"
00022  
00023   // Logging
00024  #define LOG(...) { if (this->logger() != NULL) { this->logger()->log(__VA_ARGS__); } }
00025  #define LOG_CONSOLE(...) { if (this->logger() != NULL) { this->logger()->logConsole(__VA_ARGS__); } }
00026  
00027  // constructor
00028  SalesForceCaseGenerator::SalesForceCaseGenerator(HTTPClient *http,Logger *logger) : SalesforceInterface(http,logger) {
00029  }
00030  
00031  // destructor
00032  SalesForceCaseGenerator::~SalesForceCaseGenerator() {
00033  }
00034  
00035  // Create an anonymous Case instance
00036  bool SalesForceCaseGenerator::createAnonymousCase(char *technician,char *description,char *status,int temperature,char *latitude,char *longitude) {
00037      bool success = false;
00038      
00039      // data buffer and result buffer
00040      ALLOC_BUFFER(result);
00041      
00042      // set addl header information needed for annonymous acceptance (only)
00043      char headers[] = "User-Agent: curl/7.33.0\nAccept: */*\n" ;
00044         
00045      // build the new case to issue to Salesforce.com 
00046      MbedJSONValue report;
00047      report["subject"]       = technician;          // we use the Subject field from Case
00048      report["description"]   = description;         // Custom field - description of technician
00049      report["condition"]     = status;              // Custom field - originally named "Condition", now "Status" of subject
00050      report["temperature"]   = temperature;         // Custom field - ambient temperature in C
00051      report["latitude"]      = latitude;            // Custom field - technician position latitude
00052      report["longitude"]     = longitude;           // Custom field - technician position longitude
00053         
00054      // covert to the HTTP data types
00055      ALLOC_BUFFER(data);
00056      strcpy(data,report.serialize().c_str());
00057      LOG_CONSOLE("Case JSON: %s",data);
00058      
00059      // prepare the result buffer
00060      HTTPJson new_case(data,strlen(data)+1);
00061      HTTPText http_result(result,MAX_BUFFER_LENGTH);
00062      
00063      // set headers  
00064      this->http()->setHeader(headers) ;
00065      
00066      // for anonymous case generation, we simply use the raw HTTP interface and POST...
00067      LOG_CONSOLE("Posting Case...");
00068      HTTPResult ret = this->http()->post(DF_CASE_GEN_URL,new_case,&http_result,7500);
00069      if (ret == HTTP_OK) {
00070         LOG_CONSOLE("Parsing Reply...");
00071         
00072         // we are looking for { "status": "ok" }...
00073         success = this->contains(result,"status","ok");
00074         if (success) {
00075             LOG_CONSOLE("Case post: SUCCESS");
00076         }
00077         else {
00078             LOG_CONSOLE("Case post: FAILED: status: %d http status: %d error: %s",ret,this->http()->getHTTPResponseCode(),result);
00079         }
00080      }
00081      else {
00082         LOG_CONSOLE("Case post: FAILED: status: %d http status: %d error: %s",ret,this->http()->getHTTPResponseCode(),result);
00083      }
00084      
00085      // return our status
00086      return success;
00087  }