Disease :
You want to use LiveCycle ES Data Services on top of a Java back-end that is using Java enums.
Prescription :
LiveCycle ES Data Services 2.6 supports Java enums. When serializing from Java to ActionScript, Data Services will transform the Java enum value into an ActionScript String. When serializing from ActionScript to Java, Data Services will assign the correct enum value to the corresponding Java attribute.
Let’s see how it looks like. To get started we create a LiveCycle ES Data Services project (What is the preferred LiveCycle Data Services development environment). In the next part we will run through all the steps needed to develop a LiveCycle ES Data Services Data Manangement application.
The Java side
The Java enum looks like this
| |
package myenum.test;
public enum MyEnum { VOLVO, PORSCHE, AUDI, BMW; }
|
|
The DTO looks like this:
| |
package myenum.test;
public class MyDTO {
public int id; public MyEnum car; public String name; }
|
|
LiveCycle ES Data Services Data Management automatically takes care of all DTO’s that are modified and retrieved by the Flex application (created, deleted and updated objects). When a client side "commit" operation is invoked the modifications are sent to the server. Those changes are mapped to your back-end via an assembler class. Typically this class implements four methods (fill, createItem, updateItem and deleteItem). In this example the assembler class looks like this:
| |
package myenum.test;
import java.util.ArrayList; import java.util.Collection; import java.util.List;
import flex.data.assemblers.AbstractAssembler;
public class MyAssembler extends AbstractAssembler {
@Override public Collection fill(List fillParameters) { ArrayList<MyDTO> list = new ArrayList<MyDTO>(); MyDTO myDTO = new MyDTO(); myDTO.id = 1; myDTO.name = "Marc"; myDTO.car = MyEnum.AUDI; list.add(myDTO); // TODO Auto-generated method stub return list; }
@Override public void createItem(Object item) { MyDTO myDTO = (MyDTO) item; System.out.format("Name : %s\nCar : %s", myDTO.name, myDTO.car); }
@Override public void deleteItem(Object previousVersion) {
MyDTO myDTO = (MyDTO) previousVersion; System.out.format("Name : %s\nCar : %s", myDTO.name, myDTO.car); }
@Override public void updateItem(Object newVersion, Object previousVersion, List changes) {
MyDTO myDTO = (MyDTO) newVersion; System.out.format("Name : %s\nCar : %s", myDTO.name, myDTO.car); } }
|
|
In the real world these methods will be calling your specific services to retrieve and process the information.
Before we go to the Flex client we need to configure a Data Management endpoint by configuring the data-management-config.xml file in your LiveCycle ES Data Services project (located in WEB-INF/flex)
| |
<destination id="myds">
<adapter ref="java-dao" /> <channels> <channel ref="my-rtmp"/> </channels>
<properties>
<auto-sync-enabled>true</auto-sync-enabled>
<use-transactions>false</use-transactions>
<source>myenum.test.MyAssembler</source> <scope>application</scope>
<metadata> <identity property="id" /> </metadata>
<network> <session-timeout>20</session-timeout> <paging enabled="false" pageSize="10" /> <throttle-inbound policy="ERROR" max-frequency="500" /> <throttle-outbound policy="REPLACE" max-frequency="500" /> </network>
<server/>
</properties>
</destination>
|
|
The Flex side
The ActionScript DTO (it maps directly to the Java class):
| |
package vo { [Managed] [RemoteClass(alias="myenum.test.MyDTO")] public class MyDTO { public function MyDTO() { } public var id:int; public var name:String; public var car:String;
} }
|
|
The Flex application:
| |
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script> <![CDATA[ import vo.MyDTO;
private function createMyDTO():void { var myDTO:MyDTO = new MyDTO(); myDTO.id = 0; myDTO.name = "Marc"; myDTO.car = "BMW"; myCol.addItem(myDTO); }
]]> </mx:Script>
<mx:DataService id="myDS" destination="myds" autoCommit="false"/> <mx:ArrayCollection id="myCol"/>
<mx:Panel x="116" y="49" width="488" height="204" layout="absolute"> <mx:DataGrid id="myDG" x="0" y="0" width="100%" height="100%" dataProvider="{myCol}" editable="true"> </mx:DataGrid> <mx:ControlBar> <mx:Button label="Query" click="myDS.fill(myCol)"/> <mx:Button label="Create" click="createMyDTO()"/> <mx:Button label="Save" enabled="{myDS.commitRequired}" click="myDS.commit()"/> <mx:Button label="Undo" enabled="{myDS.commitRequired}" click="myDS.revertChanges()"/> </mx:ControlBar> </mx:Panel> </mx:Application>
|
|
On the Java side "car" is a enum while in ActionScript it is a String. Assigning "BMW" to a newly created myVO results serverside in a new myVO with the car enum equal to BMW.
Notice that there are no explicit calls made to the server for creating, deleting or updating the DTO’s. The only interaction with the server happens through the fill and the save operation. LiveCycle ES Data Services Data Management takes care of the hard work.
You can download the sources for this sample as a LiveCycle ES Data Services project archive from Adobe Share, next import this into your Eclipse + Flex plugin environment.
Tip to stay healthy:
Check out Christophe Coenraets‘ website to find more examples about LiveCycle ES Data Services. You can download LiveCycle ES Data Services 2.6 (beta) from Adobe Labs.