Issue in retrieving documents through contract and executing document upload script as connector
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:
import java.util.logging.Logger;
int debugCount = 0;
boolean debug = true;
Logger logs = Logger.getLogger("org.bonitasoft.myFirstRetailerForm");
ProcessRuntimeAPI processRuntimeAPI = apiAccessor.getProcessAPI();
String processName = processRuntimeAPI.getProcessInstance(processInstanceId).getName();
logs.severe("Document list size : "+formDocuments.size());
List<Long> xList = new ArrayList<Long>();
def doc_aliases = ["site_verify", "laf"];
int docCounter = 0;
for (def i = 0; i <formDocuments.size(); i++){
if (formDocuments[docCounter] != null){
// def doc_name = (formDocuments[docCounter]["fileName"]);
Long docId = apiAccessor.getProcessAPI().getLastDocument(processInstanceId, doc_aliases[i]).getId();
xList.add(docId);
}
docCounter = docCounter+1;
}
logs.severe("List prepared!!");
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
is your formDocuments.size() greater than your doc_aliases.size()?
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.
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
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:
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoException;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSInputFile;
import org.bonitasoft.engine.api.DocumentAPI;
import org.bonitasoft.engine.api.ProcessRuntimeAPI;
import org.bonitasoft.engine.bpm.document.Document;
import org.bonitasoft.engine.bpm.document.DocumentValue;
import java.util.logging.Logger;
ProcessRuntimeAPI processRuntimeAPI = apiAccessor.getProcessAPI();
final String MONGO_USER = "XXX";
final String MONGO_PWD = "XXXX";
final String MONGO_HOSTNAME = "XXXXX";
final int MONGO_PORT = 27017;
//retrieve the ID for retailer through contract
final String retailerCode = externalId;
Logger logger = Logger.getLogger("org.bonitasoft.uploadToMongo");
logger.info("Retailer Code - "+retailerCode);
ArrayList<Long> docVals = uploadedDocs;
logger.info("Document IDs retrieved - "+docVals.size().toString());
for (DocumentValue docVal : docVals){
Document d1 = apiAccessor.getProcessAPI().getDocument(docVal.documentId);
//get the meta data for the file
String documentName = d1.getContentFileName();
String mimeType = d1.getContentMimeType();
final byte[] contentByte = ((DocumentAPI) processRuntimeAPI).getDocumentContent(d1.getContentStorageId());
final ByteArrayInputStream docContent = new ByteArrayInputStream(contentByte);
int doclength = contentByte.length;
logger.info("Content Length - "+doclength);
saveFileToMongo(docContent, documentName, mimeType);
}
// creating a connection with Mongo instance
void saveFileToMongo(InputStream streamFile, String fName, String mimeType){
try{
logger.info("Preparing to upload file - "+fName);
MongoClient newMongoConn = new MongoClient(new MongoClientURI("mongodb://"+MONGO_USER+":"+MONGO_PWD+"@"+MONGO_HOSTNAME+":"+MONGO_PORT));
DB dbInstance = newMongoConn.getDB("test");
DBCollection coll = dbInstance.getCollection("fs.files");
//create a namespace
GridFS gfs = new GridFS(dbInstance, "retailerDocs");
GridFSInputFile inputFile = gfs.createFile(streamFile);
inputFile.setFilename(fName);
inputFile.setContentType(mimeType);
inputFile.save();
}
catch(UnknownHostException ue){
ue.printStackTrace();
}
catch(MongoException me){
me.printStackTrace();
}
catch(IOException ie){
ie.printStackTrace();
}
}
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
Hi,
Did you add the JAR as a process dependencies? You can check that by looking the Configure --> Process dependencies
Cheers