1 Reply Latest reply on Jan 3, 2008 10:54 AM by jrwsampson

    convertEntity not working with a4j:support

    jrwsampson

      I have a multi-select dropdown that I want to use with a4j:support. When I add Seam's convertEntity tag the selectManyListbox doesn't bind the selected items to the value, it returns an empty array every time. Without the convertEntity tag the AJAX call sends up an array of strings with the package name and the seam id, e.g.: com.namemedia.Advertiser@19b028.

      Is there a workaround, or something I am missing? I am going to try writing a custom converter, but I am not sure what to do with the seam id... in other words how do i get the entities db id from the strings that get bound to the listbox?

      <h:outputLabel value="Please select one or more advertisers."><div />
       <h:selectManyListbox value="#{campaignSelector.selectedAdvertisers}">
       <s:selectItems value="#{campaignSelector.availableAdvertisers}" var="advertiser" label="#{advertiser.name}" />
       <a4j:support event="onchange" reRender="campaignSelector" />
       <s:convertEntity />
       </h:selectManyListbox>
       </h:outputLabel>
      




        • 1. Re: convertEntity not working with a4j:support
          jrwsampson

          I ended up defining a custom converter for the object, which appears to be doing its job correctly. The problem it still does not bind the selected items to the list in the h:selectManyListbox's value attribute. This appears to happen when I use s:convertEntity (or a custom converter) in conjunction with a4j:support. If I remove the converter, I do get a list of Strings bound to the value attribute... but this doesn't really help me. What is going on here?

          My xhtml:

           <h:selectManyListbox value="#{campaignSelector.selectedAdvertisers}" converter="genericEntityConverter">
           <s:selectItems value="#{campaignSelector.availableAdvertisers}" var="advertiser" label="#{advertiser.name}" />
           <a4j:support event="onchange" reRender="campaignSelector" />
           </h:selectManyListbox>
          


          My converter:
          //This converter can be used for entities extending ConvertibleEntity
          @Name("genericEntityConverter")
          @Scope(org.jboss.seam.ScopeType.APPLICATION)
          @org.jboss.seam.annotations.faces.Converter(id="genericEntityConverter")
          public class GenericEntityConverter implements Converter {
          
           @In
           private EntityManager entityManager;
          
           public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String entityId) throws ConverterException {
           String[] strings = entityId.split("@");
           if(strings.length == 2) {
          
           String tableName = strings[0];
           Integer id = Integer.valueOf(strings[1]);
           String query = "from " + tableName + " where id=:id";
           Object entity = (Object) entityManager.createQuery(query).setParameter("id", id).getSingleResult();
           return entity;
           } else {
           throw new ConverterException("The converter could not get the object associated with the id: " + entityId);
           }
           }
          
           public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object object) {
           ConvertibleEntity entity = (ConvertibleEntity) object;
           return entity.getEntityId();
           }
          
          
          }
          
          public abstract class ConvertibleEntity {
           public abstract String getEntityId();
           public abstract String getEntityName();
          }
          
          //example of ConvertibleEntity implementation
           @Transient
           public String getEntityId() {
           return "Advertiser@" + this.getId().toString();
           }
          
           @Transient
           public String getEntityName() {
           return "Advertiser";
           }
          
          



          I have stepped through in the debugger, and the converter's getAsObject method is being called and getting the Object from the EM... but when I get into the getAvailableCampaigns method on the action bean, the selectedAdvertisers list is still empty... what is going on here?