/**
*
* Copyright (C) 2021 Akorbi Digital RMP
*
* This file is part of RunMyProcess SDK-JS.
*
* RunMyProcess SDK-JS is free software: you can redistribute it and/or modify
* it under the terms of the Apache License Version 2.0 (the "License");
*
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Global user login data variable
var context = new Context;
context.load_status = 'NL';
context.version = '1.0.0';
/**
Constructor containing all the login user information.
@constructor
@property {Array} link - Array of links.
*/
function Context() {
this.link = [];
this.constructors = [];//may be used to limit filters
};
/**
Initializes the context variable with the data from the login user
@method
@param {object} options - options to be used during the call<br/>
@param {Resource~onSuccess} options.onSuccess - a callback function called in case of a success
@param {Resource~onFailure} [options.onFailure] - a callback function called in case of a failure
@param {String} [options.baseUrl] - base URL. If not set the current base URL will be used
@see Resource#load
*/
Context.prototype.init = function (options) {
var father = this;
context.load_status = 'L';
var urlBase = options.baseUrl || RMPApplication.getBaseUrl();
var resource = new Resource();
resource.url=urlBase + 'login';
var rOptions = {};
rOptions.onSuccess = function (rdata) {
//aphend values to the context object
context.object = rdata;
var my_categories = resource.getArray(rdata, "category");
for (var i = 0; i < my_categories.length; i++) {
switch (my_categories[i].term) {
case 'profile':
context.profile = my_categories[i].label;
break;
case 'language':
context.language = my_categories[i].label;
break;
case 'status':
context.status = my_categories[i].label;
break;
case 'customer_id':
context.customer_id = my_categories[i].label;
break;
case 'receive_alert':
context.receive_alert = my_categories[i].label;
break;
case 'billing_status':
context.billing_status = my_categories[i].label;
break;
case 'bo_feature':
context.bo_feature = my_categories[i].label;
break;
}
}
context.link.customerConfig = 'config/' + context.customer_id + '/';
context.link.useraccess = context.link.customerConfig+'useraccess';
var entries = resource.getArray(rdata, 'entry');
var lnks=resource.entryLinks(entries);
context.link.project = resource.linkSearch('project', lnks) ;
context.link.lane = resource.linkSearch('lanes', lnks) ;
context.link.user = resource.linkSearch('user', lnks) ;
context.link.process_host = resource.linkSearch('process_host', lnks) ;
context.link.host = resource.linkSearch('host', lnks) ;
context.link.process = resource.linkSearch('process', lnks) ;
context.link.customers = resource.linkSearch('brand_customers', lnks) || resource.linkSearch('all_customers', lnks);
context.link.traffics = resource.linkSearch('traffic', lnks) ;
context.link.subscriptions = resource.linkSearch('subscription', lnks) ;
context.link.customLists = resource.linkSearch('custom_lists', lnks) ;
context.link.appInstanceReport = resource.linkSearch('app_instances_report', lnks) ;
context.link.appInstances = resource.linkSearch('app_instances', lnks) ;
var my_links = resource.getArray(rdata, "link");
for (var i = 0; i < my_links.length; i++) {
switch (my_links[i].rel) {
case 'self':
context.link.self = my_links[i].href;
break;
case 'customer':
context.link.customer = my_links[i].href;
break;
case 'logout':
context.link.logout = my_links[i].href;
break;
case 'portal':
context.link.portal = my_links[i].href;
break;
case 'ggadget_template':
context.link.ggadget_template = my_links[i].href;
break;
case 'ping':
context.link.ping = my_links[i].href;
break;
case 'domain':
context.link.domain = my_links[i].href;
break;
case 'user_lane':
context.link.user_lane = my_links[i].href;
break;
case 'script':
context.link.script = my_links[i].href;
break;
case 'oauth_access':
context.link.oauth_access = my_links[i].href;
break;
case 'suport_auth':
context.link.suport_auth = my_links[i].href;
break;
case 'reset_password':
context.link.reset_password = my_links[i].href;
break;
case 'metadata':
context.link.metadata = my_links[i].href;
break;
case 'preferences':
context.link.preferences = my_links[i].href;
break;
}
}
context.load_status = 'C'; //changes the status to Compleated
if (options.OnSuccess !== undefined) {
options.OnSuccess(rdata); //callback
}
};
rOptions.onFailure = function (e) {
context.load_status = 'NL';
options.eObject=e;
resource.errorManager(options);
};
rOptions.url=resource.url;
resource.dataLoader(rOptions);
};
/**
Generates the filter string to be posted to the server
@method
@param {Array} fArray - List of filters to me generated with the following parameters<br/>
@param {String} [fArray[index].constructor] - The constructor for which the filter is required
@param {String} [fArray[index].filter] - the filter to be used
@param {String} [fArray[index].operator] - the operator of the filter
@param {String} [fArray[index].value] - the value of the filter
*/
Context.prototype.generateFilters= function (fArray){
var father = this;
var filterStr='';
var operatorStr='';
var valueStr='';
for(var i=0;i<fArray.length;i++){
if (!fArray[i].context){//CHECK FOR FILTER PERMS
var cIndex = -1;
for(var j=0;i<father.constructors.length;j++){
if(father.constructors[j].constructor===fArray[i].context){
cIndex=j;
break;
}
};
if (cIndex !== -1) {
var consFArray = father.constructors[cIndex].filters;
if (jQuery.inArray(fArray[i].filter, consFArray) !== -1){
if (filterStr===''){//if its the first filter
filterStr=fArray[i].filter;
operatorStr=fArray[i].operator;
valueStr=fArray[i].value;
}else{
filterStr=filterStr+'+'+fArray[i].filter;
operatorStr=operatorStr+'+'+fArray[i].operator;
valueStr=valueStr+'+'+fArray[i].value;
}
};
};//END CHECKER
}else{
if (filterStr===''){//if its the first filter
filterStr=fArray[i].filter;
operatorStr=fArray[i].operator;
valueStr=fArray[i].value;
}else{
filterStr=filterStr+'+'+fArray[i].filter;
operatorStr=operatorStr+'+'+fArray[i].operator;
valueStr=valueStr+'+'+fArray[i].value;
}
}
};
if (filterStr==='') return '';
return 'filter='+filterStr+'&operator='+operatorStr+'&value='+valueStr;
};