OK, how to do the document upload.
Create the subroutine to do the uploads
- Create a NEW DIAGRAM call it pushDocumentToDatabase
- In Execution->Instantiation Form select No Form
- Add a Script Task
- Add an End icon
- Add a pool variable LONG docID
- Save
Now to send the data to the subroutine:
- In your process Pool add a POOL Variable outputDocuments of type Object->List
- After the Start icon add a Script/Service Task
- In the Execution Tab select Operations, Add an Operation
- On the Left select the outputDocuments variable → TAKES VALUE OF → EDIT PENCIL to open the script editor.
- Add the following code:
import org.bonitasoft.engine.api.ProcessRuntimeAPI;
import java.util.logging.Logger;
int dI = 0;
boolean debug = true;
ProcessRuntimeAPI processRuntimeAPI = apiAccessor.getProcessAPI();
String processName = processRuntimeAPI.getProcessInstance(processInstanceId).getName();
//set the name of the routine
String thisTrace = " "+processName+ " getUpdatedOutputs: "
Logger logger= Logger.getLogger(“org.bonitasoft”);
if(debug){dI++; logger.severe(dI+thisTrace+“Trace Start”);}
//TODO - Code goes in here
List xList = new ArrayList();
if(debug){
dI++;if(0<=outputFiles){logger.severe(dI+thisTrace+"outputFiles: "+outputFiles.toString())}else{logger.severe(dI+thisTrace+"outputFiles: "+outputFiles.toString())}
dI++;if(1<=outputFiles){logger.severe(dI+thisTrace+"outputDoc01: “+outputDoc01.getVersion()+” "+outputDoc01.getContentFileName())}
dI++;if(2<=outputFiles){logger.severe(dI+thisTrace+"outputDoc02: “+outputDoc02.getVersion()+” "+outputDoc02.getContentFileName())}
dI++;if(3<=outputFiles){logger.severe(dI+thisTrace+"outputDoc03: “+outputDoc03.getVersion()+” "+outputDoc03.getContentFileName())}
dI++;if(4<=outputFiles){logger.severe(dI+thisTrace+"outputDoc04: “+outputDoc04.getVersion()+” "+outputDoc04.getContentFileName())}
dI++;if(5<=outputFiles){logger.severe(dI+thisTrace+"outputDoc05: “+outputDoc05.getVersion()+” "+outputDoc05.getContentFileName())}
dI++;if(6<=outputFiles){logger.severe(dI+thisTrace+"outputDoc06: “+outputDoc06.getVersion()+” "+outputDoc06.getContentFileName())}
dI++;if(7<=outputFiles){logger.severe(dI+thisTrace+"outputDoc07: “+outputDoc07.getVersion()+” "+outputDoc07.getContentFileName())}
dI++;if(8<=outputFiles){logger.severe(dI+thisTrace+"outputDoc08: “+outputDoc08.getVersion()+” "+outputDoc08.getContentFileName())}
dI++;if(9<=outputFiles){logger.severe(dI+thisTrace+"outputDoc09: “+outputDoc09.getVersion()+” "+outputDoc09.getContentFileName())}
dI++;if(10<=outputFiles){logger.severe(dI+thisTrace+"outputDoc10: “+outputDoc10.getVersion()+” "+outputDoc10.getContentFileName())}
}
def x;
if(1<=outputFiles){if (outputDoc01 == null){ /* do nothing / } else{x = apiAccessor.getProcessAPI().getLastDocument(processInstanceId, “outputDoc01”).getId(); xList.add(x);}}
if(2<=outputFiles){if (outputDoc02 == null){ / do nothing / } else{x = apiAccessor.getProcessAPI().getLastDocument(processInstanceId, “outputDoc02”).getId(); xList.add(x);}}
if(3<=outputFiles){if (outputDoc03 == null){ / do nothing / } else{x = apiAccessor.getProcessAPI().getLastDocument(processInstanceId, “outputDoc03”).getId(); xList.add(x);}}
if(4<=outputFiles){if (outputDoc04 == null){ / do nothing / } else{x = apiAccessor.getProcessAPI().getLastDocument(processInstanceId, “outputDoc04”).getId(); xList.add(x);}}
if(5<=outputFiles){if (outputDoc05 == null){ / do nothing / } else{x = apiAccessor.getProcessAPI().getLastDocument(processInstanceId, “outputDoc05”).getId(); xList.add(x);}}
if(6<=outputFiles){if (outputDoc06 == null){ / do nothing / } else{x = apiAccessor.getProcessAPI().getLastDocument(processInstanceId, “outputDoc06”).getId(); xList.add(x);}}
if(7<=outputFiles){if (outputDoc07 == null){ / do nothing / } else{x = apiAccessor.getProcessAPI().getLastDocument(processInstanceId, “outputDoc07”).getId(); xList.add(x);}}
if(8<=outputFiles){if (outputDoc08 == null){ / do nothing / } else{x = apiAccessor.getProcessAPI().getLastDocument(processInstanceId, “outputDoc08”).getId(); xList.add(x);}}
if(9<=outputFiles){if (outputDoc09 == null){ / do nothing / } else{x = apiAccessor.getProcessAPI().getLastDocument(processInstanceId, “outputDoc09”).getId(); xList.add(x);}}
if(10<=outputFiles){if(outputDoc10 == null){ / do nothing */ } else{x = apiAccessor.getProcessAPI().getLastDocument(processInstanceId, “outputDoc10”).getId(); xList.add(x);}}
if(debug){dI++; logger.severe(dI+thisTrace+“xList”+xList.toString());}
if(debug){dI++; logger.severe(dI+thisTrace+“Trace End”);}
return xList;
NOTE(1): Debug code is included and can be read on the log
NOTE(2): the code is for hardcoded documents we have - you need to change the two major code sections to loop over the Document List variable that you’ve defined. The first section prints all the information on the log, the second created the usable outputDocuments variable previously defined
6. After the Script/Service Task add an ABSTRACT Task->General->Iteration
7. Select Parallel or Sequential multi-Instantiation as required
8. Select Create instances from a list
9. Input is variable outputDocuments
10. Iterator is docID
11. goto General->Process to call and give the name of pushDocumentToDatabase subroutine
12. Execution->data to send and add docID Assigned to Data docID
That gives you everything except the last bit to turn the data into a blob that can be uploaded:
Document d1 = apiAccessor.getProcessAPI().getDocument(docID);
String documentname = d1.getContentFileName();
String mimeType = d1.getContentMimeType();
// use fileName if title is empty
title = (title==null||title.isEmpty()) ? d1.getContentFileName():title;
final byte[] contentByte = ((DocumentAPI) processRuntimeAPI).getDocumentContent(d1.getContentStorageId());
final ByteArrayInputStream gubedocument = new ByteArrayInputStream(contentByte);
int doclength = contentByte.length;
and the final prepared statement that uploads the data:
PreparedStatement ps = con.prepareStatement("INSERT INTO " + tble
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
//1 documentid
//2 tdocumentname
//3 documentname
//4 versionmajor
//5 versionminor
//6 versionmini
//7 uploader
//8 approver
//9 uploaddate
//10 approvaldate
//11 status
//12 document - the blob
ps.setLong(1, 0);
ps.setString(2, removeExtension(documentname.replace("_", " "))); // remove
ps.setString(3, gubedocumentname);
ps.setLong(4, 0);
ps.setLong(5, 0);
ps.setLong(6, 0);
ps.setString(7, "Uploader");
ps.setString(8, "Approver");
ps.setTimestamp(9, date);
ps.setTimestamp(10, date);
ps.setString(11, "Pending");
ps.setBinaryStream(12, document, doclength);
ps.executeUpdate();
ps.close();
con.close();
It’s all up to you now,
regards
Seán