package com.oracle.determinations.interview.engine.userplugins.commentary;
import java.io.StringBufferInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.oracle.determinations.interview.engine.InterviewSession;
import com.oracle.determinations.interview.engine.exceptions.UnsupportedException;
import com.oracle.determinations.interview.engine.plugins.InterviewSessionPlugin;
import com.oracle.determinations.interview.engine.plugins.InterviewSessionRegisterArgs;
import com.oracle.determinations.interview.engine.plugins.commentary.CommentaryProviderPlugin;
import com.oracle.determinations.interview.util.TypedInputStream;
/**
* Examples and Tutorials - Plugins - Commentary - Sample Code (DerbyCommentary)
*/
public class DerbyCommentary implements CommentaryProviderPlugin {
private String driver = "org.apache.derby.jdbc.ClientDriver";
private String dbName = "commentary";
private String connectionURL = "jdbc:derby://localhost:1527/" + dbName;
private Connection connection = null;
private Statement statement;
/**
* Constructor
*/
public DerbyCommentary() {
try {
Class.forName(driver);
System.out.println(driver + "loaded.");
} catch (ClassNotFoundException e) {
System.err.print("Class not found: " + e.getMessage());
}
}
/**
* Connects to the database and creates a statement
*
* @throws SQLException
*/
private void connectDBObjects() throws SQLException {
connection = DriverManager.getConnection(connectionURL);
System.out.println("Connected to database " + dbName);
statement = connection.createStatement();
}
/**
* Closes database objects and terminates the connection
*
* @throws SQLException
*/
private void closeDBObjects() throws SQLException {
statement.close();
connection.close();
System.out.println("Closed connection");
}
/**
* Executes an SQL statement
*
* @param sql
* the sql statement to execute
* @param filter
* the filter to the rows
* @param field
* the field to retrieve
* @return the input stream containing the value of the field
* @throws RuntimeException
*/
private TypedInputStream executeSQL(String sql, String filter, String field)
throws RuntimeException {
TypedInputStream content = null;
try {
connectDBObjects();
if (statement.execute(String.format(sql, filter))) {
ResultSet result = statement.getResultSet();
if (result.next()) {
content = new TypedInputStream(
"text/html",
new StringBufferInputStream(new String(result.getString(field))));
}
result.close();
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
try {
closeDBObjects();
} catch (SQLException e) {
throw new RuntimeException(e.getMessage());
}
}
return content;
}
/** ******************************** */
/* Web-determinations method calls */
/** ******************************** */
/**
* Returns the commentary content for the target if hasCommentary(session,
* target) is satisfied
*/
public TypedInputStream getCommentaryContent(InterviewSession session,
String target) {
TypedInputStream commentContent = null;
try {
// build full target, i.e. with locale
String fullTarget = session.getLocale() + "/" + target;
commentContent = executeSQL(
"select COMMENT_CONTENT from COMMENTARY where COMMENT_LOCATION='%s'",
fullTarget,
"COMMENT_CONTENT"
);
} catch (RuntimeException e) {
throw e;
}
return commentContent;
}
/**
* Should not be called at all because we are returning false in
* isCommentaryRedirect(session, target)
*/
public String getCommentaryURL(InterviewSession session, String target) {
throw new UnsupportedException(
"Should not call getCommentaryURL when is isCommentaryRedirect returns false.");
}
/**
* Returns true if the commentary for the target is available
*/
public boolean hasCommentary(InterviewSession session, String target) {
TypedInputStream commentLocation = null;
// build full target, i.e. with locale
String fullTarget = session.getLocale() + "/" + target;
try {
commentLocation = executeSQL(
"select COMMENT_LOCATION from COMMENTARY where COMMENT_LOCATION='%s'",
fullTarget,
"COMMENT_LOCATION"
);
} catch (RuntimeException e) {
throw e;
}
return (commentLocation != null);
}
/**
* Returns true if the commentary for this Web Determinations Interview is
* available
*/
public boolean isCommentaryEnabled(InterviewSession session) {
// check database connection
try {
connectDBObjects();
closeDBObjects();
return true;
} catch (Exception e) {
return false;
}
}
/**
* The example always returns false because the plugin doesn't redirect to
* another URL to fetch commentary content.
*/
public boolean isCommentaryRedirect(InterviewSession session, String target) {
return false;
}
/**
* Registers the plugin if and only if the rulebase is
* 'ExtFrameworkCommentary'
*/
public InterviewSessionPlugin getInstance(InterviewSessionRegisterArgs args) {
System.out.println("Rulebase identifier is " + args.getSession().getRulebase().getIdentifier());
if (args.getSession().getRulebase().getIdentifier().equals("ExtFrameworkCommentary")) {
return new DerbyCommentary();
}
return null;
}
}