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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.modules.actions.portlets;
1819import org.apache.jetspeed.portal.portlets.VelocityPortlet;
20import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
21import org.apache.jetspeed.services.logging.JetspeedLogger;
2223// Turbine stuff24import org.apache.turbine.util.RunData;
25import org.apache.turbine.services.TurbineServices;
26// Velocity Stuff27import org.apache.velocity.context.Context;
2829//import org.apache.jetspeed.util.StringUtils;30import org.apache.jetspeed.webservices.finance.stockmarket.StockQuoteService;
31import org.apache.jetspeed.webservices.finance.stockmarket.StockQuote;
32import org.apache.jetspeed.services.resources.JetspeedResources;
3334import org.apache.jetspeed.util.PortletConfigState;
35import org.apache.jetspeed.util.StringUtils;
3637/***38 * This action sets up the template context for retrieving stock quotes.39 *40 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>41 * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a>42 */4344publicclassStockQuoteActionextendsVelocityPortletAction45 {
46privatestaticfinal String SYMBOLS = "symbols";
47privatestaticfinal String COLUMNS = "columns";
48privatestaticfinal String QUOTES = "quotes";
49privatestaticfinal String[] ALL_COLUMNS = {"Symbol","Price","Change","Volume"};
50privatestaticfinal String SELECTED_COLUMNS = "selected-columns";
5152/***53 * Static initialization of the logger for this class54 */55privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(StockQuoteAction.class.getName());
5657/***58 * Build the maximized state content for this portlet. (Same as normal state).59 *60 * @param portlet The velocity-based portlet that is being built.61 * @param context The velocity context for this request.62 * @param rundata The turbine rundata context for this request.63 */64protectedvoid buildMaximizedContext( VelocityPortlet portlet,
65 Context context,
66 RunData rundata )
67 {
68 buildNormalContext( portlet, context, rundata);
69 }
7071/***72 * Build the configure state content for this portlet.73 * TODO: we could configure this portlet with configurable skins, etc..74 *75 * @param portlet The velocity-based portlet that is being built.76 * @param context The velocity context for this request.77 * @param rundata The turbine rundata context for this request.78 */79protectedvoid buildConfigureContext( VelocityPortlet portlet,
80 Context context,
81 RunData rundata )
82 {
83 buildNormalContext( portlet, context, rundata);
8485 setTemplate(rundata, "stock-quote-customize");
868788 }
8990/***91 * Build the normal state content for this portlet.92 *93 * @param portlet The velocity-based portlet that is being built.94 * @param context The velocity context for this request.95 * @param rundata The turbine rundata context for this request.96 */9798protectedvoid buildNormalContext( VelocityPortlet portlet,
99 Context context,
100 RunData rundata )
101 {
102try103 {
104// Get reference to stock quote web service105StockQuoteService service = (StockQuoteService) TurbineServices.getInstance().
106 getService(StockQuoteService.SERVICE_NAME);
107108// Retrieve portlet parameters109 String symbols = PortletConfigState.getParameter(portlet, rundata, SYMBOLS, "IBM,MSFT,ORCL,SUNW");
110 String columns = PortletConfigState.getParameter(portlet, rundata, COLUMNS,
111 StringUtils.arrayToString(ALL_COLUMNS, ","));
112 String[] selectedColumnsArray = StringUtils.stringToArray(columns, ",");
113114// Request stock quote(s) from the stock quote web service115 String[] symbolArray = StringUtils.stringToArray(symbols, ",");
116StockQuote[] quotes = service.fullQuotes(symbolArray);
117118// Place appropriate objects in Velocity context119 context.put(QUOTES, quotes);
120 context.put(SELECTED_COLUMNS, selectedColumnsArray);
121 context.put(COLUMNS, columns);
122 }
123catch (Exception e)
124 {
125// log the error msg126 logger.error("Exception", e);
127128 rundata.setMessage("Error in Jetspeed Stock Quotes: " + e.toString());
129 rundata.setStackTrace(org.apache.turbine.util.StringUtils.stackTrace(e), e);
130 rundata.setScreenTemplate(JetspeedResources.getString("template.error","Error"));
131 }
132 }
133134135136 }
137