Questetra Role Membership Add
Adds a member to a Questetra “Role” and stores the communication log in a String-type Data Item.
2017 (C) Questetra, Inc. (MIT License)
Configs
  • A: Set OAuth2 Config Name (at [OAuth 2.0 Setting]) *
  • A2: Set Application-Root URI ( https:// … ) * #{EL}
  • B: Select STRING/CHECKBOX DATA for Qrole Id (Role Id) *
  • C: Select STRING/USER DATA for Quser Email *
  • X: Select STRING DATA for Access Log (update)
Script
//// == Config Retrieving ==
var oauth2  = configs.get( "conf_OAuth2" ) + "";
var applicationRoot = configs.get( "conf_ApplicationRoot" ) + "";
var dataIdB = configs.get( "conf_DataIdB" ) + "";
var dataIdC = configs.get( "conf_DataIdC" ) + "";
var dataIdX = configs.get( "conf_DataIdX" ) + "";
// convert 'java.lang.String' to 'javascript string'


//// == Data Retrieving ==
var targetGroups = [];
if( engine.findDataDefinitionByNumber( dataIdB ).matchDataType("SELECT_CHECKBOX") ){
  var selectedGroups = engine.findDataByNumber( dataIdB );
  for( var i = 0; i < selectedGroups.size(); i++ ){ // java.util.ArrayList
    var pushGroup = selectedGroups.get(i).getValue() + "";
    targetGroups.push( pushGroup );
  }
}else{
  var pushGroup = engine.findDataByNumber( dataIdB ) + "";
  targetGroups.push( pushGroup );
}
var userId = "";
if( engine.findDataDefinitionByNumber( dataIdC ).matchDataType("QUSER") ){
  userId = engine.findDataByNumber( dataIdC ).getId() + "";
  // com.questetra.bpms.core.event.scripttask.QuserView
}else{
  var userEmail = engine.findDataByNumber( dataIdC ) + "";
  userId = quserDao.findByEmail( userEmail ).getId() + "";
}


//// == Calculating ==
var accessLog = "";
var token  = httpClient.getOAuth2Token( oauth2 );
var apiUri = applicationRoot + "API/UGA/RoleMembership/add";

for( var i = 0; i < targetGroups.length; i++  ){ // javascript array
  // preparing for API Request
  var apiRequest = httpClient.begin(); // HttpRequestWrapper
  // com.questetra.bpms.core.event.scripttask.HttpClientWrapper
  apiRequest = apiRequest.bearer( token );
  apiRequest = apiRequest.formParam( "quserId",  userId );
  apiRequest = apiRequest.formParam( "qroleId", targetGroups[i] );

  // Access to the API (POST, GET, PUT, etc)
  var response = apiRequest.post( apiUri ); // HttpResponseWrapper
  var httpStatus = response.getStatusCode() + "";
  accessLog = "---POST request--- " + httpStatus + "\n";
  accessLog += response.getResponseAsString() + "\n";

  // Error Handling
  if( httpStatus !== "200" ){
    if( dataIdX !== "" ){ engine.setDataByNumber( dataIdX, accessLog ); }
    throw new Error( accessLog );
  }
}


//// == Data Updating ==
if ( dataIdX !== "" ){ engine.setDataByNumber( dataIdX, accessLog ); }

Download

Capture

Notes

  • Register an OAuth2 Client and aquire the Client ID and Client secret at [OAuth2 Clients] < [API Clients] < [System Settings]

See also

%d bloggers like this: