How do I convert Fudge Message to blob?

Hi

Just wondering how do I convert blob to fudge message and read it as a string or access as an object?

Our code as per DbExchangeMaster

LobHandler lob = getDialect().getLobHandler();
byte[] bytes = lob.getBlobAsBytes(rs, “DETAIL”);
ManageableExchange exchange = FUDGE_CONTEXT.readObject(ManageableExchange.class, new ByteArrayInputStream(bytes));

Thank you very much, I appreciate all the help. I’m very new to OpenGamma, Spring Framework, Hibrinate and Fudge messages.

Hi

Can you give me an idea what I need to send as an object in DbConnector dbConnector

For example do do the following?
Connection conn = DriverManager.getConnection(“jdbc:hsqldb:”
+ db_file_name_prefix, // filenames
"OpenGamma", // username
"OpenGamma");

dbConnector = conn;

I know its incorrect as they are different data type but I just want to know how do I send the connection to dbConnector

A DbConnector is the OpenGamma general class for handling database connections. You do not work with DriverManager and Connection directly.

A DbConnector should be created using DbConnectorFactoryBean. That requires a DataSource, which is the standard element available from many places. If you need to create one manually, see SimpleDriverDataSource or BoneCPDataSource.

See this file for how we normally set this up:
/examples-simulated/config/fullstack/fullstack-examplessimulated-infrastructure-spring.xml

OK, please let me know if this is the correct way of connecting

Lets say for example I create a class: UiApplication

