Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Stardust/Knowledge Base/Java API/Attaching Documents

Attaching Documents to a Process via the API

There may be requirements where one needs to attach documents from the activity screen instead of launching Context portal:
• User may like to attach the document while filling in the information from the same activity screen.
• One may need to attach reports, which they download from some http location.

Attaching a Document from an interactive Application Activity, e.g. JSF Activity:

1. Edit the xhtml file and add following entry

<trh:rowLayout>
<tr:inputFile label="Attach Document: " value="#{SwitchDetailsDataEntryBean.file}" />
</trh:rowLayout>

This entry in xhtml will create a control with “Attach Document” as label, an edit control next to this that will display the path of the browsed file and a ‘Browse’ button, that opens up Open Windows Dialog box.


2. In your backing bean class add the following code:

package com.sungard.iworks.web.jsf.beans.iworkscompass.switchrequest;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.myfaces.trinidad.context.RequestContext;
import org.apache.myfaces.trinidad.model.UploadedFile;
import ag.carnot.web.jsf.common.ManagedBeanUtils;
import ag.carnot.web.jsf.common.beans.SessionContext;
import ag.carnot.web.jsf.common.structureddata.ComplexTypeWrapper;
import ag.carnot.web.jsf.processportal.beans.ActivityInstanceDialogBean;
import ag.carnot.workflow.runtime.DmsUtils;
import ag.carnot.workflow.runtime.Document;
import ag.carnot.workflow.runtime.DocumentInfo;
import ag.carnot.workflow.runtime.DocumentManagementService;
import ag.carnot.workflow.runtime.Folder;
import ag.carnot.workflow.runtime.ProcessInstance;</pre><pre>public class SwitchDetailsDataEntryBean {
private UploadedFile file; 
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public SwitchDetailsDataEntryBean(){
super();
RequestContext requestContext = RequestContext.getCurrentInstance();
requestContext.getPageFlowScope().put("carnotActivityUsesUpload", new Boolean(true));
} 
public List complete(){
System.out.println("Complete method Started");
if(file&nbsp;!= null){
try{
SessionContext sessionContext = SessionContext.findSessionContext(); 
DocumentManagementService documentManagementService = sessionContext.getServiceFactory().
getDocumentManagementService();
String fileName = this.getFile().getFilename();
DocumentInfo documentInfo = DmsUtils.createDocumentInfo(fileName);
// documentInfo.setProperties((Map)
// this.documentPropertiesWrapper.getComplexType());
System.out.println("File Type&nbsp;: " + file.getContentType());
documentInfo.setContentType(file.getContentType()); 
ByteArrayOutputStream out = new ByteArrayOutputStream(); 
try {
InputStream in = this.getFile().getInputStream();
int b;
while ((b = in.read())&nbsp;!= -1){
out.write(b);
}
out.close();
in.close();
}
catch (Exception e) {
e.printStackTrace();
} 
byte[] documentContent = out.toByteArray(); 
System.out.println("File length: " + documentContent.length); 
Document scannedSwitchDocument = documentManagementService.createDocument("/", documentInfo,
documentContent, null);
attachments.add(scannedSwitchDocument);
System.out.println("Document created."); 
return attachments;
}
catch(Exception e){
System.out.println("in catch");
e.printStackTrace();}
} 
return null;
} 
}


3. Add ‘Process Attachments’ as an OUT Data to that particular activity in the model.

Attaching a Document from a URL

1. Have an Application activity in your model, whose service method should look like as below:

package com.sungard.iworks.web.jsf.beans.iworkscompass.switchrequest; 
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List; 
import ag.carnot.web.jsf.common.beans.SessionContext;
import ag.carnot.workflow.runtime.DmsUtils;
import ag.carnot.workflow.runtime.Document;
import ag.carnot.workflow.runtime.DocumentInfo;
import ag.carnot.workflow.runtime.DocumentManagementService;</pre><pre>public class AttachReportToProcess{ 
public List attachReport(String repFileName, List documents) {
InputStream is = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try{ 
SessionContext sessionContext = SessionContext.findSessionContext(); 
DocumentManagementService documentManagementService = sessionContext.getServiceFactory().
getDocumentManagementService();
DocumentInfo documentInfo = DmsUtils.createDocumentInfo(repFileName); 
System.out.println("Downloaded Started.");
String fileURL ="http://iwks-nat-frmsvr:7778/reportspdf/" + repFileName;
URL fileUrl = new URL(fileURL);
documentInfo.setContentType("application/pdf");
//os = new BufferedOutputStream(new FileOutputStream("c:\\src\\A00004056238.pdf"));
 
// openConnection method on a URL.
URLConnection URLConn = fileUrl.openConnection();
is = URLConn.getInputStream(); 
//InputStream in = this.getFile().getInputStream();
int b;
while ((b = is.read())&nbsp;!= -1){
out.write(b);
}
out.close();
is.close(); 
byte[] documentContent = out.toByteArray(); 
System.out.println("File length: " + documentContent.length); 
Document scannedSwitchDocument = documentManagementService.createDocument("/", documentInfo, 
documentContent, null);
List attachments = new ArrayList();
List paths = new ArrayList ();
paths.add("/");
System.out.println("documents&nbsp;: "+documents);
//ArrayList existingAttachments = new ArrayList (documentManagementService.getDocuments(paths));
attachments.addAll(documents);
attachments.add(scannedSwitchDocument);
return attachments; 
 
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
 
}

2. Make sure to have the existing attachments passed in as an IN parameter to this method and also use that to add all existing attachments to the process attachment that you are going to return from this service method. Otherwise this code will attach the current doc (the report only) and deletes all existing attachments.

3. Have PROCESS_ATTACHMENTS passed as IN and OUT data to this application activity in the model.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.