<commit_before>/* coreBOS Webservice API Service */
'use strict';
angular.module('coreBOSAPIservice', [])
  .value('version', 'coreBOS2.1')
  .factory('coreBOSWSAPI', function($http, md5, coreBOSAPIStatus) {

		// Webservice access point
		var _servicebase = 'webservice.php';
		var _serviceurl = _servicebase;

		// Webservice user credentials
		var _serviceuser= false;
		var _servicekey = false;

		// Webservice login validity
		var _servicetoken=false;

		var corebosAPI = {};
		var apiConfigured = false;
		
		corebosAPI.setConfigured = function() {
			apiConfigured = !((_servicekey === false || _servicekey=='' || _servicekey=='PUT YOUR USER ACCESS KEY HERE') && coreBOSAPIStatus.getSessionInfo()=={});
		};
		corebosAPI.setConfigured();
		corebosAPI.isConfigured = function(newkey) {
			return apiConfigured;
		};
		corebosAPI.setcoreBOSUser = function(newkey,dologin) {
			dologin = typeof dologin !== 'undefined' ? dologin : false;
			_serviceuser = newkey;
			if (dologin)
				corebosAPI.doLogin(corebosAPI.getcoreBOSUser(),corebosAPI.getcoreBOSKey()).then(function() {});
		};
		corebosAPI.getcoreBOSUser = function() {
			return _serviceuser;
		};
		corebosAPI.setcoreBOSKey = function(newkey,dologin) {
			dologin = typeof dologin !== 'undefined' ? dologin : false;
			_servicekey = newkey;
			corebosAPI.setConfigured();
			if (dologin)
				corebosAPI.doLogin(corebosAPI.getcoreBOSUser(),corebosAPI.getcoreBOSKey()).then(function() {});
		};
		corebosAPI.getcoreBOSKey = function() {
			return _servicekey;
		};
		
		// Get the URL for sending webservice request.
		function getWebServiceURL(url) {
			if(url.indexOf(_servicebase) == -1) {  // no servicebase so we setup the whole thing
				if(url.charAt(url.length-1) != '/') {
					url = url + '/';
				}
				url = url + _servicebase;
			} // if not we suppose the URL is correct
			return url;
		}
		corebosAPI.setURL = function(url,dologin) {
			dologin = typeof dologin !== 'undefined' ? dologin : false;
			_serviceurl = getWebServiceURL(url);
			coreBOSAPIStatus.setServiceURL(url); // to control invalid access
			if (dologin)
				corebosAPI.doLogin(corebosAPI.getcoreBOSUser(),corebosAPI.getcoreBOSKey()).then(function() {});
		};
		corebosAPI.getURL = function() {
			return _serviceurl;
		};
		
		// Last operation error information
		var _lasterror  = false;
		
		corebosAPI.getlasterror = function() {
			return corebosAPI._lasterror;
		};
		// Check if result has any error.
		corebosAPI.hasError = function(resultdata) {
			if (resultdata != null && resultdata['success'] == false) {
				corebosAPI._lasterror = resultdata['error'];
				return true;
			}
			corebosAPI._lasterror = false;
			return false;
		};

		/**
		 * Perform the challenge
		 * @access private
		 */
		function __doChallenge(username) {
			var getdata = {
				'operation' : 'getchallenge',
				'username'  : username
			};
			return $http({
				method : 'GET',
				url : _serviceurl,
				params: getdata
			});
		};

		function processDoChallenge(res, status) {
			var resobj = res.data;
			if(corebosAPI.hasError(res.data) == false) {
				var result = resobj['result'];
				_servicetoken = result.token;
				coreBOSAPIStatus.setServerTime(result.serverTime);
				coreBOSAPIStatus.setExpireTime(result.expireTime);
			}
		}

		/**
		 * Do Login Operation
		 */
		corebosAPI.doLogin = function(username, accesskey, withpassword) {
			if (username==undefined) username = corebosAPI.getcoreBOSUser();
			if (accesskey==undefined) accesskey = corebosAPI.getcoreBOSKey();
			if (withpassword==undefined) withpassword = false;
			return __doChallenge(username).then(function(res){
				processDoChallenge(res);
				if(_servicetoken == false) {
					return false;  // Failed to get the service token
				}
	
				corebosAPI.setcoreBOSUser(username);
				corebosAPI.setcoreBOSKey(accesskey);

				var postdata = {
					'operation' : 'login',
					'username'  : username,
					'accessKey' : (withpassword ? _servicetoken + accesskey : md5.createHash(_servicetoken + accesskey))
				};
				return $http({
					method : 'POST',
					url : _serviceurl,
					data: postdata
				});
			}); // end then doChallenge
		};

		/**
		 * Do Login Portal Operation
		 */
		corebosAPI.doLoginPortal = function(username, password) {
			var getdata = {
				'operation' : 'loginPortal',
				'username'  : username,
				'password'  : password
			};
			return $http({
				method : 'GET',
				url : _serviceurl,
				params: getdata
			});
		};

		/**
		 * Get actual record id from the response id.
		 */
		corebosAPI.getRecordId = function(id) {
			if (typeof id === 'undefined') return 0;
			var ids = id.split('x');
			return ids[1];
		};

		/**
		 * Do Query Operation.<commit_msg>format/unformat date helper methods to help work with coreBOS dates<commit_after>/* coreBOS Webservice API Service */
'use strict';
angular.module('coreBOSAPIservice', [])
  .value('version', 'coreBOS2.1')
  .factory('coreBOSWSAPI', function($http, md5, coreBOSAPIStatus) {

		// Webservice access point
		var _servicebase = 'webservice.php';
		var _serviceurl = _servicebase;

		// Webservice user credentials
		var _serviceuser= false;
		var _servicekey = false;

		// Webservice login validity
		var _servicetoken=false;

		var corebosAPI = {};
		var apiConfigured = false;
		
		corebosAPI.setConfigured = function() {
			apiConfigured = !((_servicekey === false || _servicekey=='' || _servicekey=='PUT YOUR USER ACCESS KEY HERE') && coreBOSAPIStatus.getSessionInfo()=={});
		};
		corebosAPI.setConfigured();
		corebosAPI.isConfigured = function(newkey) {
			return apiConfigured;
		};
		corebosAPI.setcoreBOSUser = function(newkey,dologin) {
			dologin = typeof dologin !== 'undefined' ? dologin : false;
			_serviceuser = newkey;
			if (dologin)
				corebosAPI.doLogin(corebosAPI.getcoreBOSUser(),corebosAPI.getcoreBOSKey()).then(function() {});
		};
		corebosAPI.getcoreBOSUser = function() {
			return _serviceuser;
		};
		corebosAPI.setcoreBOSKey = function(newkey,dologin) {
			dologin = typeof dologin !== 'undefined' ? dologin : false;
			_servicekey = newkey;
			corebosAPI.setConfigured();
			if (dologin)
				corebosAPI.doLogin(corebosAPI.getcoreBOSUser(),corebosAPI.getcoreBOSKey()).then(function() {});
		};
		corebosAPI.getcoreBOSKey = function() {
			return _servicekey;
		};
		
		// Get the URL for sending webservice request.
		function getWebServiceURL(url) {
			if(url.indexOf(_servicebase) == -1) {  // no servicebase so we setup the whole thing
				if(url.charAt(url.length-1) != '/') {
					url = url + '/';
				}
				url = url + _servicebase;
			} // if not we suppose the URL is correct
			return url;
		}
		corebosAPI.setURL = function(url,dologin) {
			dologin = typeof dologin !== 'undefined' ? dologin : false;
			_serviceurl = getWebServiceURL(url);
			coreBOSAPIStatus.setServiceURL(url); // to control invalid access
			if (dologin)
				corebosAPI.doLogin(corebosAPI.getcoreBOSUser(),corebosAPI.getcoreBOSKey()).then(function() {});
		};
		corebosAPI.getURL = function() {
			return _serviceurl;
		};
		
		// Last operation error information
		var _lasterror  = false;
		
		corebosAPI.getlasterror = function() {
			return corebosAPI._lasterror;
		};
		// Check if result has any error.
		corebosAPI.hasError = function(resultdata) {
			if (resultdata != null && resultdata['success'] == false) {
				corebosAPI._lasterror = resultdata['error'];
				return true;
			}
			corebosAPI._lasterror = false;
			return false;
		};

		/**
		 * Perform the challenge
		 * @access private
		 */
		function __doChallenge(username) {
			var getdata = {
				'operation' : 'getchallenge',
				'username'  : username
			};
			return $http({
				method : 'GET',
				url : _serviceurl,
				params: getdata
			});
		};

		function processDoChallenge(res, status) {
			var resobj = res.data;
			if(corebosAPI.hasError(res.data) == false) {
				var result = resobj['result'];
				_servicetoken = result.token;
				coreBOSAPIStatus.setServerTime(result.serverTime);
				coreBOSAPIStatus.setExpireTime(result.expireTime);
			}
		}

		/**
		 * Do Login Operation
		 */
		corebosAPI.doLogin = function(username, accesskey, withpassword) {
			if (username==undefined) username = corebosAPI.getcoreBOSUser();
			if (accesskey==undefined) accesskey = corebosAPI.getcoreBOSKey();
			if (withpassword==undefined) withpassword = false;
			return __doChallenge(username).then(function(res){
				processDoChallenge(res);
				if(_servicetoken == false) {
					return false;  // Failed to get the service token
				}
	
				corebosAPI.setcoreBOSUser(username);
				corebosAPI.setcoreBOSKey(accesskey);

				var postdata = {
					'operation' : 'login',
					'username'  : username,
					'accessKey' : (withpassword ? _servicetoken + accesskey : md5.createHash(_servicetoken + accesskey))
				};
				return $http({
					method : 'POST',
					url : _serviceurl,
					data: postdata
				});
			}); // end then doChallenge
		};

		/**
		 * Do Login Portal Operation
		 */
		corebosAPI.doLoginPortal = function(username, password) {
			var getdata = {
				'operation' : 'loginPortal',
				'username'  : username,
				'password'  : password
			};
			return $http({
				method : 'GET',
				url : _serviceurl,
				params: getdata
			});
		};

		/**
		 * Get actual record id from the response id.
		 */
		corebosAPI.getRecordId = function(id) {
			if (typeof id === 'undefined') return 0;
			var ids = id.split('x');
			return ids[1];
		};

		/**
		 * Helper method to work with dates
		 * Given a day, month and year it will return a date string formatted in the current user's format
		 * NOTE: this method requires that the session variable in coreBOSAPIStatus have a dateformat property
		 *  according to the connected user. Due to the way the REST API works this must be set outside this service
		 */
		corebosAPI.coreBOSformatDate = function(fday,fmonth,fyear) {
			switch (coreBOSAPIStatus.getSessionInfo().dateformat) {
				case 'dd-mm-yyyy':
					fdate = fday + '-' + fmonth + '-' + fyear;
					break;
				case 'mm-dd-yyyy':
					fdate = fmonth + '-' + fday + '-' + fyear;
					break;
				default:
					fdate = fyear + '-' + fmonth + '-' + fday;
			}
			return fdate;
		};

		/**
		 * Helper method to work with dates
		 * Given a date that came from coreBOS in the current user's format
		 * It returns the three components of the date in an object
		 *  {year: yyyy, month:mm, day: dd}
		 * NOTE: this method requires that the session variable in coreBOSAPIStatus have a dateformat property
		 *  according to the connected user. Due to the way the REST API works this must be set outside this service
		 */
		corebosAPI.coreBOSunformatDate = function(fdate) {
			var dr = fdate.split('-');
			switch (coreBOSAPIStatus.getSessionInfo().dateformat) {
				case 'dd-mm-yyyy':
					rdate = {day: dr[0], month: dr[1], year: dr[2]};
					break;
				case 'mm-dd-yyyy':
					rdate = {day: dr[1], month: dr[0], year: dr[2]};
					break;
				default:
					rdate = {day: dr[2], month: dr[1], year: dr[0]};;
			}
			return rdate;
		};

		/**
		 * Do Query Operation.
{'fraction_non_alphanumeric': 0.13006146059291396, 'fraction_numerical': 0.002259580621836587, 'mean_word_length': 3.3700631911532386, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '52144', 'n_tokens_mistral': 4431, 'n_tokens_neox': 3604, 'n_words': 844}