Adding a Data Model to a Mobile Project
Data Modeling includes the Entity Modeling Language to define a project specific data model and its support within the application model. Developers are able to declare a data model using a Java-like syntax, including syntax highlighting, auto-completion and formatting.
Every EML data model consists of Entities and Enumerations. An Enumeration consists of a list of literals and is represented by a Java class, that contains a public static member for each literal. Each member is identified by an int value (starting with 0). An Entity represents a Java class and may own a number of Variables, Collections and Operations.
Variable:
Integer, Float, Double, Char, Byte, String, Object, Image, Boolean, Long
Collection:
List<Type>, Map<Type, Type>, Stack<Type>
Operations:
void print(String[] args);
Variables can also be used as an Array. The language supports cross-referencing (using another Entity as the type of a Variable) and inheritance (using the "extends" keyword) of Entities to build a hierarchically data model.
A convenient way to model a bi-directional cross-referencing relationship is to use the @Opposite annotation. If at runtime the reference to an entity is changed, the generated code takes care to also change the opposite reference of that relationship. Supported Use-Cases:
1:1 relation between variables of type Entity
1:Many relation between variable of type Entity and List of type Entity
Many:Many relation between two Lists of type Entity
Two convenience methods will be generated, if the opposite is a List:
addItemOn<Entity>(final Abstract<Entity> item)
removeItemOn<Entity>(final Abstract<Entity> item)
a setter will not be generated in that case
The @Opposite annotation is not supported for:
Arrays, Enums, Maps, Stacks
Variables using a simple type (e.g. int, boolean, etc.)
After code generation, Entities and Enumerations can be used as normal Java classes. An Entity must have a name, which will be taken as name for the resulting Java class. As an example, the Entities:
package entities;
entity ProcessInstance {
String id;
String name;
int status;
@Opposite(member=processInstance)
List taskInstances;
void print(String[] args)
}
entity TaskInstance {
String description;
@Opposite(member=taskInstances)
ProcessInstance processInstance;
}
will result in the Java class ProcessInstance.java and TaskInstances.java, located in your src/<yourBundleName>model/entities package. The resulting Java class will then be generated to include each of the Variables and Collections as Java class properties:
public abstract class AbstractProcessInstance implements IEntity {
private String id;
private String name;
private Integer status;
private Vector taskinstances = new Vector();
public abstract void print(final String[] args);
public AbstractProcessInstance setTaskInstancesAtIndex
(final AbstractTaskInstance item, final int index){
if (taskinstances != null && taskinstances.size() > index && index >= 0){
final AbstractTaskInstance oldItem = (AbstractTaskInstance)
taskinstances.elementAt(index);
taskinstances.setElementAt(item, index);
item.setProcessInstance(this);
oldItem.setProcessInstance(null);
}
return this;
}
}
The class will also contain getter and setter methods for your properties and helper methods to access contents of Collections. Furthermore convenience methods like updateFromJson(), getAppController() and a constructor with a JSONObject parameter are generated. With these, it is easily possible to create or update an entity based on a JSONObject/JSONString and access the AppController.
By using the keyword abstract for an Entity, the generated Java class will be an abstract class. An Entity must not have a relation to an abstract Entity by defining a variable of the abstract type. Entities can inherit from abstract Entities.
Data Modeling also includes two Entity based data sources - called EntityDataSource and EntityStore. An EntityDataSource can be configured to request a remote Json resource. The Json document is automatically parsed into an Entity. Furthermore the Entity is transformed back into a Json document to be able to send the data back to a remote Json resource. The Entities will be used as a list data source for
ListView and
ListViewElement objects. For more information, see
EntityDataSource Properties. The EntityStore provides a single Entity and can be used to bind a single data object against UI elements. E.g. having a login mask with input fields for user name and password, you can bind both input fields against a Credentials Entity using a DataBinding object.
To add a Data Model:
2. In the Model section of the Outline Editor, expand the project in order to display the Entities container node. If the model does not have a Entities container node, add one by right-clicking the root application and selecting New Child > Entities.
3. Select the Entities container node and add a new Package by right-clicking the Entities node and selecting New Child > Package.
4. In the resulting dialog box, enter a new name for the Package and press Ok.
A new EML file has now been added to the Mobile Project and an editor opens immediately to let you edit the Data Model.