View Javadoc

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  import org.apache.jetspeed.portal.portlets.VelocityPortlet;
20  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
21  import org.apache.jetspeed.services.logging.JetspeedLogger;
22  
23  // Turbine stuff
24  import org.apache.turbine.util.RunData;
25  import org.apache.turbine.services.TurbineServices;
26  // Velocity Stuff
27  import org.apache.velocity.context.Context;
28  
29  //import org.apache.jetspeed.util.StringUtils;
30  import org.apache.jetspeed.webservices.finance.stockmarket.StockQuoteService;
31  import org.apache.jetspeed.webservices.finance.stockmarket.StockQuote;
32  import org.apache.jetspeed.services.resources.JetspeedResources;
33  
34  import org.apache.jetspeed.util.PortletConfigState;
35  import org.apache.jetspeed.util.StringUtils;
36  
37  /***
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   */
43  
44  public class StockQuoteAction extends VelocityPortletAction
45  {
46      private static final String SYMBOLS = "symbols";
47      private static final String COLUMNS = "columns";
48      private static final String QUOTES = "quotes";
49      private static final String[] ALL_COLUMNS = {"Symbol","Price","Change","Volume"};
50      private static final String SELECTED_COLUMNS = "selected-columns";
51  
52      /***
53       * Static initialization of the logger for this class
54       */    
55      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(StockQuoteAction.class.getName());     
56      
57      /***
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       */
64      protected void buildMaximizedContext( VelocityPortlet portlet,
65                                            Context context,
66                                            RunData rundata )
67      {
68          buildNormalContext( portlet, context, rundata);
69      }
70  
71      /***
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       */
79      protected void buildConfigureContext( VelocityPortlet portlet,
80                                            Context context,
81                                            RunData rundata )
82      {
83          buildNormalContext( portlet, context, rundata);
84  
85          setTemplate(rundata, "stock-quote-customize");
86  
87  
88      }
89  
90      /***
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       */
97  
98      protected void buildNormalContext( VelocityPortlet portlet,
99                                         Context context,
100                                        RunData rundata )
101     {
102         try
103         {
104             // Get reference to stock quote web service
105             StockQuoteService service = (StockQuoteService) TurbineServices.getInstance().
106                 getService(StockQuoteService.SERVICE_NAME);
107 
108             // Retrieve portlet parameters
109             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, ",");
113 
114             // Request stock quote(s) from the stock quote web service
115             String[] symbolArray = StringUtils.stringToArray(symbols, ",");
116             StockQuote[] quotes = service.fullQuotes(symbolArray);
117 
118             // Place appropriate objects in Velocity context
119             context.put(QUOTES, quotes);
120             context.put(SELECTED_COLUMNS, selectedColumnsArray);
121             context.put(COLUMNS, columns);
122         }
123         catch (Exception e)
124         {
125            // log the error msg
126             logger.error("Exception", e);
127 
128             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     }
133 
134 
135 
136 }
137