Managing Multiple Row Types in the Same Table
For each row in such a table, the target object type is represented by an instance of IlsSwRow whose name is specified is the dynamic view specification. The name of this representation object is considered as the row type.
Code Sample
subscribe origin Department:
represent IlsSwTable table:
Collector employesCollector=employees->employeeRow;
Collector managersCollector=managers->managerRow;
string rowType="Employee,employeeRow";
string collectorName="employesCollector";
...
propagate employees;
propagate managers;
subscribe Employee:
represent IlsSwRow employeeRow when (...):
mandatory Ref<IlsSwTable> table=view.origin->table;
...
represent IlsSwRow managerRow when (...):
mandatory Ref<IlsSwTable> table=view.origin->table;
...
This code sample can be illustrated as follows:
Multiple-Row-Type Table
Two rows represent Employee objects. They reference the same table but they are represented by two different row types: one is of type employeeRow, the other is of type managerRow.
In
Code Sample, Rogue Wave® Server subscribes to the two relations
employees and
managers and propagate notification to those two relations. Rogue Wave Server also creates two different rows for
employees and
managers but stores them in the same table.
Note that if you do not apply any representation condition for the row, each Employee will be represented by an employeeRow object and by a managerRow object.
To determine if Employee objects are stored in the employees relation or in the managers relation, you must specify a representation condition that is true only for one type of row. To do so in this example, you can use a predicate method isEmployee or isManager.
Warning: When representing several row types in the same table, each column of the table must be of the same type for each row type. This condition can also be expressed by the following rule: each row type schema must be compatible with the schema of the table it refers to. For example, you cannot use column1 to store a string attribute for one row type and an integer attribute for another row type referencing the same table. |
Creating a Row
When you insert a new row in this table, you have to specify:
the type the row you want to create,
the collector you want to use to insert this row.
The collector determines in which server relation the new server object will be stored.
Default Collector
Rogue Wave Server Studio generates a collector for all editable n-ary relations used to create a row. A default collector can be chosen using the Edit Table Representation Properties dialog box. This default collector is then notified to the table using a table predefined property: collectorName. You can specify which collector you want to use by calling the member function IlsSwTable::setDefaultCollector. In the previous example, the default collector is employeesCollector and the row created in the table creates an Employee object and adds it to the relation employees.
Row Type
If the table manages more than one row type, you have to select a row type before creating a new row. Rogue Wave Server Studio generates a default row type in the table predefined property rowType. This property references a server object type and a row type. In most cases, selecting a collector to insert the row uniquely determines which type of row must be created.
In
Code Sample, if you select the collector
managersCollector, the row created in the table will create an
Employee object that will be added to the relation
managers.
If the relation represented by the default collector can store objects of different types, you must specify what type of object must be created. To do so, you can use the function IlsSwTable::setRowType(serverType, rowType).
As the same row type can exist in different representations, you must specify the server type of the object you want to create.
Adding a new row to the table requires that the collector type be compatible with the row type you want to use:
The row type must be the representation name of an
IlsSwRow object that represents a server object.
The server object type represented by this row can be used as the target of the relation specified by the collector. It must be the type used for the target of the relation or a derived type.
In
Code Sample, let us assume that
Executive is a subclass of
Employee which is represented by a subtype of
IlsSwRow named
executiveRow in the same table. To create a new executive and add it to the
managers relation, you would have to write:
table->setDefaultCollector("managersCollector");
table->setRowType("Executive","executiveRow");
Adding a new row to the table now creates an Executive server object and adds it to the managers relation of the class Department.
Deleting a Row
The member function IlsRpObject::onRmFromCollection is called when you delete a row from the data source.
When you suppress a row from a data source table, Rogue Wave Server needs to know from which collection the corresponding server object must be removed. Using the row type, Rogue Wave Server tries to find the corresponding collector and, from there, infers what relation must be updated.
In
Code Sample, if you remove a
managerRow object, Rogue Wave Server automatically selects the collector
managersCollector to remove the row. As a result, the corresponding employee is removed from the
managers relation of the class
Department.
In case of inheritance, it is not always possible to determine which collector to use. In that case, Rogue Wave Server tries to remove the row using the default collector , but if this action is not allowed in the server (for instance, because the server object is not stored in this relation), the server will rolls back the transaction. To handle this case, you have to specify the right collector using the IlsSwTable API.
In
Code Sample, if you have created an
executive row and stored it in the
managers relation, you must specify the
managersCollector collector via the member function
IlsSwTable::setDefaultCollector before removing the row. The
IlsSwRow::DeleteRowSymbol callback can be useful to achieve this operation.
Version 6.3
Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.