LCDS Data management – adding items to a destination

Last week I received an interesting question: what should we do if we intend to create several items which are connected to a destination (and propagate the changes to all registered clients)?

A common use case could be:

  • You have a datagrid showing all the matches from a sport competition. You can create a new match (by adding a new line in grid) or you can modify the existing ones
  • And more important: you can execute a service call from the server which will add new matches, insert them into the database and propagate them on the other clients

In the first case, you create the items on the client and send the data to the server using the Flex Data Services API – so the clients are the producers. For the second you are creating the items directly on the server – so both the clients and the server are the producers. Since I have not yet seen an example showing that, I thought it would be a good idea to present one.

Below is a code snippet showing the code:

   1: DataDestination dt = DataDestination.getDataDestination("Test");
   2:  
   3:  DataMessage fillMessage = InternalMessage.createMessage();
   4:  fillMessage.setOperation(DataMessage.CREATE_OPERATION);
   5:      
   6:  ASObject test = new ASObject();
   7:  test.put("name", "Test created at:"+System.nanoTime());            
   8:  test.setType("com.test.Test");            
   9:  BeanProxy proxy = new BeanProxy(test);
  10:     
  11:  fillMessage.setBody(proxy);
  12:  DataServiceTransaction tx = null; 
  13:      
  14:  tx = DataServiceTransaction.begin(false);  
  15:  Collection<Object> result = (Collection<Object>)dt.getAdapter().invoke(fillMessage);
  16:               
  17:  tx.updateItem("Test",result.toArray()[0],null,null);
  18:     
  19:  tx.commit(); 

You can download the files here (it is a Flex project archive). It uses MySQL – the script to create the test table is in database/create.sql

Leave a Reply