I am using a action listener event to upload documents on click of button. But when I click on the button it is not getting into the actionlistener
method. But once I refresh the page the event starts working.
I tried invoking the event using phaseId
as well ,but it is not working.
Let me know if I can do this in any other way.
My xhtml
:
<ace:fileEntry id="fileEntryForDocUpld" disabled="#{triggerRequestFormBean.disableDocUpload}"
absolutePath="#{triggerRequestFormBean.uploadDirectory}"
maxFileCount="10"
maxFileCountMessage="Limited to 10 files uploaded concurrantly."
fileEntryListener="#{triggerRequestFormBean.uploadDocumentListener}"
required="false" useOriginalFilename="true"
useSessionSubdir="true" immediate="true" >
</ace:fileEntry>
<h:commandButton id="upldDoc"
disabled="#{triggerRequestFormBean.disableDocUpload}"
actionListener="#{triggerRequestFormBean.docUploadListener}"
partialSubmit ="true"
value="Load" styleClass="buttonStyle"
title="Load" >
</h:commandButton>
**java class -**
public void uploadDocumentListener(final FileEntryEvent event) {
if (logger.isInfoEnabled()) {
logger.info(LOG_METHOD_ENTER);
}
if (event.getPhaseId() == PhaseId.ANY_PHASE) {
event.setPhaseId(PhaseId.INVOKE_APPLICATION);
event.queue();
}
getBaseBean().getErrorMessagesList().clear();
try {
setFile(FileUploadUtility.fileEntryUploadListener(event));
if (getFile() != null) {
String fileNameWithExtension = getFile().getName();
int endIndex = fileNameWithExtension.lastIndexOf(ConstantKeys.DOT);
String uploadedDocFormat = fileNameWithExtension.substring(
endIndex + 1, fileNameWithExtension.length()).toLowerCase();
String docFormats = factory.getMessage(ConfigKeys.EDM_SUPPORTING_FILE_FORMAT);
List<String> supportedFormatList = Arrays.asList(docFormats.split("\\s*,\\s*"));
if(!supportedFormatList.contains(uploadedDocFormat)){
getBaseBean().getErrorMessagesList().add(
messageFactory.getMessage(ERROR_SELECT_VALID_FILE_FORMAT));
} else {
setDocLocation(getFile().getName());
setPercentDoc(100);
final TriggerRequestDocumentDataBean docBean = new TriggerRequestDocumentDataBean();
docBean.setTrgrRqstDocNm(appendTimestampToFile(getFile()
.getName()));
//Throw Error incase if the document name is greater than 60 after appending timestamp
if(docBean.getTrgrRqstDocNm()!=null && docBean.getTrgrRqstDocNm().length()>60){
getBaseBean().getErrorMessagesList().add(
messageFactory.getMessage(ERROR_PLEASE_SELECT_FILE_NAME_BELOW_60CHAR));
return;
}
docBean.setTrgrRqstAttach(FileUploadUtility
.readFileAsBytes(getFile()));
final CommonBO commonBO = (CommonBO) FacesUtils
.findBean(COMMON_BO);
final Date currentDt = commonBO.getSysdate();
docBean.setCreatedByEmpNbr(getLoginUserIdAsLong());
docBean.setCreatedDt(currentDt);
docBean.setLastUpdtByEmpNbr(getLoginUserIdAsLong());
final Employee employee = commonBO
.populateEmployeeDetails(getLoginUserIdAsString());
if (employee != null) {
final StringBuilder sbEmpName = new StringBuilder(employee
.getFirstNm()).append(SPACE).append(
employee.getLastNm());
docBean.setLastUpdtBy(sbEmpName.toString());
}
docBean.setLastUpdtDt(currentDt);
docBean.setNewRecord(true);
if (CommonUtil.isListNotEmpty(getTrfDocList())) {
getTrfDocList().add(0, docBean);
} else {
getTrfDocList().add(docBean);
}
}
}
} catch (final Exception e) {
logger.error(e.getMessage(), e);
} finally {
}
if (logger.isInfoEnabled()) {
logger.info(LOG_METHOD_EXIT);
}
}
/**
* This method will be invoke on click of File Upload button from UI
*/
public String docUploadListener(ActionEvent event) {
if (event.getPhaseId() == PhaseId.ANY_PHASE) {
event.setPhaseId(PhaseId.INVOKE_APPLICATION);
event.queue();
}
if (logger.isInfoEnabled()) {
logger.info(LOG_METHOD_ENTER);
}
try {
if (getFile() == null) {
getBaseBean().getErrorMessagesList().add(
messageFactory.getMessage(ERROR_PLEASE_UPLD_DOC));
}
} catch (final Exception e) {
logger.error(e.getMessage(), e);
} finally {
}
if (logger.isInfoEnabled()) {
logger.info(LOG_METHOD_EXIT);
}
/*JavascriptContext.addJavascriptCall(FacesContext
.getCurrentInstance(), "window.location.reload();");*/
return null;
}