In the Ada Web Application: Setting up the project we have seen how to create a new AWA project. In this second article, we will see how to design the UML model, generate the Ada code and create the database tables from our UML design.
Introduction
A Model driven engineering or MDE promotes the use of models to ease the development of software and systems. The Unified Modeling Language is used to modelize various parts of the software. UML is a graphical type modelling language and it has many diagrams but we are only going to use one of them: the Class Diagram.
The class diagram is probably the most powerful diagram to design, explain and share the data model of any application. It defines the most important data types used by an application with the relation they have with each other. In the class diagram, a class represents an abstraction that encapsulates data member attributes and operations. The class may have relations with others classes.
For the UML model, we are going to use ArgoUML that is a free modelization tool that works pretty well. For the ArgoUML setup, we will use two profiles:
- The Dynamo profile that describes the base data types for our UML model. These types are necessary for the code generator to work correctly.
- The AWA profile that describes the tables and modules provided by AWA. We will need it to get the user UML class definition.
These UML profiles are located in the /usr/share/dynamo/base/uml
directory after Dynamo and AWA are installed. To configure ArgoUML, go in the Edit -> Settings
menu and add the directory in the Default XMI directories
list. Beware that you must restart ArgoUML to be able to use the new profiles.
Modelize the domain model in UML
The UML model must use a number of Dynamo artifacts for the code generation to work properly. The artifact describes some capabilities and behavior for the code generator to perform its work. Stereotype names are enclosed within markers. Dynamo uses the following stereotypes:
- The
DataModel
stereotype must be applied on the package which contains the model to generate. This stereotype activates the code generation (other packages are not generated). - The
Table
stereotype must be applied to the class. It controls which database table and Ada type will be generated. - The
PK
stereotype must be defined in at most one attribute of the class. This indicates the primary key for the database table. The attribute type must be an integer or a string. This is a limitation of the Ada code generator. - The
Version
stereotype must be applied on the attribute that is used for the optimistic locking implementation of the database layer.
In our UML model, the Review
table is assigned the Table
stereotype so that an SQL table will be created as well as an Ada tagged type to represent our table. The id
class attribute represents the primary key and thus has the PK
stereotype. The version
class attribute is the database column used by the optimistic locking implementation provided by ADO. This is why is has the Version
stereotype. The title
, site
, create_date
, text
and allow_comments
attributes represent the information we want to store in the database table. They are general purpose attributes and thus don't need any specific stereotype. For each attribute, the Dynamo code generator will generate a getter and a setter operation that can be used in the Ada code.
To tune the generation, several UML tagged values can be selected and added on the table or on a table attribute. By applying a stereotype to the class, several tagged values can be added. By selecting the Tagged Values
tab in ArgoUML we can edit and setup new values. For the Review
table, the dynamo.table.name
tagged value defines the name of the SQL database table, in our case atlas_review
.
The text
attribute in the Review
table is a string that can hold some pretty long text. To control the length of the SQL column, we can set the dynamo.sql.length
tagged value and tell what is that length.
Once the UML model is designed, it is saved in the project directory uml
. Dynamo will be able to read the ArgoUML file format (.zargo
extension) so there is no need to export the UML in XMI.
The Review application UML model
The final UML model of our review application is fairly simple. We just added a table and a bean declaration. To benefit from the user management in AWA, we can use the AWA::Users::Models::User
class that is defined in the AWA UML model. The reviewed-by
association will create an attribute reviewer
in our class. The code generator will generate a Get_Reviewer
and Set_Reviewer
operation in the Ada code. The SQL table will contain an additional column reviewer
that will hold the primary key of the reviewer.
The Review_Bean
class is an Ada Bean abstract class that will be generated by the code generator. The Bean
stereotype activates the bean code generator and the generator will generate some code support that is necessary to turn the Review_Bean
tagged record into an Ada Bean aware type. We will see in the next tutorial that we will only have to implement the save
and delete
operation that are described in this UML model.
Makefile setup
The Makefile.in
that was generated by the Dynamo create-project
command must be updated to setup a number of generation arguments for the UML to Ada code generator. Edit the Makefile.in
to change:
DYNAMO_ARGS=--package Atlas.Reviews.Models db uml/atlas.zargo
The --package
option tells Dynamo to generate only the model for the specified package. The db
directory is the directory that will contain the SQL model files.
Once the Makefile.in
is updated, the Makefile
must be updated by using the following command:
./config.status
Or if you prefer, you may run again the configure
script to re-configure the whole project.
We need the code!!
To run the generator, we can use the generate
make target:
make generate
The Dynamo code generator reads the file uml/atlas.zargo
and the UML model it contains and generates:
- the Ada package
Atlas.Reviews.Models
which contains the definition of theReview
table. The model files are created in the directorysrc/models
which is separate from your Ada sources. - the SQL files to create the MySQL or SQLite database. Depending on the AWA modules which are used, the generated SQL files will contain additional tables that are used by the AWA modules. The SQL files are generated in the
db/mysql
anddb/sqlite
directories.
Let's create the database
Until now we designed our application UML model, we have our Ada code generated, but we need a database with the tables for our application. We can do this by using the create-database
command in Dynamo. This command needs several arguments:
- The directory that contains the SQL model files. In our case, this is
db
. - The information to connect to the database, the database name, the user and its password. This information is passed in the form of a database connection string.
- The name of the database administration account to connect to the server and create the new database.
- The optional password for the database administration account.
If the MySQL server is running on your host and the admin account does not have any password, you can use the following command:
dynamo create-database db 'mysql://localhost/demo_atlas?user=demo&password=demo' root
The create-database
creates the database (demo_atlas
) with the tables that are necessary for the application. It also creates the demo
user and give it the necessary MySQL grants to connect to the demo_atlas
database.
The Review Web Application UML video
To help you in building the UML model and see who the whole process looks like in reality, I've created the following short video that details the above tutorial steps.
Conclusion
Thanks to ArgoUML and Dynamo, generating the Ada model and database tables becomes a simple and fun task. We have not written any line of code yet in this Review Web Application project, everything has been generated but we achieved a big progress:
- The Review Web Application server is built and can be launched,
- The database is initialized and contains our application data model schema.
The next tutorial will explain how to design the review form, implement the operations to create and populate the database with the new review.