// GraalJS Script (engine type: 2)
//////// START "main()" /////////////////////////////////////////////////////////////////
main();
function main(){
//// == Config Retrieving / 工程コンフィグの参照 ==
const strAuthzSetting = configs.get ( "AuthzConfU" ); /// REQUIRED
engine.log( " AutomatedTask Config: Authz Setting: " + strAuthzSetting );
const strInputfileId = configs.get ( "StrConfA1" ); /// REQUIRED
if( strInputfileId === "" ){
throw new Error( "\n AutomatedTask ConfigError:" +
" Config {A1: FileID} is empty \n" );
}
let strSaveas = configs.get ( "StrConfB1" ); // NotRequired
if( strSaveas === "" ){
engine.log( " AutomatedTask ConfigWarning:" +
" Config {B1: ExportedSaveas} is empty" );
}
const filesPocketDownload = configs.getObject( "SelectConfB2" ); /// REQUIRED
const strPocketNameOnDrive = configs.getObject( "SelectConfB3" ); // NotRequired
const strPocketMimetype = configs.getObject( "SelectConfB4" ); // NotRequired
//// == Data Retrieving / ワークフローデータの参照 ==
let filesAttached = engine.findData( filesPocketDownload ); // java.util.ArrayList
if( filesAttached === null ) {
engine.log( " AutomatedTask FilesArray {B2} (empty)" );
filesAttached = new java.util.ArrayList();
}else{
engine.log( " AutomatedTask FilesArray {B2}: " +
filesAttached.size() + " files" );
}
//// == Calculating / 演算 ==
/// Gets a file's metadata by ID.
/// Google Workspace for Developers > Google Drive for Developers > v3
/// https://developers.google.com/drive/api/v3/reference/files/get
// request1, prepare
let request1Uri = "https://www.googleapis.com/drive/v3/files/" + strInputfileId;
let request1 = httpClient.begin(); // HttpRequestWrapper
request1 = request1.authSetting( strAuthzSetting ); // with "Authorization: Bearer XX"
// https://questetra.zendesk.com/hc/en-us/articles/360024574471-R2300#HttpRequestWrapper
request1 = request1.queryParam( "supportsAllDrives", "true" );
// request1, try
const response1 = request1.get( request1Uri ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest1 Start: " + request1Uri );
const response1Code = response1.getStatusCode() + "";
const response1Body = response1.getResponseAsString() + "";
engine.log( " AutomatedTask ApiResponse Status: " + response1Code );
if( response1Code !== "200"){
throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
response1Code + "\n" + response1Body + "\n" );
}
// response1, parse
/* engine.log( response1Body ); // debug
{
"kind": "drive#file",
"id": "0BwSW_lixFpUuQ2JyV1pqU3VzTGs",
"name": "Questetra-Intro-en.pdf",
"mimeType": "application/pdf"
}
*/
const response1Obj = JSON.parse( response1Body );
engine.log( " AutomatedTask ApiResponse: File Name OnDrive: " + response1Obj.name );
engine.log( " AutomatedTask ApiResponse: File MIME-Type: " + response1Obj.mimeType );
if( strSaveas === "" ){
strSaveas = response1Obj.name + ".txt";
}
/// Export as PDF from Google Drive to BPMS.
/// Google Workspace for Developers > Google Drive for Developers > v3
/// https://developers.google.com/drive/api/v3/reference/files/export
let request2Uri = "https://www.googleapis.com/drive/v3/files/" + strInputfileId + "/export";
let request2 = httpClient.begin(); // HttpRequestWrapper
request2 = request2.authSetting( strAuthzSetting ); // with "Authorization: Bearer XX"
// https://questetra.zendesk.com/hc/en-us/articles/360024574471-R2300#HttpRequestWrapper
request2 = request2.queryParam( "mimeType", "text/plain" );
// request2, try
const response2 = request2.get( request2Uri ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest2 Start: " + request2Uri );
const response2Code = response2.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse Status: " + response2Code );
if( response2Code !== "200"){
const response2Body = response2.getResponseAsString() + "";
throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
response2Code + "\n" + response2Body + "\n" );
}
// response2, parse
const fileTmp = new com.questetra.bpms.core.event.scripttask.NewQfile(
strSaveas, response2.getContentType(), response2.getResponse()
);
filesAttached.add( fileTmp );
//// == Data Updating / ワークフローデータへの代入 ==
engine.setData( filesPocketDownload, filesAttached );
if( strPocketNameOnDrive !== null ){
engine.setData( strPocketNameOnDrive, response1Obj.name );
}
if( strPocketMimetype !== null ){
engine.setData( strPocketMimetype, response1Obj.mimeType );
}
} //////// END "main()" /////////////////////////////////////////////////////////////////
/*
Notes:
- Text extraction of contracts and manuals created with Google Docs is automated.
- The File ID can be obtained from the URI or the sharing settings screen.
- The exported content is limited to 10MB.
- To extract string data (string type) from text file (file type),
- Converter (Text File to String type data)
- https://support.questetra.com/addons/converter-textfile-to-string/
Notes-ja:
- Google ドキュメントで作成された契約書やマニュアルのテキスト情報抽出を自動化できます。
- File ID は URI や共有設定画面等から取得します。
- Exportされるファイルのサイズ上限は 10MB です。
- テキスト(ファイル型)から文字列データ(文字列型)を抽出するには別途自動工程を配置します。
- コンバータ (テキストファイル to 文字型データ)
- https://support.questetra.com/ja/addons/converter-textfile-to-string/
APPENDIX-en
- Setting example of "HTTP Authentication" (OAuth2)
- Authorization Endpoint URL:
- https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force
- Token Endpoint URL:
- https://accounts.google.com/o/oauth2/token
- Scope:
- https://www.googleapis.com/auth/drive.readonly
- Client ID, Consumer Secret:
- ( from https://console.developers.google.com/ )
- Redirect URLs: https://s.questetra.net/oauth2callback
APPENDIX-ja
- "HTTP認証"(OAuth2)の設定例
- Authorization Endpoint URL:
- https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force
- Token Endpoint URL:
- https://accounts.google.com/o/oauth2/token
- Scope:
- https://www.googleapis.com/auth/drive.readonly
- Client ID, Consumer Secret:
- ( from https://console.developers.google.com/ )
- Redirect URLs: https://s.questetra.net/oauth2callback
*/