1 2 Previous Next 25 Replies Latest reply on Oct 29, 2007 7:37 AM by terryb

    Destroying Context Variable

    terryb

      How can one destroy or reinitialise say seam session context variable. I've got values (extracted from database) in List, for a pulldown menu outjeced to session in @factory method. When I add/update record in db, I would like to refresh the session var.

      I manullay invoke the @factory methods after insert/update/delete record, but context var values remain unchanged.

      may be @factory method, only outject if invoked by seam?

      
      @Factory(scope=ScopeType.SESSION)
      public List<SelectItem> getOrganisationListAll() {
      
       log.info("Loading organisationListAll...");
      
       OrganisationList orgList = (OrganisationList) Component.getInstance(OrganisationList.class, true);
       orgList.setOrder("longName asc");
       orgList.refresh();
      
       List<Organisation> orgs = orgList.getResultList();
      
       List<SelectItem> orgSelectItems = new ArrayList<SelectItem>();
       for (Organisation org : orgs) {
       orgSelectItems.add(new SelectItem(org.getId().toString(), org.getLongName()));
       }
      
       log.info("Loaded organisationListAll: " + orgSelectItems.size());
      
       return orgSelectItems;
      }
      
      
      //I invoke vollowing, after insert/update/delete of record.
       //Query in above methods gets records correctly but "organisationListAll" doesn't get refreshed.
      
      public void reloadContextVar() {
       ...
       getOrganisationListAll();
       ...
      }
      


      New records are only laoded if I log off/on.

        • 1. Re: Destroying Context Variable
          nickarls

          try

          Contexts.removeFromAllContexts("organisationListAll");


          • 2. Re: Destroying Context Variable
            terryb

            magic, thanks Nick!

            • 3. Re: Destroying Context Variable

              Where should Contexts.removeFromAllContexts be used?
              I´ve a clean seam-gened app. No scopes or annotations added up to now. I´ve added some data in the database. If I go the the list of a POJO and view the details they are showed. But if after that I want to create a new register of that POJO the pojoEdit page shows the data of the last pojo details. In the wire of the pojoHome I´ve tried Contexts.removeFromAllContexts with the list query but it doesn´t work. is that the place of doing it?

              On the other hand, if I clone the pojoHome lets call the clone pojohome2. If in the pojolist.xhtml and pojolist.page.xml I reference the pojohome2 it works. It makes sense because of using diferente componenetshome. But I think cleaning the context in the proper place would be better.
              thanks in advance!

              • 4. Re: Destroying Context Variable
                terryb

                james, I call it in @override persist, update, remove methods in MyEntityHome class. after success of corresponding operation. Since in my case I wanted data lists to be rebuilt from database at next use.

                I guess in your case, you could have bean action method which gets invoked by your New Record JSF form submission button; there you could call Context... to clear out instance.

                From the JSF form itself, you could also just pass the aprameter without value to let it auto get new instance. hope this helps.

                • 5. Re: Destroying Context Variable

                  Thank you, but it doesn´t work (I´ve created a new method wich is invoked from the button. The method after persisting executes Contexts.removeFromAllContexts..)
                  I´ll start a new thread with this issue in the forum with more details. Thank you anyway!!
                  By the way, could you provide an example of "pass the parameter without value to let it auto get new instance"
                  Currently I´m doing:
                  <h:commandButton id="save1"
                  value="#{messages['grabar']}"
                  action="#{pojoHome.save}"
                  rendered="#{!pojoHome.managed}"/>

                  • 6. Re: Destroying Context Variable
                    terryb

                    James, I guess some guru can help out on your first query.

                    with parameter passing: I meant passing url query parameter to JSF Edit/Insert page. Say your JSF employee edit/insert page uses employeeId param to pass PK to backing bean; and you create a link link New Employee; on this link you could pass empty url query param employeeId to JSF Edit/Insert form. This worked for me.



                    • 7. Re: Destroying Context Variable

                      thank you very much Terry.
                      I´m not passing any parameter to the page, I´m just invoking this way:
                      <rich:menuItem value="#{messages['New']}" action="/RstReportEdit.xhtml"/>

                      And in teh Info.page,xml fiel
                      there is by default:

                      I´ve changed it to:
                      and it doesn´t work either. ;-(




                      • 8. Re: Destroying Context Variable

                        Finally I´ve managed to work it properly with Contexts.removeFromAllContexts.
                        Thank you Terry and Nick.

                        • 9. Re: Destroying Context Variable
                          terryb

                          James, so where did you put Contexts.removeFromAllContexts.. ?

                          • 10. Re: Destroying Context Variable

                            I tried in the wire after checking a param, but it doesn´t work as good as I thought. What string do you return in the override persist, remove and update methods?

                            • 11. Re: Destroying Context Variable
                              terryb

                              Same as default: persisted, updated, removed.

                              @Override
                              public String persist() {
                              
                               if (Strings.isNull(getInstance().getId()) && !this.isManaged()) {
                              
                               KeyGenerator keyGen = (KeyGenerator) Component.getInstance(KeyGenerator.class, true);
                               getInstance().setId(keyGen.getKey(Constant.Database.KeySuffix.ADMIN));
                              
                               getInstance().setLastUpdateDate((new CurrentDatetime()).getCurrentDatetime());
                               getInstance().setInsertDate((new CurrentDatetime()).getCurrentDatetime());
                               }
                              
                               //setCreatedMessage(null);
                               String returnStatus = super.persist();
                              
                               if ("persisted".equals(returnStatus)) {
                               //FacesMessages.instance().addFromResourceBundle(FacesMessage.SEVERITY_INFO, "au.edu.tisc.RecordInserted", " organisation user " + getInstance().getUsername());
                              
                               try {
                               ((ActivityLogger) Component.getInstance(ActivityLogger.class, true)).logOrgUserAuthenticated(
                               ActivityLogger.Code.RECORD_CREATED,
                               String.format(ActivityLogger.Code.Desc.RECORD_CREATED, getInstance().getClass().getName() + "#" + getInstance().getId()));
                               } catch (ActivityLoggerException e) {
                               //skip
                               }
                               }
                               return returnStatus;
                              }
                              
                              
                              


                              • 12. Re: Destroying Context Variable

                                Um, I don´t see where do yo use Contexts.removeFromAllContexts in that persist method. I thought you used it in the persist, update and remove.

                                • 13. Re: Destroying Context Variable
                                  terryb

                                  I had cut it to reduce confusion, since you asked for what I return from those methods. Yes I use it those methods see below, but here I use it to remove context variables for pulldown lists for jsf page.

                                  @Override
                                  public String persist() {

                                  ...

                                  } catch (ActivityLoggerException e) {
                                  //skip
                                  }
                                  Contexts.removeFromAllContexts("organisationUserListAll");
                                  }
                                  return returnStatus;
                                  }

                                  • 14. Re: Destroying Context Variable

                                    Thank you very much, now I manage to clean the context. After creating a new register, if just after that, I create a new one, the edit window doesn´t display anymore the data of the previous crerated one and the new one is created withouth overriding the info of the former.

                                    One more question, if I go to my list, and select one of the registers,and view its details. Lets´call it pojo3.
                                    Then, the pojoHome is associated to that pojo3 info, and in the pojo.xhtml detail view that pojo 3 info is displayed. Ok, that´s correct up to that moment. If later, I go from the seam-gened pojo.xhtml file to the pojoedit.xhtml file to create a new one, as both pages share the same pojohome object, in the edit window is displayed the pojo 3 info in the fields, and whenever I change one field pojo 3 data is updated right away in the database.
                                    If in the wire I check if that´s a new one and I use contexts.remove... the fields in the edit window are clened, but if I change one field pojo 3 data is updated right away in the database. I don´t see why again it is referencing to that.
                                    Does it happen to you. any idea?

                                    1 2 Previous Next