0

I am trying to find a way to populate a map from multiple p:selectManyCheckbox. However, when i go look at the selected data in the map I get an array of Object array of Attribute which I cant access.

<p:dataList id="serviceCatalogueCriteria" var="category" value="#{serviceCatalogueController.categories}">
<ui:fragment
    rendered="#{categoryService.isMultipleSelect(category)}">
    <p:selectManyCheckbox
            value="#{serviceCatalogueController.categoryToAttributes[category]}"
            layout="pageDirection" columns="1"
            converter="#{attributeConverter}">
            <f:selectItems value="#{category.attributes.toArray()}"
                var="attribute" itemLabel="#{attribute.name}"
                itemValue="#{attribute}" />
    </p:selectManyCheckbox>
</ui:fragment>
</p:dataList>

In the back bean I have

private Map<Category, List<Attribute>> categoryToAttributes = new HashMap<Category,List<Attribute>>();

for (Category cat : categoryToAttributes.keySet()) {
        for (Attribute attr : categoryToAttributes.get(cat)) {
            finalList.add(attributeDAO.fetchAttributeWithCategoryAndName(cat.getInternalName(), attr));
        }
    }

When I run through this I get a nice

[Ljava.lang.Object; cannot be cast to java.util.List

Blows up at categoryToAttributes.get(cat). Cant get the object I also tried using a converter but I get pretty much the same thing.

The converter looks like

@Override
public Object getAsObject(FacesContext context, UIComponent component,
        String value) {
    if (value == null || value.length() == 0)
        return null;
    try {
        Long id = Long.decode(value);
        return attributeDAO.fetchAttributesWithIds(Collections.singletonList(id)).get(0);
    } catch (NumberFormatException e) {
        return null;
    }
}

@Override
public String getAsString(FacesContext context, UIComponent component,
        Object value) {
    if (value == null || !(value instanceof Attribute))
        return null;
    return "" + ((Attribute)value).getId();
}

If I remove the converter I get this

java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.util.List
allegjdm93
  • 592
  • 6
  • 24
  • `categoryToAttributes.get(cat)` returns the *value* belonging to the specified *key* (`cat`). Your `Map`'s value is of type `List`, which you cannot assign to a `String` (`attr` in your case)... – jp-jee Feb 02 '15 at 17:05
  • @jp-jee i has previously tried it with a converter but it didnt work either. with the converter i get an object array which lists the right values with type but i cant access. without the converter i get an object array with string element which i cant access either – allegjdm93 Feb 02 '15 at 18:14
  • What you experienced without the converter is different from what you're experiencing with it. You *need* the converter, at a very basic level. Post enough code to help us establish the connection between your view code and backing bean code. As it stands, I don't see the connection between `value="#{serviceCatalogueController.categoryToAttributes[category]` and what you've shown of your backing bean code – kolossus Feb 03 '15 at 03:50

1 Answers1

1

excuse me by my peer English, i hope can help you.

At *.xhtml

<p:dataList id="serviceCatalogueCriteria" var="category" 
value="#{serviceCatalogueController.categories}">

<ui:fragment
rendered="#{categoryService.isMultipleSelect(category)}">
<p:selectManyCheckbox
        value="#{serviceCatalogueController.attributesByCategory}"
        layout="pageDirection" columns="1"
        converter="#{attributeConverter}">
        <f:selectItems value="#{category.attributes.toArray()}"
            var="attribute" itemLabel="#{attribute.name}"
            itemValue="#{attribute}" />
<p:ajax listener="#{alta.almacenarModulos(category)}"/>
</p:selectManyCheckbox>
</ui:fragment>
</p:dataList>

And at your Bean:

private Map<Category, List<Attribute>> attributesMap=new HashMap<>();
private List<Attribute> attributesByCategory;
public void almacenarModulos(Categorycategory)
   {
     attributesMap.put(category,attributesByCategory);
   }

I do some like this by a scholar proyect.