primefaces-extensions / primefaces-extensions.github.com

Organization repo, only for homepage, wiki and issue tracker
https://primefaces-extensions.github.io/
68 stars 22 forks source link

Exporter breaks on complex value expressions #102

Closed rootkit007 closed 11 years ago

rootkit007 commented 11 years ago

If my dataTable contains value expressions like:

<p:column>
    <h:outputText value="#{myBean.getData(var.property)}">
    </h:outputText>
</p:column>

it will render fine on the screen but throw 'Property getData not found on myBean' exception when exporting. This is apparently caused by this code in Exporter class:

//Try to guess else { ValueExpression expr = component.getValueExpression("value"); if (expr != null) { Class<?> valueType = expr.getType(context.getELContext()); if (valueType != null) { Converter converterForType = context.getApplication().createConverter(valueType);

                    if (converterForType != null)
                        return converterForType.getAsString(context, component, value);
                }
            }
        }

on line expr.getType

ova2 commented 11 years ago

Assigned to Sudheer.

gustavocampos commented 11 years ago

Same problem here for any expression containing a method or something that is not a get/set property. I tried setting style="display: none;", but the outputText is read even if it's not shown.

Maybe data that is not visible on screen shouldn't be exported.

sudheerj commented 11 years ago

Could you please post your datatable and exporter code

gustavocampos commented 11 years ago

I'm using the default ExcelExporter (xlsx) and traced the problem to the same line as rootkit007, it happens when expr.getType() is called for a datatable component like:

h:outputText value="#{fundReturns.getFundClassName(fundReturns.navReturns[date][eCounter.index].idtLinkedTo)}"

The error happens when the code tries to get the expected type to find a converter, because getType is for properties and not for methods.

rootkit007 commented 11 years ago

I remember seeing the same discussion about PF dataExporter component. One of proposed fixes was to change that call to getExpectedType(), but I havent tried this myself

gustavocampos commented 11 years ago

I have just tried and it works, but it returned me an Object, even with the return type of my method being a String, so I don't know whether it will break other things or not.

ova2 commented 11 years ago

Hi,

We use getType() in other components too. The correct method is in ComponentsUtils.java. Sudheer, please use the method getConverter() from there. It works as follows:

public static Converter getConverter(final FacesContext fc, final UIComponent component) {
    if (!(component instanceof EditableValueHolder)) {
        return null;
    }

    Converter converter = ((EditableValueHolder) component).getConverter();
    if (converter != null) {
        return converter;
    }

    ValueExpression valueExpression = component.getValueExpression("value");
    if (valueExpression == null) {
        return null;
    }

    Class<?> converterType = valueExpression.getType(fc.getELContext());
    if (converterType == null || converterType == String.class || converterType == Object.class) {
        // no conversion is needed
        return null;
    }

    return fc.getApplication().createConverter(converterType);
}
gustavocampos commented 11 years ago

It works for me.

ova2 commented 11 years ago

Do you mean my code above with "it works for me"?

gustavocampos commented 11 years ago

Yes, I called getConverter() from ComponentUtils.java

ova2 commented 11 years ago

Good to hear. This is how it works in JSF impl. too. PF didn't have these lines

if (converterType == null || converterType == String.class || converterType == Object.class) {
    // no conversion is needed
    return null;
}

and it was the problem. Don't know about the current PF version.

So, Sudheer, please fix the Exporter asap :-)

tandraschko commented 11 years ago

The String type check isn't right in all cases because user can have string converter for trimming etc. Please just call our ComponentUtils#getConverter, i will later change it to PF ComponentUtils

ova2 commented 11 years ago

Set for 0.7.1

sudheerj commented 11 years ago

Fixed.