package com.oracle.determinations.interview.engine.userplugins;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.oracle.determinations.engine.Attribute;
import com.oracle.determinations.interview.engine.InterviewSession;
import com.oracle.determinations.interview.engine.plugins.InterviewSessionPlugin;
import com.oracle.determinations.interview.engine.plugins.InterviewSessionRegisterArgs;
import com.oracle.determinations.interview.engine.plugins.listprovider.ListProviderPlugin;
import com.oracle.determinations.interview.engine.screens.InputInterviewControl;
import com.oracle.determinations.interview.engine.screens.ListOption;
public class DerbyListProvider implements ListProviderPlugin {
// ## DEFINE VARIABLES SECTION ##
// define the driver to use
String driver = "org.apache.derby.jdbc.ClientDriver";
// the database name
String dbName="listprovider";
// define the Derby connection URL to use
String connectionURL = "jdbc:derby://localhost:1527/" + dbName + ";";
Connection conn = null;
Statement s;
private static String STATES_TABLE = "AU_STATES";
private static String STATES_TABLE_VALUE_COL = "STATE_SHORTNAME";
private static String STATES_TABLE_DISPLAY_COL = "STATE_FULLNAME";
private static String POLITICAL_ALIGNMENT_TABLE = "POLITICAL_ALIGNMENTS";
private static String PA_TABLE_VALUE_COL = "PA_NAME";
private static String PA_TABLE_DISPLAY_COL = "PA_NAME";
/**
* parameter-less constructor to satisfy Plugin requirement
*/
public DerbyListProvider()
{
try {
Class.forName(driver);
System.out.println(driver + " loaded. ");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
}
public List getListOptions(InputInterviewControl controlInstance,
InterviewSession session) {
List listOptions = new ArrayList();
Attribute controlAttribute = controlInstance.getAttribute(); //Get the Attribute assigned to the InputControl
//Load dataset for rulebase attribute 'state' or 'political alignment'
try {
connectDBObjects();
String tableToUse = "";
String displayTextColumnToUse = "";
String valueColumnToUse = "";
//Determine what database table to use based on the attribute of the List InputControl
if(controlAttribute.getName().equals("state"))
{
tableToUse = STATES_TABLE;
displayTextColumnToUse = STATES_TABLE_DISPLAY_COL;
valueColumnToUse = STATES_TABLE_VALUE_COL;
}
if(controlAttribute.getName().equals("political_alignment"))
{
tableToUse = POLITICAL_ALIGNMENT_TABLE;
displayTextColumnToUse = PA_TABLE_DISPLAY_COL;
valueColumnToUse = PA_TABLE_VALUE_COL;
}
if(tableToUse != "" && displayTextColumnToUse != "" && valueColumnToUse != "")
{
if(s.execute("select * from " + tableToUse))
{
ResultSet rs = s.getResultSet();
while (rs.next())
{
ListOption currentListOption = new ListOption(rs.getString(valueColumnToUse), rs.getString(displayTextColumnToUse));
listOptions.add(currentListOption);
}
rs.close();
return listOptions;
}
else
{
return null; //returning null means that Web Determinations will use the default list of values provided by the rule author in Oracle Policy
Modeling
}
}
else
{
return null;
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
finally{
try {
closeDBObjects();
} catch (SQLException e) {
throw new RuntimeException(e.getMessage());
}
}
}
public InterviewSessionPlugin getInstance(InterviewSessionRegisterArgs args) {
//Demonstration of a Plugin only registering if the current rulebase is 'ExtFrameworkListProvider'
if (args.getSession().getRulebase().getIdentifier().equals("ExtFrameworkListProvider")){
return new DerbyListProvider();
} else {
return null;
}
}
private void closeDBObjects() throws SQLException {
s.close();
conn.close();
System.out.println("Closed connection");
}
private void connectDBObjects() throws SQLException {
conn = DriverManager.getConnection(connectionURL);
System.out.println("Connected to database " + dbName);
s = conn.createStatement();
}
}