📄 webserviceshelper.java
字号:
/*
* @author : Elangovan
* @version : 1.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the File : WebServiceHelper.java
*
* Creation / Modification History
* Elangovan 26-Apr-2002 Created
* Elangovan 19-Aug-2003 Modified
*
* This class uses Google Web APIs(TM) service for fetching the latest finance news.
*
* By using this service ("Google Web APIs") you agree to be bound by the
* terms and conditions (the "Terms and Conditions").
*
* For "Terms and Conditions" refer to http://www.google.com/apis/api_terms.html
*
* Copyright (c) 2002 Google
*
*/
package oracle.otnsamples.ibfbs.admin.helper;
import java.util.Iterator;
import java.util.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Collection;
import java.net.URLEncoder;
import oracle.otnsamples.ibfbs.utils.ConnectionParams;
import oracle.otnsamples.ibfbs.toplink.News;
import oracle.otnsamples.ibfbs.toplink.Symbol;
import oracle.otnsamples.ibfbs.toplink.Stockrate;
import oracle.otnsamples.ibfbs.admin.exception.WebServiceAccessException;
/**
* This class access the webservices to fetch news and stockrates for the given
* symbols.
*
* @see GoogleSearchResult.java
* @see GoogleSearchServiceStub.java
* @see ResultElement.java
*/
public class WebServicesHelper {
private GoogleSearchServiceStub search = null;
private XMethodsStockQuoteServiceStub stockService = null;
/**
* Constructor, initializes the proxy connection properties and the news
* search class.
*
* @since 1.0
*/
public WebServicesHelper() {
// Initialize Google Search stub
search = new GoogleSearchServiceStub();
// Initialize stock quote service stub
stockService = new XMethodsStockQuoteServiceStub();
}
/**
* This method retrieves the latest news for a list of symbol by accessing
* the news webservice.
*
* @param symbols list of stock symbols
* @param maxResults maximun number of results for the list of symbols
* @return News for all given symbols
* @exception WebServiceAccessException if accessing webservice fails
*/
public Collection fetchLatestNews(Collection symbols, int maxResults)
throws WebServiceAccessException {
ArrayList newsList = new ArrayList();
Date today = new Date();
int julianDate = this.toJulian(today);
Iterator symIter = symbols.iterator();
try {
while(symIter.hasNext()) {
Symbol symbol = (Symbol)symIter.next();
// Cook the search string
String searchQuery = new StringBuffer().append("intitle:")
.append(symbol.getCompanyname())
.append(" finance stock news")
.append(" daterange:")
.append(julianDate-30)
.append("-")
.append(julianDate).toString();
// Search
GoogleSearchResult searchResult = search.doGoogleSearch(
ConnectionParams.googleSearchKey,
searchQuery,
new Integer(0),
new Integer(maxResults),
new Boolean(false),
"",
new Boolean(true),"","","");
ResultElement[] result = searchResult.getResultElements();
// Number of matching results
int noofresults = searchResult.getEndIndex().intValue();
// Loop throught the results, construct News instance and add them
// to the return collection
for (int i = 0; i < noofresults; i++) {
News news = new News();
news.setNewsdate(new java.sql.Timestamp(today.getTime()));
news.setSymbol(symbol.getSymbol());
news.setNewstitle(removeHTML(result[i].getTitle()));
news.setNewsurl(URLEncoder.encode(result[i].getURL(),"UTF-8"));
newsList.add(news);
}
searchResult = null;
result = null;
}
} catch (Exception ex) {
ex.printStackTrace();
throw new WebServiceAccessException(" Stock News service error : "
+ ex.toString());
}
return newsList;
}
/**
* This method retrieves the stockrate from the stock quote webservice.
*
* @param symbol stock symbol
* @return StockRate for the symbols
* @exception WebServiceAccessException if accessing webservice fails
* @since 1.0
*/
public Stockrate fetchStockRate(String symbol)
throws WebServiceAccessException {
float rate;
try {
rate = stockService.getQuote(symbol).floatValue();
} catch (Exception ex) {
throw new WebServiceAccessException(" Stock Quote service error : "
+ ex.toString());
}
// Construct Stock Rate
// Increment the actual rate by 0.5 for setting the high price
Stockrate newRate = new Stockrate();
newRate.setSymbol(symbol);
newRate.setStockdate(new java.sql.Timestamp(new Date().getTime()));
newRate.setLowprice(new Double(String.valueOf(rate)));
newRate.setHighprice(new Double(String.valueOf(rate+0.5)));
return newRate;
}
/**
* This method retrieves the stockrate for the list of symbols from the
* stock quote webservice.
*
* @param symbol stock symbol
* @return stockrate for the list of symbols
* @exception WebServiceAccessException if accessing webservice fails
* @since 1.0
*/
public Collection fetchStockRates(Collection symbols)
throws WebServiceAccessException {
float rate;
ArrayList stockRates = new ArrayList();
Date today = new Date();
Iterator symIter = symbols.iterator();
try {
// For each symbol, retrieve the stock rate and populate the array list
// with stock rates
while(symIter.hasNext()) {
Symbol symbol = (Symbol) symIter.next();
rate = stockService.getQuote(symbol.getSymbol()).floatValue();
// Construct a Stockrate instance and add to the return collection
Stockrate newRate = new Stockrate();
newRate.setSymbol(symbol.getSymbol());
newRate.setStockdate(new java.sql.Timestamp(today.getTime()));
newRate.setLowprice(new Double(String.valueOf(rate)));
newRate.setHighprice(new Double(String.valueOf(rate+0.5)));
stockRates.add(newRate);
}
} catch (Exception ex) {
throw new WebServiceAccessException(" Stock Quote service error : "
+ ex.toString());
}
return stockRates;
}
/**
* This method removes HTML tags from the given string.
*
* @param string string with HTML tags.
* @return string without HTML tags
* @since 1.0
*/
public String removeHTML(String string) {
int start = 0, end;
// while no more tags are found
while (start != -1) {
// find any string that start with <
start = string.indexOf("<", start);
// find the end of occurrence of the tag
end = string.indexOf(">", start);
// if find was successful
if ((start != -1) && (end != -1)) {
// remove the tag
String first, last;
// take out the first part, before the tag
first = string.substring(0, start);
// take out the last part, after the tag
last = string.substring(end + 1);
// merge two parts
string = first + last;
}
}
return string;
}
/**
* This method converts the given date to Julian Date format.
*
* This algorithm is from Press et al., Numerical Recipes
* in C, 2nd ed., Cambridge University Press 1992
*
* @param dt Date to be converted in Julian format
* @return Julian int corresponding to the given java.util.Date
*/
private int toJulian(Date dt) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(dt);
int year = cal.get(Calendar.YEAR);
// Gregorian Calendar's month starts with zero
int month = cal.get(Calendar.MONTH)+1;
int day = cal.get(Calendar.DATE);
int jy = year;
if (year < 0) jy++;
int jm = month;
if (month > 2) jm++;
else {
jy--;
jm += 13;
}
int jul = (int) (java.lang.Math.floor(365.25 * jy)
+ java.lang.Math.floor(30.6001*jm) + day + 1720995.0);
int IGREG = 15 + 31*(10+12*1582);
// Gregorian Calendar adopted Oct. 15, 1582
if (day + 31 * (month + 12 * year) >= IGREG) {
int ja = (int)(0.01 * jy);
jul += 2 - ja + (int)(0.25 * ja);
}
return jul;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -