Thursday, December 23, 2010

ADF Fileupload - InputFile with autosubmit & valuechange listener

Issue Description: JSF page have inputFile component to upload file which return uploadFile object.
I have binded this component to backingbeans uploadFile object. Also this component has autosubmit true and value change listener method and this form has submit button at the end. I want the value changelister to validate file type and few other validations and submit button to upload document to specific location on server. The problem is the value changelister is working fine but the submit button action method was returning null for this.uploadFile object (this does not happen when I set autosubmit property to false for inputFile component).

Sol: I created inputFile and a (hidden) textfield. I binded the textfield to the uploadfile object in backing bean instead of the inputFile component. The autosubmit and valuchangelistener remains as its is I just assigned the value to this textfield the upload file object as input which I can typecast in the submit action button. sample code:

package demo.view.backing;

import demo.view.ExcelADF;

import demo.view.UnitSubmission;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.ArrayList;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import javax.faces.event.ValueChangeEvent;

import oracle.adf.view.rich.component.rich.RichDocument;
import oracle.adf.view.rich.component.rich.RichForm;
import oracle.adf.view.rich.component.rich.input.RichInputFile;
import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.nav.RichCommandButton;
import oracle.adf.view.rich.component.rich.output.RichMessages;

import oracle.adf.view.rich.context.AdfFacesContext;

import org.apache.myfaces.trinidad.context.RequestContext;
import org.apache.myfaces.trinidad.model.UploadedFile;

public class SampleFileUpload {
private RichForm f1;
private RichDocument d1;
private RichInputFile if1;
private RichCommandButton cb1;
private RichMessages m1;
private UploadedFile uploadedFile;
private UploadedFile uploadedFile1;
private String filename;
private long filesize;
private String filecontents;
private String filetype;

ArrayList dataHolder;
private RichInputText it1;


public void setF1(RichForm f1) {
this.f1 = f1;
}

public RichForm getF1() {
return f1;
}

public void setD1(RichDocument d1) {
this.d1 = d1;
}

public RichDocument getD1() {
return d1;
}


public void setIf1(RichInputFile if1) {
this.if1 = if1;
}

public RichInputFile getIf1() {
return if1;
}


public void setCb1(RichCommandButton cb1) {
this.cb1 = cb1;
}

public RichCommandButton getCb1() {
return cb1;
}

public void setM1(RichMessages m1) {
this.m1 = m1;
}

public RichMessages getM1() {
return m1;
}



public UploadedFile getUploadedFile() {
return uploadedFile;
}

public void setFilename(String filename) {
this.filename = filename;
}

public String getFilename() {
return filename;
}

public void setFilesize(long filesize) {
this.filesize = filesize;
}

public long getFilesize() {
return filesize;
}

public void setFilecontents(String filecontents) {
this.filecontents = filecontents;
}

public String getFilecontents() {
return filecontents;
}

public void setFiletype(String filetype) {
this.filetype = filetype;
}

public String getFiletype() {
return filetype;
}


public void setDataHolder(ArrayList dataHolder) {
this.dataHolder = dataHolder;
}

public ArrayList getDataHolder() {
return dataHolder;
}


public void fileUpdate(ValueChangeEvent valueChangeEvent) {
RichInputFile inputFileComponent = (RichInputFile)valueChangeEvent.getComponent();
UploadedFile newFile = (UploadedFile)valueChangeEvent.getNewValue();
newFile = (UploadedFile)valueChangeEvent.getNewValue();
it1.setValue(newFile);
// this.uploadedFile = newFile;
if (!newFile.getFilename().endsWith("xls")) {
FacesContext.getCurrentInstance().addMessage(inputFileComponent.getClientId(FacesContext.getCurrentInstance()),
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Only Excel files are allowed",
"This file (" +
newFile.getFilename() +
") is not allowed; the extension is not allowed."));
inputFileComponent.resetValue();
inputFileComponent.setValid(false);
}



}



public String SubmitAction() {
uploadedFile = (UploadedFile)it1.getValue();
System.out.println("File Name: "+uploadedFile.getFilename());
return null;
}

public void setUploadedFile1(UploadedFile uploadedFile1) {
this.uploadedFile1 = uploadedFile1;
}

public UploadedFile getUploadedFile1() {
return uploadedFile1;
}


public void setIt1(RichInputText it1) {
this.it1 = it1;
}

public RichInputText getIt1() {
return it1;
}
}