You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by ta...@apache.org on 2006/10/27 03:09:49 UTC

svn commit: r468220 - in /portals/jetspeed-2/trunk/applications/demo/src: java/org/apache/portals/applications/desktop/WeatherPortlet.java webapp/WEB-INF/portlet.xml webapp/WEB-INF/view/weather.vm

Author: taylor
Date: Thu Oct 26 18:09:48 2006
New Revision: 468220

URL: http://svn.apache.org/viewvc?view=rev&rev=468220
Log:
weather portlet

Added:
    portals/jetspeed-2/trunk/applications/demo/src/java/org/apache/portals/applications/desktop/WeatherPortlet.java
    portals/jetspeed-2/trunk/applications/demo/src/webapp/WEB-INF/view/weather.vm
Modified:
    portals/jetspeed-2/trunk/applications/demo/src/webapp/WEB-INF/portlet.xml

Added: portals/jetspeed-2/trunk/applications/demo/src/java/org/apache/portals/applications/desktop/WeatherPortlet.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/trunk/applications/demo/src/java/org/apache/portals/applications/desktop/WeatherPortlet.java?view=auto&rev=468220
==============================================================================
--- portals/jetspeed-2/trunk/applications/demo/src/java/org/apache/portals/applications/desktop/WeatherPortlet.java (added)
+++ portals/jetspeed-2/trunk/applications/demo/src/java/org/apache/portals/applications/desktop/WeatherPortlet.java Thu Oct 26 18:09:48 2006
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2000-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.applications.desktop;
+
+import java.io.IOException;
+import java.util.StringTokenizer;
+
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import javax.portlet.PortletException;
+import javax.portlet.PortletPreferences;
+import javax.portlet.RenderRequest;
+import javax.portlet.RenderResponse;
+
+import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
+import org.apache.velocity.context.Context;
+
+public class WeatherPortlet extends GenericVelocityPortlet
+{
+
+    public static final String WEATHER_CITY_INFO = "weather_city_info";
+
+    public static final String WEATHER_STATE = "weather_state";
+
+    public static final String WEATHER_CITY = "weather_city";
+
+    public static final String WEATHER_STATION = "weather_station";
+
+    public static final String WEATHER_STYLE = "weather_style";
+
+    public void doView(RenderRequest request, RenderResponse response)
+            throws PortletException, IOException
+    {
+        Context context = super.getContext(request);
+
+        String cityInfo = (String) request.getPortletSession().getAttribute(
+                WEATHER_CITY_INFO);
+
+        PortletPreferences prefs = request.getPreferences();
+        String city = prefs.getValue(WEATHER_CITY, "Bakersfield");
+        String state = prefs.getValue(WEATHER_STATE, "CA");
+        String station = prefs.getValue(WEATHER_STATION, null);
+        cityInfo = getCityInfo(city, state, station);
+        context.put(WEATHER_CITY_INFO, cityInfo);
+
+        String style = prefs.getValue(WEATHER_STYLE, "infobox");
+        context.put(WEATHER_STYLE, style);
+
+        super.doView(request, response);
+    }
+    
+    public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException
+    {
+        response.setContentType("text/html");        
+        doPreferencesEdit(request, response);
+    }    
+    /**
+     * Builds the path for US cities. The format is US/ST/City.html, i.e. for
+     * New York City, the city path is US/NY/New_York
+     * 
+     * @param city
+     * @param state
+     * @return
+     */
+    private String getUSInfo(String city, String state)
+    {
+        city = city.trim().toLowerCase() + " ";
+        if (city.indexOf(" ") > 0)
+        {
+            String newCity = "";
+            StringTokenizer st = new StringTokenizer(city, " ");
+            while (st.hasMoreTokens())
+            {
+                String token = st.nextToken();
+                newCity = newCity + token.substring(0, 1).toUpperCase()
+                        + token.substring(1) + "_";
+            }
+            city = newCity.substring(0, newCity.length() - 1); // remove last
+                                                                // '_'
+        }
+        state = state.toUpperCase();
+        return "US/" + state + "/" + city;
+    }
+
+    /**
+     * Builds the city path for US or other world cities. For world cities, the
+     * city path is global/station/station_number, i.e. for Istanbul, Turkey, it
+     * is global/stations/17060. The station numbers need to be obtained from
+     * the Weather Underground's site.
+     * 
+     * @param city
+     * @param state
+     * @param station
+     * @return
+     */
+    private String getCityInfo(String city, String state, String station)
+    {
+        String cityInfo = null;
+        if (city != null && state != null && !city.equals("")
+                && !state.equals(""))
+        {
+            cityInfo = getUSInfo(city, state);
+        } else
+            if (station != null && !station.equals(""))
+            {
+                cityInfo = "global/stations/" + station;
+            }
+        return cityInfo;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.portals.bridges.velocity.GenericVelocityPortlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
+     */
+    public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException
+    {
+        String city = request.getParameter(WEATHER_CITY);
+        String state = request.getParameter(WEATHER_STATE);
+        String style = request.getParameter(WEATHER_STYLE);
+        String station = request.getParameter(WEATHER_STATION);
+        PortletPreferences prefs = request.getPreferences();
+        prefs.setValue(WEATHER_CITY, city);
+        prefs.setValue(WEATHER_STATE, state);
+        prefs.setValue(WEATHER_STYLE, style);
+        prefs.setValue(WEATHER_STATION, station);
+        prefs.store();
+        super.processAction(request, response);
+    }
+    
+}

Modified: portals/jetspeed-2/trunk/applications/demo/src/webapp/WEB-INF/portlet.xml
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/trunk/applications/demo/src/webapp/WEB-INF/portlet.xml?view=diff&rev=468220&r1=468219&r2=468220
==============================================================================
--- portals/jetspeed-2/trunk/applications/demo/src/webapp/WEB-INF/portlet.xml (original)
+++ portals/jetspeed-2/trunk/applications/demo/src/webapp/WEB-INF/portlet.xml Thu Oct 26 18:09:48 2006
@@ -824,6 +824,51 @@
       </preference>
     </portlet-preferences>
   </portlet>         
-    
+
+  	<portlet id="weather">
+		<description>Weather Underground Portlet</description>
+		<portlet-name>WeatherPortlet</portlet-name>
+		<display-name>Weather</display-name>
+		<portlet-class>org.apache.portals.applications.desktop.WeatherPortlet</portlet-class>
+        <init-param>
+            <name>ViewPage</name>
+            <value>/WEB-INF/view/weather.vm</value>
+        </init-param>		        
+        <init-param>
+            <name>EditPage</name>
+            <value>/WEB-INF/view/edit-prefs.vm</value>
+        </init-param>			
+		<expiration-cache>0</expiration-cache>
+		<supports>
+			<mime-type>text/html</mime-type>
+			<portlet-mode>VIEW</portlet-mode>
+		    <portlet-mode>EDIT</portlet-mode>	  
+		</supports>
+		<supported-locale>en</supported-locale>        
+        <portlet-info>
+           <title>Weather</title>
+           <short-title>Weather</short-title>
+           <keywords>weather</keywords>
+        </portlet-info>        
+        <portlet-preferences>
+		<preference>
+			<name>weather_state</name>
+			<value>CA</value>
+		</preference>
+		<preference>
+			<name>weather_city</name>
+			<value>Oakland</value>
+		</preference>
+		<preference>
+			<name>weather_station</name>
+			<value></value>
+		</preference>
+		<preference>
+			<name>weather_style</name>
+			<value>infobox</value>
+		</preference>
+		</portlet-preferences>
+	</portlet>         
+        
 </portlet-app>
 

Added: portals/jetspeed-2/trunk/applications/demo/src/webapp/WEB-INF/view/weather.vm
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/trunk/applications/demo/src/webapp/WEB-INF/view/weather.vm?view=auto&rev=468220
==============================================================================
--- portals/jetspeed-2/trunk/applications/demo/src/webapp/WEB-INF/view/weather.vm (added)
+++ portals/jetspeed-2/trunk/applications/demo/src/webapp/WEB-INF/view/weather.vm Thu Oct 26 18:09:48 2006
@@ -0,0 +1,23 @@
+#*
+Copyright 2004 The Apache Software Foundation
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*#
+#if (!$weather_city_info)
+Please configure your Weather settings.
+#else
+<a href="http://www.wunderground.com/${weather_city_info}.html"  
+target="_blank"><img src="http://banners.wunderground.com/banner/$!weather_style/language/www/${weather_city_info}.gif"
+alt="Click for $weather_city_info Forecast" border="0"></a>
+#end
+



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org