/**
*
* 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 CustomList
@constructor
@property {String} title - title of the CustomList
@property {User} author - Contains the author's information.
@property {Date} updated - The updated date and time
@property {Object} project - CustomList's project
@property {String} content - Contains the content data
@property {String} contentContentType - Contains content type
@see ProjectList
@see Resource
*/
function CustomList(){
this.title;
this.author = new User();
this.updated = new Date();
this.project = new Project();
this.content = "";
this.contentContentType = "";
//this.detach//TODO
//this.history//TODO
};
/**
@borrows
Inherits Resource
*/
CustomList.prototype = new Resource();
/**
Overrides Resource's loadPreset method.
@method
@see Resource#loadPreset
*/
CustomList.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
*/
CustomList.prototype.loadSet = function (rObject) {
var father = this;
if (father.selfUrl.indexOf('?')!=-1)
father.selfUrl = father.selfUrl.substr(0, father.selfUrl.indexOf('?'));//CLEAN SELFURL
father.links = father.links.concat(father.getArray(father.object.entry, 'link'));
father.author.name = rObject.author.name;
father.author.selfUrl = rObject.author.uri;
father.updated = new Date(rObject.updated);
father.title = rObject.title;
if (rObject.entry.content){
father.content = rObject.entry.content.P_value;
father.contentContentType = rObject.entry.content.type;
}
father.project.selfUrl = father.linkSearch('project', father.links);
};
/**
Create a new CustomList.
@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 CustomList
var cl = new CustomList();
cl.project.selfUrl = "config/620821136/project/36866";
cl.title = "mySPRcustomList"
cl.content ="eyJsaXN0IjpbeyJ2YWx1ZSI6IlZhbHVlIiwibGFiZWwiOiJMYWJlbCJ9XX0=";
cl.create({
onSuccess : function(){
alert("custom List Created with id = "+cl.id);
}
});
*/
CustomList.prototype.create = function (options) {
var father = this;
var customListList = new CustomListList();
/**
Overrides Resource's savePreset method.
@method
@see Resource#savePreset
*/
customListList.savePreset = function () {
/**
Overrides Resource's generateUrl method to return the request url
@method
@see Resource#generateUrl
*/
customListList.generateUrl = function () {
return context.link.customLists;
};
};
var cOpt = {};
cOpt.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);
};
cOpt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
cOpt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
cOpt.update = false;
customListList.xml = father.generate_xml(cOpt).trim();
customListList.save(cOpt);
};
/**
Save a new Custom List.
@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
*/
CustomList.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 clOpt = {};
clOpt.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);
};
clOpt.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
clOpt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
clOpt.update = true;
father.xml = father.generate_xml(clOpt).trim();
father.resourceUpdate(clOpt);
};
/**
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
*/
CustomList.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_project = father.project.selfUrl;
if (father.content){
var v_contentType = father.contentContentType||'text/base64';
var v_content = '<content type="'+v_contentType+'">'+father.content+'</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> '
+ '<title>' + v_title + '</title> '
+ v_content
+ '<category term="public_access" label="false"/>'
+ '<link rel="project" href="'+v_project+'"/>'
+ '</entry> '
+ '</feed>';
return xml;
};