1 Reply Latest reply on Aug 19, 2007 12:29 PM by ellenzhao

    how to catch unhandled exception in the conversation bean

    ellenzhao

      In my view code, the inputText fields are tied to the field setters of managed entities. My conversation bean manages UseCase-related states.

      Let's say there is a UseCase called updateAFood. There is an action button "submit" which calls this method in the conversation bean:

      private List<Exception> errors = new ArrayList<Exception>();
      private UseCaseStack;
      
      public void tryFinishUpdateAFood(){
       if (there is no exception from entity bean's setter methods) {
       this.finishUpdateAFood();
      } else {
       this.errors.addAll(exceptions from entity beans' setter methods);
      }
      }
      
      private void finishUpdateAFood(){
       Conversation.instance().end();
       this.useCases.removeUseCase(UseCase.updateAFood);
       this.food.setStatus(CRUDStatus.UPDATED.getSymbolInDB());
      }
      


      Here is my utility class UseCaseStack.java
      public class UseCaseStack {
      
       private UseCase[] stack;
       private int nEntries;
       private int size;
      
       public UseCaseStack() {
       size = 10;
       stack = new UseCase[size];
       nEntries = 0;
       }
      
       public Integer size() {
       return nEntries;
       }
      
       public Boolean isEmpty() {
       return nEntries == 0;
       }
      
       public void push(UseCase uc) {
       if (nEntries == size) {
       UseCase[] newstack = new UseCase[2 * size];
       for (int i = 0; i < size; i++)
       newstack = stack;
       stack = newstack;
       size *= 2;
       }
      
       this.stack[nEntries++] = uc;
       }
      
       public UseCase pop() {
       nEntries--;
       UseCase uc = this.stack[nEntries];
       this.stack[nEntries] = null;
       return uc;
       }
      
       public UseCase peek() {
       if (this.nEntries == 0) {
       return UseCase.startApp;
       } else {
       return this.stack[nEntries - 1];
       }
       }
      
       public UseCase previous() {
       if (this.nEntries <= 1) {
       return UseCase.startApp;
       } else {
       return this.stack[nEntries - 2];
       }
       }
      
       public Boolean contains(UseCase uc) {
       boolean result = false;
       for (int i = nEntries - 1; i >= 0; i--) {
       if (this.stack.equals(uc)) {
       result = true;
       break;
       }
       }
       return result;
       }
      
       public void removeUseCase(UseCase uc) {
       for (int i = nEntries - 1; i >= 0; i--) {
       if (this.stack.equals(uc)) {
       for (int j = i; j < nEntries - 2; j++)
       this.stack[j] = this.stack[j + 1];
      
       this.nEntries--;
       break;
       }
       }
       }
      
       public void removeUpto(UseCase uc) {
       while (pop() != uc);
       }
      }
      
      public enum UseCase { .... }
      


      Any hint on how to catch the exceptions from the entity bean's setter classes in the conversation bean would be highly appreciated!


      Regards,
      Ellen

        • 1. Re: how to catch unhandled exception in the conversation bea
          ellenzhao

          I'm going to try this:

          @Name("foodManager")
          @Scope(COVERSATION)
          @Stateful
          public class FoodManagerImpl extends AppControllerImpl implements FoodManager, Serializable {
          
          @In(required = false)
          private IllegalPriceException ipe;
          ...
          
          /* List<Exception> errors and UseCaseStack useCases are protected fields in the AppControllerImpl class and they are heavily used in the RHS of "rendered= ..." rules in the view codes. This way, the rendering of every field of an entity can be controlled using these conditions in the view code. This way, the reusability of entity class and its view code is maximized. The view of a use case is simply composed of one or more entity views. */
          
          @Override
          public void tryFinishUpdateAFood(){
           boolean noInputError = true;
           if (this.ipe != null) {
           noInputError = false;
           this.errors.add(this.ipe);
           }
          
           if .....
          
           if (noInputError) this.finishUpdateAFood();
          }
          
          private void finishUpdateAFood(){
           Conversation.instance().end();
           this.useCases.removeUseCase(UseCase.updateAFood);
           this.food.setStatus(CRUDStatus.UPDATED.getSymbolInDB());
          }
          }
          
          


          fingers crossed hoping the exceptions can be injected to the manager bean....