001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.portals.applications.desktop;
018:
019: import java.io.IOException;
020: import java.util.StringTokenizer;
021:
022: import javax.portlet.ActionRequest;
023: import javax.portlet.ActionResponse;
024: import javax.portlet.PortletException;
025: import javax.portlet.PortletPreferences;
026: import javax.portlet.RenderRequest;
027: import javax.portlet.RenderResponse;
028:
029: import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
030: import org.apache.velocity.context.Context;
031:
032: public class WeatherPortlet extends GenericVelocityPortlet {
033:
034: public static final String WEATHER_CITY_INFO = "weather_city_info";
035:
036: public static final String WEATHER_STATE = "weather_state";
037:
038: public static final String WEATHER_CITY = "weather_city";
039:
040: public static final String WEATHER_STATION = "weather_station";
041:
042: public static final String WEATHER_STYLE = "weather_style";
043:
044: public void doView(RenderRequest request, RenderResponse response)
045: throws PortletException, IOException {
046: Context context = super .getContext(request);
047:
048: String cityInfo = (String) request.getPortletSession()
049: .getAttribute(WEATHER_CITY_INFO);
050:
051: PortletPreferences prefs = request.getPreferences();
052: String city = prefs.getValue(WEATHER_CITY, "Bakersfield");
053: String state = prefs.getValue(WEATHER_STATE, "CA");
054: String station = prefs.getValue(WEATHER_STATION, null);
055: cityInfo = getCityInfo(city, state, station);
056: context.put(WEATHER_CITY_INFO, cityInfo);
057:
058: String style = prefs.getValue(WEATHER_STYLE, "infobox");
059: context.put(WEATHER_STYLE, style);
060: response.setProperty("david", "taylor");
061: super .doView(request, response);
062: }
063:
064: public void doEdit(RenderRequest request, RenderResponse response)
065: throws PortletException, IOException {
066: response.setContentType("text/html");
067: doPreferencesEdit(request, response);
068: }
069:
070: /**
071: * Builds the path for US cities. The format is US/ST/City.html, i.e. for
072: * New York City, the city path is US/NY/New_York
073: *
074: * @param city
075: * @param state
076: * @return
077: */
078: private String getUSInfo(String city, String state) {
079: city = city.trim().toLowerCase() + " ";
080: if (city.indexOf(" ") > 0) {
081: String newCity = "";
082: StringTokenizer st = new StringTokenizer(city, " ");
083: while (st.hasMoreTokens()) {
084: String token = st.nextToken();
085: newCity = newCity + token.substring(0, 1).toUpperCase()
086: + token.substring(1) + "_";
087: }
088: city = newCity.substring(0, newCity.length() - 1); // remove last
089: // '_'
090: }
091: state = state.toUpperCase();
092: return "US/" + state + "/" + city;
093: }
094:
095: /**
096: * Builds the city path for US or other world cities. For world cities, the
097: * city path is global/station/station_number, i.e. for Istanbul, Turkey, it
098: * is global/stations/17060. The station numbers need to be obtained from
099: * the Weather Underground's site.
100: *
101: * @param city
102: * @param state
103: * @param station
104: * @return
105: */
106: private String getCityInfo(String city, String state, String station) {
107: String cityInfo = null;
108: if (city != null && state != null && !city.equals("")
109: && !state.equals("")) {
110: cityInfo = getUSInfo(city, state);
111: } else if (station != null && !station.equals("")) {
112: cityInfo = "global/stations/" + station;
113: }
114: return cityInfo;
115: }
116:
117: /* (non-Javadoc)
118: * @see org.apache.portals.bridges.velocity.GenericVelocityPortlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
119: */
120: public void processAction(ActionRequest request,
121: ActionResponse response) throws PortletException,
122: IOException {
123: String city = request.getParameter(WEATHER_CITY);
124: String state = request.getParameter(WEATHER_STATE);
125: String style = request.getParameter(WEATHER_STYLE);
126: String station = request.getParameter(WEATHER_STATION);
127: PortletPreferences prefs = request.getPreferences();
128: prefs.setValue(WEATHER_CITY, city);
129: prefs.setValue(WEATHER_STATE, state);
130: prefs.setValue(WEATHER_STYLE, style);
131: prefs.setValue(WEATHER_STATION, station);
132: prefs.store();
133: super.processAction(request, response);
134: }
135:
136: }
|