<%
include("/jagg/jagg.jag");
var log=new Log();
var ws = require("ws");

var completeTask = function (taskId,taskType,status,description,endpoint) {

var version = new ws.WSRequest();
var options = new Array();
options.useSOAP = 1.2;
options.action = "http://docs.oasis-open.org/ns/bpel4people/ws-humantask/api/200803/complete";
var cookie=session.get("workflowCookie");

options["HTTPHeaders"] = [
                              { name : "Cookie", value :cookie },
                        ];


var refId=getRefId(taskId,endpoint,taskType);
var taskPayload;

if(taskType=="subscription"){
taskPayload="&lt;sch:SubscriptionApprovalResponse xmlns:sch=&quot;http://workflow.subscription.apimgt.carbon.wso2.org&quot;&gt;"+
            "&lt;sch:status&gt;"+status+"&lt;/sch:status&gt;"+
            "&lt;sch:workflowExternalRef&gt;"+refId+"&lt;/sch:workflowExternalRef&gt;"+
            "&lt;sch:description&gt;"+description+"&lt;/sch:description&gt;" +
            "&lt;/sch:SubscriptionApprovalResponse&gt;";

} else if(taskType=="user-signup"){
taskPayload="&lt;sch:UserApprovalResponse xmlns:sch=&quot;http://workflow.registeruser.apimgt.carbon.wso2.org&quot;&gt;"+
            "&lt;sch:status&gt;"+status+"&lt;/sch:status&gt;"+
            "&lt;sch:workflowExternalRef&gt;"+refId+"&lt;/sch:workflowExternalRef&gt;"+
            "&lt;sch:description&gt;"+description+"&lt;/sch:description&gt;" +
            "&lt;/sch:UserApprovalResponse&gt;";

} else if(taskType=="application"){
taskPayload="&lt;sch:ApplicationApprovalResponse xmlns:sch=&quot;http://workflow.application.apimgt.carbon.wso2.org&quot;&gt;"+
            "&lt;sch:status&gt;"+status+"&lt;/sch:status&gt;"+
            "&lt;sch:workflowExternalRef&gt;"+refId+"&lt;/sch:workflowExternalRef&gt;"+
            "&lt;sch:description&gt;"+description+"&lt;/sch:description&gt;" +
            "&lt;/sch:ApplicationApprovalResponse&gt;";

} else if(taskType=="appRegistration"){
taskPayload="&lt;sch:ApplicationRegistrationResponse xmlns:sch=&quot;http://workflow.application.apimgt.carbon.wso2.org&quot;&gt;"+
            "&lt;sch:status&gt;"+status+"&lt;/sch:status&gt;"+
            "&lt;sch:workflowExternalRef&gt;"+refId+"&lt;/sch:workflowExternalRef&gt;"+
            "&lt;sch:description&gt;"+description+"&lt;/sch:description&gt;" +
            "&lt;/sch:ApplicationRegistrationResponse&gt;";

}

var payload = '<ns:complete xmlns:ns="http://docs.oasis-open.org/ns/bpel4people/ws-humantask/api/200803">'+
              '<ns:identifier>'+taskId+'</ns:identifier><ns:taskData>'+String(taskPayload)+'</ns:taskData></ns:complete>';
var result;

try {
version.open(options,endpoint, false);
version.send(payload);
result = version.responseE4X;
return {
error:false
}

} catch (e) {
log.error(e.toString());
return {
error:true
}
}
};

var getRefId = function (taskId,endpoint,taskType) {
var refId;
var version = new ws.WSRequest();
var options = new Array();
options.useSOAP = 1.2;
options.action = "http://docs.oasis-open.org/ns/bpel4people/ws-humantask/api/200803/getInput";
var cookie=session.get("workflowCookie");

options["HTTPHeaders"] = [
                              { name : "Cookie", value :cookie },
                        ];


var payload = '<ns:getInput xmlns:ns="http://docs.oasis-open.org/ns/bpel4people/ws-humantask/api/200803"><ns:identifier>'+taskId+'</ns:identifier></ns:getInput>';
var result;

try {
version.open(options,endpoint, false);
version.send(payload);
result = version.responseE4X;
var start=result.toString().indexOf("&lt;workflowExternalRef");
var end= result.toString().indexOf("&lt;/workflowExternalRef");
var splitVal="";
var namespace="";
if(taskType=="subscription"){
splitVal="&gt;"
} else if(taskType=="user-signup"){
namespace="http://workflow.registeruser.apimgt.carbon.wso2.org";
splitVal='\"'+namespace+'\"&gt;';
} else if(taskType=="application"){
splitVal="&gt;";
} else if(taskType=="appRegistration"){
splitVal="&gt;";
}

refId=result.toString().substring(start,end).split(splitVal)[1] ;

return refId;

} catch (e) {
log.error(e.toString());
return 0;
}
};





%>