문제

I am trying to generate an export file out of some items, which should conform to some "criteria" in order to be able to be exported. The thing is: the user should select some items (by using checkboxes) and then click on the ICEFaces' OutputResource in order to export (hopefully all) the selected items.

The parts involved in this process are the following:

The OutputResource in the XHTML:

<ice:outputResource rendered="#{not myBackingBean.emptySelection}" resource="#{myBackingBean.excelResource}" label="export to Excel" shared="false" target="_self" />

The backing bean holding the resource:

@ManagedBean(name = "myBackingBean")
@ViewScoped
@WindowDisposed
public class MyBackingBean implements Serializable
{
 ...
 private ExcelResource resource;
 ...
}

And, finally, the actual resource:

...
import com.icesoft.faces.context.Resource;
...

public class ExcelResource implements Resource
{
  ...

  @Override
  public InputStream open() throws IOException
  {
    //do some selection here. If there is no valid ticket to export then
    //this method will return null, otherwise it will return an InputStream
    //and everything will work properly

    if (everythingOk)
    {
        return new ByteArrayInputStream(...);
    }

    //hopefully, it won't get to this point
    return null;
  }

As you can see, I'm implementing the com.icesoft.faces.context.Resource interface and overriding the open() method to create the Excel export "on the fly".

Now, once againg, what I want to do is to filter some of the originally selected items and, in case no item is left, navigate to some error page. If this was an h:commandButton or an ice:commandLink then I would use the action property to do it, but I cannot do this here because this is an ice:outputResource. Is there some workaround for this?. Please notice it is not enough to use the rendered property to do this because the user can select something (which will immediately render the ice:outputResource) but the selection should be filtered before exporting it.

Last but not least: I'm using Websphere 8 and ICEFaces 3 to do this.

Thanks in advance!

도움이 되었습니까?

해결책

I've managed to do what I was looking for, this time using some buttons with real actions to redirect to pages.

What I did was the following:

  • MyBackingBean has now a method to determine whether or not the selection is empty;
  • It also has a method to determine if the selection is valid (this is: not empty and without any invalid item in it);
  • There is an <ice:commandLink> rendered when the selection is invalid. This commandLink has the actual redirection to the error page.
  • The <ice:outputResource> will be rendered when the <ice:commandLink> is not rendered (i.e.: when the selection is completely valid).

And now, the code:

First, the commandLink:

<ice:commandLink rendered="#{not myBackingBean.validSelection}" 
     disabled="#{myBackingBean.emptySelection}" 
     label="download excel report}"
     action="redirect_to_error_page" />

Remember:

  • MyBackingBean.isEmptySelection() returns true if there is no item selected.
  • MyBackingBean.isValidSelection() returns true if there is at least one item selected and also each and every selected item is a valid item (this is: an item that is valid for export)

Now, the outputResource:

<ice:outputResource 
    rendered="#{myBackingBean.validSelection}" 
    resource="#{myBackingBean.excelResource}" 
    label="download excel report}" shared="false" target="_self" />

Last but not least, you may have figured out the fact that the <ice:outputResource> will now handle only valid selection (the actual redirection to the error page is being done by the <ice:commandLink>). This means there has to be a way to filter the items before passing them to the resource for the actual export. Well, in my case I decided to create a filter(...) method in the backing bean.

@ManagedBean(name = "myBackingBean")
@ViewScoped
@WindowDisposed
public class MyBackingBean implements Serializable
{
 ...
 private ExcelResource resource;
 ...

 public List<MyItems> getFilteredList(List<MyItems> allSelectedItems)
 {
  ...
  //do some selection here and return a list containing only valid items
  return validItemsList;
 }

 public ExcelResource getExcelResource()
 {
   return new ExcelResource(getFilteredList(allSelectedItems));
 }

 public boolean isEmptySelection()
 {
   //return true if the selection is EMPTY, false otherwise.
 }

 public boolean isValidSelection()
 {
   //return true if the selection is NOT EMPTY and it has
   //only VALID items in it, false otherwise.
 }
}

This way you can generate the ExcelResource "on the fly" with nothing but valid items in it. By the time the <ice:outputResource> is being rendered in the XHTML, it will contain only valid and exportable items!.

I hope someone will find this useful :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top