/*
* $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPortal/Sources/es/udc/myportal/services/stockquote/model/delegate/StockQuoteFacadeDelegate.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.stockquote.model.delegate;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;
import es.udc.mypersonalizer.kernel.util.exceptions.
InternalErrorException;
import es.udc.myportal.services.stockquote.model.service.QuickQuote;
import es.udc.myportal.services.stockquote.model.service.XigniteQuotes;
import es.udc.myportal.services.stockquote.model.service.XigniteQuotesLocator;
import es.udc.myportal.services.stockquote.model.service.XigniteQuotesSoap;
/**
* This is a facade for accessing quotes so the client (the portal) do
* not need to deal with the underneath technology.
*
* @author Abel Iago Toral Quiroga
* @since 1.0
*/
public class StockQuoteFacadeDelegate {
/** An instance of the remote service stub */
private static XigniteQuotesSoap quotesSoapStub = 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 StockQuoteFacadeDelegate() throws InternalErrorException {
try {
/* Get an instance of the web service stub */
XigniteQuotes xIgniteQuotes = new XigniteQuotesLocator();
quotesSoapStub = xIgniteQuotes.getXigniteQuotesSoap();
} catch (Exception e) {
throw new InternalErrorException(e);
}
}
/**
* Gets quotes given a set of symbols.
* @param symbols a <code>Collection</code> of <code>String</code>s
* with the desired symbols.
* @throws <code>InternalErrorException</code> if an error happens.
* @return a <code>Collection</code> of <code>MyPortalQuote</code> objects.
*/
public Collection getQuotes(Collection symbols)
throws InternalErrorException {
try {
/* Convert collection of symbols to a simple String */
String symbolsAsString = convertToSymbolsString(symbols);
/* Get quick quotes */
QuickQuote[] quickQuotes =
quotesSoapStub.getQuickQuotes(symbolsAsString).getQuickQuote();
/* Return quotes */
return convertToMyPortalQuoteCollection(quickQuotes);
} catch (Exception e) {
throw new InternalErrorException(e);
}
}
/**
* Converts an array of <code>QuickQuote</code> to a
* <code>Collection</code> of <code>MyPortalQuote</code>
* @param quickQuotes the array to convert
* @return a <code>Collection</code> of <code>MyPortalQuote</code>.
*/
private Collection convertToMyPortalQuoteCollection(
QuickQuote[] quickQuotes) {
Collection myPortalQuotes = new ArrayList();
for (int i=0; i<quickQuotes.length; i++) {
QuickQuote quickQuote = quickQuotes[i];
MyPortalQuote myPortalQuote =
new MyPortalQuote(
quickQuote.getSymbol(),
quickQuote.getLast(),
quickQuote.getChange());
myPortalQuotes.add(myPortalQuote);
}
return myPortalQuotes;
}
/**
* 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;
}
}