1 Reply Latest reply on Mar 25, 2008 11:19 AM by mars1412

    newbie: how to access enums in webpage

      situation:
      a webUser has a status field, which is of type WebUserStatus (that's my enum)


      in the page, I want to restrict access, dependant on the status:


      only those 2 methods came to my mind:


      1) create a seam-component, with a function like this


           public WebUserStatus getWebUserStatusByName(String status) {
                return WebUserStatus.valueOf(status);
           }



      and compare this:


      rendered="#{webUser.status.equals(enumHelper.webUserStatusByName('ACTIVE'))">




      • I have to use hardcoded strings for the enumerations, and a typo will result in a runtime exception

      • here one could argue, that all other EL-expressions on the page are also just dump strings - but those strings will be checked by JBDS and I'll get warnings, errors, code-completion, etc. - but 'ACTIVE' is really just a string where these features cannot work



      2) create one comparefunctin for each status:


      public class Webuser {
         public boolean isActive() {
            return status == WebUserStatus.ACTIVE;
         }
         public boolean isDeactivated() {
            return status == WebUserStatus.DEACTIVED;
         }
         ...
      }




      • if I add a new status, I also have to write a new compare function (violates DRY)



      any other options?



        • 1. Re: newbie: how to access enums in webpage

          finally I decided to use a variation of version 2:


          I add one check function for every status on my enum entitiy - seems to be the best compromise for me.


          public enum WebUserStatus {
                  ACTIVE,
                  DEACTIVATED,
                  ...
          
                  public boolean isRegistered() {
                          return this.equals(REGISTERED);
                  }
                  public boolean isDeactivated() {
                          return this.equals(DEACTIVATED);
                  }



          so I can use this in my page:


          rendered="#{webUser.status.deactivated}">