6 Replies Latest reply on Apr 29, 2007 9:59 PM by saeediqbal1

    If NoResultException, then logic in JSF or Java

    saeediqbal1

      In my simple application that i am writing, now i can edit records if a single resultset exist in database, pull it in the form and edit it and click save to modify the contents fine.

      Now the problem is that when nothing is returned from that database, for example an activity record for that user, if it doesn't already exist. then i am having a hard time figuring out what to do.

      Should i use JSF like in other examples to check if

      <s:div rendered="#{reportActivity.value == null}">
       PUT THE FORMS HERE
       </s:div>
      ... for some reason i'm not sure if the object returned is null even if no row is found but i dont know

      if that happens then i put the forms inside the s div tags. and if it is not null then put the forms with value binding in between those s divs at the bottom. Or currently i am playing with creating a new object and putting sample values in it in the .java files. check this

      @Create
       public String reportActivity() {
       try {
       value = (Activity) em
       .createQuery(
       "select o from Activity o where o.username = #{identity.username}")
       .getSingleResult();
      
       }
       catch(javax.persistence.NoResultException ex) {
      
       value = (Activity) new Activity();
       value.setUsername("#{identity.username}");
       value.setFirstact("Not Available");
       value.setSecondact("Not Available");
       value.setThirdact("Not Available");
       value.setFourthact("Not Available");
       value.setFifthact("Not Available");
       em.persist(value);
       log.info("reportActivity.reportActivity() action called with: #{reportActivity.value}" + getUsername());
       return "failure";
       }
      
       return "success";
       }



      that one is going horribly wrong, it doesn't even pick up the username. and when submitted it enters data in mysql twice 2 rows instead of one and puts in #identity.username instead of the username.

      In addition the page flow xml file seems to not work properly either. When my method above should return failure it should go to a different page but it doesnt.

      see

      <page view-id="/reportActivity.xhtml" login-required="true">
       <navigation>
       <rule if-outcome="failure">
       <redirect view-id="/ActivityEdit.xhtml"/>
       </rule>
       </navigation>
       <navigation from-action="#{reportActivity.updateActivity}">
       <redirect view-id="/ActivityList.xhtml"/>
       </navigation>
       </page>



      what are the best practices for this? I know im still stuck at the basics. sorry