title: How can I use RequestFactory to create an object and initialize a collection whithin it with objects retrieved from another ReqFactory?
body: <p>I am struggling with an issue using RequestFactory in GWT.
  I have a User object : this object has login and password fields and other fields which are of collection type.</p>
  
  <pre><code>public class User {
      private String login;
      private String password;
      private Set&lt;Ressource&gt; ressources;
  
      // Getters and setters removed for brievety
  } 
  </code></pre>
  
  <p>I need to persist this object in db so I used RequestFactory because it seems like a CRUD-type operation to me.</p>
  
  <p>Now for the RequestFactory part of the code, this is how I have tried to do it :</p>
  
  <ol>
  <li><p>I create a UserRequestContext object to create a request object for the new User. Which gives something like :</p>
  
  <pre><code>public interface MyRequestFactory extends RequestFactory {
      UserRequestContext userRequestContext();
      RessourceRequestContext ressourceRequestContext();
  }
  </code></pre>
  
  <p>and to create the user object I have something like this : </p>
  
  <pre><code>public class UserAddScreen extends Composite {
      private UserProxy userProxy;
      EventBus eventBus = new SimpleEventBus();
      MyRequestFactory requestFactory = GWT.create(MyRequestFactory.class);
      ...
  
      public UserAddScreen() {
          ...
          requestFactory.initialize(eventBus);
      }
  
      public showUserAddScreen() {
          // Textbox for password and login
          // Listbox for ressources
      }
  
  }
  </code></pre></li>
  </ol>
  
  <p>I have tried to implement it as a wizard. So at the beginning of the UserAddScreen, I have a 
  a userProxy object.
  This object fields are initialized at each step of the wizard :</p>
  
  <ul>
  <li>the first step is adding the login and password</li>
  <li>the second step is adding ressources to the userProxy object.</li>
  </ul>
  
  <p>for this last step, I have two list boxes the first one containing the list of all the ressources i have in my DB table Ressources that I got from RessourceRequestContext.getAllRessource (I have a loop to display them as listbox item with the RessourceId as the value) and the second allows me to add the selected Ressources from this first listbox. Here is the first listbox :</p>
  
  <pre><code>    final ListBox userRessourcesListBox = new ListBox(true);
      Receiver&lt;List&lt;RessourceProxy&gt;&gt; receiver = new Receiver&lt;List&lt;RessourceProxy&gt;&gt;() {
  
          @Override
          public void onSuccess(List&lt;RessourceProxy&gt; response) {
              for(RessourceProxy ressourceProxy : response) {
                  ressourcesListBox.addItem(ressourceProxy.getNom() + " " + ressourceProxy.getPrenom(), String.valueOf(ressourceProxy.getId()));
              }
          }
      };
      RessourceRequestContext request = requestFactory.ressourceRequestContext();
      request.getAllRessource().fire(receiver);
  </code></pre>
  
  <p>So, as you can see, my code loops over the retrieved proxies from DB and initializes the items within the listbox.</p>
  
  <p>Here are the control buttons :</p>
  
  <pre><code>final Button addButton = new Button("&gt;");
      addButton.addClickHandler(new ClickHandler() {
  
          public void onClick(ClickEvent event) {
              for (int i = 0; i &lt; ressourcesListBox.getItemCount(); i++) {
                  boolean foundInUserRessources = false;
                  if (ressourcesListBox.isItemSelected(i)) {
                      for (int j = 0; j &lt; userRessourcesListBox
                              .getItemCount(); j++) {
                          if (ressourcesListBox.getValue(i).equals(
                                  userRessourcesListBox.getValue(j)))
                              foundInUserRessources = true;
                      }
  
                      if (foundInUserRessources == false)
                          userRessourcesListBox.addItem(ressourcesListBox
                                  .getItemText(i), ressourcesListBox
                                  .getValue(i));
                  }
              }
          }
      });
  </code></pre>
  
  <p>So when somebody selects one or more users and click on a ">" button, all the selected items go to the second listbox which is named userRessourceListBox</p>
  
  <pre><code>userRessourcesListBox.setWidth("350px");
      userRessourcesListBox.setHeight("180px");
  </code></pre>
  
  <p>After that, I have a FINISH button, which loops over the items in the second listbox (which are the ones I have selected from the first one) and I try to make a request (again) with RequestFactory to retrieve the ressourceProxy object and initialize the userProxy ressources collection with the result</p>
  
  <pre><code>final Button nextButton = new Button("Finish");
  
      nextButton.addClickHandler(new ClickHandler() {
  
          public void onClick(ClickEvent event) {
              RessourceRequestContext request = requestFactory.ressourceRequestContext();
              for(int i = 0; i &lt; userRessourcesListBox.getItemCount(); i++) {
                  Receiver&lt;RessourceProxy&gt; receiver = new Receiver&lt;RessourceProxy&gt;() {
                      @Override
                      public void onSuccess(RessourceProxy response) {
                          userProxy.getRessource().add(response);
                      }
                  };
                  request.find(Long.parseLong(userRessourcesListBox.getValue(i))).fire(receiver);
              }
                creationRequest.save(newUserProxy).fire(new Receiver&lt;Void&gt;() {
  
                  @Override
                  public void onSuccess(Void response) {
                      Window.alert("Saved");
                  }
              });
          }
  
      });
  </code></pre>
  
  <p>Finally, (in the code above) I try to save the UserProxy object (with the initial request context I have created userProxy with)... but it doesn't work</p>
  
  <pre><code>creationRequest.save(newUserProxy).fire(...)
  </code></pre>
  
  <p>It seems like when looping over the result in the onSuccess method :</p>
  
  <pre><code>userProxy.getRessource().add(response);
  </code></pre>
  
  <p>I retrieve the response (of type RessourceProxy) but beyond this method, for example when I try to save the userProxy object AFTER THE LOOP, there are no RessourceProxy objects in the ressourceProxy collection of userProxy...</p>
  
  <p>Have you guys ever experienced something like this ?
  Perhaps I am not doing it right : do I have to get the ressource with the UserRequestContext ? so that my newUser object and ressources are managed by the same request Context ?
  if yes then I think it's a little bit weird to have something mixed together : I mean what is the benefit of having a Ressource-related operation in the User-related request context.</p>
  
  <p>any help would be really really ... and I mean really appreciated ;-)
  Thanks a lot</p>