Issue in retrieving documents through contract and executing document upload script as connector

1
0
-1

Hello,

This question is in reference to and a follow-up of the query I had posted earlier on this link.

So I have a simple doc upload form that has a list of documents and an file upload widget with each.
In my contract, I have defined a variable of type file that accepts multiple objects --> formDocuments.
In my UI designer I have a similar JSON that takes files that are uploaded through the form.

In order to retrieve the documents through the contract, I have written the following script:

  1. import org.bonitasoft.engine.api.ProcessRuntimeAPI;
  2. import java.util.logging.Logger;
  3.  
  4. int debugCount = 0;
  5. boolean debug = true;
  6.  
  7. Logger logs = Logger.getLogger("org.bonitasoft.myFirstRetailerForm");
  8. ProcessRuntimeAPI processRuntimeAPI = apiAccessor.getProcessAPI();
  9. String processName = processRuntimeAPI.getProcessInstance(processInstanceId).getName();
  10.  
  11. logs.severe("Document list size : "+formDocuments.size());
  12.  
  13.  
  14. List<Long> xList = new ArrayList<Long>();
  15. def doc_aliases = ["site_verify", "laf"];
  16.  
  17. int docCounter = 0;
  18. for (def i = 0; i <formDocuments.size(); i++){
  19. if (formDocuments[docCounter] != null){
  20. // def doc_name = (formDocuments[docCounter]["fileName"]);
  21. Long docId = apiAccessor.getProcessAPI().getLastDocument(processInstanceId, doc_aliases[i]).getId();
  22. xList.add(docId);
  23. }
  24. docCounter = docCounter+1;
  25. }
  26. logs.severe("List prepared!!");
  27. return xList;

Now when I run this task in debug mode through the studio, I am able to see that the contract is fulfilled but it throws an exception saying Groovy script throws an exception of type class org.bonitasoft.engine.bpm.document.DocumentNotFoundException with message = org.bonitasoft.engine.commons.exceptions.SObjectNotFoundException: Document not found

I can't figure out where I am falling short as syntactically, we will retrieve the ID of the doc from the name of the document. But the way I am trying to access it, is not working.

Appreciate the help I can get in this regard!

Regards
Megha

Comments

Submitted by Sean McP on Sat, 04/15/2017 - 21:05

is your formDocuments.size() greater than your doc_aliases.size()?

Submitted by megha on Sat, 04/15/2017 - 21:24

No, they are of the same size. In the logs that I print for every run, the size of the formDocuments is 2 which is also the size of the doc_aliases array.

Submitted by Sean McP on Sun, 04/16/2017 - 02:43

I would suggest that the document names you are giving in trying doc_aliases are wrong. You need to somehow assert the correct names for the files.

from the JavaDoc:

DocumentNotFoundException - If the specified documentName does not refer to an existing document attached to this process instance

regards
Seán

Submitted by megha on Mon, 04/17/2017 - 12:49

I was able to find out the problem, I had not defined the documents variable in the process definition due to which the context from where to access the documents was not found.

Here's the document map that I retrieve when I run my script to get documents from the contract:
[DocumentValue{mimeType='application/pdf', fileName='site_verify.pdf', url='null', hasContent=true, documentId=null, hasChanged=false, index=-1}, DocumentValue{mimeType='application/pdf', fileName='laf.pdf', url='null', hasContent=true, documentId=null, hasChanged=false, index=-1}]

Issue

Why is the documentId being registered null in this case?
Moreover, I get the following error when trying to execute the Mongo upload script:

*org.bonitasoft.engine.connector.exception.SConnectorException: java.util.concurrent.ExecutionException: org.bonitasoft.engine.connector.exception.SConnectorException: org.bonitasoft.engine.connector.ConnectorException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 2: unable to resolve class com.mongodb.DBCollection
*
Below is the code for your reference:

  1. import com.mongodb.DB;
  2. import com.mongodb.DBCollection;
  3. import com.mongodb.MongoClient;
  4. import com.mongodb.MongoClientURI;
  5. import com.mongodb.MongoException;
  6. import com.mongodb.gridfs.GridFS;
  7. import com.mongodb.gridfs.GridFSInputFile;
  8. import org.bonitasoft.engine.api.DocumentAPI;
  9. import org.bonitasoft.engine.api.ProcessRuntimeAPI;
  10. import org.bonitasoft.engine.bpm.document.Document;
  11. import org.bonitasoft.engine.bpm.document.DocumentValue;
  12. import java.util.logging.Logger;
  13.  
  14. ProcessRuntimeAPI processRuntimeAPI = apiAccessor.getProcessAPI();
  15. final String MONGO_USER = "XXX";
  16. final String MONGO_PWD = "XXXX";
  17. final String MONGO_HOSTNAME = "XXXXX";
  18. final int MONGO_PORT = 27017;
  19. //retrieve the ID for retailer through contract
  20. final String retailerCode = externalId;
  21. Logger logger = Logger.getLogger("org.bonitasoft.uploadToMongo");
  22.  
  23. logger.info("Retailer Code - "+retailerCode);
  24. ArrayList<Long> docVals = uploadedDocs;
  25. logger.info("Document IDs retrieved - "+docVals.size().toString());
  26. for (DocumentValue docVal : docVals){
  27. Document d1 = apiAccessor.getProcessAPI().getDocument(docVal.documentId);
  28.  
  29. //get the meta data for the file
  30. String documentName = d1.getContentFileName();
  31. String mimeType = d1.getContentMimeType();
  32.  
  33. final byte[] contentByte = ((DocumentAPI) processRuntimeAPI).getDocumentContent(d1.getContentStorageId());
  34. final ByteArrayInputStream docContent = new ByteArrayInputStream(contentByte);
  35. int doclength = contentByte.length;
  36. logger.info("Content Length - "+doclength);
  37. saveFileToMongo(docContent, documentName, mimeType);
  38. }
  39.  
  40. // creating a connection with Mongo instance
  41. void saveFileToMongo(InputStream streamFile, String fName, String mimeType){
  42. try{
  43. logger.info("Preparing to upload file - "+fName);
  44. MongoClient newMongoConn = new MongoClient(new MongoClientURI("mongodb://"+MONGO_USER+":"+MONGO_PWD+"@"+MONGO_HOSTNAME+":"+MONGO_PORT));
  45. DB dbInstance = newMongoConn.getDB("test");
  46. DBCollection coll = dbInstance.getCollection("fs.files");
  47.  
  48. //create a namespace
  49. GridFS gfs = new GridFS(dbInstance, "retailerDocs");
  50. GridFSInputFile inputFile = gfs.createFile(streamFile);
  51. inputFile.setFilename(fName);
  52. inputFile.setContentType(mimeType);
  53. inputFile.save();
  54. }
  55. catch(UnknownHostException ue){
  56. ue.printStackTrace();
  57. }
  58. catch(MongoException me){
  59. me.printStackTrace();
  60. }
  61. catch(IOException ie){
  62. ie.printStackTrace();
  63. }
  64. }

I have added the requisite JAR files to the studio (mongo dependencies), so I cannot figure out why I am getting this error.
Appreciate all the help I can get in this regard!

Thanks
Regards
Megha

Submitted by Lionel Palacin on Wed, 04/26/2017 - 21:07

Hi,

Did you add the JAR as a process dependencies? You can check that by looking the Configure --> Process dependencies

Cheers

No answers yet.
Notifications