001: /**
002: * $Id: SettingsHandler.java,v 1.2 2006/04/17 20:08:42 jtb Exp $
003: * Copyright 2003 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.rssportlet;
014:
015: import java.io.IOException;
016: import java.util.Arrays;
017: import java.util.LinkedList;
018: import javax.portlet.PortletConfig;
019: import javax.portlet.PortletPreferences;
020: import javax.portlet.PortletRequest;
021: import javax.portlet.PortletSession;
022: import javax.portlet.ReadOnlyException;
023: import javax.portlet.ValidatorException;
024:
025: /**
026: * This class builds a <code>SettingsBean</code> object.
027: */
028: public class SettingsHandler {
029: //
030: // portlet preference keys
031: //
032: private interface PrefKeys {
033: public static final String FEEDS = "feeds";
034: public static final String MAX_DESCRIPTION_LENGTH = "maxDescriptionLength";
035: public static final String CACHE_TIMEOUT = "cacheTimeout";
036: public static final String NEWWIN = "newWindow";
037: public static final String MAX_AGE = "maxAge";
038: public static final String MAX_ENTRIES = "maxEntries";
039: public static final String DISABLE_MAX_AGE = "disableMaxAge";
040: }
041:
042: //
043: // session attribute keys
044: //
045: private interface SessionKeys {
046: public static final String SELECTED_FEED = "selectedFeed";
047: }
048:
049: private SettingsBean settingsBean;
050:
051: private PortletRequest portletRequest = null;
052: private PortletConfig portletConfig = null;
053:
054: /**
055: * Initialize the <code>RssPortletBean</code>.
056: *
057: * This is called after the bean has been set into this handler.
058: */
059: private void initSettingsBean() {
060: LinkedList feeds = new LinkedList(Arrays
061: .asList(getPortletPreferences().getValues(
062: PrefKeys.FEEDS, new String[] {})));
063: getSettingsBean().setFeeds(feeds);
064: int maxAge = getIntPreference(PrefKeys.MAX_AGE, 72);
065: getSettingsBean().setMaxAge(maxAge);
066: int cacheTimeout = getIntPreference(PrefKeys.CACHE_TIMEOUT,
067: 3600);
068: getSettingsBean().setCacheTimeout(cacheTimeout);
069: int maxDescriptionLength = getIntPreference(
070: PrefKeys.MAX_DESCRIPTION_LENGTH, 512);
071: getSettingsBean().setMaxDescriptionLength(maxDescriptionLength);
072: boolean newWindow = getBooleanPreference(PrefKeys.NEWWIN, false);
073: getSettingsBean().setNewWindow(newWindow);
074: boolean disableMaxAge = getBooleanPreference(
075: PrefKeys.DISABLE_MAX_AGE, false);
076: getSettingsBean().setDisableMaxAge(disableMaxAge);
077: int maxEntries = getIntPreference(PrefKeys.MAX_ENTRIES, 5);
078: getSettingsBean().setMaxEntries(maxEntries);
079:
080: initSelectedFeed();
081:
082: getSettingsBean().setLocale(getPortletRequest().getLocale());
083: }
084:
085: /**
086: * Initialize the <code>selectedFeed</code>
087: * field of the <code>RssPortletBean</code>.
088: */
089: private void initSelectedFeed() {
090: // first, try to get from session
091: String selectedFeed = (String) getPortletRequest()
092: .getPortletSession().getAttribute(
093: SessionKeys.SELECTED_FEED);
094:
095: if (selectedFeed == null || selectedFeed.trim().length() == 0) {
096: // next, try to get from start feed
097: selectedFeed = getSettingsBean().getStartFeed();
098: }
099:
100: // might be null
101: getSettingsBean().setSelectedFeed(selectedFeed);
102: }
103:
104: /**
105: * Persist this handler's <code>RssPortletBean</code>, if necessary.
106: */
107: public void persistSettingsBean(SettingsBean delta)
108: throws ReadOnlyException, IOException, ValidatorException {
109: boolean store = false;
110:
111: // selected feed
112: if (delta.getFeeds() != null && delta.getFeedsSize() == 0) {
113: // if there are no feeds, set the selected feed to null
114: getPortletSession().setAttribute(SessionKeys.SELECTED_FEED,
115: null);
116: } else if (delta.getSelectedFeed() != null) {
117: getPortletSession().setAttribute(SessionKeys.SELECTED_FEED,
118: delta.getSelectedFeed());
119: }
120:
121: // feeds
122: if (delta.getFeeds() != null) {
123: getPortletPreferences().setValues(PrefKeys.FEEDS,
124: (String[]) delta.getFeeds().toArray(new String[0]));
125: store = true;
126: }
127:
128: // max age
129: if (delta.isMaxAgeSet()) {
130: getPortletPreferences().setValue(PrefKeys.MAX_AGE,
131: Integer.toString(delta.getMaxAge()));
132: store = true;
133: }
134:
135: // disable max age
136: if (delta.isDisableMaxAgeSet()) {
137: getPortletPreferences().setValue(PrefKeys.DISABLE_MAX_AGE,
138: Boolean.toString(delta.isDisableMaxAge()));
139: store = true;
140: }
141:
142: // max entries
143: if (delta.isMaxEntriesSet()) {
144: getPortletPreferences().setValue(PrefKeys.MAX_ENTRIES,
145: Integer.toString(delta.getMaxEntries()));
146: store = true;
147: }
148:
149: // new window
150: if (delta.isNewWindowSet()) {
151: getPortletPreferences().setValue(PrefKeys.NEWWIN,
152: Boolean.toString(delta.isNewWindow()));
153: store = true;
154: }
155:
156: if (store) {
157: getPortletPreferences().store();
158: }
159: }
160:
161: /** Get a portlet preference as an int. */
162: private int getIntPreference(String key, int def) {
163: String s = getPortletPreferences().getValue(key, null);
164: int i = def;
165: try {
166: i = Integer.parseInt(s);
167: } catch (NumberFormatException nfe) {
168: i = def;
169: }
170:
171: return i;
172: }
173:
174: /** Get a portlet preference as a boolean. */
175: private boolean getBooleanPreference(String key, boolean def) {
176: Boolean b = Boolean.valueOf(getPortletPreferences().getValue(
177: key, "false"));
178: return b.booleanValue();
179: }
180:
181: /** Get the Rss portlet bean. */
182: public SettingsBean getSettingsBean() {
183: return settingsBean;
184: }
185:
186: /** Set the Rss portlet bean. */
187: public void setSettingsBean(SettingsBean settingsBean) {
188: this .settingsBean = settingsBean;
189: initSettingsBean();
190: }
191:
192: /** Get the portlet request. */
193: private PortletRequest getPortletRequest() {
194: return portletRequest;
195: }
196:
197: /** Set the portlet request. */
198: public void setPortletRequest(PortletRequest portletRequest) {
199: this .portletRequest = portletRequest;
200: }
201:
202: /** Get the portlet config. */
203: private PortletConfig getPortletConfig() {
204: return portletConfig;
205: }
206:
207: /** Set the portlet config. */
208: public void setPortletConfig(PortletConfig portletConfig) {
209: this .portletConfig = portletConfig;
210: }
211:
212: /** Get the portlet preferences. */
213: private PortletPreferences getPortletPreferences() {
214: return getPortletRequest().getPreferences();
215: }
216:
217: /** Get the portlet session. */
218: private PortletSession getPortletSession() {
219: return getPortletRequest().getPortletSession();
220: }
221: }
|