Groovy connector file reading script returns null

1
0
-1

Hello, I created a groovy script connector in order to read a CSV file.
With a local file, I can get the result without error; however, I could not read the CSV file with document url as follows. The url is correct and tested already.
It returns null.
Would you someone know the reason why? The filename in the script is not valid?

Any help would be appreciated.
Thanks in advanced.

//def fileName= "D:/BonitaAddOns/csv/testFile.csv"
def fileName= "/bonita/portal/"+ csvObjVar.docUrl

    File file= new File(fileName)
    List<List<String>> lines = new ArrayList<>()
    Scanner inputStream

    try{
        inputStream = new Scanner(file)

        while(inputStream.hasNext()){
            String line= inputStream.next()
            String[] values = line.split(",") 
            lines.add(Arrays.asList(values))

        }

        inputStream.close()
        return lines


    }catch (FileNotFoundException e) {
        e.printStackTrace()
    }
1 answer

1
+1
-1
This one is the BEST answer!

If you read the documentation of Groovy File class you will see that it does not add constructors, this means you should refer to the documentation of Java File class. From the Java documentation you can find out that File is used to manipulate files located on the file system. This explain why your solution does not work.

In order to access the content of a file stored and managed by Bonita (i.e. declared in process definition and upload by the user using a form) you need to use Bonita Engine API.

You should take a look at the getDocumentContent method of the DocumentAPI. To get a reference to DocumentAPI do: apiAccessor.documentAPI.

Comments

Submitted by donghee.baik on Thu, 05/16/2019 - 17:52

Thank you for your answer.
I added the file contents into the script, however, the file still doesn't recognize yet.

ProcessAPI processAPI = apiAccessor.getProcessAPI();
byte[] content = processAPI.getDocumentContent(csvDocument.getContentStorageId());

I may need to manipulate file location I still don't get it...
Is there any example or reference I can understand more?
I appriciate your help.
Thanks.

Submitted by antoine.mottier on Tue, 05/21/2019 - 18:13

I just create an example that might help you a little bit. It's using a REST API extension to do the CSV parsing and generation. But as REST API extension are coded in Groovy it should be quite easy to turn this into a script used by the Groovy script connector.

One of the difference between using a connector and a REST API extension is that the connector requires a process to start or a task to be performed in order to be executed. Whereas a REST API extension can be called from a form or a page without any impact on the process execution.

Notifications