Enum and Derived Attributes
Creating enums using the platform
The EntryType
class identified during the design phase is an enumeration. The JSON blueprint for the same, is supposed to look as follows :
{
"entry_type": {
"purpose": "POSSIBLE_VALUE",
"values": {
...
}
}
}
Notice that instead of using RESOURCE
as the purpose, we specify it as POSSIBLE_VALUE
to denote an enum.
In the values
field, we are expected to specify the different constants that define the set of elements of the enum. Each constant has an id
field and a name
field. This is illustrated in the example below :
{
"entry_type": {
"purpose": "POSSIBLE_VALUE",
"values": {
"going_out": {
"id": "going_out",
"name": "Going Out"
},
"coming_in": {
"id": "coming_in",
"name": "Coming In"
},
"going_home": {
"id": "going_home",
"name": "Going Home"
},
"coming_from_home": {
"id": "coming_from_home",
"name": "Coming From Home"
}
}
}
}
Enums as data types
Once we have the JSON specification for the EntryType
enum ready, we can use it in the StudentMovement
class as the data type for the entry_type
field.
This is done by mentioning the type
field of the entry_type
attribute as String
and then using the possible_value
field to specify the corresponding enum class along with the correct package descriptor.
"entry_type": {
"length": 128,
"type": "String",
"possible_value": "{groupId}.{artifactId}.resource.EntryType"
}
Note that the class name EntryType
is qualified by the correct name of the package it is supposed to belong to once the resources that been generated. This can be verified by creating a Java class in the src/main/java/{groupId}/{artifactId}/resource
directory using an IDE like IntelliJ IDEA.
Foreign Derived Attributes
Consider the student_batch_name
attribute of the Student
class. The singleton set {student_batch_name}
is functionally dependent on the singleton set {student_batch_id}
. In other words, if the value of student_batch_id
is known for a particular student, the value of student_batch_name
is uniquely determined and can be fetched from the Batch
table. This makes the latter a derived attribute.
The platform is equipped to handle such situations without requiring custom logic to be written. We can address this particular case as follows :
- Since
student_batch_id
shall behave as the foreign key to theBatch
table, we add theforeign
attribute to its JSON blueprint :
"student_batch_id": {
"index": true,
"name": "student_batch_id",
"length": 128,
"type": "String",
"foreign": {
"resource": "batch"
}
}
The resource
attribute points to the Batch
table referenced by the foreign key student_batch_id
.
- Now, to ensure that the values of the
student_batch_name
field are filled in using values from theBatch
table, we add theforeign_derived
attribute to its blueprint.
"student_batch_name": {
"length": 128,
"type": "String",
"foreign_derived": {
"parent_field": "student_batch_id",
"foreign_field": "name"
}
}
parent_field
: Denotes the foreign key in theStudent
table.foreign_field
: Denotes the attribute in theBatch
table from which the value ofstudent_batch_name
is derived from.
Now, when one creates a student, only the student_batch_id
field needs to be supplied. And while querying for students, the student_batch_name
field will be filled in from the Batch
table.
This puts us in a position to completely specify the JSON blueprint for the StudentMovement
class - complete with derived fields and enums :
{
"student_movement": {
"cluster": "DB_PROFILE",
"ui": false,
"api": true,
"display_name": "Student Movement",
"fields": {
"id": {
"index": true,
"name": "id",
"length": 16,
"required": true,
"type": "String"
},
"student_id": {
"name": "student_id", "length": 128, "type": "String",
"foreign": {
"resource": "student"
}
},
"student_name": {
"length": 128, "type": "String", "name": "student_name",
"foreign_derived": {
"parent_field": "student_id",
"foreign_field": "student_name"
}
},
"entry_time": {
"type": "timestamp", "name": "entry_time",
},
"entry_gate": {
"type": "String", "length": 128, "name": "entry_gate",
},
"home_entry": {
"type": "boolean", "name": "home_entry",
},
"entry_type": {
"length": 128, "type": "String",
"possible_value": "org.rasp.iiitb360.resource.EntryType"
},
"student_batch_id": {
"name": "student_batch_id", "length": 128, "type": "String",
"foreign_derived": {
"parent_field": "student_id",
"foreign_field": "student_batch_id"
}
}
}
}
}