📄 newshelper.java
字号:
/*
* @author : Elangovan
* @version 1.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the File : NewsHelper.java
*
* Creation / Modification History
* Elangovan 20-Aug-2003 Created
*
*/
package oracle.otnsamples.ibfbs.usermanagement.helper;
import java.util.Collection;
import java.util.ArrayList;
import oracle.toplink.tools.sessionmanagement.SessionManager;
import oracle.toplink.sessions.UnitOfWork;
import oracle.toplink.tools.sessionconfiguration.XMLLoader;
import oracle.toplink.threetier.ServerSession;
import oracle.toplink.threetier.ClientSession;
import oracle.toplink.expressions.ExpressionBuilder;
import oracle.toplink.queryframework.ReadAllQuery;
import oracle.toplink.queryframework.ReportQuery;
import oracle.toplink.exceptions.DatabaseException;
import oracle.otnsamples.ibfbs.toplink.News;
import oracle.otnsamples.ibfbs.usermanagement.exception.UserManagementEventException;
/**
* This class encapsulates and manages all access to News information. It
* loads/fetches latest news using Toplink persistence framewok.
*
* @see oracle.otnsamples.ibfbs.toplink.News.java
*/
public class NewsHelper {
// Toplink server session
private ServerSession server = null;
/**
* Creates a new instance of NewsHelper
*
* @exception UserManagementEventException if initializing server session fails
*/
public NewsHelper()
throws UserManagementEventException{
try {
// Initialize ServerSession from session.xml
server = (ServerSession) SessionManager.getManager().getSession(new XMLLoader(),
"FBSServerSession",
Thread.currentThread().getContextClassLoader());
}catch(DatabaseException dbEx) {
throw new UserManagementEventException(" Error initializing Server Session :"+dbEx);
}
}
/**
* Method to get the Stock News for the particular symbol.
*
* @param symbol Stock Symbol of the Company in which user is interested.
* @return Collection containing the News objects corresponding to the given symbol.
* @exception UserManagementEventException if retrieving latest news fails
*
*/
public Collection getLatestNews(String symbol)
throws UserManagementEventException {
ClientSession client = null;
ArrayList newsList = new ArrayList(3);
try {
// Initialize client session
client = server.acquireClientSession();
// Initilize read query to read multiple news items
ReadAllQuery readNews = new ReadAllQuery(News.class);
ExpressionBuilder newsBuilder = new ExpressionBuilder();
ExpressionBuilder symbolBuilder = new ExpressionBuilder();
ReportQuery subQuery = new ReportQuery(News.class, symbolBuilder);
// SELECT MAX(NewsDate) FROM News WHERE Symbol = ?
subQuery.addMaximum("newsdate");
subQuery.setSelectionCriteria(symbolBuilder.get("symbol").equalsIgnoreCase(symbol));
// Specify the return collection class [ default: Vector ]
readNews.useCollectionClass(ArrayList.class);
// SELECT News WHERE Symbol = ? AND NewsDate = ?
readNews.setSelectionCriteria(newsBuilder.get("newsdate").equal(subQuery).and(
newsBuilder.get("symbol").equalsIgnoreCase(symbol)));
newsList.addAll((ArrayList)client.executeQuery(readNews));
} catch (DatabaseException ex) {
throw new UserManagementEventException(" Error while getting "
+ " News:\n" + ex.toString());
}
return newsList;
}
/**
* Method to populate News into the database tables.
*
* @param newsCollection Collection of News objects
* @exception DatabaseException if retrieving news fails
*/
public void loadNews(Collection newsCollection)
throws DatabaseException {
// Initialize client session
ClientSession client = server.acquireClientSession();
// Enable batch-writing [ Write Optimization ]
client.getLogin().useBatchWriting();
// Get a transaction
UnitOfWork uow = client.acquireUnitOfWork();
// Register the objects with transaction
uow.registerAllObjects(newsCollection);
// Commit
uow.commit();
}
/**
* Method to get the Stock News for a list of symbols.
*
* @param symbols list of symbols for which stock news is required
* @return Collection containing the News objects corresponding to the given symbols.
* @exception UserManagementEventException if retrieving latest news fails
* @since 1.0
*/
public Collection getLatestNews(String[] symbols)
throws UserManagementEventException {
ClientSession client = null;
ArrayList newsList = new ArrayList(symbols.length);
try {
client = server.acquireClientSession();
// Initialize read query to read multiple news items
ReadAllQuery readNews = new ReadAllQuery(News.class);
ExpressionBuilder newsBuilder = new ExpressionBuilder();
ExpressionBuilder symbolBuilder = new ExpressionBuilder();
ReportQuery subQuery = new ReportQuery(News.class, symbolBuilder);
subQuery.addMaximum("newsdate");
for(int i=0;i<symbols.length;i++) {
// SELECT MAX(NewsDate) FROM News WHERE Symbol = ?
subQuery.setSelectionCriteria(symbolBuilder.get("symbol").equalsIgnoreCase(symbols[i]));
// Specify to use return collection as ArrayList [ default: Vector ]
readNews.useCollectionClass(ArrayList.class);
// SELECT News WHERE Symbol = ? AND NewsDate = ?
readNews.setSelectionCriteria(newsBuilder.get("newsdate").equal(subQuery)
.and(newsBuilder.get("symbol").equalsIgnoreCase(symbols[i])));
newsList.addAll((ArrayList)client.executeQuery(readNews));
}
} catch (DatabaseException ex) {
throw new UserManagementEventException(" Error while getting "
+ " Stock News:\n" + ex.toString());
}
return newsList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -