1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.modules.actions.portlets;
18
19
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
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
44 {
45 private static final String SYMBOLS = "symbols";
46 private static final String COLUMNS = "columns";
47 private static final String QUOTES = "quotes";
48 private static final String SORT = "sort";
49 private static final String SELECTED_COLUMNS = "selected-columns";
50 private static final String[] ALL_COLUMNS = {"Symbol","Price","Change","Volume"};
51 private String 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
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
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
95 StockQuoteService service = (StockQuoteService) TurbineServices.getInstance().
96 getService(StockQuoteService.SERVICE_NAME);
97
98
99 String symbols = (String) PortletSessionState.getAttributeWithFallback(portlet, rundata, SYMBOLS);
100
101 this.sort = (String) 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 columns = PortletConfigState.getParameter(portlet, rundata, COLUMNS,
109 StringUtils.arrayToString(ALL_COLUMNS, ","));
110 String[] selectedColumnsArray = StringUtils.stringToArray(columns, ",");
111
112
113
114 String[] symbolArray = StringUtils.stringToArray(symbols, ",");
115 StockQuote[] quotes = service.fullQuotes(symbolArray);
116
117
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
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 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 entry1, Object entry2)
146 {
147 if (this.sort.equalsIgnoreCase("price"))
148 {
149 Float entrycol1 = new Float(((StockQuote) entry1).getPrice());
150 Float entrycol2 = new Float(((StockQuote) entry2).getPrice());
151 return entrycol1.compareTo(entrycol2);
152 }
153 else if (this.sort.equalsIgnoreCase("symbol"))
154 {
155 String entrycol1 = ((StockQuote) entry1).getSymbol();
156 String entrycol2 = ((StockQuote) entry2).getSymbol();
157 return entrycol1.compareTo(entrycol2);
158 }
159 else if (this.sort.equalsIgnoreCase("change"))
160 {
161 Double entrycol1 = new Double(((StockQuote) entry1).getChange());
162 Double entrycol2 = new Double(((StockQuote) entry2).getChange());
163 return entrycol1.compareTo(entrycol2);
164 }
165 else
166 {
167 Long entrycol1 = new Long(((StockQuote) entry1).getVolume());
168 Long entrycol2 = new Long(((StockQuote) entry2).getVolume());
169 return entrycol1.compareTo(entrycol2);
170 }
171
172 }
173
174 }
175