package com.oracle.determinations.interview.exampleplugins;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import com.oracle.determinations.interview.engine.EngineConfiguration;
import com.oracle.determinations.interview.engine.InterviewRulebase;
import com.oracle.determinations.interview.engine.SecurityToken;
import com.oracle.determinations.interview.engine.plugins.InterviewEnginePlugin;
import com.oracle.determinations.interview.engine.plugins.InterviewEngineRegisterArgs;
import com.oracle.determinations.interview.engine.plugins.rulebaseservice.RulebaseServicePlugin;
/**
* Tutorials and Examples - Plugins Rulebase Service - Sample Code
* (DerbyRulebaseService)
*
* This plugin expects a database named 'rulebases' containing a table called
* 'rulebases' with the following schema:
*
* rulebaseidentifier : VARCHAR(255) PRIMARY KEY
* rulebase : BLOB
*/
public class DerbyRulebaseService implements RulebaseServicePlugin {
// columns for the table rulebases
private final static String RULEBASE_IDENTIFIER_COLUMN = "rulebaseidentifier";
private final static String RULEBASE_COLUMN = "rulebase";
// DERBY
private final static String driver = "org.apache.derby.jdbc.ClientDriver";
private final static String dbName = "rulebases";
private final static String tableName = "rulebases";
private Connection connection = null;
private Statement statement = null;
private String connectionURL = "jdbc:derby://localhost:1527/" + dbName
+ ";";
private EngineConfiguration configuration = null;
private HashMap<String, InterviewRulebase> rulebases = new HashMap<String, InterviewRulebase>();
private long listVersion = 0;
/**
* Constructor with no parameters is requied by the plugin architecture.
*/
public DerbyRulebaseService() {
}
/**
* Constructor
*
* @param config the interview engine configuration
* @throws ClassNotFoundException
*/
public DerbyRulebaseService(EngineConfiguration config)
throws ClassNotFoundException {
Class.forName(driver);
configuration = config;
}
/**
* Connects to the database and creates database objects
*
* @throws SQLException
*/
private void connectDBObjects() throws SQLException {
connection = DriverManager.getConnection(connectionURL);
System.out.println("Connected to database " + dbName);
statement = connection.createStatement();
}
/**
* Closes open database objects and terminates the connection
* @throws SQLException
*/
private void closeDBObjects() throws SQLException {
if (statement != null) {
statement.close();
statement = null;
}
if (connection != null) {
connection.close();
connection = null;
}
System.out.println("Closed connection");
}
/**
* Use for authenticating a user using the security token
* @param token
* @return true to allow or false
*/
private boolean authenticateUser(SecurityToken token) {
// implement security authentication here
return true;
}
/**
* Queries the databases for rulebases and populates the map for rulebases
*/
private void initializeRulebases() {
try {
connectDBObjects();
if (statement.execute("SELECT * FROM " + tableName)) {
rulebases.clear();
ResultSet result = statement.getResultSet();
while (result.next()) {
String identifier = result
.getString(RULEBASE_IDENTIFIER_COLUMN);
Blob blob = result.getBlob(RULEBASE_COLUMN);
rulebases.put(identifier, new InterviewRulebase(identifier,
blob.getBinaryStream(), configuration));
System.out.println("Rulebase '" + identifier + "' found in database.");
}
result.close();
listVersion++;
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
try {
closeDBObjects();
} catch (SQLException e) {
throw new RuntimeException(e.getMessage());
}
}
}
/** ******************************** */
/* Web Determinations Method Calls */
/** ******************************** */
/**
* Returns a rulebase that has been loaded
*/
public InterviewRulebase getRulebase(SecurityToken token, String identifier) {
InterviewRulebase result = null;
if (authenticateUser(token)) {
if (rulebases.isEmpty()) {
initializeRulebases();
}
if (rulebases.containsKey(identifier)) {
result = rulebases.get(identifier);
}
if (result == null){
throw new IllegalArgumentException("No rulebase with identifier '" +
identifier + "' was found.");
}
}
return result;
}
/**
* Forces the list of rulebases to be refreshed for the
* rulebase to be reloaded
*/
public InterviewRulebase loadRulebase(SecurityToken token, String identifier) {
System.out.println("loadRulebase");
InterviewRulebase result = null;
if (authenticateUser(token)) {
initializeRulebases();
if (rulebases.containsKey(identifier)) {
result = rulebases.get(identifier);
}
if (result == null){
throw new IllegalArgumentException("No rulebase with identifier '" +
identifier + "' was found.");
}
}
return result;
}
/**
* Returns the version of the rulebase list
*/
public long getRulebaseListVersion() {
return listVersion;
}
/**
* Returns the rulebases that are stored in database
*/
public InterviewRulebase[] listRulebases(SecurityToken token) {
System.out.println("listRulebases");
InterviewRulebase[] results = new InterviewRulebase[] {};
if (authenticateUser(token)) {
initializeRulebases();
results = rulebases.values().toArray(
new InterviewRulebase[rulebases.size()]);
}
return results;
}
/**
* Registers the rulebase service plugin
*/
public InterviewEnginePlugin getInstance(InterviewEngineRegisterArgs args) {
InterviewEnginePlugin plugin;
try {
System.out.println("getInstance");
plugin = new DerbyRulebaseService(args.getEngine().getConfig());
connectDBObjects();
} catch (Exception e) {
// DERBY driver was not found, do not register
System.out.println(e.getMessage());
plugin = null;
} finally {
try {
closeDBObjects();
} catch (SQLException e) {
throw new RuntimeException(e.getMessage());
}
}
return plugin;
}
}