📄 datapopulator.java
字号:
/*
* @author : Elangovan
* @version : 1.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the File : DataPopulator.java
*
* Creation / Modification History
* Elangovan 28-Aug-2003 Created
*
*/
package oracle.otnsamples.ibfbs.admin.helper;
import java.util.ArrayList;
import java.util.Collection;
import oracle.toplink.tools.sessionmanagement.SessionManager;
import oracle.toplink.sessions.DatabaseSession;
import oracle.toplink.sessions.UnitOfWork;
import oracle.toplink.tools.sessionconfiguration.XMLLoader;
import oracle.toplink.threetier.ServerSession;
import oracle.toplink.threetier.ClientSession;
import oracle.toplink.exceptions.DatabaseException;
import oracle.toplink.tools.schemaframework.SchemaManager;
import oracle.otnsamples.ibfbs.toplink.News;
/**
* This class creates the tables and sequences required by the FBS application.
* Once the schema is created, it populates it with sample data. The schema is
* created in the user specified in the data-source "jdbc/FBSDS".
*
* @see SampleData.java
* @see LoadDataServletCtxLsnr.java
*/
public class DataPopulator {
// Toplink Server session handle
private ServerSession server = null;
/**
* Creates a new instance of DataPopulator.
*/
public DataPopulator() {
}
/**
* Initilizes the Server session.
*
* @exception DatabaseException if intializing server session fails
*/
private void initServerSession()
throws DatabaseException {
// Initialize ServerSession from session.xml
try {
// Get session details from session.xml
server = (ServerSession) SessionManager.getManager().getSession(new XMLLoader(),
"FBSServerSession",
Thread.currentThread().getContextClassLoader());
} catch(DatabaseException dbEx) {
System.err.println(" Fatal Error : Could not connect to database, check "+
" if you have provided proper values in "+
"<SAMPLE_HOME>/config/META-INF/data-sources.xml !!" );
System.err.println(" Detailed Error : "+dbEx.toString());
throw dbEx;
}
}
/**
* Creates the tables and sequences required by FBS application and populates
* them with sample data.
*
* @exception DatabaseException if schema creation or population fails
*/
public void populateSampleData()
throws DatabaseException {
this.initServerSession();
// Initialize Schema manager
SchemaManager schemaManager = new SchemaManager((DatabaseSession) server);
// Check if data is already present
if(!isDataLoaded()) {
// Data not present
// Create tables
schemaManager.createObject(SampleData.getExchangeTableDefn());
schemaManager.createObject(SampleData.getSymbolTableDefn());
schemaManager.createObject(SampleData.getNewsTableDefn());
schemaManager.createObject(SampleData.getStockrateTableDefn());
// Create sequences
schemaManager.createObject(SampleData.getOracleSequenceDefn("NEWS_SEQ",50,50));
schemaManager.createObject(SampleData.getOracleSequenceDefn("PORTFOLIO_SEQ",1,1));
schemaManager.createObject(SampleData.getOracleSequenceDefn("ALERTS_SEQ",1,1));
schemaManager.createObject(SampleData.getOracleSequenceDefn("ACCOUNTNUMBER_SEQ",1,1));
schemaManager.createObject(SampleData.getOracleSequenceDefn("TRADE_SEQ",1,1));
schemaManager.createObject(SampleData.getOracleSequenceDefn("PREFERENCES_SEQ",1,1));
// Populate data
populateData();
}
}
/**
* Checks if data is loaded in FBS schema.
*
* @return true - if data exists, false otherwise
*/
private boolean isDataLoaded() {
boolean loaded = false;
try {
// Try to read a News object
Object obj = server.readObject(News.class);
if(obj != null) {
// Success
loaded=true;
}
} catch(Exception ex) {
// Data not present
loaded = false;
}
return loaded;
}
/**
* Populate FBS schema with sample data.
*
* @exception DatabaseException if populating sample data fails
*/
private void populateData()
throws DatabaseException {
// List to hold domain objects
Collection data = new ArrayList();
// Populate all data into 'data' Collection
data.addAll(SampleData.populateExchange());
data.addAll(SampleData.populateSymbol());
data.addAll(SampleData.populateStockRate());
data.addAll(SampleData.populateStockNews());
// Initialize client session
ClientSession client = server.acquireClientSession();
// Get a transaction
UnitOfWork unitOfWork = client.acquireUnitOfWork();
// New or Updated objects have to be registered to get inserted/updated in datasource
unitOfWork.registerAllObjects(data);
// Commit
unitOfWork.commit();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -