1 16 17 package org.apache.jetspeed.modules.actions.portlets; 18 19 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 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 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 56 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JspStockQuoteAction.class.getName()); 57 58 64 protected void buildNormalContext(Portlet portlet, RunData rundata) 65 { 66 67 this.doRefresh(rundata, portlet); 69 } 70 71 77 public void doSort(RunData rundata, Portlet portlet) 78 { 79 this.doRefresh(rundata, portlet); 81 logger.info("JspStockQuoteAction: sorting..."); 82 } 83 84 90 public void doRefresh(RunData rundata, Portlet portlet) 91 { 92 try 93 { 94 StockQuoteService service = (StockQuoteService) TurbineServices.getInstance(). 96 getService(StockQuoteService.SERVICE_NAME); 97 98 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 String [] symbolArray = StringUtils.stringToArray(symbols, ","); 115 StockQuote[] quotes = service.fullQuotes(symbolArray); 116 117 if (this.sort != null) 119 { 120 QuickSort.quickSort(quotes, 0, quotes.length - 1, this); 121 rundata.getRequest().setAttribute(SORT, this.sort); 122 } 123 124 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 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 176 | Popular Tags |