KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > modules > actions > portlets > JspStockQuoteAction


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.modules.actions.portlets;
18
19 // Turbine stuff
20
import org.apache.turbine.util.RunData;
21 import org.apache.turbine.services.TurbineServices;
22 import org.apache.turbine.util.Comparable;
23 import org.apache.turbine.util.QuickSort;
24
25 // Jetspeed stuff
26
import org.apache.jetspeed.portal.Portlet;
27 import org.apache.jetspeed.modules.actions.portlets.JspPortletAction;
28 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
29 import org.apache.jetspeed.services.logging.JetspeedLogger;
30 import org.apache.jetspeed.webservices.finance.stockmarket.StockQuoteService;
31 import org.apache.jetspeed.webservices.finance.stockmarket.StockQuote;
32 import org.apache.jetspeed.util.PortletConfigState;
33 import org.apache.jetspeed.util.PortletSessionState;
34 import org.apache.jetspeed.util.StringUtils;
35
36 /**
37  * This action sets up the template context for retrieving stock quotes.
38  *
39  * @author <a HREF="mailto:morciuch@apache.org">Mark Orciuch</a>
40  * @version $Id: JspStockQuoteAction.java,v 1.3 2004/02/23 02:56:58 jford Exp $
41  */

42
43 public class JspStockQuoteAction extends JspPortletAction implements Comparable JavaDoc
44 {
45     private static final String JavaDoc SYMBOLS = "symbols";
46     private static final String JavaDoc COLUMNS = "columns";
47     private static final String JavaDoc QUOTES = "quotes";
48     private static final String JavaDoc SORT = "sort";
49     private static final String JavaDoc SELECTED_COLUMNS = "selected-columns";
50     private static final String JavaDoc[] ALL_COLUMNS = {"Symbol","Price","Change","Volume"};
51     private String JavaDoc sort = null;
52
53     /**
54      * Static initialization of the logger for this class
55      */

56     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JspStockQuoteAction.class.getName());
57     
58     /**
59      * Build the normal state content for this portlet.
60      *
61      * @param portlet The jsp-based portlet that is being built.
62      * @param rundata The turbine rundata context for this request.
63      */

64     protected void buildNormalContext(Portlet portlet, RunData rundata)
65     {
66
67         // We always fetch the most current quotes so might as well call refresh from here
68
this.doRefresh(rundata, portlet);
69     }
70
71     /**
72      * Sort the quotes.
73      *
74      * @param portlet The jsp-based portlet that is being built.
75      * @param rundata The turbine rundata context for this request.
76      */

77     public void doSort(RunData rundata, Portlet portlet)
78     {
79         // We always fetch the most current quotes so might as well call refresh from here
80
this.doRefresh(rundata, portlet);
81         logger.info("JspStockQuoteAction: sorting...");
82     }
83
84     /**
85      * Refresh the portlet content.
86      *
87      * @param portlet The jsp-based portlet that is being built.
88      * @param rundata The turbine rundata context for this request.
89      */

90     public void doRefresh(RunData rundata, Portlet portlet)
91     {
92         try
93         {
94             // Get reference to stock quote web service
95
StockQuoteService service = (StockQuoteService) TurbineServices.getInstance().
96                 getService(StockQuoteService.SERVICE_NAME);
97
98             // Retrieve portlet parameters
99
String JavaDoc symbols = (String JavaDoc) PortletSessionState.getAttributeWithFallback(portlet, rundata, SYMBOLS);
100
101             this.sort = (String JavaDoc) PortletSessionState.getAttributeWithFallback(portlet, rundata, SORT);
102             if (this.sort != null)
103             {
104                 PortletSessionState.setAttribute(portlet, rundata, SORT, sort);
105                 rundata.getRequest().setAttribute(SORT, sort);
106             }
107
108             String JavaDoc columns = PortletConfigState.getParameter(portlet, rundata, COLUMNS,
109                                                              StringUtils.arrayToString(ALL_COLUMNS, ","));
110             String JavaDoc[] selectedColumnsArray = StringUtils.stringToArray(columns, ",");
111
112
113             // Request stock quote(s) from the stock quote web service
114
String JavaDoc[] symbolArray = StringUtils.stringToArray(symbols, ",");
115             StockQuote[] quotes = service.fullQuotes(symbolArray);
116
117             // Sort the entries
118
if (this.sort != null)
119             {
120                 QuickSort.quickSort(quotes, 0, quotes.length - 1, this);
121                 rundata.getRequest().setAttribute(SORT, this.sort);
122             }
123
124             // Place appropriate objects in jsp context
125
rundata.getRequest().setAttribute(QUOTES, quotes);
126             rundata.getRequest().setAttribute(COLUMNS, selectedColumnsArray);
127             rundata.getRequest().setAttribute(SELECTED_COLUMNS, columns);
128
129             logger.info("JspStockQuoteAction: refreshing...");
130         }
131         catch (Exception JavaDoc e)
132         {
133             logger.error("Exception", e);
134         }
135     }
136
137     /**
138      * Compare to another <code>StockQuote</code>. Used by the
139      * <code>QuickSort</code> class to determine sort order.
140      *
141      * @param entry1 The first <code>StockQuoteEntry</code> object.
142      * @param entry2 The second <code>StockQuoteEntry</code> object.
143      * @return An <code>int</code> indicating the result of the comparison.
144      */

145     public int compare(Object JavaDoc entry1, Object JavaDoc entry2)
146     {
147         if (this.sort.equalsIgnoreCase("price"))
148         {
149             Float JavaDoc entrycol1 = new Float JavaDoc(((StockQuote) entry1).getPrice());
150             Float JavaDoc entrycol2 = new Float JavaDoc(((StockQuote) entry2).getPrice());
151             return entrycol1.compareTo(entrycol2);
152         }
153         else if (this.sort.equalsIgnoreCase("symbol"))
154         {
155             String JavaDoc entrycol1 = ((StockQuote) entry1).getSymbol();
156             String JavaDoc entrycol2 = ((StockQuote) entry2).getSymbol();
157             return entrycol1.compareTo(entrycol2);
158         }
159         else if (this.sort.equalsIgnoreCase("change"))
160         {
161             Double JavaDoc entrycol1 = new Double JavaDoc(((StockQuote) entry1).getChange());
162             Double JavaDoc entrycol2 = new Double JavaDoc(((StockQuote) entry2).getChange());
163             return entrycol1.compareTo(entrycol2);
164         }
165         else
166         {
167             Long JavaDoc entrycol1 = new Long JavaDoc(((StockQuote) entry1).getVolume());
168             Long JavaDoc entrycol2 = new Long JavaDoc(((StockQuote) entry2).getVolume());
169             return entrycol1.compareTo(entrycol2);
170         }
171
172     }
173
174 }
175
176
Popular Tags