Handle document list (delete / add) with 7.2
Hello
I have a process with a document list. The document list is created on the instantiaction form.
In one of the human tasks a user should be able to view / add / delete his documents. I first changed formInput to a Javascript expression to get the documents from the context (and other REST API variables):
return {
"marchePublicRequestInput": $data.marchePublicRequest,
"newStatus": "resend",
"piecesJointesInput": $data.context.piecesJointes_ref
}
I also have an empty JSON object newDocuments
In the form I have a container iterating on $data.formInput.piecesJointesInput displaying links to the document and a delete $item button. I then added a container on newDocuments
with fileupload widget.
The formOutput merge the two collections:
return {
'marchePublicRequestInput': $data.formInput.marchePublicRequestInput,
'newStatus': $data.formInput.newStatus,
"piecesJointesInput":
$data.formInput.piecesJointesInput == null ?
$data.nouvellesPiecesJointes :
$data.formInput.piecesJointesInput.merge($data.nouvellesPiecesJointes)
};
In the operations I am trying to write a groovy script to handle this mix of existing / new documents but I do not really know where to start (I read this thread but this works for a single document). My groovyscript tries to iterate over the input but I don't know how to handle this exactly? Can I detect object type and create my documentValue from this?
Any hint welcome (or a better solution than this one!).
The solution I got with is the one described in the comment.
On the contract have 2 multiple inputs:
- existingDocumentsInput : COMPLEX. You need at least an id attribute (integer).
- newDocumentsInput : FILE
In the UI Designer:
Change formInput from JSON to Javascript Expression returning a JSON object. existingDocumentsInput should be context document list reference.
Add an iterating container list of links for existingDocumentsInput so user can review its documents. Link text is {{ $item.fileName }}
, link URL is "/bonita/portal/" + $item.url
and target should be _top to avoid leaving current form. Add a delete button for this $collection of the $item.
Add an iterating container list of upload file widgets for newDocumentsInput so user can add new documents. Do not forget add/delete buttons to your liking!
In the studio
Add an operation for your list of documents. You need a groovy script to merge existingDocumentsInput and newDocumentsInput. The script I wrote is:
import org.bonitasoft.engine.bpm.document.DocumentValue;
// Reconstruire la liste des pièces-jointes à partir du résultat
List<DocumentValue> documentsMerged = new ArrayList<DocumentValue>();
DocumentValue dv;
existingDocumentsInput.each { pj ->
// pj is a java.util.Map
Integer documentId = pj.get("id");
dv = new DocumentValue( documentId );
documentsMerged.add(dv);
}
newDocumentsInput.each { pj ->
// pj is a FileInputValue
dv = new DocumentValue( pj.getContent(), null, pj.getFileName() );
documentsMerged.add(dv);
}
return documentsMerged;
Hope this helps. If you have another (hopefully better) solution to handle document lists in tasks I very would like to hear of them!
Comments
Hi g.lapierre
you are alife saver. I have been trying to do so for many days but always getting the same error that you mentioned earlier.
You code seems to work for me. Thanks a lot.
here is my question that I had posted
http://community.bonitasoft.com/questions-and-answers/need-some-help-doc...
Here is another article i came across --
http://community.bonitasoft.com/questions-and-answers/how-modify-documen...
good but didnt work for me
Thanks a lot..
Comments
In fact my solution (merging two arrays) does not work as this does not pass contract validation process (existing document cannot be converted to FILE).
A solution would be to have two contract input: one for new documents and one for existing ones. That one should be a multiple complex type variable.
Then the groovy script in the operation should merge the two contract inputs to the global document list.
There should be a better way to handle a list of document...