Adding a Model (Data Entity) - struct-by-lightning/wpi-suite GitHub Wiki

Model Overview

In WPI Suite TNG, a Model is a representation of database contents (or data from another persistent data source). Examples of Model data might be: A Defect in the DefectTracker module, a Project in the Core module. Models offers persistence and extensible functionality to the data layer.

How to add a Model

The following is a step-by-step instructional on creating/implementing a new Model. It does not describe the API Adapter implementation.

1. Create your Model class

Within the module's /models/ folder, create new class for your Model that implements the Model interface, which describes all of the methods necessary for a Model to work with the WPI Suite Core. AbstractModel is available for extending and has a few methods required to be able to be saved to the database already implemented. The following functions are needed for the Model to be sent over the network: toJSON and fromJSON. See the above mentioned example Models to see ways to implement these functions.

Additional Notes:

  • The member variables of the Model class represent the attributes of the data you would like to persist in the database.
  • Each model class should specify an attribute as the Model's 'unique identifier'. For example, the User Model has the 'username' field as its unique identifier attribute.
  • With the current database implementation, there are restrictions on what a Model can hold.

2. Create EntityManager for the Model

Within the the module's /entityManager/ folder, create an implementation of the EntityManager interface interface for the Model. The generic fields for the EntityManager<T, C> correspond to the following: (T) Model Class, (C) Datatype of the Model's unique identifier field. Entity managers are explained further here.

3. Register Model's EntityManager with the ManagerLayer

Data manipulation of the Model class is handled through the EntityManager package. Currently, the ManagerLayer, which you can find more information about here, keeps track of Entity Mangers.

To register your Entity Manager, you need to make the following changes to the method below from the ManagerLayer.

  1. Add your module's name to the list of module names on the last line of the example below. This will be the address of your module and will work for multiple Entity Managers in the same module.
  2. Put your Entity Manager in the HashMap of Entity Managers. Simply add a map.put("module" + "model", new ModelManager(data)); where model is the new Model, module is your current module, and ModelManager is the Entity Manager you wrote earlier.
private ManagerLayer()
	{
		data = DataStore.getDataStore();
		map = new HashMap<String, EntityManager>();
		sessions = new SessionManager();

		//TODO pull these mappings from some config file and reflect them
		map.put("coreproject", new ProjectManager(data));
		map.put("coreuser", new UserManager(data));
		map.put("defecttrackerdefect", new DefectManager(data));
		map.put("defecttrackercomment", new CommentManager(data));
		map.put("postboardpostboardmessage", new PostBoardEntityManager(data));
		map.put("requirementmanager" + "requirement", new RequirementEntityManager(data));
		map.put("requirementmanager" + "iteration", new IterationEntityManager(data));

		//add just your module to this list
		String[] fullModuleList = {"core","defecttracker","postboard","requirementmanager"};
                ...

The strings in the above code segment function as the address for your Entity Managers, which you will use when Networking with janeway.

⚠️ **GitHub.com Fallback** ⚠️