Related Models
The Administrator can configure Pages that have fields from Related Model Levels.
When the Administrator clicks the Add Related Model button on the Page Editor, a dialog opens that allows him to select which type of related model to add:
The related model's fields will then be added into the Page Editor Widget List for the Administrator to use:
If a related model is added to the Page, then the PageFlow allows one to specify "Related Model Actions". These selections will be used to Create/Update related model data when saving the page. In the case of 1-to-Many related models or Direct Child Models, one can also specify a Delete action in the Page Flow which will be used if any record is marked as deleted from the related model grid.
There are several types of related models:
1-to-1 Related Model
A 1-to-1 Related Model is any model where there exist exactly zero or 1 instance(s) of that model relative to the main Model Level for the Page. Any ModelLink field on the main Model Level would qualify since a ModelLink can have only one value.
For each 1-to-1 Related Model added, a new node is inserted into the Page Editor's tree of field widgets. There are 3 ways a 1-to-1 relationship between Models can occur:
Model Link from the main Model to the Related Model
Model Link from the Related Model to the main Model, where the Model Link is the only Natural Key (other than Value Chain Id) on the Related Model
Model Link from the Related Model to the Model which have some specific criteria defined within module code using UserDefinedPageService.registerSynthetic1to1. This creates a synthetic model relationship
E.g. If the main Model Level is ShipmentHeader, then Organization is a 1-to-Many Related Model because ShipmentHeader has a SHIPMENT_CREATION_ORG modellink
1-to-Many Related Model Level
Any Models having a ModelLInk back to the main Model.
E.g. If the main Model Level is Organization, then ShipmentHeader is a 1-to-Many Related Model because each Shipment has a SHIPMENT_CREATION_ORG modellink
Unlike 1-to-1 Related Models, 1-to-Many models are provided as a single grid widget. Each column represents a Field within the Related Model.
Ancestor Model Level
If a Model Level is an ancestor level of the main Model Level, then model is called as Ancestor Model Level.
E.g. If the main Model Level is ShipmentLine, then ShipmentHeader is an Ancestor Model Level.
The behavior is the same as 1-to-1 Related Models, where a new section is inserted into the Page Editor's tree of field widgets, each of which can be dragged into the workspace.
Direct Child Model Level
If a Model Level is an immediate child level of the main Model Level then the model is a Direct Child Model Level.
E.g. If the main Model Level is OrderHeader, then OrderLine is a Direct Child Model Level.
The behavior of Direct Child Model Level is the same as 1-to-Many Related Model. A new grid widget is inserted in the Page Editor's tree under a 'Grid Widgets' tree item where each column represents a Field within the Related Model. However, in the case of Direct Child Model, there is no need to specify Related Model Actions in the Page Flow. This is because the actions of main Model are used to Create/Update child model data. However, one is permitted to specify a Delete action which will be used to delete records from the Child Level Model grid.
Synthetic 1-to-1 Models
Developers can create synthetic 1-to-1 relationships between 2 models as long as there exists some systematic way a developer can get from the related model to the main model. Synthetic model relationships are dependent on developer-specified criteria. For example, let's say we have 2 models, Classroom and Student, and Student has a model link to Classroom. Each classroom could have a concept of a single currently presenting student, and to accommodate that, Student could have a field called CurrentlyPresenting.
A Developer can set up this "CurrentlyPresentingStudent" synthetic relationship based on the Classroom model link and CurrentlyPresenting fields, and then an Administrator can add the "CurrentlyPresentingStudent" related model to their user-defined page. Below are steps to add a synthetic related model using the Classroom and Student models as an example.
The Developer registers the synthetic model relationship. The best place to do this is on server startup, within theModuleContextListener:
public
class
ModuleContextListener
implements
com.onenetwork.platform.env.module.ModuleContextListener {
...
public
void
moduleInitialized(Module app)
throws
Exception {
UserDefinedPageService udpService = Services.get(UserDefinedPageService.
class
);
SyntheticModel syntheticModel =
new
SyntheticModel(
"SHOW.CurrentlyPresentingStudent"
,
Classroom.
class
,
Student.
class
,
"SHOW_STUDENT.SYS_CLASSROOM_ID = SHOW_CLASSROOM.SYS_CLASSROOM_ID AND SHOW_STUDENT.CURRENTLY_PRESENTING = 1"
,
new
SyntheticModelListener() {
@Override
public
void
preAction(String actionName, Model c, Model s, PlatformUserContext arg4) {
Classroom classroom = (Classroom) c;
Student student = (Student) s;
student.setSysClassroomId(classroom.getSysId(),
false
);
if
(!student.isSetCurrentlyPresenting()) {
student.setCurrentlyPresenting(
true
);
}
}
@Override
public
void
postAction(String actionName, Model arg2, Model arg3, PlatformUserContext arg4) {
}
}
);
udpService.registerSynthetic1to1(syntheticModel);
}
}
A SyntheticModel instance is constructed with the following:
A globally unique name
The main model class
The related model class
A SQL query clause that ties the 2 models together
A listener which has callbacks that allow the Developer to enforce the relationship when actions are executed
This is then passed into UserDefinedPageService.registerSynthetic1to1() from your ModuleContextListener during application startup.
The Developer supplies an internationalized label for the synthetic relationship by adding the following to messages.properties:
meta.page.SyntheticModel.1to1.SHOW.CurrentlyPresentingStudent=Presenting Student
After rebuilding the module and restarting the server, a new 1-to-1 related model should be available for the Classroom model within the Add Related Model dialog:
The Administrator can then begin adding fields from the related model: