0 Replies Latest reply on Feb 22, 2007 12:18 AM by Jason Long

    Load messages from Database example

    Jason Long Master

      I decided that I would like to store my application messages in the database and I came up with the code shown below. I extended org.jboss.seam.core.ResourceBundle and override

      protected java.util.ResourceBundle loadBundle(String bundleName)


      ApplicationMessage is just an entity I created that I has the properties

      of
      Long id;
      String bundleName;
      String locale;
      String key;
      String message;
      String description;

      I didn't put much time it this and I am open to suggestions for improvement.

      Since the messages are stored in the session, how can I refresh them when the database is updated? I could easily outject a new copy into the user's session that modified the messages, but is there any way to invalidate all the messages for all the sessions so that on the next request these will be reloaded?

      @Name("org.jboss.seam.core.resourceBundle")
      public class ResourceBundle extends org.jboss.seam.core.ResourceBundle
      {
      
       @Override
       protected java.util.ResourceBundle loadBundle(String bundleName)
       {
       if(bundleName.equals("messages"))
       {
       return new DatabaseListResourceBundle(bundleName);
       }
       else
       {
       return super.loadBundle(bundleName);
       }
       }
      
       public static class DatabaseListResourceBundle extends ListResourceBundle
       {
       private String bundleName;
      
       public DatabaseListResourceBundle(String bundleName)
       {
       this.bundleName=bundleName;
       };
      
       @Override
       protected Object[][] getContents()
       {
       return getObjectArray(loadLocalDatabaseMessages(Locale.instance()));
       }
      
       @SuppressWarnings("unchecked")
       private List<ApplicationMessage> loadLocalDatabaseMessages(java.util.Locale locale)
       {
       EntityManager entityManager = (EntityManager) Component.getInstance("entityManager");
       List<ApplicationMessage> list = entityManager.createQuery("from ApplicationMessage m where m.locale=:locale and m.bundleName=:bundleName order by m.key")
       .setParameter("bundleName", bundleName)
       .setParameter("locale", locale.toString())
       .getResultList();
       return list;
       }
      
       public Object[][] getObjectArray(List<ApplicationMessage> list)
       {
       Object[][] messageArray = new Object[list.size()][2];
       for(int k=0; k<list.size(); k++)
       {
       ApplicationMessage am = list.get(k);
       messageArray[k][0] = am.getKey();
       messageArray[k][1] = am.getMessage();
       }
       return messageArray;
       }
       }
      
      }