/**
*
* 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.
*/
/**
Create a new instance of Customer
@constructor
@property {String} status - status of the customer loaded
@property {String} title - customer's title
@property {String} country - customer's country
@property {String} referer - customer's referer
@property {Date} published - published date
@property {Date} renewalDate - date of renewal
@property {Number} userCount - number of users
@property {Number} userCredit - number of credit users
@property {Array} subscriptions - list of subscriptions asociated to this customer
@property {Array} traffics - list of traffics asociated to this customer
@property {Object} i18n - internationalization obect containing different libraries
@property {Array} users - list of users asociated to this customer
@property {Array} domains - list of domains asociated to this customer
@property {Object} overdue - overdue users status and quantity
@property {Object} inOrder - inOrder users status and quantity
@see CustomerList
@see Resource
@example Load customer
var c = new Customer();
c.load({
onSuccess:function(){
alert('"'+c.title + '" loaded!');
}
});
@example Load customer User information
var c = new Customer();
c.load({
onSuccess:function(){
alert(c.inOrder.ACTIVE);
}
});
*/
function Customer(){
this.status;
this.title;
this.country;
this.referer;
this.published = new Date();
this.renewalDate = new Date();
this.userCount;
this.userCredit;
this.subscriptions = [];
this.traffics = [];
this.i18n;
this.users = [];
this.domains = [];
this.overdue={};
this.inOrder={};
};
/**
Inherits Resource
@borrows Resource.js
*/
Customer.prototype = new Resource();
/**
Overrides Resource's loadPreset method.
@method
@see Resource#loadPreset
*/
Customer.prototype.loadPreset = function () {
/**
Overrides Resource's generateUrl method to return the request url
@method
@see Resource#generateUrl
*/
this.generateUrl=function () {
return this.selfUrl||context.link.customer;
};
};
/**
Overrides Resource's loadSet method to set local variables after request.
@method
@param {json} rObject - JSON representation of the loaded data.
@see Resource#loadSet
*/
Customer.prototype.loadSet = function (rObject) {
try{
var entryCats = this.entries[0].category;
this.status = this.termSearch('status',entryCats).label;
this.title = this.object.title;
this.country = this.termSearch('country',entryCats).label;
this.referer = this.termSearch('referer',entryCats).label;
this.renewalDate = new Date(this.termSearch('renewal_date',entryCats).label);
this.userCount = Number(this.termSearch('user_count',entryCats).label);
this.userCredit = Number(this.termSearch('users_credit',entryCats).label);
this.inOrder = {};
this.overdue = {};
for (var i=0 ; i<entryCats.length; i++){
if(entryCats[i].scheme){
if(entryCats[i].scheme=="IN_ORDER")this.inOrder[entryCats[i].term]=entryCats[i].label;
if(entryCats[i].scheme=="OVERDUE")this.overdue[entryCats[i].term]=entryCats[i].label;
}
}
this.published = new Date(this.entries[0].published);
var i18nObj = new I18n();
i18nObj.selfUrl = this.linkSearch('i18n',this.entries[0].link);
this.i18n = i18nObj;
var ul = new UserList();
ul.selfUrl = this.linkSearch('users',this.entries[0].link);
this.userList = ul;
var dl = new CustomerDomainList();
dl.selfUrl = this.linkSearch('domains',this.entries[0].link);
this.domainList = dl;
}catch (e) {
alert(e);
};
};
/**
Overrides Resource's loadPreset method.
@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#update
@example update a Customer
function updateCustomer(cust){//loaded customer object
cust.title = 'new_title';//data change example
cust.update({
onSuccess:function(){
alert("Customer Updated!");
}
});
}
*/
Customer.prototype.update = function (options) {
var father = this;
father.updatePreset = function () {
father.generateUrl = function () {
return father.selfUrl;
};
father.xml = father.generate_xml(options).trim();
};
father.resourceUpdate(options);
};
/**
Generates a save/update xml to be posted to the server
@method
*/
Customer.prototype.generate_xml = function (options) {
var v_baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
var v_title = this.title;
var v_ctylbl = this.termSearch('country',this.entries[0].category).scheme || '';
var v_ctysch = this.termSearch('country',this.entries[0].category).scheme || '';
var v_status = this.termSearch('status',this.entries[0].category).label || '';
var v_ref = this.termSearch('referer',this.entries[0].category).label || '';
var xml = '<?xml version="1.0" encoding="UTF-8"?> '
+ '<feed xml:base="' + v_baseUrl
+ '" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="http://www.w3.org/2005/Atom"> '
+ '<title>'+v_title+'</title>'
+ '<rights>(c) RunMyProcess</rights>'
+ '<entry>'
+ '<title>'+v_title+'</title>'
+ '<category term="country" label="'+v_ctylbl+'" scheme="'+v_ctysch+'"/>'
+ '<category term="status" label="'+v_status+'"/>'
+ '<category term="referer" label="'+v_ref+'"/>'
+ '</entry>'
+ '</feed>';
return xml;
};
/**
Load a list the list of subscriptions.
@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
@example Load subscriptions
function loadSubscriptions(p_customer){//customer object
p_customer.loadSubscriptions({
onSuccess:function(){
alert("there are " +p_customer.subscriptions.length+" subscriptions");
}
});
};
*/
Customer.prototype.loadSubscriptions = function (options) {
var father = this;
var subs=new CustomerSubscriptionList();
if (context.link.customer === father.selfUrl){
subs.selfUrl = context.link.subscriptions;
}else{
subs.selfUrl = father.linkSearch('subscription', father.entries[0].links);
}
if (!subs.selfUrl){
options.eObject={};
options.eObject.message='The requested load is not available';
father.errorManager(options);
return;
}
var opt = {};
opt.onSuccess = function(){
father.subscriptions = subs.subscriptions;
options.onSuccess();
};
opt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
opt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
subs.load(opt);
};
/**
Load a list the list of traffics.
@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
@example Load traffics
function loadTraffics(p_customer){//customer object
p_customer.loadTraffics({
onSuccess:function(){
alert("there are " +p_customer.traffics.length+" traffics");
}
});
};
*/
Customer.prototype.loadTraffics = function (options) {
var father = this;
var trfks=new CustomerTrafficList();
if (context.link.customer === father.selfUrl){
trfks.selfUrl = context.link.traffics;
}else{
trfks.selfUrl = father.linkSearch('traffic', father.entries[0].links);
}
if (!trfks.selfUrl){
options.eObject={};
options.eObject.message='The requested load is not available';
father.errorManager(options);
return;
}
var opt = {};
opt.onSuccess = function(){
father.traffics = trfks.traffics;
options.onSuccess();
};
opt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
opt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
trfks.load(opt);
};
/**
Load a list the list of Users.
@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
@example Load users
function loadUsers(p_customer){//customer object
p_customer.loadUsers({
onSuccess:function(){
alert("there are " +p_customer.users.length+" users");
}
});
};
*/
Customer.prototype.loadUsers = function (options) {
var father = this;
var usrl=new UserList();
if (context.link.customer === father.selfUrl){
usrl.selfUrl = context.link.user;
}else{
usrl.selfUrl = father.linkSearch('user', father.entries[0].links);
}
if (!usrl.selfUrl){
options.eObject={};
options.eObject.message='The requested load is not available';
father.errorManager(options);
return;
}
var opt = {};
opt.onSuccess = function(){
father.users = usrl.users;
options.onSuccess();
};
opt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
opt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
usrl.load(opt);
};
/**
Load a list the list of Domains.
@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
@example Load domains
function loadDomains(p_customer){//customer object
p_customer.loadDomains({
onSuccess:function(){
alert("there are " +p_customer.domains.length+" domains");
}
});
};
*/
Customer.prototype.loadDomains = function (options) {
var father = this;
var doml=new CustomerDomainList();
if (context.link.customer === father.selfUrl){
doml.selfUrl = context.link.domain;
}else{
doml.selfUrl = father.linkSearch('domain', father.entries[0].links);
}
if (!doml.selfUrl){
options.eObject={};
options.eObject.message='The requested load is not available';
father.errorManager(options);
return;
}
var opt = {};
opt.onSuccess = function(){
father.domains = doml.domains;
options.onSuccess();
};
opt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
opt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
doml.load(opt);
};
Customer.prototype.findSchemeElements = function (needle, haystack) {
var elements = [];
if (!haystack.length){
if (haystack.scheme == needle)return elements.haystack;
} else{
for (var i = 0; i < haystack.length; i++) {
if (haystack[i].scheme == needle) {
return haystack[i];
}
}
return elements;
}
return [];
};