/**
*
* 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 Lanes
@constructor
@property {User} author - Contains the author's information.
@property {String} title - the title of the lane
@property {Object} summary - the summary of the lane
@property {String} isEverybody - term that signifies if the lane is set to alow everybody
@property {String} isDynamic - term that signifies if the lane is set to dynamic
@property {String} type - Type of the lane
@property {Object} parent - the parent pool of the lane
@property {Array} users - All the users associated to the lane
@property {String} usersUrl - The URL of the users list
@property {String} parentUrl - The URL of the parent
@property {String} script - script of the lane
@property {Array} acceptanceUsers - All the acceptance users associated to the lane
@property {Boolean} isRemove - flag to set a lane as lane to be removed.
@property {Boolean} isLoadedUserLane - Flag that represents if the la was loaded as a userLane
@see UserLaneList
@see Resource
@example Load a lane
function loadUserLanes(user){//User object
user.loadLanes({
onSuccess:function(){
var lane = user.lanes[0];
}
});
};
@example Load all users
function loadAllLaneUsers(lane){//Lane object
var optL = {};
optL.onSuccess = function(){
alert("Users Loaded! There are "+lane.allUsers.users.length+" users in the list");
};
lane.loadAllUsers(optL);
};
*/
function Lane(){
this.author = new User();
this.title;
this.summary={};
this.isEverybody;
this.isDynamic;
this.type;
this.parent = {};
this.users = [];
this.usersUrl;
this.parentUrl;
this.poolUrl;//Deprecated
this.pool = new Pool();
this.script;
this.acceptanceUsers = [];
this.isRemove = false;
this.isLoadedUserLane = false;
};
/**
@borrows
Inherits Resource
*/
Lane.prototype = new Resource();
/**
Overrides Resource's loadPreset method.
@method
@see Resource#loadPreset
*/
Lane.prototype.loadPreset = function () {
/**
Overrides Resource's generateUrl method to return the request url
@method
@see Resource#generateUrl
*/
this.generateUrl = function () {
return this.selfUrl;
};
};
/**
Overrides Resource's loadSet method to set local variables after request.
@method
@param {json} rObject - JSON representation of the loaded data.
@see Resource#loadSet
*/
Lane.prototype.loadSet = function (rObject) {
var father = this;
father.author.name = rObject.author.name;
father.author.selfUrl = rObject.author.uri;
father.title = rObject.title;
father.type = father.termSearch('context',rObject.entry.category).label;
if (rObject.entry.summary){
father.summary.data = rObject.entry.summary.P_value;
father.summary.type = rObject.entry.summary.type;
}
if (father.entries[0].content){
father.script = father.entries[0].content.P_value;
}
father.isEverybody = father.termSearch('allow_everybody',rObject.entry.category).label;
father.isDynamic = father.termSearch('allow_everybody',rObject.entry.category).label;//SOMETHING WRONG HERE!!!
var ppool = new Pool();
var poolLinkObject = father.linkObjectSearch('pool',rObject.entry.link);
ppool.selfUrl = poolLinkObject.href;
ppool.title = poolLinkObject.title;
father.pool = ppool;
father.parentUrl = father.linkSearch('parent',rObject.entry.link);
if (father.parentUrl){
father.parent = new Lane();
father.parent.selfUrl = father.parentUrl;
}
};
/**
* Load all users.
@method
@param {object} options - options to be used during the call<br/>
@param {Array} options.filters - array of filters
@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
@param {Object} [options.pagination] -an Object with pagination variables (nb and first)
@param {Array} [options.filters] - an array of filters
@example Load Users
function loadUsers(user){//User object
user.loadLanes({
onSuccess:function(){
var lane = user.lanes[0];
lane.load({
onSuccess:function(){
lane.loadUsers({
onSuccess:function(){
alert(lane.users.length);
}
});
}
});
}
});
};
*/
Lane.prototype.loadUsers = function (options) {
var alu = new AllLaneUserList();
var father = this;
/**
Overrides Resource's loadPreset method.
@method
@see Resource#loadPreset
*/
alu.loadPreset = function () {
/**
Overrides Resource's generateUrl method to return the request url
@method
@see Resource#generateUrl
*/
alu.generateUrl = function () {
return alu.selfUrl;
};
};
if (!father.usersUrl){
alu.selfUrl=father.linkSearch('all_users', father.links);
}else {
alu.selfUrl=father.usersUrl+'/'; // filter addition removes last slash in loadUsers the slash is necessary
}
var lOpt = jQuery.extend(true, {}, options);
lOpt.onSuccess = function(){
father.users = alu.users;
options.onSuccess();
};
lOpt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
lOpt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
alu.load(lOpt);
};
/**
* Load all Acceptance users.
@method
@param {object} options - options to be used during the call<br/>
@param {Array} options.filters - array of filters
@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
@param {Object} [options.pagination] -an Object with pagination variables (nb and first)
@param {Array} [options.filters] - an array of filters
@example Load Acceptance Users
function loadAcceptanceUsers(user){//User object
user.loadLanes({
onSuccess:function(){
var lane = user.lanes[0];
lane.load({
onSuccess:function(){
lane.loadAcceptanceUsers({
onSuccess:function(){
alert(lane.acceptanceUsers.length);
}
});
}
});
}
});
};
*/
Lane.prototype.loadAcceptanceUsers = function (options) {
var alu = new AllLaneUserList();
alu.filters = options.filters;
alu.pagination = options.pagination;
var father = this;
/**
Overrides Resource's loadPreset method.
@method
@see Resource#loadPreset
*/
alu.loadPreset = function () {
/**
Overrides Resource's generateUrl method to return the request url
@method
@see Resource#generateUrl
*/
alu.generateUrl = function () {
return alu.selfUrl;
};
};
alu.selfUrl=father.linkSearch('all_acceptance_users', father.links);
var lOpt = {};
lOpt.onSuccess = function(){
father.acceptanceUsers = alu.users;
options.onSuccess();
};
lOpt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
lOpt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
alu.filters=options.filters || [];
alu.load(lOpt);
};
/**
* Add a User to a lane.
@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 {User} options.user - the user to add
@param {Array} [options.filters] - an array of filters
*/
Lane.prototype.addUser = function (options) {
var alu = new AllLaneUserList();
var father = this;
/**
Overrides Resource's savePreset method.
@method
@see Resource#savePreset
*/
alu.savePreset = function () {
/**
Overrides Resource's generateUrl method to return the request url
@method
@see Resource#generateUrl
*/
alu.generateUrl = function () {
//var v_mode = options.mode||RMPApplication.get("P_mode");
alu.selfUrl = father.selfUrl+'/user/';//?P_mode='+v_mode+'&P_version='+RMPApplication.get("P_version");//HARDCODED
return alu.selfUrl;
};
};
var opt = {};
opt.onSuccess = function(rData){
options.onSuccess(rData);
};
opt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
opt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
opt.update = false;
alu.xml = father.generateAddUser_xml(options).trim();
opt.accept = 'text/plain; charset=utf-8';
opt.mode = options.mode;
alu.resourceSave(opt);
};
/**
generate xml to add a user
@method
@param {String} [options.baseUrl] - base URL. If not set the current base URL will be used
@param {String} options.user - the user to be added
@see Resource#resourceSave
*/
Lane.prototype.generateAddUser_xml = function (options) {
var father = this;
var v_baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
var v_user = options.user;
var xml =''
+' <?xml version="1.0" encoding="UTF-8"?>'
+' <feed xmlns="http://www.w3.org/2005/Atom" xml:base="https://live.runmyprocess.com">'
+' <title>'+v_user.title+'</title>'
+' <rights>(c) RunMyProcess</rights>'
+' <entry>'
+' <title>'+v_user.title+'</title>'
+' <author>'
+' <name>'+v_user.name+'</name>'
+' <email>'+v_user.email+'</email>'
+' </author>'
+' <id>'+v_user.id+'</id>'
+' </entry>'
+' </feed>'
return xml;
};
/**
* delete User From lane
@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 {User} options.user - the user to delete
@param {Array} [options.filters] - an array of filters
*/
Lane.prototype.removeUser = function (options) {
var alu = new AllLaneUserList();
var father = this;
/**
Overrides Resource's deletePreset method.
@method
@see Resource#deletePreset
*/
alu.deletePreset = function () {
/**
Overrides Resource's generateUrl method to return the request url
@method
@see Resource#generateUrl
*/
alu.generateUrl = function () {
var v_mode = options.mode||RMPApplication.get("P_mode");
alu.selfUrl = father.selfUrl+'/user/'+options.user.id+'/;'//?P_mode='+v_mode;//HARDCODED
return alu.selfUrl;
};
};
alu.loadSet = function (rObject) {
};
alu.remove(options);
};
/**
Create a new Lane.
@method
@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#resourceSave
@example Create a lane
function CreateLane(opt){// object
var lane = new Lane();
lane.title = "Lane name";
lane.poolUrl = opt.poolUrl; //Url of the pool
lane.script = opt.script; //Script of the lane
lane.create({
onSuccess:function(){
alert("Lane " + lane.id + " created.");
}
});
};
*/
Lane.prototype.create = function (options) {
var father = this;
var pool = new Pool();
pool.selfUrl = father.poolUrl;
/**
Overrides Resource's savePreset method.
@method
@see Resource#savePreset
*/
pool.savePreset = function () {
/**
Overrides Resource's generateUrl method to return the request url
@method
@see Resource#generateUrl
*/
pool.generateUrl = function () {
return pool.selfUrl;
};
};
var pOpt = {};
pOpt.onSuccess = function(rData){
father.object = rData;
father.id = father.object.id;
father.entries = father.getArray(father.object, "entry");
father.categories = father.getArray(father.object, "category");
father.links = father.getArray(father.object, "link");
father.selfUrl = father.linkSearch('self', father.links);
father.rights = father.object.rights;
father.loadSet(father.object);
options.onSuccess(father.object);
};
pOpt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
pOpt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
pOpt.update = false;
pool.xml = father.generate_xml(pOpt).trim();
pool.save(pOpt);
};
/**
Update a Lane.
@method
@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#resourceSave
*/
Lane.prototype.update = function (options) {
var father = this;
/**
Overrides Resource's savePreset method.
@method
@see Resource#savePreset
*/
father.updatePreset = function () {
/**
Overrides Resource's generateUrl method to return the request url
@method
@see Resource#generateUrl
*/
father.generateUrl = function () {
return father.selfUrl;
};
};
var lOpt = {};
lOpt.onSuccess = function(rData){
options.onSuccess(rData);
};
lOpt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
lOpt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
lOpt.update = true;
father.xml = father.generate_xml(lOpt).trim();
father.resourceUpdate(lOpt);
};
/**
generate xml to create/update
@method
@param {String} [options.baseUrl] - base URL. If not set the current base URL will be used
@see Resource#resourceSave
*/
Lane.prototype.generate_xml = function (options) {
var father = this;
var v_baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
var v_title = father.title;
var v_rights = '(c) RunMyProcess';
var v_summaryType = father.summary.type || 'xhtml';
var v_summary = father.summary.data || '';
var v_parent = '';
if(father.parentUrl) {
v_parent = '<link rel="parent" href="'+father.parentUrl+'"/> ';
}
var v_pool = '';
if(father.pool.selfUrl && !options.update) {
v_pool = '<link rel="pool" href="'+father.pool.selfUrl+'"/> ';
}
var v_type = '';
if (father.type){
v_type = '<category term="context" label="'+father.type+'"/>';
}
var v_isEverybody = father.isEverybody;
var v_users = '';
for (var i = 0; i < father.users.length; i++) {
var v_userType='';
if(father.users[i].laneUserType) v_userType ='type="'+father.users[i].laneUserType+'"';
v_users = v_users+'<link rel="user" '+v_userType+' href="'+father.users[i].selfUrl+'"/>';
}
for (var i = 0; i < father.acceptanceUsers.length; i++) {
var v_userType='';
if(father.acceptanceUsers[i].laneUserType) v_userType ='type="'+father.acceptanceUsers[i].laneUserType+'"';
v_users = v_users+'<link rel="user_acceptance" '+v_userType+' href="'+father.acceptanceUsers[i].selfUrl+'"/>';
}
var v_content = '';
if(father.script){
v_content = v_content+'<content type="script">'+father.script+'</content>';
}
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>'+v_rights+'</rights> '
+ '<entry> '
+ v_content
+ '<title>' + v_title + '</title> '
+ '<summary type="'+v_summaryType+'">'+v_summary+' </summary>'
+ v_parent
+ v_pool
+ v_type
+ '<category term="allow_everybody" label="'+v_isEverybody+'"/>'
+ v_users
+ '</entry> '
+ '</feed>';
return xml;
};