In some cases you like to attach documents from the activity screen instead of launching the Stardust Portal, e.g.:
http location.For example to attach a document from an interactive JSF Activity, you can perform the following steps:
<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.
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;
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 != 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 : " + file.getContentType());
documentInfo.setContentType(file.getContentType());
ByteArrayOutputStream out = new ByteArrayOutputStream();
try
{
InputStream in = this.getFile().getInputStream();
int b;
while ((b = in.read()) != -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;
}
}To attach a document from a URL:
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;
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()) != -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 : "+documents);
//ArrayList existingAttachments = new ArrayList (documentManagementService.getDocuments(paths));
attachments.addAll(documents);
attachments.add(scannedSwitchDocument);
return attachments;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
}