Continued from here.
The src folder of serverbompopulate contains two classes DomainValueProvider and DomainHelper. DomainValueProvider class is the implementation of the IlrBOMDomainValueProvider class. It Relies on a DomainHelper instance to retrieve the item name, verbalization and translation of the static references defined in the Database.
This helper class combines connectivity properties for a database along with specific settings for BOM dynamic domains. A static function provides a helper for a dedicated fullyqualified class, which is responsible for providing the db values. Apart from the methods in the DomainValueProvider class which are used to set the item name, verbalization and translation ,it has a method called getValues() which has the following code:
public Collection<String> getValues(IlrClass clazz) {
this.className = clazz.getFullyQualifiedName();
DomainHelper helper = DomainHelper.getDomainHelper(className);
helper.initValues();
return helper.getItemNames();
}
Here IlrClass is the representation of classes in the object model. For eg., we have created a virtual class in bom with name Currency in trade package.
Here the currency used in the rule is of type “trade.Currency”. So when we access the currency attribute, the IlrClass will be representing the Currency class. clazz.fullyQualifiedName() returns the trade.Currency which will be assigned to className attribute.
DomainHelper class constructor is defined as private, so we cannot create the object directly. Hence getDomainHelper() is called for creating the object for DomainHelper.
public static DomainHelper getDomainHelper(String className) {
DomainHelper helper = classdomains.get(className);
if (helper == null) {
helper = new DomainHelper(className);
classdomains.put(className, helper);
}
return helper;
}
Here classdomains is the HashMap which is defined as follows
private static HashMap<String,DomainHelper> classdomains = new HashMap<String, DomainHelper>
DomainHelper helper = classdomains.get(className) code tries to find whether there is already an object in the HashMap with the specified key i.e,trade.Currency. If the object exists, the same object will be assigned to the helper object or else, it will call the parameterized constructor of DomainHelperclass which is private.
private DomainHelper(String fullyQualifiedName) {
this.className = fullyQualifiedName;
}
This.className refers to the attribute which is defined in the DomainHelper class with String type. In this context, “trade.Currency” string will be assigned to the className attribute. The execution returns to the getDomainHelper(). classdomains.put(className, helper); code adds the className which is trade.Currency as key and the DomainHelper object with refence as helper.
This helper object will be returned to the DomainValueProvider class. Then the code helper.initValues() in the getValues() calls the initValues() in the DomainHelper class. Here the initValues() is as follows
public void initValues() {
domainItems.clear();
if (this.className.equals("trade.StatusType")) {
readValuesFromStatusTable();
} else if (this.className.equals("trade.CurrencyType")) {
readValuesFromCurrencyTable();
} else if (this.className.equals("trade.MessageType")) {
readValuesFromMessagesTable();
}
}
Here the domains items is the ArrayList which is declared as follows
private List<Item> domainItems = new ArrayList<Item>();
Here Item is a static inner class which contains three attributes i.e., name, verbalization and code. When the className string equals any of the following strings, the corresponding method will be called.
In the example we have assumed, the className string is set to “trade.Currency”, so readValuesFromCurrencyTable(); will be called.
public void readValuesFromCurrencyTable() {
Connection connection = DataBaseConnector.openConnection();
if (connection != null) {
try {
String query = "SELECT * FROM BOMDOMAINSAMPLE.CURRENCY";
Statement statement = connection.createStatement();
ResultSet set = statement.executeQuery(query);
domainItems.clear();
while (set.next()) {
String name = set.getString(1);
String verbalization = set.getString(2);
String translation = set.getString(3);
domainItems.add(new Item(name, verbalization, translation));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
DataBaseConnector.closeConnection();
}
This method executes the select statement to fetch the values from the DB and it will be placed in the ResultSet object and every record will be added to the “domainItems” arraylist. Now the execution flow returns to getValues() of DomainValueProvider class and return helper.getItemNames(); returns the domain values to the Currency Class.
0 comments:
Post a Comment