2 Replies Latest reply on May 21, 2012 11:16 AM by Marc Marco

    how to add programmatically RichFaces a4j:attachQueue ?

    Marc Marco Newbie

      hey,

       

      I tried so hard but I cannot get a simple Richfaces a4j:attachQueue programmatically inserted in my page. I generate the RichFaces components from Java

      code, not xhtml.

       

      I have some problems with the output of the data. Every request should be queued and fired after 2 seconds.If there are more than one request, only the

      last request should be fired, the other request should be queued and destroyed

      <a4j:queue requestDelay="2000" ignoreDupResponses="true"></a4j:queue>

       

      I want that, if the User selects an item of the DropDownMenu 1 ( see method  getSelectOneMenu1() ) and than select an item of the DropDownMenu 2

      ( see method  getSelectOneMenu2() ), the first request should be queued and destroyed, RichFace should show only the data of the last request.

      For example , the user select Quinie of the DropDownMenu 1  and then selects Pierrette of the DropDownMenu 2, only Pierrette should be showed ,

      not Quinie and then after 2 seconds Pierrette. At the moment i receive two names from the same queue  Quinie and after 2 seconds Pierrette.

      That is not what i,ve expected, because i,ve implemented UIAttachQueue and uiAttachQueue.setRequestGroupingId("mygroupingId");

      My big problem ,i don,t know how to attach programmatically a4j:attachQueue to a4j:ajax as child .I want to generate programmatically something

      like this, with java not with xhml.

       

                    <h:selectOneMenu value="#{queueBean.username}">

                             <f:selectItem itemValue="Quinie" itemLabel="Quinie" />

                             <f:selectItem itemValue="William" itemLabel="William" />

                             <f:selectItem itemValue="Aron" itemLabel="Aron" />

                             <f:selectItem itemValue="Faith" itemLabel="Faith" />

                             <f:selectItem itemValue="Divine" itemLabel="Divine" />                                             

                             <a4j:ajax event="change" execute="@this"   

                             listener="#{queueBean.selectComponents}" render="rep">

                               <a4j:attachQueue requestGroupingId="group" name="myQueue"/>

                             </a4j:ajax>                                                                  

                      </h:selectOneMenu> 

       

      Please thake a look of my Java code .

       

      Any help will be appreciated!

       

      cheers,

       

       

      Marco

       

       

      That's what I do:

       

      queuedemo.xhtml

       

       

      <h2>JSF and RichFaces Queue</h2>

       

          <a4j:queue id="myQueue" name="myQueue" requestDelay="2000" ignoreDupResponses="true"></a4j:queue>

       

          <h:form id="form_id">      

       

              <rich:panel>

       

                <f:facet name="header">

       

                    Programmatic RichFaces components

       

                  </f:facet>

       

                  <h:panelGrid binding="#{queueBean.panelGrid}" ></h:panelGrid>

       

                  <rich:panel>        

       

                      <a4j:outputPanel id="outputPanel_id" ajaxRendered="false" layout="block">

       

                      <h:outputText binding="#{queueBean.outputText}" />

       

                  </a4j:outputPanel>         

       

                  </rich:panel>

       

              </rich:panel>     

       

      </h:form>

       

       

       

      Here is my ManagedBean

       

       

       

      @ManagedBean(name = "queueBean")

      @RequestScoped

      public class MyQueueBean {

          private String            username;

          private String            firstname;

          private String            lastname;

          private List<String>    renderComponents;

          private HtmlPanelGrid    panelGrid    = null;

          private HtmlOutputText    outputText;

       

          @PostConstruct

          public void init() {

              this.renderComponents = new ArrayList<String>();

          }

       

          private Application    application    = null;

       

          private Application getApplication() {

              if (null == this.application) {

                  this.application = FacesContext.getCurrentInstance().getApplication();

              }

              return this.application;

          }

       

          @SuppressWarnings("unchecked")

          private <T> T createComponent(String componentType) {

              T component = (T) getApplication().createComponent(componentType);

              return component;

          }

       

          private ELContext getELContext() {

              return FacesContext.getCurrentInstance().getELContext();

          }

       

          private ExpressionFactory getExpressionFactory() {

              return getApplication().getExpressionFactory();

          }

       

          private ValueExpression createValueExpression(String binding, Class<?> expectedType) {

              return getExpressionFactory().createValueExpression(getELContext(), binding, expectedType);

          }

       

          public AjaxBehavior createAjaxBehavior(String expression, List<String> rederList, List<String> executeList) {

              AjaxBehavior ajaxBehavior = (AjaxBehavior) getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);

              if (StringUtils.isNotEmpty(expression)) {

                  ajaxBehavior.addAjaxBehaviorListener(createAjaxBehaviorListener(expression));

              }

              if (CollectionUtils.isNotEmpty(executeList)) {

                  ajaxBehavior.setExecute(executeList);

              }

              if (CollectionUtils.isNotEmpty(rederList)) {

                  ajaxBehavior.setRender(rederList);

              }

              return ajaxBehavior;

          }

       

          private MethodExpression createMethodExpression(String expression, Class<?> expectedReturnType,

                  Class<?>[] expectedParamTypes) {

              return getExpressionFactory().createMethodExpression(getELContext(), expression, expectedReturnType,

                      expectedParamTypes);

          }

       

          private MethodExpressionAjaxBehaviorListener createAjaxBehaviorListener(String expression) {

              return new MethodExpressionAjaxBehaviorListener(createMethodExpression(expression, Void.class,

                      new Class[] { AjaxBehaviorEvent.class }));

          }

       

          public HtmlPanelGrid getPanelGrid() {

              if (null == this.panelGrid) {

                  this.panelGrid = createComponent(HtmlPanelGrid.COMPONENT_TYPE);

                  this.panelGrid.setId("panel_id");

                  this.panelGrid.setColumns(3);

                  this.panelGrid.getChildren().add(getSelectOneMenu1());

                  this.panelGrid.getChildren().add(getSelectOneMenu2());

                  this.panelGrid.getChildren().add(getSelectOneMenu3());

              }

              return this.panelGrid;

          }

       

          public void setPanelGrid(HtmlPanelGrid panelGrid) {

              this.panelGrid = panelGrid;

          }

       

          private UIAttachQueue createUIAttachQueue() {

              UIAttachQueue uiAttachQueue = createComponent(UIAttachQueue.COMPONENT_TYPE);

              uiAttachQueue.setName("myQueue");

              uiAttachQueue.setRequestGroupingId("mygroupingId");

              return uiAttachQueue;

          }

       

          private HtmlSelectOneMenu getSelectOneMenu1() {

              HtmlSelectOneMenu selectOneMenu1 = createComponent(HtmlSelectOneMenu.COMPONENT_TYPE);

              selectOneMenu1.setId("select_1");

              UISelectItems selectItems = createUISelectItemsComponent(createFirstItems());

              selectOneMenu1.getChildren().add(selectItems);

              selectOneMenu1.setValueExpression("value", createValueExpression("#{queueBean.username}", String.class));

              UIAttachQueue uiAttachQueue = createUIAttachQueue();

              uiAttachQueue.setId("uiqueueId1");

              List<String> idsToBeRendered = new ArrayList<String>();

              idsToBeRendered.add("outputPanel_id");

              AjaxBehavior ajaxBehavior = createAjaxBehavior("#{queueBean.selectComponents}", idsToBeRendered,

                      Arrays.asList("@this"));

              ajaxBehavior.setQueueId(uiAttachQueue.getName());

              uiAttachQueue.associateWith(ajaxBehavior);

              selectOneMenu1.addClientBehavior("change", ajaxBehavior);

              uiAttachQueue.associateWith(selectOneMenu1);

              return selectOneMenu1;

          }

       

          private HtmlSelectOneMenu getSelectOneMenu2() {

              HtmlSelectOneMenu selectOneMenu2 = createComponent(HtmlSelectOneMenu.COMPONENT_TYPE);

              selectOneMenu2.setId("select_2");

              UISelectItems selectItems = createUISelectItemsComponent(createSecondItems());

              selectOneMenu2.getChildren().add(selectItems);

              selectOneMenu2.setValueExpression("value", createValueExpression("#{queueBean.username}", String.class));

              UIAttachQueue uiAttachQueue = createUIAttachQueue();

              uiAttachQueue.setId("uiqueueId2");

              List<String> idsToBeRendered = new ArrayList<String>();

              idsToBeRendered.add("outputPanel_id");

              AjaxBehavior ajaxBehavior = createAjaxBehavior("#{queueBean.selectComponents}", idsToBeRendered,

                      Arrays.asList("@this"));

              ajaxBehavior.setQueueId(uiAttachQueue.getName());

              uiAttachQueue.associateWith(ajaxBehavior);

              selectOneMenu2.addClientBehavior("change", ajaxBehavior);

              uiAttachQueue.associateWith(selectOneMenu2);

              return selectOneMenu2;

          }

       

          private HtmlSelectOneMenu getSelectOneMenu3() {

              HtmlSelectOneMenu selectOneMenu3 = createComponent(HtmlSelectOneMenu.COMPONENT_TYPE);

              selectOneMenu3.setId("select_3");

              UISelectItems selectItems = createUISelectItemsComponent(createThirdItems());

              selectOneMenu3.getChildren().add(selectItems);

              selectOneMenu3.setValueExpression("value", createValueExpression("#{queueBean.username}", String.class));

              UIAttachQueue uiAttachQueue = createUIAttachQueue();

              uiAttachQueue.setId("uiqueueId3");

              List<String> idsToBeRendered = new ArrayList<String>();

              idsToBeRendered.add("outputPanel_id");

              AjaxBehavior ajaxBehavior = createAjaxBehavior("#{queueBean.selectComponents}", idsToBeRendered,

                      Arrays.asList("@this"));

              ajaxBehavior.setQueueId(uiAttachQueue.getName());

              selectOneMenu3.addClientBehavior("change", ajaxBehavior);

              uiAttachQueue.associateWith(ajaxBehavior);

              uiAttachQueue.associateWith(selectOneMenu3);

              return selectOneMenu3;

          }

       

          public HtmlOutputText getOutputText() {

              if (this.outputText == null) {

                  this.outputText = createComponent(HtmlOutputText.COMPONENT_TYPE);

                  this.outputText.setId("output_id");

                  this.outputText.setValueExpression("value",

                          createValueExpression("Selected Name : #{queueBean.username}", String.class));

              }

              return this.outputText;

          }

       

          public void setOutputText(HtmlOutputText outputText) {

              this.outputText = outputText;

          }

       

          private List<SelectItem> createFirstItems() {

              FacesContext.getCurrentInstance();

              List<SelectItem> asList = Arrays.asList(new SelectItem("Quinie", "Quinie"),

                      new SelectItem("William", "William"), new SelectItem("Aron", "Aron"));

              return asList;

          }

       

          private List<SelectItem> createSecondItems() {

              List<SelectItem> asList = Arrays.asList(new SelectItem("Pierrette", "Pierrette"), new SelectItem("Biblias",

                      "Biblias"), new SelectItem("Pauline", "Pauline"));

              return asList;

          }

       

          private List<SelectItem> createThirdItems() {

              List<SelectItem> asList = Arrays.asList(new SelectItem("Durand", "Durand"), new SelectItem("Igor", "Igor"),

                      new SelectItem("Nathalie", "Nathalie"));

              return asList;

          }

       

          private <U> UISelectItems createUISelectItemsComponent(List<U> list) {

              UISelectItems items = new UISelectItems();

              items.setId("id_" + String.valueOf(new Random().nextInt(1000)));

              items.setValue(list);

              return items;

          }

       

          public void selectComponents(AjaxBehaviorEvent event) {

              this.renderComponents.add(this.username);

          }

       

          public String getUsername() {

              return this.username;

          }

       

          public void setUsername(String username) {

              this.username = username;

          }

       

          public List<String> getRenderComponents() {

              return this.renderComponents;

          }

       

          public void setRenderComponents(List<String> renderComponents) {

              this.renderComponents = renderComponents;

          }

       

          public String getFirstname() {

              return this.firstname;

          }

       

          public void setFirstname(String firstname) {

              this.firstname = firstname;

          }

       

          public String getLastname() {

              return this.lastname;

          }

       

          public void setLastname(String lastname) {

              this.lastname = lastname;

          }

      }

        • 1. Re: how to add programmatically RichFaces a4j:attachQueue ?
          Lukáš Fryč Master

          Hi Marc,

           

          couldn't you just use disable the attachQueue when it is not needed?

          In other words, use attribute @rendered and bind to model value using EL expression?

          http://docs.jboss.org/richfaces/latest_4_X/vdldoc/a4j/attachQueue.html

           

          Another way could be adding attachQueue directly to h:select* component.

           

          Btw dynamic generation of components is very tough question (upstream JSf implementations issues).

          I have seen so many issues like this.

           

          ~ Lukas

          • 2. Re: how to add programmatically RichFaces a4j:attachQueue ?
            Marc Marco Newbie

            Hi Lukas,

             

            thanks for you answer. I can,t disable the attachQueue, because i need them.

            I, ve added attachQueue directy to h:select*

             

            private HtmlSelectOneMenu getSelectOneMenu1() {

                    HtmlSelectOneMenu selectOneMenu1 = createComponent(HtmlSelectOneMenu.COMPONENT_TYPE);

                    selectOneMenu1.setId("select_1");

                    UISelectItems selectItems = createUISelectItemsComponent(createFirstItems());

                    selectOneMenu1.getChildren().add(selectItems);

                    selectOneMenu1.setValueExpression("value", createValueExpression("#{queueBean.username}", String.class));

                    UIAttachQueue uiAttachQueue = createUIAttachQueue();

                    uiAttachQueue.setId("uiqueueId1");

                    List<String> idsToBeRendered = new ArrayList<String>();

                    idsToBeRendered.add("outputPanel_id");

                    AjaxBehavior ajaxBehavior = createAjaxBehavior("#{queueBean.selectComponents}", idsToBeRendered,

                            Arrays.asList("@this"));

                    ajaxBehavior.setQueueId(uiAttachQueue.getName());

                    uiAttachQueue.associateWith(ajaxBehavior);

                    selectOneMenu1.addClientBehavior("change", ajaxBehavior);

                    uiAttachQueue.associateWith(selectOneMenu1);

                    return selectOneMenu1;

                }

             

            Take a look please of the methods getSelectOneMenu1(), getSelectOneMenu2() and getSelectOneMenu3()

            I want to generate dynamicallly 3 HtmlSelectOneMenu components , all added attachQueue. 

            The components can be generated, but attachQueue is not attached correctly to the components. And that is my problem

             

             

            Marc