How can we upload documents using the upload widget in Bonita BPM 7.4.2 by binding it with a DB connector?

1
0
-1

My use case is to allow the user to upload document(s) to a MongoDB server from the UI.

I have designed the form for this purpose but I am encountering few problems in configuring the DB Connector and have little to no idea how to bind the upload widget with the DB connector. Specifically, the upload widget has a property - URL that can be configured to tell where the data needs to go; I wish to know how I can specify this property if I have a configured DB Connector.

Thanks in advance!!

2 answers

1
0
-1
This one is the BEST answer!
  • If I am facilitating the upload of the documents via a script in the operations tab; I can do all tasks such as connecting to DB and actually uploading docs through. Do I still need a connector configuration?

No not really, but one thing we found very quickly was we wanted to use it again, and again, and again, and...I think you get the picture.

It is easy to think you will do this once when in-fact we use it at least 560 times. A subroutine was the only way to go.

  • How does the information go back to the operations tab by clicking the Submit Button - does this need to be mentioned somewhere in the properties of the button?

How else would it get back? You should format the formOutput variable to send back the collection. In your table you will have ADD button to add a new document, this in-fact adds to the collection, it is the collection you will be sending back and then save to a variable which you can process via a Script Task calling the subroutine which calls the upload script.

  • How can I give positive/ negative feedback to the user if the actual upload succeeded or not?

We don't do that, we only allow the Admin to sort that out as the file is uploaded on submit, how exactly we don't worry about it. We trust the system in this case. ;)

Comments

Submitted by megha on Tue, 03/28/2017 - 09:43

Thanks a lot, Sean! I think I am getting to understand the picture piece by piece so some rudimentary doubts keep coming up.

This is great help!!

Submitted by Sean McP on Tue, 03/28/2017 - 09:48

That being said, I've just throw a quick something together and I'm also confused a little...

Will get back to you on this.

regards

1
+1
-1

I'll be honest - just use a script connector, it's much easier to control and use.

And you'll know the code.

The UI will return the document as part of a contract - you then just have to access the document through the documentID from the output operation, from there it's quite easy to build the document as a byte stream and upload it to the database through a Insert command.

regards
Seán

regards
Seán

PS: As this reply offers an answer your question, and if you like it, please Mark UP and/or as Resolved.

Comments

Submitted by megha on Tue, 03/28/2017 - 07:15

All right, thank you for this. It seems easier than trying to configure a MongoDB Connector with the UI.

Please pardon my naivety, but I just need to confirm if I am doing the right thing to facilitate the upload of documents. So, I need to have a contract for the files being uploaded as well as one for the document ID (or documentName as well, optionally). My doubt is more inclined towards knowing this:
I made a simple form where a user can upload multiple documents by clicking the add button. So, when I click the paperclip button and choose a file for upload, where does the file get stored -- is it some local/temp folder on the server where the form is served? I assume, that this shall work only if I actually run the application and shall not work if I run in Preview mode(as I encountered an error stating "Upload failed" when I ran it in Preview Mode).

The script connector that you mentioned in the answer will be used to convert the file object to a byte stream and upload to the server -- is this understanding correct? Can I facilitate the triggering of this script on the action of a button?

Your help and guidance is greatly appreciated!!

Thanks

Submitted by Sean McP on Tue, 03/28/2017 - 08:11

the way we do it is:

Add the file widget to the page
click submit
submit sends the file information back to the operations tab
in the operations tab we take the documentID and just upload the document to the database.

As you're doing a variable number of documents the contract will really be for the collection of documents I suppose. there is no need to give a contract for the file name this will be accessible from the documentID.

The file will as you say get stored in in /temp directory which will then be accessible through the documentID.

And yes, this will only work as part of the process - not preview.

the Script Connector - yes it will be used to upload the file to the database, and there will be no need to trigger the script by button because you'll either have it as an operation, or a follow on Script task.

We actually created our own Connector (Development -> Connector Implementation) to do this for us, all we send it is the db parameters (login etc.) and the docID.

I think you will do something like in the operation

(target) fileContainer set value as uiContainerWithFiles

Then the next task is a Script Task that multiInstantiates a subroutine that does the actual upload.

