View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.portals.applications.desktop;
18  
19  import java.io.IOException;
20  import java.util.StringTokenizer;
21  
22  import javax.portlet.ActionRequest;
23  import javax.portlet.ActionResponse;
24  import javax.portlet.PortletException;
25  import javax.portlet.PortletPreferences;
26  import javax.portlet.RenderRequest;
27  import javax.portlet.RenderResponse;
28  
29  import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
30  import org.apache.velocity.context.Context;
31  
32  public class WeatherPortlet extends GenericVelocityPortlet
33  {
34  
35      public static final String WEATHER_CITY_INFO = "weather_city_info";
36  
37      public static final String WEATHER_STATE = "weather_state";
38  
39      public static final String WEATHER_CITY = "weather_city";
40  
41      public static final String WEATHER_STATION = "weather_station";
42  
43      public static final String WEATHER_STYLE = "weather_style";
44  
45      public void doView(RenderRequest request, RenderResponse response)
46              throws PortletException, IOException
47      {
48          Context context = super.getContext(request);
49  
50          String cityInfo = (String) request.getPortletSession().getAttribute(
51                  WEATHER_CITY_INFO);
52  
53          PortletPreferences prefs = request.getPreferences();
54          String city = prefs.getValue(WEATHER_CITY, "Bakersfield");
55          String state = prefs.getValue(WEATHER_STATE, "CA");
56          String station = prefs.getValue(WEATHER_STATION, null);
57          cityInfo = getCityInfo(city, state, station);
58          context.put(WEATHER_CITY_INFO, cityInfo);
59  
60          String style = prefs.getValue(WEATHER_STYLE, "infobox");
61          context.put(WEATHER_STYLE, style);
62          response.setProperty("david", "taylor");
63          super.doView(request, response);
64      }
65      
66      public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException
67      {
68          response.setContentType("text/html");        
69          doPreferencesEdit(request, response);
70      }    
71      /***
72       * Builds the path for US cities. The format is US/ST/City.html, i.e. for
73       * New York City, the city path is US/NY/New_York
74       * 
75       * @param city
76       * @param state
77       * @return
78       */
79      private String getUSInfo(String city, String state)
80      {
81          city = city.trim().toLowerCase() + " ";
82          if (city.indexOf(" ") > 0)
83          {
84              String newCity = "";
85              StringTokenizer st = new StringTokenizer(city, " ");
86              while (st.hasMoreTokens())
87              {
88                  String token = st.nextToken();
89                  newCity = newCity + token.substring(0, 1).toUpperCase()
90                          + token.substring(1) + "_";
91              }
92              city = newCity.substring(0, newCity.length() - 1); // remove last
93                                                                  // '_'
94          }
95          state = state.toUpperCase();
96          return "US/" + state + "/" + city;
97      }
98  
99      /***
100      * Builds the city path for US or other world cities. For world cities, the
101      * city path is global/station/station_number, i.e. for Istanbul, Turkey, it
102      * is global/stations/17060. The station numbers need to be obtained from
103      * the Weather Underground's site.
104      * 
105      * @param city
106      * @param state
107      * @param station
108      * @return
109      */
110     private String getCityInfo(String city, String state, String station)
111     {
112         String cityInfo = null;
113         if (city != null && state != null && !city.equals("")
114                 && !state.equals(""))
115         {
116             cityInfo = getUSInfo(city, state);
117         } else
118             if (station != null && !station.equals(""))
119             {
120                 cityInfo = "global/stations/" + station;
121             }
122         return cityInfo;
123     }
124 
125     /* (non-Javadoc)
126      * @see org.apache.portals.bridges.velocity.GenericVelocityPortlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
127      */
128     public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException
129     {
130         String city = request.getParameter(WEATHER_CITY);
131         String state = request.getParameter(WEATHER_STATE);
132         String style = request.getParameter(WEATHER_STYLE);
133         String station = request.getParameter(WEATHER_STATION);
134         PortletPreferences prefs = request.getPreferences();
135         prefs.setValue(WEATHER_CITY, city);
136         prefs.setValue(WEATHER_STATE, state);
137         prefs.setValue(WEATHER_STYLE, style);
138         prefs.setValue(WEATHER_STATION, station);
139         prefs.store();
140         super.processAction(request, response);
141     }
142     
143 }