A very simple AbstractMemStorage implementation

Contains useful source code for specific use cases.

A very simple AbstractMemStorage implementation

Postby Development@SIB » Sun Mar 20, 2011 3:40 pm

A server side memory storage with the column names: ID, NAME, PATH. The column PATH is not visible on the client-side, but is important for server-side. If "error" is set as NAME, an Exception is thrown!

Code: Select all
public class SimpleMemStorage extends AbstractMemStorage
{
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   // Abstract methods implementation
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

   /**
    * {@inheritDoc}
    */
   @Override
   public RowDefinition getRowDefinition() throws ModelException
   {
      RowDefinition rowdef = new RowDefinition();
      rowdef.addColumnDefinition(new ColumnDefinition("ID", new BigDecimalDataType()));
      rowdef.addColumnDefinition(new ColumnDefinition("NAME", new StringDataType()));
      rowdef.addColumnDefinition(new ColumnDefinition("PATH", new StringDataType()));

      rowdef.setPrimaryKeyColumnNames(new String[] {"ID"});
      
      rowdef.setColumnView(null, new ColumnView("ID", "NAME"));
      
      return rowdef;
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public void loadData(MemDataBook pBook, ICondition pFilter) throws ModelException
   {
      pBook.deleteAllDataRows();
      
      pBook.insert(false);
      pBook.setValues(new String[] {"ID", "NAME", "PATH"},
                      new Object[] {BigDecimal.valueOf(0), "First", "/home/first"});
      pBook.insert(false);
      pBook.setValues(new String[] {"ID", "NAME", "PATH"},
                      new Object[] {BigDecimal.valueOf(1), "Second", "/home/second"});
      pBook.insert(false);
      pBook.setValues(new String[] {"ID", "NAME", "PATH"},
                      new Object[] {BigDecimal.valueOf(2), "Third", "/home/third"});
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public void insert(DataBookEvent pEvent) throws ModelException
   {
      if ("error".equals(pEvent.getChangedDataBook().getValueAsString("NAME")))
      {
         throw new ModelException("not allowed");
      }
   }

   @Override
   public void delete(DataBookEvent pEvent)
   {
   }

   @Override
   public void update(DataBookEvent pEvent) throws ModelException
   {
      if ("error".equals(pEvent.getChangedDataBook().getValueAsString("NAME")))
      {
         throw new ModelException("not allowed");
      }
   }
   
}   // SimpleMemStorage
User avatar
Development@SIB
 
Posts: 311
Joined: Mon Sep 28, 2009 1:54 pm

Return to Code snippets