FileUpload: Adding an extra parameter to fileUploadListener
frer Sep 3, 2009 9:46 AMHi,
I'm encountering an issue. My application contains N languages (I don't know in advance and it can change dynamically). In my administration I therefore want a form that allows me to enter an input field for each locale. I was able to get this working for h:inputText for example but not for a rich:fileUpload.
Here is the code for the h:inputText (that works)
<a4j:repeat id="item-itemName-locale-repeat" var="localeContext" value="#{localizationContext.locales}"> <div class="attribute" id="item-itemName"> <h:outputLabel id="item-itemName-label" class="label-locale" for="item-itemName-value" value="#{localeContext.getDisplayName(locale)}" /> <h:inputText value="#{formAction.entity.getLocalization(localeContext).itemName}" id="item-itemName-value" required="false"> <rich:ajaxValidator event="onblur"/> </h:inputText> </h:outputPanel> </div> <rich:message for="item-itemName-value" styleClass="message" infoClass="success" errorClass="error" warnClass="warning"/> </a4j:repeat>
As you can see, I iterate on the locales and have an input text for each of them. The method being set is the formAction.entity.getLocalization(localeContext).itemName. This works fine.
But how can I do this with the fileUpload knowing I cannot pass any parameters to the fileUploadListener (or at least I don't know how)?
Here is what I would have done:
<a4j:repeat id="productPriority-priorityCodeImage-locale-repeat" var="localeContext" value="#{localizationContext.locales}"> <div class="attribute" id="productPriority-priorityCodeImage"> <h:outputLabel id="productPriority-priorityCodeImage-label" class="label-locale" for="productPriority-priorityCodeImage-value" value="#{localeContext.getDisplayName(locale)}" /> <s:div id="productPriority-priorityCodeImage-value-container"> <rich:fileUpload id="productPriority-priorityCodeImage-value-upload" fileUploadListener="#{formAction.uploadPriorityCodeImage(event, locale)}" maxFilesQuantity="1" immediateUpload="true" immediate="true" acceptedTypes="jpg, gif, png, bmp" addControlLabel="#{messages['org.mdarad.framework.resources.list.add']} PriorityCodeImage" cancelEntryControlLabel="#{messages['org.mdarad.framework.resources.list.remove']} PriorityCodeImage" rendered="#{formAction.entity.getLocalization(locale).priorityCodeImage == null}"> <a4j:support event="onuploadcomplete" ajaxSingle="true" reRender="productPriority-priorityCodeImage-value-container" /> </rich:fileUpload> <h:panelGrid columns="2"> <s:graphicImage id="productPriority-priorityCodeImage-value-preview" value="#{formAction.entity.getLocalization(locale).priorityCodeImage.content}" style="width:48px"/> <a4j:commandLink id="productPriority-priorityCodeImage-value-remove" action="#{formAction.clearPriorityCodeImage(locale)}" value="#{messages['org.mdarad.framework.resources.list.remove']} PriorityCodeImage" rendered="#{formAction.entity.getLocalization(locale).priorityCodeImage != null}" reRender="productPriority-priorityCodeImage-value-container"/> </h:panelGrid> </s:div> </div> <rich:message for="productPriority-priorityCodeImage-value" styleClass="message" infoClass="success" errorClass="error" warnClass="warning"/> </a4j:repeat>
and here is my backing beam method called by the fileUploadListener:
public void uploadPriorityCodeImage(UploadEvent event, Locale locale) throws Exception { UploadItem item = event.getUploadItem(); //Get data from temporary created file File file = item.getFile(); byte[] data = new byte[(int) file.length()]; FileInputStream fileInputStream = new FileInputStream(file); fileInputStream.read(data); //Now update object org.dataisland.primitives.datatype.Blob blob = new org.dataisland.primitives.datatype.Blob(); ((ProductPriorityLocalization) getEntity().getLocalization(locale)).setPriorityCodeImage(blob); ((ProductPriorityLocalization) getEntity().getLocalization(locale)).getPriorityCodeImage().setContent(data); ((ProductPriorityLocalization) getEntity().getLocalization(locale)).getPriorityCodeImage().setContentType(item.getContentType()); ((ProductPriorityLocalization) getEntity().getLocalization(locale)).getPriorityCodeImage().setContentFileName(item.getFileName()); }
Unfortunately, I get a NPE when entering the method because the event seems to be null.... is there any way I can pass an extra parameter to the method (my locale)? Otherwise what other options do I have?
THank you for your help,
Francois