Posts in Category "Adobe Data Model"

Creating a Custom Data Model

In this blog entry I’m going to discuss how you can modify the generated Adobe Data Model Action Script source code. You might ask why would you want to?  Most often you probably don’t need to, but what if you wanted to add more logic, custom behavior etc, this is how you can achieve the advanced features you might be looking for.

Setup

I have already created a LiveCycle Application called “Precompiled”, and my Adobe Data Model is called “Customized.fml”.  You’ll be seeing these names in the screenshots and throughout the article.

Code Generation

When you start off building a LiveCycle Guide, you need to base it off of an existing Data Model (fml) file and clicked finish.  Did you notice what happened next?  You might have noticed a dialog that looked similar to this.

Guide Builder took the selected fml file (in this case Customized.fml), and had the Adobe Data Model APIs generate the corresponding ActionScript code for that Data Model. The result of this operation was twofold. The first is the compiled swf, this is your runtime Data Model that gets added to your .guide file.  The second was a collection of ActionScript files. When Guide Builder generates the corresponding source files they are written to the

“Application Data directory\Adobe\LiveCycle\ES2\Guides\generated”

folder.

The generated directory contains a cache of the compiled Adobe Data Models, and ActionScript source code that was used to generate your Guides.  The “Precompiled” folder that we see in the above screenshot is the name of my LiveCycle Application.  When we dig deeper into the directories we see the output of the compile as shown here.

Excellent!  Here is where get to start jumping into customizing the Data Model code.

Code Customizing

It’s time to fire up Flex/Flash Builder. To start off you’ll be creating a ActionScript project. File -> New -> ActionScript Project. 

Note you must configure FlashBuilder to use the Flex SDK that is shipped on the LiveCycle DVD. 

Enter a name for your Project and point the Project contents to the generated folder, drill down until you find the source, or simply copy the generated code to your own preferred location.  Click Next to configure your library path.

You may need to include additional swc files to successfully compile the Data Model.  The following screenshot shows you what you’ll need.  I had to add the framework.swc, and rcp.swc files to the library path for things to compile successfully.

After adding the required swc files you can click finish.

Now, depending on your approach you might have to move files around.  The above screenshot said that my Main source folder was src.  I had to move the generated code into that folder such to satisfied the project settings.

Phew, okay the hard part is over. 

You will notice that the new ActionScript project created a Customized.as file.  You can delete this file as we’ll assign the _Customized.as file to be the Primary Application thereby allowing the compiler to successfully build the swf.  To set the _Customized.as as the Primary Application you can right click the _Customized.as file and select “Set as Default Application”, or right click your project, select Project Properties, and select ActionScript Applications.  You’ll have to click the add button and navigate to the _Customized.as file.  Once this is complete, click okay. 

Hopefully at this point you should be able to compile the current code into a swf.  Check the output folder (bin-debug, by default) to see if it was generated. Now the sky is really the limit to what you can do to make modifications to the Data Model source code. 

In this example I’m going to make a modification to one of the get functions to change the string value that gets returned.

Hack Time!

Okay, lets dig our heals into the generated code.  I started with a very simple Data Model which contains a single Entity (Person), with one property (age), because this is going to be my root node, this entity is persistent and contains an id property.

Before hacking the code, here is a screenshot of what the Guide looks like after dragging on the age property, and dragging on a Text field, and binding the Text value to the age value.

When I preview the Guide and enter the value 20, this is the result.

This is our unmodified Guide.  This Guide is running the Generated swf file.  Not the one we’re about to code.

The generated code that I’m going to play with is in a class called _Super_Person.as.  Cracking this file open I modify the get method for age to simply return a more elaborate string than the user’s entered value. Save and recompile.

This is a really basic modification, but it allows you to see how you can go about customizing the Data Model code and building in your own logic.

When the compile has completed, it’s time to wire up the existing Guide to use our precompiled Data Model.  To do this, flip back to Guide Builder.  First we need to add our newly compiled Data Model to our Application.  Copy and paste, or drag and drop the new Data Model swf into your Application. 

Next is to make sure the Data Model View is visible.  If for some reason you can’t locate this view you can reopen it from the menu, Windows -> Show View -> Data Model.

If the Data Model View isn’t selected, select it now to bring it to the forefront. Right click within the Data Model View, and select the “Data Model Settings…” menu option.

The result is the Data model settings dialog is displayed.

You will see that there is a “Pre-build data model (SWF)” field entry which is blank at this point. Click the browse button to navigate to your newly compiled Data Model swf.

Select it and press OK.  The Pre-build data model entry should now be populated with the path to the compiled Data Model.

At this point we can execute a Preview and see the results of our glorious code changes.

When the Guide fires up, there is no value so we see the result of the get age call.  “Less than 20″.  If I were to input a value greater than 20, the resulting text would also change.

Reusing The Data Model

Remember way back when you click “File -> New Guide…”, on the second page of the New Guide Wizard there was a check box way down on the bottom saying something to the point of using a precompiled data model.  Well, now this makes sense!  The next time you want to create a Guide based off the same fml file with the same customized code, here is where you can select the precompiled Data Model swf file.  Guide Builder will refrain from compiling the fml file and will use the custom Data Model swf.   Nice!

Creating your own Data Model code probably isn’t an everyday operation as most of your logic and rules should be part of your Data Model’s fml file, however if you can’t get everything you want from this, you now know how to get into the bowels of creating  your own custom Data Model code.