/*
* $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPortal/Sources/es/udc/myportal/services/stocknews/model/delegate/StockNewsFacadeDelegate.java,v 1.1.1.1 2004/03/25 12:10:42 fbellas Exp $
* $Revision: 1.1.1.1 $
* $Date: 2004/03/25 12:10:42 $
*
* =============================================================================
*
* Copyright (c) 2003, The MyPersonalizer Development Group
* (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
* University Of A Coruna
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of the University Of A Coruna nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package es.udc.myportal.services.stocknews.model.delegate;
import java.util.Collection;
import java.util.ArrayList;
import es.udc.mypersonalizer.kernel.util.exceptions.
InternalErrorException;
import es.udc.myportal.services.stocknews.model.service.StockNews;
import es.udc.myportal.services.stocknews.model.service.XigniteNews;
import es.udc.myportal.services.stocknews.model.service.XigniteNewsLocator;
import es.udc.myportal.services.stocknews.model.service.XigniteNewsSoap;
/**
* This is a facade for accessing stock news so the client (the portal) do
* not need to deal with the underneath technology.
*
* @author Abel Iago Toral Quiroga
* @since 1.0
*/
public class StockNewsFacadeDelegate {
/** An instance of the remote service stub */
private static XigniteNewsSoap newsSoapStub = null;
/**
* Creates an instance of this class. Constructs an instance of the
* remote service stub.
* @throws <code>InternalErrorException</code> if can't create an
* instance of the remote service stub.
*/
public StockNewsFacadeDelegate() throws InternalErrorException {
try {
/* Get an instance of the web service stub */
XigniteNews xIgniteNews = new XigniteNewsLocator();
newsSoapStub = xIgniteNews.getXigniteNewsSoap();
} catch (Exception e) {
throw new InternalErrorException(e);
}
}
/**
* Gets stock news given a set of symbols and a maximun number of
* headlines.
* @param symbols a <code>Collection</code> of <code>String</code>s
* with the desired symbols.
* @param maxHeadlines the maximun number of headlines to retrieve for
* each given symbol.
* @throws <code>InternalErrorException</code> if an error happens.
* @return a <code>Collection</code> of <code>MyPortalStockNews</code>
* objects.
*/
public Collection getStockNews(Collection symbols,
int maxHeadlines) throws InternalErrorException {
try {
/* Convert collection of symbols to a simple String */
String symbolsAsString = convertToSymbolsString(symbols);
/* Get stock news */
StockNews[] stockNews =
newsSoapStub.getStockHeadlines(symbolsAsString, maxHeadlines).
getStockNews();
/* Return stock news */
return convertToMyPortalStockNewsCollection(stockNews);
} catch (Exception e) {
throw new InternalErrorException(e);
}
}
/**
* Converts an array of <code>StockNews</code> to a
* <code>Collection</code> of <code>MyPortalStockNews</code>
* @param stockNews the array to convert.
* @return a <code>Collection</code> of <code>MyPortalStockNews</code>.
*/
private Collection convertToMyPortalStockNewsCollection(
StockNews[] stockNews) {
Collection myPortalStockNews = new ArrayList();
for (int i=0; i<stockNews.length; i++) {
StockNews stockNewsItem = stockNews[i];
MyPortalStockNews myPortalStockNewsItem =
new MyPortalStockNews(
stockNewsItem.getHeadline(),
stockNewsItem.getDate(),
stockNewsItem.getTime(),
stockNewsItem.getSource(),
stockNewsItem.getUrl());
myPortalStockNews.add(myPortalStockNewsItem);
}
return myPortalStockNews;
}
/**
* Converts a <code>Collection</code> of <code>String</code>s with
* a quote symbols to a comma separated <code>String</code> of symbols.
* @param symbols an array with symbols.
* @return a comma separated list of symbols.
*/
private String convertToSymbolsString(Collection symbols) {
String[] symbolsAsArray = (String[]) symbols.toArray(new String[0]);
if (symbolsAsArray.length <= 0) return "";
String symbolsAsString = symbolsAsArray[0];
for (int i=1; i<symbolsAsArray.length; i++) {
symbolsAsString += "," + symbolsAsArray[i];
}
return symbolsAsString;
}
}