here is our PostgreSQL code :) :

  1. package org.xxxxrnare.connector.xxxxPutDocumentToPostgres110Impl;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Timestamp;
  10. import java.util.ArrayList;
  11. import java.util.Calendar;
  12. import java.util.HashMap;
  13. import java.util.List;
  14. import java.util.Map;
  15. import java.util.logging.Logger;
  16.  
  17. import org.bonitasoft.engine.api.DocumentAPI;
  18. import org.bonitasoft.engine.api.ProcessRuntimeAPI;
  19. import org.bonitasoft.engine.bpm.document.Document;
  20. import org.bonitasoft.engine.bpm.document.DocumentNotFoundException;
  21. import org.bonitasoft.engine.connector.ConnectorException;
  22.  
  23. /**
  24. *The connector execution will follow the steps
  25. * 1 - setInputParameters() --> the connector receives input parameters values
  26. * 2 - validateInputParameters() --> the connector can validate input parameters values
  27. * 3 - connect() --> the connector can establish a connection to a remote server (if necessary)
  28. * 4 - executeBusinessLogic() --> execute the connector
  29. * 5 - getOutputParameters() --> output are retrieved from connector
  30. * 6 - disconnect() --> the connector can close connection to remote server (if any)
  31. */
  32. public class xxxxPutDocumentToPostgres110Impl extends
  33. AbstractxxxxPutDocumentToPostgres110Impl {
  34.  
  35. Logger logger= Logger.getLogger("org.bonitasoft");
  36. String module = null;
  37.  
  38. //inputs
  39. Map<String, String> dbParms = new HashMap<String, String>();
  40. Long docID;
  41.  
  42. //outputs
  43. Boolean success;
  44. List<String> listOutput = new ArrayList<String>();
  45.  
  46. //locals
  47. Boolean debug = false;
  48.  
  49. //set basics for db
  50. Connection con = null;
  51. PreparedStatement pst = null;
  52. ResultSet rs = null;
  53. String sql2 = null;
  54. String dbTableDOM = null;
  55. String dbTableTSK = null;
  56. Boolean firstReturnSet = false;
  57.  
  58. String tble = "xxxxdocuments";
  59.  
  60. String title = null;
  61.  
  62. @SuppressWarnings("unchecked")
  63. @Override
  64. protected void executeBusinessLogic() throws ConnectorException{
  65. //Get access to the connector input parameters
  66. //getDbParms();
  67.  
  68. getClassName();
  69.  
  70. dbParms = getDbParms();
  71. docID = getDocID();
  72.  
  73. if(debug){dumpDataInputs();}
  74. initializeOutputs();
  75.  
  76. //TODO execute your business logic here
  77.  
  78. try {
  79. Class.forName("org.postgresql.Driver");
  80. if(debug){logger.severe(module+": url: "+url);}
  81. con = DriverManager.getConnection(url, dbParms.get("dbUser"),dbParms.get("dbPassword"));
  82. if(debug){logger.severe(module+": con: "+con);}
  83.  
  84. PreparedStatement ps = con.prepareStatement("INSERT INTO " + tble
  85. + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  86.  
  87. // document stuff
  88.  
  89. ProcessRuntimeAPI processRuntimeAPI = apiAccessor.getProcessAPI();
  90.  
  91. Document d1 = apiAccessor.getProcessAPI().getDocument(docID);
  92. String xxxxdocumentname = d1.getContentFileName();
  93. String mimeType = d1.getContentMimeType();
  94. // use fileName if title is empty
  95. title = (title==null||title.isEmpty()) ? d1.getContentFileName():title;
  96. final byte[] contentByte = ((DocumentAPI) processRuntimeAPI).getDocumentContent(d1.getContentStorageId());
  97. final ByteArrayInputStream xxxxdocument = new ByteArrayInputStream(contentByte);
  98. int xxxxdoclength = contentByte.length;
  99.  
  100. if(debug){logger.severe(module+"@String xxxxdocumentname "+xxxxdocumentname);}
  101. if(debug){logger.severe(module+"@String mimeType "+mimeType);}
  102. if(debug){logger.severe(module+"@title "+title);}
  103.  
  104. Calendar calendar = Calendar.getInstance();
  105. Timestamp date = new java.sql.Timestamp(calendar.getTime().getTime());
  106.  
  107. // calendar.set(Calendar.YEAR, 2015);
  108. // calendar.set(Calendar.MONTH, 1);
  109. // calendar.set(Calendar.DAY_OF_MONTH, 1);
  110. // Date date = new Date();
  111.  
  112. //1 xxxxdocumentid
  113. //2 xxxxyyyyydocumentname
  114. //3 xxxxdocumentname
  115. //4 xxxxversionmajor
  116. //5 xxxxversionminor
  117. //6 xxxxversionmini
  118. //7 uploader
  119. //8 approver
  120. //9 uploaddate
  121. //10 approvaldate
  122. //11 xxxxstatus
  123. //12 xxxxdocument
  124.  
  125. ps.setLong(1, 0);
  126. ps.setString(2, removeExtension(xxxxdocumentname.replace("_", " "))); // remove
  127. ps.setString(3, xxxxdocumentname);
  128. ps.setLong(4, 0);
  129. ps.setLong(5, 0);
  130. ps.setLong(6, 0);
  131. ps.setString(7, "Uploader");
  132. ps.setString(8, "Approver");
  133. ps.setTimestamp(9, date);
  134. ps.setTimestamp(10, date);
  135. ps.setString(11, "Pending");
  136. ps.setBinaryStream(12, xxxxdocument, xxxxdoclength);
  137. ps.executeUpdate();
  138.  
  139. ps.close();
  140. con.close();
  141.  
  142. } catch (ClassNotFoundException e) {
  143. logger.info("After Load Class: ClassNotFoundException");
  144. } catch (SQLException eSQL) {
  145. // TODO Auto-generated catch block
  146. eSQL.printStackTrace();
  147. } catch (DocumentNotFoundException eDoc) {
  148. // TODO Auto-generated catch block
  149. eDoc.printStackTrace();
  150. }
  151.  
  152. //WARNING : Set the output of the connector execution. If outputs are not set, connector fails
  153. dumpDataOutputs();
  154. setSuccess(success);
  155.  
  156. }
  157.  
  158. private void getClassName(){
  159. Class<?> enclosingClass = getClass().getEnclosingClass();
  160. if (enclosingClass != null) {
  161. module = enclosingClass.getName();
  162. } else {
  163. module = getClass().getName();
  164. }
  165. }
  166.  
  167. private void dumpDataInputs(){
  168. if(debug){logger.info(module+": Start");
  169. logger.info(module+": Starting: getDbParms: "+getDbParms());
  170. logger.info(module+": Starting: docID: "+docID.toString());}
  171. }
  172.  
  173. private void dumpDataOutputs(){
  174. if(debug){logger.info(module+": Ending: success: "+success);
  175. logger.info(module+": End");}
  176. }
  177.  
  178. private void initializeOutputs(){
  179. //set default outputs
  180. success = true;
  181. }
  182.  
  183. private static String removeExtension(String s) {
  184.  
  185. String separator = System.getProperty("file.separator");
  186. String filename;
  187.  
  188. // Remove the path upto the filename.
  189. int lastSeparatorIndex = s.lastIndexOf(separator);
  190. if (lastSeparatorIndex == -1) {
  191. filename = s;
  192. } else {
  193. filename = s.substring(lastSeparatorIndex + 1);
  194. }
  195.  
  196. // Remove the extension.
  197. int extensionIndex = filename.lastIndexOf(".");
  198. if (extensionIndex == -1)
  199. return filename;
  200.  
  201. return filename.substring(0, extensionIndex);
  202. }
  203.  
  204. @Override
  205. public void connect() throws ConnectorException{
  206. //[Optional] Open a connection to remote server
  207.  
  208. }
  209.  
  210. @Override
  211. public void disconnect() throws ConnectorException{
  212. //[Optional] Close connection to remote server
  213.  
  214. }
  215.  
  216. }

Hope it helps :)

Submitted by megha on Tue, 03/28/2017 - 08:33

Thank you so much for taking the time to clarify my doubts regarding the entire process. This is really helpful
Though, I still have last couple of clarifications -

  • If I am facilitating the upload of the documents via a script in the operations tab; I can do all tasks such as connecting to DB and actually uploading docs through. Do I still need a connector configuration?

  • How does the information go back to the operations tab by clicking the Submit Button - does this need to be mentioned somewhere in the properties of the button?

  • How can I give positive/ negative feedback to the user if the actual upload succeeded or not?

Thanks a lot!

Notifications