Rational Team Concert - Use HttpConnectorParameters on Calculated Value script based custom attribute
Hi everybody!
I want to ask if there's a way to use HttpConnectorParameters on a Calculated Value (script based) for custom attributes.
This is an extample of how to use HttpConnectorParameters on a Set Value (script based):
dojo.provide("net.jazz.example.scripted.oslc.wiids.ValueSetProvider");
dojo.require("com.ibm.team.workitem.api.common.connectors.HttpConnectorParameters");
(function() {
dojo.declare("net.jazz.example.scripted.oslc.wiids.ValueSetProvider", null, {
getValueSet: function(attributeId, workItem, context){
var params= new com.ibm.team.workitem.api.common.connectors.HttpConnectorParameters();
params.url="<server>/ccm/oslc/contexts/<pareauuid>/workitems";
params.xpath= "//Collection/ChangeRequest";
params.columnXpaths= ["./identifier"];
params.columnIds= ["wid"];
params.ignoreInvalidCertificates=true;
params.useOAuth=true;
var connector= context.getDataConnector("HttpConnector");
var values= connector.get(params);
var result= [];
while(values.hasNext()){
var entry= values.next();
var wiIDs= entry.getById("wid");
result.push(wiIDs);
}
return result;
}
});
})();
We want to do something similar but to the Calculated Value custom attribute.
We want to return to a field something that is calculated from the HttpConnectorParameters. Something like this:
dojo.provide("com.example.ValueProvider");
dojo.require("com.ibm.team.workitem.api.common.connectors.HttpConnectorParameters");
(function() {
dojo.declare("com.example.ValueProvider", null, {
getValue: function(attribute, workItem, context) {
var params= new com.ibm.team.workitem.api.common.connectors.HttpConnectorParameters();
params.url="<server>/ccm/oslc/contexts/<pareauuid>/workitems";
params.xpath= "//Collection/ChangeRequest";
params.columnXpaths= ["./identifier"];
params.columnIds= ["wid"];
params.ignoreInvalidCertificates=true;
params.useOAuth=true;
var connector= context.getDataConnector("HttpConnector");
var values= connector.get(params);
var result= '';
while(values.hasNext()){
var entry= values.next();
var wiIDs= entry.getById("wid");
result = result + ', ' + wiIDs;
}
return result;
}
});
})();
2 answers
Ah, wishful thinking: the third argument to getValue() is configuration, not context as for the value provider. See https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Calculated_values and https://jazz.net/wiki/bin/view/Main/AttributeCustomization#API_for_Javascript
From https://jazz.net/wiki/bin/view/Main/DataSourceValueSetProviders#Data_source_connectors you can see that
Inside a Value Set provider you can get a new instance of any contributed data connector by calling:
- For Javascript:
context.getDataConnector("connectorId")
is a feature of being in the context of a Value Set provider -- this simply isn't a feature of a Calculated Value customization. If you had debugging turned on or were watching your JavaScript console, I assume you would have seen errors. Some details here: https://jazz.net/library/article/1360
Hello Juan,
As an alternative, as you cannot access the "HttpConnectorParameters " within a calculated value, you can use a http client in the javascript of your calculated value to connect to a requested service (OSLC, REST, etc.) and then retrieve the info you need.
Based on Millard Ellingsworth comment, the http reuest cannot be asynchronous within a Calculated value:
https://jazz.net/forum/questions/128516/work-item-calculated-values-using-soa-call
But you can still create a synchronous http request to RTC and gather the info you need:
var url_resource = "http://myServer"
var request = {url: url_resource,
sync : "true",
handleAs: "json",
headers: {
accept: "application/json"
}};
var d = jazz.client.xhrGet(request);
This will return a JSON object.
Take into account that this is a synchronous call, with the consequences that it implies:
http://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean
I have one question open about the possibility of asynchronous calls in case you want to follow it:
https://jazz.net/forum/questions/218076/asynchronous-http-calls-in-a-script-based-calculated-value
As an alternative, as you cannot access the "HttpConnectorParameters " within a calculated value, you can use a http client in the javascript of your calculated value to connect to a requested service (OSLC, REST, etc.) and then retrieve the info you need.
Based on Millard Ellingsworth comment, the http reuest cannot be asynchronous within a Calculated value:
https://jazz.net/forum/questions/128516/work-item-calculated-values-using-soa-call
But you can still create a synchronous http request to RTC and gather the info you need:
var url_resource = "http://myServer"
var request = {url: url_resource,
sync : "true",
handleAs: "json",
headers: {
accept: "application/json"
}};
var d = jazz.client.xhrGet(request);
This will return a JSON object.
Take into account that this is a synchronous call, with the consequences that it implies:
http://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean
I have one question open about the possibility of asynchronous calls in case you want to follow it:
https://jazz.net/forum/questions/218076/asynchronous-http-calls-in-a-script-based-calculated-value
Comments
Millard Ellingsworth
FORUM ADMINISTRATOR / JAZZ DEVELOPER Mar 14 '14, 12:51 p.m.Are you saying that you use the first one successfully yet the second one doesn't work? I can't quite tell what your question is. If you have tried the second example and it doesn't work, what happens? And what versions of RTC are involved? Also, because of how these are called, I'd be carefully doing any possible long-running operation in a scripted customization. Calculated Value scripts are run on both the client and the server, so whatever you do must work from either side and produce the same result no matter how many times it is run..
Juan Martín Alberti
Mar 14 '14, 2:06 p.m.Millard,