/**
*
* 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 Report
@constructor
@property {String} content - The base64 report content
@property {String} title - The report title
@property {Date} published - The date published
@property {Array} reportFilters - Array of the report filters
@property {Array} reportColumns - Array of the report Columns
@property {Object} currentUser - The current user of the report
@property {Object} project - The report's project
@property {String} paginatedDetailUrl - The url of the paginated Detail
@see AppInstanceReport
@see Resource
*/
function Report(){
this.content;
this.title;
this.published;
this.reportFilters=[];
this.reportColumns=[];
this.currentUser = new User();
this.project = new Project();
this.paginatedDetailUrl;
};
/**
@borrows
Inherits Resource
*/
Report.prototype = new Resource();
/**
Overrides Resource's loadPreset method.
@method
@see Resource#loadPreset
*/
Report.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
*/
Report.prototype.loadSet = function (rObject) {
var father = this;
father.content = father.entries.content;
father.title = father.entries.title;
father.published = new Date(father.entries.published);
father.selfUrl = father.linkSearch('self',father.links);
father.category = father.entries.category;
father.rights = rObject.rights;
father.currentUser = father.linkSearch('current_user',father.links);
father.paginatedDetailUrl = father.linkSearch('paginated_detail',father.links);
var project = new Project();
project.selfUrl = father.linkSearch('project',father.links);
father.project = project;
};
/**
Create a new report.
@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
*/
Report.prototype.create = function (options) {
var father = this;
var appInstanceRep = new AppInstanceReport();
appInstanceRep.selfUrl = context.link.appInstanceReport;
/**
Overrides Resource's savePreset method.
@method
@see Resource#savePreset
*/
appInstanceRep.savePreset = function () {
/**
Overrides Resource's generateUrl method to return the request url
@method
@see Resource#generateUrl
*/
appInstanceRep.generateUrl = function () {
return appInstanceRep.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(rData);
};
pOpt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
pOpt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
pOpt.update = false;
appInstanceRep.xml = father.generate_xml(pOpt).trim();
appInstanceRep.save(pOpt);
};
/**
Update a new Report.
@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
*/
Report.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.split("?")[0];//REMOVE TRASH
};
};
var pOpt = {};
pOpt.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);
};
pOpt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
pOpt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
pOpt.update = true;
father.xml = father.generate_xml(pOpt).trim();
father.resourceUpdate(pOpt);
};
Report.prototype.generateReportFilterStr= function (filters){
var filterStr='';
var operatorStr='';
var valueStr='';
for(var i=0;i<filters.length;i++){
if (filterStr==''){//if its the first filter
filterStr=encodeURIComponent(filters[i].filter).replace("_","%5F");
operatorStr=encodeURIComponent(filters[i].operator).replace("_","%5F");
valueStr=encodeURIComponent(filters[i].value).replace("_","%5F");
}else{
filterStr=filterStr+'%20'+encodeURIComponent(filters[i].filter).replace("_","%5F");
operatorStr=operatorStr+'%20'+encodeURIComponent(filters[i].operator).replace("_","%5F");
valueStr=valueStr+'%20'+encodeURIComponent(filters[i].value).replace("_","%5F");
};
};
if (filterStr=='') return '';
return 'filter='+filterStr+'&operator='+operatorStr+'&value='+valueStr;
};
Report.prototype.generateReportColumnStr= function (columns){
var columnStr='';
for(var i=0;i<columns.length;i++){
if (columnStr==''){//if its the first column
columnStr=encodeURIComponent(columns[i]).replace("_","%5F");
}else{
columnStr=columnStr+'%20'+encodeURIComponent(columns[i]).replace("_","%5F");
};
};
if (columnStr=='') return '';
return 'column='+columnStr;
};
/**
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
*/
Report.prototype.generate_xml = function (options) {
var father = this;
var v_baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
var v_title = father.title;
var v_self = father.self||'';
var v_rights = father.rights || '(c) RunMyProcess';
var v_currentUser = father.currentUser.selfUrl;
var v_project = father.project.selfUrl;
var v_paginatedDetail = '';
//Filters
var filtersStr = father.generateReportFilterStr(father.reportFilters);
//end Filters
//Columns
var columnStr = father.generateReportColumnStr(father.reportColumns);
//end Filters
father.paginatedDetailUrl = context.link.appInstances.split("?")[0]+'?'+'nb={numRows}&first={startRow}&'+filtersStr+'&'+columnStr;
if (father.paginatedDetailUrl)v_paginatedDetail='<link rel="paginated_detail" href="'+father.paginatedDetailUrl+'"/>';
var v_contentSrc='';
/*if (father.content){//CHECK WHY THE LOAD IS NOT SETTING THE CORRECT VALUE FOR UPDATE
v_contentSrc = father.content.src;
}else {*/
v_contentSrc=father.paginatedDetailUrl;
//}
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> '
+ '<title>' + v_title + '</title> '
+ '<link rel="VIEW" href="'+v_currentUser+'"/>'
+ '<link rel="self" href="'+v_self+'"/>'
+ '<content src="'+v_contentSrc+'"/>'
+ '<link rel="project" href="'+v_project+'"/>'
+ v_paginatedDetail
+ '</entry> '
+ '</feed>';
return xml;
};