Skip to content

Instantly share code, notes, and snippets.

@paulparton
Created October 31, 2014 05:49
Show Gist options
  • Save paulparton/8057cb2a7cad52730b14 to your computer and use it in GitHub Desktop.
Save paulparton/8057cb2a7cad52730b14 to your computer and use it in GitHub Desktop.
Report It v3 CMRS service connectivity
var crms = angular.module('reportIt.crmsService', ['ngStorage', 'reportIt.config']);
/**
* Crms factory caches data from the CRMS webservice and makes it available as JSON to the app
*/
appServices.factory('Crms',['$http','$q', '$localStorage', 'config', 'cacheTimeOut',
function($http, $q, $localStorage, config, cacheTimeOut){
//Create a promise to return the CRMS objet from the service
var deferred = $q.defer();
//Fetch a copy of the CRMS data from the server
var getCrmsFromServer = function(){
//CRMS webservice credentials (move to configuration?)
var soapEnvelope = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="urn:DefaultNamespace"><soap:Body></soap:Body></soap:Envelope>',
credentials = config.encodedWebserviceCredentials,
//Promise to return
deferred = $q.defer();
//connect to webservice
$http.post(config.webServices.cmrsServiceUrl,
soapEnvelope,
{
headers: {
'Authorization': 'Basic ' + credentials,
'SOAPAction': 'GETALLALLOWABLEREQUESTTYPES',
'Content-Type': "'Content-Type', 'text/xml'"
},
transformResponse: function(data) {
// convert the data to JSON and provide
// it to the success function below
var x2js = new X2JS();
var json = x2js.xml_str2json(data););
return json.Envelope.Body.GETALLOWABLEWEBREQUESTTYPESReturn.REQUESTTYPES;
}
}).success(function(results){
deferred.resolve(results);
}).error(function(err){
deferred.resolve({'error': err});
});
return deferred.promise;
}
//Create a data model object for the CRMS data to return from the service
var createCrmsObject = function(results){
return {
//Return all the primary request types
primaryRequestTypes: function(){
//Variables
var i,o,
tempArray = [],
returnArray = [];
for (i=0, o=results.length; i<o; i+=1){
tempArray.push(results[i].PRIMARYLISTWORD);
}
//Remove duplicates
returnArray = tempArray.filter(function(item, pos, inputArray) {
return inputArray.indexOf(item) == pos;
});
return returnArray;
},
//Return all the secondary request types of a primary type
secondaryRequestTypes: function(type){
//Variables
var i, o, secondaryTypes = [];
//Loop through every result record
for (i=0, o=results.length; i<o; i+=1){
//Identify items that match the specified type
if(results[i].PRIMARYLISTWORD == type){
secondaryTypes.push(results[i].SECONDARYLISTWORD);
}
}
return secondaryTypes;
},
//Return a single request stype
requestType: function(type, subType){
//Variables
var i, o;
//Loop through every record
for(i=0, o = results.length; i<o; i+=1){
//Idenfify records that match the supplied primary and secondary request types
if(results[i].PRIMARYLISTWORD == type && results[i].SECONDARYLISTWORD == subType){
return results[i];
}
}
}
}
};
//Check if a local cache of the CRMS data exists, and check that it hasn't expired
if(!$localStorage.crmsCache || cacheTimeOut($localStorage.crmsCacheExpires, 60000)){
//Load the CRMS data from server
getCrmsFromServer().then(function(data){
//Once the CRMS object promise is resolved, store it to localStorage
$localStorage.crmsCache = JSON.stringify(data);
//Store the time that the cache was updated
$localStorage.crmsCacheExpires = new Date().getTime();
//resolve the promise to return the CRMS object out of the service
deferred.resolve(createCrmsObject(JSON.parse($localStorage.crmsCache)));
});
//If the cache is available and not expired
}else{
//Return the cached CRMS data
deferred.resolve(createCrmsObject(JSON.parse($localStorage.crmsCache)));
}
//Return a promise for the CRMS data
return deferred.promise;
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment