Source: Project/ProjectVersion.js

/**
 *
 * @author Malcolm Haslam <mhaslam@runmyprocess.com>
 *
 * Copyright (C) 2013 Fujitsu RunMyProcess
 *
 * 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 ProjectVersion
  	@constructor
	@property {String} title - Array of projects
	@property {Array} webInterfaces - Array of webInterfaces
	@property {Array} processes - Array of processes
	@property {Object} summary - Contains the projects summery
	@property {User} author - Contains the author's informations
	@property {Project} project - Contains the project associated to the version
	@see ProjectVersionList
	@see Resource
	@example Load version
		function loadVersionList(p_project){//loaded project object
            p_project.loadVersions({
				onSuccess:function(){
					var version = p_project.versions[1];
					version.load({
						onSuccess:function(){
							alert("Version loaded");
						}
					});
				}
			});
		};
 */
function ProjectVersion(){
	this.title;
	this.webInterfaces = [];
	this.processes = [];
	this.summary = {};
	this.author = new User();
	this.project = new Project();
};
/**
	* @borrows
	* Inherits Resource
*/
ProjectVersion.prototype = new  Resource();
/**
	Overrides Resource's loadPreset method.
	@method
	@see Resource#loadPreset
*/
ProjectVersion.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
*/
ProjectVersion.prototype.loadSet = function (rObject) {

};
/**
  Create a new ProjectVersion.
	@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
 */
ProjectVersion.prototype.create = function (options) {
	var father = this;
	var versionList = new ProjectVersionList();
	versionList.selfUrl = father.project.versionList.selfUrl;
	/**
		Overrides Resource's savePreset method.
		@method
		@see Resource#savePreset
	*/
	versionList.savePreset = function () {
		/**
			Overrides Resource's generateUrl method to return the request url
			@method
			@see Resource#generateUrl
		*/
		versionList.generateUrl = function () {
			return versionList.selfUrl;
		};
	};
	var verOpt = {};
	verOpt.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);
	};
	verOpt.onFailure = function(e){
		options.eObject=e;
		father.errorManager(options);
	};
	verOpt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
	verOpt.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].rel===father.links[j].rel){
              defExists = true;
              break;
            }
        }
        if (!defExists){
           father.links.push(defLks[i]);
        }
	}
	father.links = father.links;
	versionList.xml = father.generate_xml(verOpt).trim();
	versionList.save(verOpt);
};
/**
  Update an Existing 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#resourceUpdate
 */
ProjectVersion.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 verOpt = {};
	verOpt.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);
	};
	verOpt.onFailure = function(e){
		options.eObject=e;
		father.errorManager(options);
	};
	verOpt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
	verOpt.update = true;
	father.xml = father.generate_xml(verOpt).trim();
	father.resourceUpdate(verOpt);

};
/**
	Load a list the list of webInterfaces.
	@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 webInterfaces
        function loadWebinterface(p_version){//version object
			p_version.loadWebInterfaces({
				onSuccess:function(){
					alert("there are " +p_version.webInterfaces.length+" web interfaces");
				}
			});
		};
*/
ProjectVersion.prototype.loadWebInterfaces = function (options) {
	var webInterfaceList = new WebInterfaceList();
	var father = this;
	var pLink= this.linkSearch('appli',this.entries[0].link);
	webInterfaceList.selfUrl = pLink;
	var opt = {};
	opt.onSuccess = function(){
		father.webInterfaces = webInterfaceList.webInterfaces;
		options.onSuccess();
	};
	opt.onFailure = function(e){
			options.eObject=e;
			father.errorManager(options);
	};
	opt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
	webInterfaceList.load(opt);
};
/**
Load a list the list of processes.
@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 processes
    function loadProcess(p_version){//version object
		p_version.loadProcesses({
			onSuccess:function(){
				alert("there are " +p_version.processes.length+" processes");
			}
		});
	};
*/
ProjectVersion.prototype.loadProcesses = function (options) {
var pocessList = new ProjectProcessList();
var father = this;
var pLink= this.linkSearch('process',this.entries[0].link);
pocessList.selfUrl = pLink;
var opt = {};
opt.onSuccess = function(){
	father.processes = pocessList.processes;
	options.onSuccess();
};
opt.onFailure = function(e){
		options.eObject=e;
		father.errorManager(options);
};
opt.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
pocessList.load(opt);
};
/**
  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
 */
ProjectVersion.prototype.generate_xml = function (options) {
	father=this;
	var v_baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
	var v_title = 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];
		   v_addCat = v_addCat + '<category ';
		   for (var prop in obj) {
				v_addCat = v_addCat + prop +'="'+father.encodeHTML(obj[prop])+'" ';
		   }
		   v_addCat = v_addCat + '/>';
		}
	}
	var v_links = '';
	if (father.links !== undefined) {
		for (var i = 0 ; i<father.links.length;i++) {
		   var obj = father.links[i];
		   v_links = v_links + '<link ';
		   for (var prop in obj) {
				v_links = v_links + prop +'="'+father.encodeHTML(obj[prop])+'" ';
		   }
		   v_links = v_links + '/>';
		}
	}
	var v_summaryType = father.summaryType || 'text/base64';
	var v_summary =  '';
	if (!jQuery.isEmptyObject(father.summary)){
		v_summary = '<summary type="'+v_summaryType+'">'+father.summary+'</summary> ';
	}

	var v_contentType = father.contentType || 'application/json';
	var v_content = father.content || '';

	var xml = '<?xml version="1.0" encoding="UTF-8"?> '
	+ '<feed xml:base="' + v_baseUrl
	+ '" xmlns="http://www.w3.org/2005/Atom"> '
	+ '<title>' + v_title + '</title> '
	+ '<rights>'+v_rights+'</rights> '
	+ '<entry> '
	+ 	v_summary
	+   '<content type="'+v_contentType+'">'+v_content+'</content> '
	+ '</entry> '
	+ '</feed>';

	return xml;
};

ProjectVersion.prototype.getDefaultCategories = function () {
	return  [];
};
ProjectVersion.prototype.getDefaultLinks = function () {
    return  [];
};