public class UiApplication extends AbstractDocumentDbMaster implements ExchangeMaster {

protected static final FudgeContext FUDGE_CONTEXT = OpenGammaFudgeContext.getInstance();

private static LobHandler lob;
private static ResultSet rs;
private static DataSource dataSource;
private static String dbname;
private static DbDialect dialect;
private static NamedParameterJdbcTemplate jdbcTemplate;
private static HibernateTemplate hibernateTemplate;
private static TransactionTemplate transactionTemplate;

public UiApplication(DbConnector dbConnector) throws SQLException {
super(dbConnector, IDENTIFIER_SCHEME_DEFAULT);
this.db = null;
dbname = dbConnector.getName();
dataSource= dbConnector.getDataSource();
dialect=dbConnector.getDialect();
jdbcTemplate =dbConnector.getJdbcTemplate();
hibernateTemplate = dbConnector.getHibernateTemplate();
transactionTemplate = dbConnector.getTransactionTemplate();
}

public static void main(String[] args) throws Exception {

DbConnector dbConnector = new DbConnector(dbname,
                                          dialect,
                                          dataSource,
                                          jdbcTemplate,
                                          hibernateTemplate,
                                          transactionTemplate);
UiApplication uiApp;
uiApp = new UiApplication(dbConnector);

lob = getDialect().getLobHandler();
this.bytes = lob.getBlobAsBytes(this.rs, “DETAIL”);
ManageableExchange exchange = FUDGE_CONTEXT.readObject(ManageableExchange.class, new ByteArrayInputStream(bytes));
}

You have two pieces of code here.

In UiApplication, you extend AbstractDocumentDbMaster which provides a getDbConnector() method, so there is no need to store the contents of the connector in instance variables.


public class UiApplication extends AbstractDbMaster {

protected static final FudgeContext FUDGE_CONTEXT = OpenGammaFudgeContext.getInstance();

public UiApplication(DbConnector dbConnector) {
super(dbConnector, “DbUi”);
}

public ManageableExchange load() {
// … make query to database, then read result set:
LobHandler lob = getDialect().getLobHandler();
byte[] bytes = lob.getBlobAsBytes(this.rs, “DETAIL”);
return FUDGE_CONTEXT.readObject(ManageableExchange.class, new ByteArrayInputStream(bytes));
}

public static void main(String[] args) throws Exception {
DbConnector dbConnector = new DbConnector(dbname, dialect,
dataSource, jdbcTemplate, hibernateTemplate, transactionTemplate);
UiApplication uiApp = new UiApplication(dbConnector);
ManageableExchange exg = uiApp.load();
}

I get the following error
Exception in thread “main” java.lang.IllegalArgumentException: Input parameter ‘name’ must not be null
at com.opengamma.util.ArgumentChecker.notNull(ArgumentChecker.java:92)
at com.opengamma.util.db.DbConnector.(DbConnector.java:95)
at com.opengamma.examples.bloomberg.server.UiApp.main(UiApp.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

I think its to do with dbname, dialect, dataSource, jdbcTemplate, HibernateTemplate, transactionTemplate values not initialised to a value?

Here is the code

public class UiApp extends AbstractDbMaster {

protected static final FudgeContext FUDGE_CONTEXT = OpenGammaFudgeContext.getInstance();
public static final String IDENTIFIER_SCHEME_DEFAULT = “DbExg”;
private ResultSet rs;

private static DataSource dataSource;
private static String dbname = dbConnector.getName();;
private static DbDialect dialect;
private static NamedParameterJdbcTemplate jdbcTemplate;
private static HibernateTemplate hibernateTemplate;
private static TransactionTemplate transactionTemplate;

public UiApp(DbConnector dbConnector) {
super(dbConnector, IDENTIFIER_SCHEME_DEFAULT);
dbname = dbConnector.getName();
dataSource= dbConnector.getDataSource();
dialect=dbConnector.getDialect();
jdbcTemplate =dbConnector.getJdbcTemplate();
hibernateTemplate = dbConnector.getHibernateTemplate();
transactionTemplate = dbConnector.getTransactionTemplate();
}

public ManageableExchange load() throws SQLException {
// … make query to database, then read result set:
LobHandler lob = getDialect().getLobHandler();
byte[] bytes = lob.getBlobAsBytes(rs, “DETAIL”);
return FUDGE_CONTEXT.readObject(ManageableExchange.class, new ByteArrayInputStream(bytes));
}

public static void main(String[] args) throws Exception {
DbConnector dbConnector;

dbConnector = new DbConnector(dbname, dialect,
                                          dataSource, jdbcTemplate, hibernateTemplate, transactionTemplate);
UiApp uiApp = new UiApp(dbConnector);
ManageableExchange exg = uiApp.load();

}
}

That code is rather strange as you never actually provide any data to setup the connector. This is a working example:

import java.io.ByteArrayInputStream;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.fudgemsg.FudgeContext;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.namedparam.EmptySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import com.opengamma.master.exchange.ManageableExchange;
import com.opengamma.util.db.DbConnector;
import com.opengamma.util.db.DbConnectorFactoryBean;
import com.opengamma.util.db.HibernateMappingFiles;
import com.opengamma.util.db.PostgresDbDialect;
import com.opengamma.util.fudgemsg.OpenGammaFudgeContext;

public class UiApp extends AbstractDbMaster {

protected static final FudgeContext FUDGE_CONTEXT = OpenGammaFudgeContext.getInstance();
public static final String IDENTIFIER_SCHEME_DEFAULT = “DbExg”;

public UiApp(DbConnector dbConnector) {
super(dbConnector, IDENTIFIER_SCHEME_DEFAULT);
}

public ManageableExchange load() throws SQLException {
// use Spring to query database, see Spring/Google/StackOverflow for more info
ManageableExchange exg = getJdbcTemplate().query(“SELECT * FROM SOMETABLE WHERE SOMETHING”, new ResultSetExtractor() {
@Override
public ManageableExchange extractData(ResultSet resultSet) throws SQLException, DataAccessException {
LobHandler lob = getDialect().getLobHandler();
byte[] bytes = lob.getBlobAsBytes(resultSet, “DETAIL”);
return FUDGE_CONTEXT.readObject(ManageableExchange.class, new ByteArrayInputStream(bytes));
}
});
return exg;
}

public static void main(String[] args) throws Exception {
// setup DataSource
SimpleDriverDataSource dataSource = new SimpleDriverDataSource(
new org.postgresql.Driver(), “jdbc:postgresql://localhost/ogdb”, “dbusername”, “dbpassword”);
// setup DbConnector
DbConnectorFactoryBean factory = new DbConnectorFactoryBean();
factory.setName(“MyConnector”);
factory.setDataSource(dataSource);
factory.setDialect(PostgresDbDialect.INSTANCE);
factory.setTransactionIsolationLevelName(“ISOLATION_READ_COMMITTED”);
factory.setTransactionPropagationBehaviorName(“PROPAGATION_REQUIRED”);
DbConnector dbConnector = factory.createObject();
// call database
UiApp uiApp = new UiApp(dbConnector);
ManageableExchange exg = uiApp.load();
}
}