Change Variable Data Source
The data source is the definition of the database connection which includes:
- IP address
- DB type
- DB driver name
- User and password
- Special considerations, such as the library list
Multiple data sources can be defined in the global Data Source Configuration file (/mrcjava/WEB-INF/classes/mrc-spring-context.xml). In each dictionary (/mrcjava/WEB-INF/classes/DD/mrc_servlet_config.xml), you can assign a single data source for all applications within that dictionary with the datasource parameter. You can override that default data source selection at the program level by specifying another data source in the application properties file (/mrcjava/WEB-INF/classes/DD/IXXXXXp.xml).
Sometimes, though, as the application designer, you might prefer to set the data source conditionally. An example of that would be a case where you would like to allow the end user to select a company to work in. In these cases, it is possible to present the user with several companies that they may work in. Behind the scenes, the user's company selection will set the data source for the current browser session. The differences in the data source might be minor, different only because of the library list definition, or major, with different IP and DB types. Subsequent applications, then, will use the selected data source instead of a pre-defined one for the current browser session.
In the example above, you can see that despite the thousands of records, the user CROWLEY can only see the 2 companies (or data connections) to which he is assigned. Clicking on the Library name will set the library for the user's session.
Here are the steps to complete this process:
- Define your separate data sources.
- Define the id parameter as a value that can be derived in the Data Source Selection screen. Use "as400_remote_LIB10" and "as400_remote_LIB20" for example, if choosing between company 10 and 20. The DB prefix such as "as400_remote_" or "as400_local_" is required. The suffix will make it unique to the desired data source.
- Configure your Data Dictionary to enable variable data sources.
- Copy file mrcdatasource.xml from /mrcjava/mrcclasses/ into the DD directory.
- Configure the entry_url with the Data Source Selection screen that you will generate in the next step, such as DD.I00010s.
- Define any applications that should be excluded from the variable data source logic, instead of using the default data source connection. Use the applications tag for that purpose, with an app entry for each application to exclude. If you create the Data Source Selection screen in that same Data Dictionary, be sure to exclude that application, or you will cause an infinite loop of redirection.
- Add the variable datasource servlet entry to the web.xml.
- Edit the web.xml in a text editor from /mrcjava/WEB-INF/ and add the following entry at the end of the file before the </web-app> tag:
<servlet> <servlet-name>SetDataInSession</servlet-name> <servlet-class>SetDataInSession</servlet-class> </servlet> <servlet-mapping> <servlet-name>SetDataInSession</servlet-name> <url-pattern>/servlet/SetDataInSession</url-pattern> </servlet-mapping>
- Once done, make sure to save the file and restart Tomcat.
*Note As the web.xml is a global file, this step only needs to be done the very first time the Variable Datasources is being configured, and does not need to repeated per dictionary. Before adding to the web.xml, make sure this entry does not already exist by doing a search for 'SetDataInSession'.
- Edit the web.xml in a text editor from /mrcjava/WEB-INF/ and add the following entry at the end of the file before the </web-app> tag:
- Create a retrieval application that will serve as the Data Source Selection screen.
- This might be built over a file that selects records by user employing the secure_by user technique or it might be a list of the same options available to all users.
- Paint the application to include a link on every row like the following to set the data source:
- SetDataInSession?data_src=as400_remote_CO1&redirect_url=DD.I00100s
- The syntax, including the href tag and the text, might look like this:
<a href="SetDataInSession?data_src=as400_remote_LIB10&redirect_url=DD.I00100s">Company 10</a>
- End User Screen Flow:
- mrcSignon (end user authentication, optional)
- Data Source Selection (DD.I00010s, for example)
- DD.I00100s (application or menu of applications)
If the data source definitions will only be different due to the library list setup, then it will be necessary to configure the applications to avoid hard-coding the data library name in the SQL statement. That is done by setting the tablename_fmt parameter to "1" (Table name only) in the Data Dictionary properties file: mrc_servlet_config.xml. This will instruct the applications to use only the table name in the SQL, not the data library name which was used at development time, allowing the library list to determine which data library to use. If you have any individual applications that should use their data library name in the SQL statement, you can add that parameter at the program level and set it appropriately there.