Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EnumConverter in primefaces editable datatable

I wrote an EnumConverter that is described in Use enum in h:selectManyCheckbox? Everything was fine until we recognize that this converter does not work properly in primefaces editable datatable. The problem is that although I added an attribute inside getAsString and getAsObject methods as following:

@Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value instanceof Enum) {
            component.getAttributes().put(ATTRIBUTE_ENUM_TYPE, value.getClass());
            return ((Enum<?>) value).name();
        } else {
            throw new ConverterException(new FacesMessage("Value is not an enum: " + value.getClass()));
        }
    }
public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Class<Enum> enumType = (Class<Enum>) component.getAttributes().get(ATTRIBUTE_ENUM_TYPE);
        try {
            return Enum.valueOf(enumType, value);
        } catch (IllegalArgumentException e) {
            throw new ConverterException(new FacesMessage("Value is not an enum of type: " + enumType));
        }
    }

In the latter method(getAsObject) could not find the attribute that I gave to the components attribute map. But out of the pprimefaces editable datatable everything is fine. Is there any solution to achieve this?

like image 521
demdem Avatar asked Nov 04 '22 15:11

demdem


1 Answers

This problem is caused because the custom component attribute was not saved in the row state of the PrimeFaces datatable (it works fine in standard h:dataTable).

We're going to need to store this information elsewhere. In the view scope along with the component ID would be one way.

In the getAsString():

context.getViewRoot().getViewMap().put(ATTRIBUTE_ENUM_TYPE + component.getId(), value.getClass());

And in the getAsObject():

Class<Enum> enumType = (Class<Enum>) context.getViewRoot().getViewMap().get(ATTRIBUTE_ENUM_TYPE + component.getId());
like image 185
BalusC Avatar answered Nov 08 '22 15:11

BalusC