Monday, October 18, 2010

File attachment using Adf faces, EJB3 and Oracle XE database Part III


Step III: Creating taskflow for file-uploading popup
Design the list of employees like following

List of employees page

Here the “New” button will open new tab for creating “Person”, “Refresh” button simply executes the "Execute" function and “Detail” will open new table populating selected record data for editing.
The edit/new person screen should be like follwing

add/edit person screen

In add/edit person screen, "Save" button updates or inserts new record into the database, close button will close the tab and "Attachment" button opens the popup for file attachment.
Lets design the taskflow for fileAttachment
Create the new taskflow for Attachment
Click on overview and select General, uncheck “Use Page fragements”

Click on Parameters and define input parameter and return parameter as follows.

Create a new java file, and in Managed Beans, provide that file path and name here as

Add a page and a return activity in your taskflow as

Designing Attachment form
Create attachment page and design it as follows

Add a table and buttons as above, Select the value for table and row selection as single.

Bind table with backingBean property.

Creating File brows popup

Create popup and inside popup add dialog

Add ActionListener for Save File button

public void commitUpload(ActionEvent actionEvent) {
  List attachmentList = (List)  AdfFacesContext.getCurrentInstance().getPageFlowScope().get("attachmentList");
  if (attachmentList == null)
    attachmentList = new ArrayList();
  System.out.println("commit upload");
  AdfFacesContext.getCurrentInstance().getPageFlowScope().put("attachmentList", attachmentList);

/** Hide upload method **/
 private void hideUploadWindow() {
   FacesContext context = FacesContext.getCurrentInstance();
   ExtendedRenderKitService service = Service.getRenderKitService(context, ExtendedRenderKitService.class);
   //callling javascript function
   service.addScript(context, "hidePopup();");

Add ActionListener for Cancel button

public void cancelUpload(ActionEvent actionEvent) {

Add valueChangeListener and BindingFor Input file

private RichInputFile inputFile;
private Attachment acchmentEntity1;

public void setInputFile(RichInputFile inputFile) {
  this.inputFile = inputFile;
public RichInputFile getInputFile() {
  return inputFile;
public void onUploading(ValueChangeEvent valueChangeEvent) {

  UploadedFile file = (UploadedFile)valueChangeEvent.getNewValue();
  String fileName = file.getFilename();
  BlobDomain blobDomain = createBlobDomain(file);
  byte[] dataContent = blobDomain.getStorageByteArray();
  acchmentEntity1 = new Attachment();

private BlobDomain createBlobDomain(UploadedFile file) {
    InputStream in = null;
    BlobDomain blobDomain = null;
    OutputStream out = null;
    try {
        in = file.getInputStream();

        blobDomain = new BlobDomain();
        out = blobDomain.getBinaryOutputStream();
        byte[] buffer = new byte[8192];
        int bytesRead = 0;

        while ((bytesRead =, 0, 8192)) != -1) {
            out.write(buffer, 0, bytesRead);
    } catch (IOException e) {
    } catch (SQLException e) {
    } finally {
        try {
        } catch (IOException e) {
    return blobDomain;

Add javascript function, and in form tag provide usesUpload="true", here in my case "uploadWindow" is the name of my popup

Here is the code for my popup
 Click on Design view, click on "Attach File" button provide actionListener for button and add showPopupBehaviour inside the button

Inside Attach file button add showPopupBehaviour provide your popupId in popupId attribute. The code for your button should looks like as above.
  Implement actionListener as below

public void openPopup(ActionEvent actionEvent) {

Call the return activity on "Done" button for returing from the taskflow
  Now we need to call attachment-task-flow from edit person page, open person-detail-task-flow and design it as follows

Provide the input parameter attachmentList from page flow,
we will next set this value from backingBean of person-detail-task-flow
Provide Run as dialog true, and display type as Inline-popup
 Calling attachment-task-flow from edit person page
Open person-detail-task-flow, and open the page make following modifications in "Attachment" button
Provide Action method and returnListener method in edit-person-task-flow manageBean as

/** **/
public String launchAttachment() {
  Person person = (Person)getCurrentRowDataProvider("findPersonByIdIterator");
  getPageFlowSceop().put("attachmentList_", person.getAttachments());
  return "to_attachment";

public void handleReturn(ReturnEvent returnEvent) {
  Map m = returnEvent.getReturnParameters();
  List attachmentList = (List)m.get("attachmentList");
  Person person = (Person)getCurrentRowDataProvider("findPersonByIdIterator");

 * Reference
protected BindingContainer getBindings() {
  return (BindingContainer)JSFUtils.resolveExpression("#{bindings}");

protected Object getCurrentRowDataProvider(String iteratorName) {
  BindingContainer bindings = getBindings();
  DCIteratorBinding dcib = (DCIteratorBinding)bindings.get(iteratorName);
  RowSetIterator iter = dcib.getRowSetIterator();
  DCDataRow row = (DCDataRow)iter.getCurrentRow();
  return row.getDataProvider();

Bindings of person_detail should looks like above.

And for the save button in person edit screen it should be binded with datacontrol method "savePerson"
  Running the file attachment use case followings are the snaps for file uploading and saving it into database


Saving person will also save the attachments which are associated with it.
 Now when you click on Save button in add/edit person the attachment also get save into the database. You can also add more files to the person. The functionality of file attachment is completed

The other functionalities like downloading file, deleting file will come on next section.
To be continue -->
Comming up next download and deleting associated files

No comments:

Post a Comment