3.3 Managing the Data Repository (datasource.xml)
When you implement a service-based solution, it is often necessary to dynamically manipulate the data repository (data sources, data models, etc.). You can use the public API to perform such common tasks as dynamically expanding a developer's data model (as the developer creates new database fields) or adding an additional tenant connection.
The following code snippet illustrates how to gain access to the sample 'Orders' data source in the repository and add an additional tenant connection. The example also expands the data model to add a new attribute to the 'Product' entity which maps to a particular database field.
import inetsoft.uql.XFactory;
import inetsoft.uql.XRepository;
import inetsoft.uql.jdbc.JDBCDataSource;
import inetsoft.uql.service.XEngine;
import inetsoft.uql.erm.XDataModel;
import inetsoft.uql.erm.XLogicalModel;
import inetsoft.uql.erm.XEntity;
import inetsoft.uql.erm.XAttribute;
import inetsoft.uql.schema.XSchema;
...
System.setProperty("sree.home", ""{Absolute Path to your sree home location}");
// Get a handle to the data repository
XRepository dataRepository = XFactory.getRepository();
// Edit the data model in the Orders data source
XDataModel dataModel = dataRepository.getDataModel("Orders");
XLogicalModel logicalModel = dataModel.getLogicalModel("Order Model");
XEntity entity = logicalModel.getEntity("Product");
// Create an attribute to map to a field in the DB
XAttribute attribute = new XAttribute("Category Num", "SA.PRODUCTS", "CATEGORY_ID", XSchema.INTEGER);
// Add this attribute to the 'Product' entity
entity.addAttribute(attribute);
// Add an additional tenant connection
JDBCDataSource tenantConnection = new JDBCDataSource();
tenantConnection.setName("Orders Client1");
tenantConnection.setURL("jdbc:derby:classpath:orders");
tenantConnection.setDriver("org.apache.derby.jdbc.EmbeddedDriver");
tenantConnection.setUser("SA");
tenantConnection.setPassword("");
// Get a handle to the default 'Orders' data source
JDBCDataSource ordersDataSource = (JDBCDataSource)dataRepository.getDataSource("Orders");
ordersDataSource.addDatasource(tenantConnection);
// Finalize changes
dataRepository.updateDataModel(dataModel);
See Also
Semantic Layer – Data Model, in Data Modeling, for information on expanding a data model.
Design for Multi-Tenant Environment, for complete information about multi-tenancy.
| << 3.2 Creating a Custom Report List | © 1996-2013 InetSoft Technology Corporation (v11.5) | 3.4 Accessing the Servlet Context >> |