1 16 17 package org.apache.jetspeed.modules.actions.portlets; 18 19 import org.apache.jetspeed.portal.portlets.VelocityPortlet; 21 import org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction; 22 import org.apache.jetspeed.util.PortletConfigState; 23 24 import org.apache.turbine.util.RunData; 26 27 import org.apache.velocity.context.Context; 29 30 import java.util.StringTokenizer ; 32 33 43 public class WeatherAction extends VelocityPortletAction 44 { 45 public static final String WEATHER_CITY_INFO = "weather_city_info"; 46 public static final String WEATHER_STATE = "weather_state"; 47 public static final String WEATHER_CITY = "weather_city"; 48 public static final String WEATHER_STATION = "weather_station"; 49 public static final String WEATHER_STYLE = "weather_style"; 50 51 59 protected void buildMaximizedContext( VelocityPortlet portlet, 60 Context context, 61 RunData rundata ) 62 { 63 buildNormalContext( portlet, context, rundata); 64 } 65 66 75 protected void buildNormalContext( VelocityPortlet portlet, 76 Context context, 77 RunData rundata ) 78 { 79 80 String cityInfo = PortletConfigState.getParameter(portlet, rundata, WEATHER_CITY_INFO, null); 81 String city = portlet.getPortletConfig().getInitParameter(WEATHER_CITY); 84 String state = portlet.getPortletConfig().getInitParameter(WEATHER_STATE); 85 String station = portlet.getPortletConfig().getInitParameter(WEATHER_STATION); 86 cityInfo = getCityInfo(city, state, station); 87 context.put(WEATHER_CITY_INFO, cityInfo); 89 91 String style = PortletConfigState.getParameter(portlet, rundata, WEATHER_STYLE, "infobox"); 92 context.put(WEATHER_STYLE,style); 93 } 94 95 103 private String getUSInfo(String city, String state) 104 { 105 city = city.trim().toLowerCase()+" "; 106 if (city.indexOf(" ")>0) 107 { 108 String newCity = ""; 109 StringTokenizer st = new StringTokenizer (city, " "); 110 while (st.hasMoreTokens()) 111 { 112 String token = st.nextToken(); 113 newCity = newCity + token.substring(0,1).toUpperCase() + 114 token.substring(1) + "_"; 115 } 116 city = newCity.substring(0, newCity.length()-1); } 118 state = state.toUpperCase(); 119 return "US/" + state + "/" + city; 120 } 121 122 133 private String getCityInfo(String city, String state, String station) 134 { 135 String cityInfo = null; 136 if (city!=null && state !=null && !city.equals("") && !state.equals("")) 137 { 138 cityInfo = getUSInfo(city, state); 139 } 140 else if (station != null && !station.equals("")) 141 { 142 cityInfo = "global/stations/" + station; 143 } 144 return cityInfo; 145 } 146 147 153 public void doCancel(RunData data, Context context) 154 { 155 VelocityPortlet portlet = (VelocityPortlet) context.get("portlet"); 156 buildNormalContext(portlet, context, data); 157 } 158 159 } 160 161 | Popular Tags |