Musings about Coding, Business and other Geek Stuff Live and Direct from somewhere on the planet
August 11, 2004
Extending NanoWeb with Prevayler

I am a fan of cool simple technology. Prevayler and NanoWeb both count as this. They are both relatively transparent and have been implemented with tiny amounts of code.

I have several smaller web projects that I’m working on, where I want to use both of these technologies. Which is why I’m adding the stuff I need.

After finishing the implementation of adding multiple views to NanoWeb yesterday. I was thinking about how I could use Prevayler with NanoWeb. I came to realize that it could be done very simply with a few changes to the NanoWebServlet.

I don’t really want to add too much more functionality to NanoWebServlet, so as a quick Hack I created PrevalentWebServlet. What you do when you write an action is implement either or both org.prevayler.Query or org.prevayler.Transaction. Actually I extedned Transaction to be ValidatingTransaction, which adds a isValid() method.

If the action implements Query prevayler processes the query before handling the action method. If it implements Transaction and has been marked by the action method as valid it processes the Transaction.

Here is an example:

public class PersonEditor implements ValidatingTransaction, Query{
     private String name;
     private String address;
     private String email;
     private String comment;
     private transient boolean exists=false;
     private transient String message;
 
 
     public String  view(){
          if (!exists){
               return edit();
           }
          return "view";
      }
     public String edit(){
          if (email==null)
              email="";
          if (name==null)
              name="";
          if (address==null)
              address="";
          if (comment==null)
              comment="";
          if (exists)
              return "edit";
          else
              return "new";
      }
     public String search(){
          if (email==null)
              email="";
          return "search";
      }
     public String getName() {
          return name;
      }
 
     public void setName(String name) {
          this.name = name;
      }
 
     public String getAddress() {
          return address;
      }
 
     public void setAddress(String address) {
          this.address = address;
      }
 
     public String getEmail() {
          return email;
      }
 
     public void setEmail(String email) {
          this.email = email;
      }
 
     public String getComment() {
          return comment;
      }
 
     public void setComment(String comment) {
          this.comment = comment;
      }
 
     public String getMessage() {
          return message;
      }
 
     public void executeOn(Object o, Date date) {
          Map map = ((Map)o);
          if (map.containsKey(email)){
               Person person=(Person) map.get(email) ;
               person.setName(name);
               person.setAddress(address);
               person.setComment(comment);
           } else
              map.put(email,new Person(name,email,address,comment));
      }
 
     public boolean isValid() {
          return (name!=null&&email!=null);
      }
 
     public Object query(Object o, Date date) throws Exception {
          if (email!=null){
               Person person=(Person) ((Map)o).get(email);
               if (person!=null){
                    exists=true;
                    if (name==null)
                        name=person.getName();
                    if (address==null)
                        address=person.getAddress();
                    if (comment==null)
                        comment=person.getComment();
                    return person;
                }
           }
          return null;
      }
}

Of course this was written in Java. Would Groovy be an option for this as well? I am guessing that yes you should be able to write Prevayler Transactions and Queries in Groovy. But I don’t know. Perhaps someone would like to take a shot at it?

I’m not 100% convinced this is all good thing so far. It probably lends itself best to very simple applications. I definitely need to clean up the implementation of it. Maybe adding somesort of PersistanceManager to NanoWeb. I don’t know, then we would be making a nice simple library more complex.

If you want to try it I’ve put up a war file containing a sample prevalent web application

Posted by pelleb at August 11, 2004 12:34 PM
This entry was posted in the following Categories: Java
Comments

Why don't you write your prevalent system separately and make it available to your actions through dependency injection?

You can even create specific services that transparently use prevayler (much in the way a DAO works)...

Posted by: Tiago Silveira on August 15, 2004 09:47 AM
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?