5 Replies Latest reply on Mar 14, 2007 5:10 AM by denis-karpov

    Overriding EntityHome.persist problems

    mikedougherty

      I've got some rules that will validate an entity before it is saved to the database, according to what I've read the best place to do that is in the Home.persist method. However, in the Home.persist method the getId and getInstance methods always return null. I expected them to contain the Entity that is about to be persisted, is this not a correct assumption?

      I injected an instance of the Entity and it seems to contain the form data. This works, and I'm cool with, but is this the *right* way?

      Thanks,

        • 1. Re: Overriding EntityHome.persist problems
          pmuir

           

          "MikeDougherty" wrote:
          I've got some rules that will validate an entity before it is saved to the database, according to what I've read the best place to do that is in the Home.persist method.


          Yup, that would be my plan (and in the update method as well if needed)

          However, in the Home.persist method the getId and getInstance methods always return null. I expected them to contain the Entity that is about to be persisted, is this not a correct assumption?


          Well, getId() would return null right (as the database has not yet assigned an id)? But getInstance should return the entity you completed on the form. Post the code/wiring you are using.

          I injected an instance of the Entity and it seems to contain the form data. This works, and I'm cool with, but is this the *right* way?


          Not as neat, but still fine IMO.

          • 2. Re: Overriding EntityHome.persist problems
            mikedougherty

            Thanks for the response.

            @Name("regionHome")
            public class RegionHome extends EntityHome<Region>
            {
            
             @RequestParameter
             Long regionId;
            
             @In(required=false)
             Region region;
            
             @In
             WorkingMemory regionManagerWorkingMemory;
            
             @Override
             public Object getId()
             {
             if (regionId==null)
             {
             return super.getId();
             }
             else
             {
             return regionId;
             }
             }
            
             @Override @Begin(join=true)
             public void create() {
             info("create()");
             super.create();
             }
            
             @Override
             public String update() {
             info("update("+regionId+")" );
             return super.update();
             }
            
             @Override
             public String persist() {
             info("getId() == #0", getId()); // #1
             info("region.getId() == #0", region != null ? region.getId() : "null" ); // #2
             info("persist("+regionId+","+ (getInstance() != null ? getInstance().getId() : "null") +")" ); // #3
            
             if( region != null ) {
             regionManagerWorkingMemory.assertObject(region);
             regionManagerWorkingMemory.fireAllRules();
             setInstance(region);
             }
             return super.persist();
             }
            
             @Override
             public String remove() {
             info("remove("+regionId+")" );
             return super.remove();
             }
            }
            


            In the persist method line #1 prints:
            17:17:08,163 INFO [RegionHome] getId() == null
            


            #2:
            17:17:08,164 INFO [RegionHome] region.getId() == 1
            


            #3:
            17:17:08,165 INFO [RegionHome] persist(null,null)
            


            I'm trying to get my head around this in the persist method using existing data (Entities) before tackling the update method...

            Actually, now that you mention it the page *should* be calling the update method. Maybe I need to be looking at the region.xhtml to see why it's calling persist instead of update first.



            • 3. Re: Overriding EntityHome.persist problems
              pmuir

              So you call persist if you are trying to add an entity to the database, update for one that is already there.

              • 4. Re: Overriding EntityHome.persist problems
                mikedougherty

                OK, it seems the managed flag is not being set when I navigate to that page from my select list. However, it does get set correctly when I navigate to the region.xhtml from an <s:link /> within a <h:dataTable /> tag. I am apparently building and submitting my list incorrectly.

                So now my question becomes, how do I build a list of entities that allows the user to navigate to the entity's edit page? I haven't seen anything like this in the examples, and my guesses do not appear to be doing the correct things.

                Thanks.

                • 5. Re: Overriding EntityHome.persist problems
                  denis-karpov

                  Why not to use pageflows for this. It is convenient and works smoothly for me.

                  Pageflow

                  .....
                   <page name="edit" view-id="/exchange/buy.xhtml">
                   <description>qqq</description>
                   <redirect/>
                   <transition name="save" to="validate"/>
                   <transition name="cancel" to="end">
                   </transition>
                   </page>
                  
                   <decision name="validate">
                   <handler class="org.jboss.seam.drools.DroolsDecisionHandler">
                   <workingMemoryName>buyWorkingMemory</workingMemoryName>
                   <assertObjects>
                   <element>#{exchOrdHome.instance}</element>
                   <element>#{facesMessages}</element>
                   </assertObjects>
                   </handler>
                   <transition to="refresh">
                   <action expression="#{exchOrdHome.merge}" />
                   </transition>
                   <transition name="invalid" to="edit"/>
                   </decision>
                  
                   <decision name="refresh" expression="1">
                   <transition name="1" to="browse">
                   <action expression="#{exchOrdHome.invalidate}" />
                   </transition>
                   </decision>
                  ..........
                  

                  Drools
                  ....
                  rule MandatorySum
                   when
                   $facesMessages : FacesMessages()
                   Sum(asFloat<=0)
                   then
                   $facesMessages.add(new FacesMessage("Sum has to be more than zero."));
                   decision.setOutcome("invalid");
                  end
                  .....