/**
*
* 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 Web Interface
@constructor
@property {I18n} i18n - the web interface's internationalization.
@property {String} summary - the web interface's summary.
@property {String} summaryType - The type of summary string.
@property {User} author - the web interface's user.
@property {String} title - the title of the web interface.
@property {String} rights - the rights of the web interface.
@property {Array} categories - the categories of the web interface.
@property {Array} links - the links of the web interface.
@property {Project} project - the project associated to the web interface.
@property {String} content - the content of the web interface.
@property {String} contentType - the content type of the content.
@property {Process} targetProcess - the target process associated to the web interface.
@property {Array} css - a list of style sheets.
@property {String} stylesheet_web - the web style sheet of the web interface.
@property {String} headerTitle - the header title of the web interface.
@property {String} portalAccept - the acceptance of the web interface.
@property {Array} users - the users associated.
@property {String} type - the type of the web interface.
@property {Array} tags - a list of tags associated to the web interface.
@property {Date} updated - the date updated.
@property {Date} published - the date published.
@see WebInterfaceList
@see Resource
*/
function WebInterface(){
this.i18n = new I18n();
this.summary;
this.summaryType;//CHANGE TO SUMMARY OBJECT?
this.author = new User();
this.title;
this.rights;
this.categories = [];
this.links = [];
this.project = new Project();
this.content;
this.contentType;
this.targetProcess = new Process();
this.css=[];
this.stylesheet_web='JHtjYWNoZV91cmxfYmFzZX1jb20ucnVubXlwcm9jZXNzLkFwcGxpY2F0aW9uUnVubmVyL2Nzcy93ZWJfYm9yZGVyLmNzcw==';
this.headerTitle='';
this.portalAccept='"web"';
this.dynamicName = '';
this.users = [];
this.type;
this.tags =[];
this.updated = new Date();
this.published = new Date();
};
/**
@borrows
Inherits Resource
*/
WebInterface.prototype = new Resource();
/**
Overrides Resource's loadPreset method.
@method
@see Resource#loadPreset
*/
WebInterface.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
*/
WebInterface.prototype.loadSet = function (rObject) {
var father = this;
father.summary = rObject.entry.summary.P_value;
father.summaryType = rObject.entry.summary.type;
father.author = new User();
father.author.name = rObject.author.name;
father.author.selfUrl = rObject.author.uri;
father.title=rObject.title;
father.categories = father.getArray(rObject.entry, "category");
var headerTitle = father.termSearch("title",father.categories);
if (headerTitle)father.headerTitle=headerTitle.label;
father.css = [];
father.updated = new Date(rObject.updated);
father.published = new Date(rObject.published);
for (var i = 0 ; i<father.categories.length;i++) {
if (father.categories[i].term){
switch (father.categories[i].term) {
case 'css':
father.css.push(father.categories[i].label);
break;
case 'tag':
father.tags.push(father.categories[i].label);
break;
case 'dynamic_name':
father.dynamicName = father.categories[i].label;
break;
case 'portal_accept':
father.portalAccept = new Array(father.categories[i].label).join();
break;
}
}
/*
if(father.categories[i].term=="css"){
father.css.push(father.categories[i].label);
}else if(father.categories[i].term=="tag"){
father.tags.push(father.categories[i].label);
}
*/
}
father.type = father.termSearch('type',father.categories).label;
father.links = father.links.concat(this.getArray(rObject.entry, "link"));
father.users = [];
for (var i = 0 ; i<father.links.length;i++) {
if(father.links[i].rel=="user"){
var user = new User();
user.selfUrl = father.links[i].href;
father.users.push(user);
}
}
father.project = new Project();
var project = father.linkObjectSearch('project',father.links );
father.project.selfUrl = project.href;
father.project.id = project.type;//PROBABLE ERROR!?
father.project.title = project.title;
if(father.linkObjectSearch('target_process',father.links )){
var process = father.linkObjectSearch('target_process',father.links );
father.targetProcess.selfUrl = process.href;
father.targetProcess.title = process.title;
}
father.content = rObject.entry.content.P_value;
father.contentType = rObject.entry.content.type;
};
/**
Create a new WebInterface.
@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
*/
WebInterface.prototype.create = function (options) {
var father = this;
var webInterfaceList = new WebInterfaceList();
/**
Overrides Resource's savePreset method.
@method
@see Resource#savePreset
*/
webInterfaceList.savePreset = function () {
/**
Overrides Resource's generateUrl method to return the request url
@method
@see Resource#generateUrl
*/
webInterfaceList.generateUrl = function () {
webInterfaceList.selfUrl = context.link.customerConfig + "appli/";
return webInterfaceList.selfUrl;
};
};
var wiOpt = {};
wiOpt.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);
};
wiOpt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
wiOpt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
wiOpt.update = false;
var defCat = father.getDefaultCategories();
var defExists = false;
for (var i = 0 ; i<defCat.length;i++) {
defExists = false;
for (var j = 0 ; j<father.categories.length;j++) {
if (defCat[i].term===father.categories[j].term){
defExists = true;
break;
}
}
if (!defExists){
father.categories.push(defCat[i]);
}
}
var defLks = father.getDefaultLinks();
defExists = false;
for (var i = 0 ; i<defLks.length;i++) {
defExists = false;
for (var j = 0 ; j<father.links.length;j++) {
if (defLks[i].term===father.links[j].term){
defExists = true;
break;
}
}
if (!defExists){
father.links.push(defLks[i]);
}
}
webInterfaceList.xml = father.generate_xml(wiOpt).trim();
webInterfaceList.save(wiOpt);
};
/**
Save a new WebInterface.
@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
*/
WebInterface.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 wiOpt = {};
wiOpt.onSuccess = function(rData){
if(rData.feed){
father.object = rData.feed;
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);
}else{
father.object = rData;
}
options.onSuccess(father.object);
};
wiOpt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
wiOpt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
wiOpt.update = true;
var defCat = father.getDefaultCategories();
var defExists = false;
for (var i = 0 ; i<defCat.length;i++) {
defExists = false;
for (var j = 0 ; j<father.categories.length;j++) {
if (defCat[i].term===father.categories[j].term && ['dynamic_name','portal_accept'].indexOf(father.categories.term)!=-1){
defExists = true;
break;
}
}
if (!defExists){
father.categories.push(defCat[i]);
}
}
father.xml = father.generate_xml(wiOpt).trim();
father.resourceUpdate(wiOpt);
};
/**
generate xml to create/update
@method
@param {object} options - options to be used during the call<br/>
@param {Boolean} options.update - Is this an xml for update
@param {String} [options.baseUrl] - base URL. If not set the current base URL will be used
@see Resource#resourceSave
*/
WebInterface.prototype.generate_xml = function (options) {
var father=this;
var v_baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
var v_title = father.encodeHTML(father.title);
var v_rights = father.rights || '(c) RunMyProcess';
var v_addCat = '';
if (father.categories !== undefined) {
for (var i = 0 ; i<father.categories.length;i++) {
var obj = father.categories[i];
if (obj.term!="css" && obj.term!="title" && obj.term!="tag"){//dont add css categories which will be added later
v_addCat = v_addCat + '<category ';
for (var prop in obj) {
v_addCat = v_addCat + prop +'="'+father.encodeHTML(obj[prop])+'" ';
}
v_addCat = v_addCat + '/>';
}
}
}
if (father.css !== undefined) {
for (var i = 0 ; i<father.css.length;i++) {
v_addCat = v_addCat + '<category term="css" label="'+father.css[i]+'" scheme=""/>'
}
}
if (father.css !== undefined) {
v_addCat = v_addCat + '<category term="title" label="'+father.encodeHTML(father.headerTitle)+'"/>'
}
var v_links = '';
var defaultLinks = father.getDefaultLinks();
for (var i = 0 ; i<defaultLinks.length;i++) {
v_links = v_links + '<link rel="'+defaultLinks[i].rel+'" href = "'+defaultLinks[i].href+'"/>';
}
var v_summaryType = father.summaryType || 'text/base64';
var v_summary = father.encodeHTML(father.summary) || '';
var v_projectUrl = father.project.selfUrl;
var v_targetProcess = '';
if(father.targetProcess.selfUrl){
v_targetProcess = '<link rel="target_process" href="'+father.targetProcess.selfUrl+'" title="'+father.encodeHTML(father.targetProcess.title)+'"/>' ;
}
var v_tag='';
for (var i = 0 ; i<father.tags.length;i++) {
v_tag = v_tag + '<category term="tag" label="'+father.encodeHTML(father.tags[i])+'" />';
}
if (father.users.length==0){
v_links = v_links +'<link rel="user" href = "'+context.link.self+'"/>';
}
for (var i = 0 ; i<father.users.length;i++) {
v_links = v_links + '<link rel="user" href = "'+father.users[i].selfUrl+'"/>';
}
var v_contentType = father.contentType || 'text/base64';
var v_content = father.content;
var updatexml = '';
if (options.update){
updatexml = ''//'<link rel="self" type="application/atom+xml" href="'+father.selfUrl+'">'
+ '<author>'
+ ' <name>'+father.author.name+'</name>'
+ ' <uri>'+father.author.selfUrl+'</uri>'
+ '</author>'
+ '<id>'+father.id+'</id>';
}
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> '
+ updatexml
+ '<entry> '
+ '<title>' + v_title + '</title> '
+ v_addCat
+ '<summary type="'+v_summaryType+'">'+v_summary+' </summary>'
+ '<link rel="project" href="'+v_projectUrl+'"/>'
+ v_targetProcess
+ v_links
+ v_tag
+ '<content type="'+v_contentType+'">'+v_content+'</content>'
+ '</entry> '
+ '</feed>';
return xml;
};
WebInterface.prototype.getDefaultCategories = function () {
father=this;
return [
{term:'type', label:'MANAGED'},
{term:'basket', label:'true',scheme:'CURRENT'},
{term:'stylesheet_web', label:father.encodeHTML(father.stylesheet_web), scheme:''},
{term:'visibility', label:'PRIVATE'},
{term:'dynamic_name', label:father.dynamicName},
{term:'subtype', label:'STANDALONE'},
{term:'header', label:'true', scheme:'relative'},
{term:'title', label:father.encodeHTML(father.headerTitle)},
{term:'portal_accept', label:'['+father.portalAccept+']'}
];
};
WebInterface.prototype.getDefaultLinks = function () {
return [
{rel:'host', href:this.encodeHTML(context.link.process_host)}
];
};