KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > modules > actions > portlets > WeatherAction


1 /*
2  * Copyright 2000-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 // Jetspeed stuff
20
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 // Turbine stuff
25
import org.apache.turbine.util.RunData;
26
27 // Velocity Stuff
28
import org.apache.velocity.context.Context;
29
30 //Java stuff
31
import java.util.StringTokenizer JavaDoc;
32
33 /**
34  * WeatherAction portlet uses WeatherUnderground's weather condition
35  * stickers/banners to build the portlet view.
36  *
37  * <p> Donated by Community Grids Java Source Package</p>
38  * <p> Provides supporting classess for portal environments.</p>
39  *
40  * @author <a HREF="mailto:obalsoy@indiana.edu">Ozgur Balsoy</a>
41  * @version $Id: WeatherAction.java,v 1.9 2004/02/23 02:56:58 jford Exp $
42  */

43 public class WeatherAction extends VelocityPortletAction
44 {
45     public static final String JavaDoc WEATHER_CITY_INFO = "weather_city_info";
46     public static final String JavaDoc WEATHER_STATE = "weather_state";
47     public static final String JavaDoc WEATHER_CITY = "weather_city";
48     public static final String JavaDoc WEATHER_STATION = "weather_station";
49     public static final String JavaDoc WEATHER_STYLE = "weather_style";
50
51     /**
52      * Nothing specific for maximized view.
53      *
54      * @param portlet
55      * @param context
56      * @param rundata
57      * @see VelocityPortletAction#buildMaximizedContext
58      */

59     protected void buildMaximizedContext( VelocityPortlet portlet,
60                                           Context context,
61                                           RunData rundata )
62     {
63         buildNormalContext( portlet, context, rundata);
64     }
65
66     /**
67      * Subclasses must override this method to provide default behavior
68      * for the portlet action
69      *
70      * @param portlet
71      * @param context
72      * @param rundata
73      * @see VelocityPortletAction#buildNormalContext
74      */

75     protected void buildNormalContext( VelocityPortlet portlet,
76                                        Context context,
77                                        RunData rundata )
78     {
79
80         String JavaDoc cityInfo = PortletConfigState.getParameter(portlet, rundata, WEATHER_CITY_INFO, null);
81         //if (cityInfo == null)
82
//{
83
String JavaDoc city = portlet.getPortletConfig().getInitParameter(WEATHER_CITY);
84             String JavaDoc state = portlet.getPortletConfig().getInitParameter(WEATHER_STATE);
85             String JavaDoc station = portlet.getPortletConfig().getInitParameter(WEATHER_STATION);
86             cityInfo = getCityInfo(city, state, station);
87         //}
88
context.put(WEATHER_CITY_INFO, cityInfo);
89         //PortletConfigState.setInstanceParameter(portlet, rundata, WEATHER_CITY_INFO, cityInfo);
90

91         String JavaDoc style = PortletConfigState.getParameter(portlet, rundata, WEATHER_STYLE, "infobox");
92         context.put(WEATHER_STYLE,style);
93     }
94
95     /**
96      * Builds the path for US cities. The format is US/ST/City.html, i.e.
97      * for New York City, the city path is US/NY/New_York
98      *
99      * @param city
100      * @param state
101      * @return
102      */

103     private String JavaDoc getUSInfo(String JavaDoc city, String JavaDoc state)
104     {
105         city = city.trim().toLowerCase()+" ";
106         if (city.indexOf(" ")>0)
107         {
108             String JavaDoc newCity = "";
109             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(city, " ");
110             while (st.hasMoreTokens())
111             {
112                 String JavaDoc token = st.nextToken();
113                 newCity = newCity + token.substring(0,1).toUpperCase() +
114                           token.substring(1) + "_";
115             }
116             city = newCity.substring(0, newCity.length()-1); // remove last '_'
117
}
118         state = state.toUpperCase();
119         return "US/" + state + "/" + city;
120     }
121
122     /**
123      * Builds the city path for US or other world cities. For world cities,
124      * the city path is global/station/station_number, i.e.
125      * for Istanbul, Turkey, it is global/stations/17060. The station numbers
126      * need to be obtained from the Weather Underground's site.
127      *
128      * @param city
129      * @param state
130      * @param station
131      * @return
132      */

133     private String JavaDoc getCityInfo(String JavaDoc city, String JavaDoc state, String JavaDoc station)
134     {
135         String JavaDoc 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     /**
148      *
149      * @param data
150      * @param context
151      * @see VelocityPortletAction#doCancel
152      */

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