001: /*
002: * (C) Copyright 2000 - 2003 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.portal.container;
021:
022: import java.io.IOException;
023: import java.io.Serializable;
024: import java.util.Enumeration;
025: import java.util.Hashtable;
026: import java.util.Iterator;
027: import java.util.Map;
028:
029: import javax.portlet.PortletPreferences;
030: import javax.portlet.PreferencesValidator;
031: import javax.portlet.ReadOnlyException;
032: import javax.portlet.ValidatorException;
033: import javax.xml.bind.annotation.XmlElement;
034: import javax.xml.bind.annotation.XmlRootElement;
035: import javax.xml.bind.annotation.XmlTransient;
036:
037: /**
038: *
039: *
040: * @author Padmanabh dabke
041: * (c) 2003 Nabh Information Systems, Inc. All Rights Reserved.
042: */
043:
044: @XmlRootElement(name="portlet-preferences")
045: public class PortletPreferencesImpl implements PortletPreferences {
046:
047: private PortletRequestImpl portletRequest = null;
048:
049: private PreferencesValidator ppiValidator = null;
050:
051: public static class PrefInfo implements Serializable {
052: private static final long serialVersionUID = 2012660389458820952L;
053:
054: @XmlElement(name="modifiable")
055: public boolean modifiable = true;
056:
057: @XmlElement(name="name")
058: public String name = null;
059:
060: @XmlElement(name="value")
061: public String[] value = null;
062:
063: public PrefInfo() {
064: };
065:
066: public PrefInfo(String name, boolean modFlag, String[] val) {
067: this .name = name;
068: modifiable = modFlag;
069: value = val;
070: }
071:
072: public PrefInfo copy() {
073: PrefInfo pInfo = new PrefInfo();
074: pInfo.name = this .name;
075: pInfo.modifiable = modifiable;
076: pInfo.value = new String[value.length];
077: for (int i = 0; i < value.length; i++) {
078: pInfo.value[i] = value[i];
079: }
080:
081: return pInfo;
082: }
083:
084: }
085:
086: @XmlTransient
087: protected Hashtable<String, PrefInfo> ppiOriginalPrefs = new Hashtable<String, PrefInfo>();
088:
089: protected Hashtable<String, PrefInfo> ppiUserPrefs = new Hashtable<String, PrefInfo>();
090: @XmlTransient
091: protected Hashtable<String, PrefInfo> ppiPrefs = new Hashtable<String, PrefInfo>();
092:
093: @XmlTransient
094: protected PreferencesStore ppiPrefStore = null;
095:
096: public PortletPreferencesImpl(
097: Hashtable<String, PrefInfo> origPrefs,
098: Hashtable<String, PrefInfo> userPrefs,
099: PreferencesStore store, PreferencesValidator validator,
100: PortletRequestImpl rReq) {
101: this .ppiPrefStore = store;
102: this .ppiValidator = validator;
103: this .portletRequest = rReq;
104:
105: if (origPrefs != null) {
106: this .ppiOriginalPrefs = origPrefs;
107: addPrefs(origPrefs);
108: }
109:
110: if (userPrefs != null) {
111: this .ppiUserPrefs = userPrefs;
112: addPrefs(userPrefs);
113: }
114: }
115:
116: private void addPrefs(Hashtable<String, PrefInfo> prefs) {
117: Iterator<Map.Entry<String, PrefInfo>> prefEntries = prefs
118: .entrySet().iterator();
119: while (prefEntries.hasNext()) {
120: Map.Entry<String, PrefInfo> entry = prefEntries.next();
121: this .ppiPrefs.put(entry.getKey(), entry.getValue().copy());
122: }
123: }
124:
125: public java.util.Map getMap() {
126: return ppiPrefs;
127: }
128:
129: /* (non-Javadoc)
130: * @see javax.portlet.PortletPreferences#isReadOnly(java.lang.String)
131: */
132: public boolean isReadOnly(String key) {
133: PrefInfo info = ppiPrefs.get(key);
134: if (info == null) {
135: return true;
136: } else {
137: return info.modifiable;
138: }
139: }
140:
141: /* (non-Javadoc)
142: * @see javax.portlet.PortletPreferences#getValue(java.lang.String, java.lang.String)
143: */
144: public String getValue(String key, String def) {
145: PrefInfo info = ppiPrefs.get(key);
146: if (info == null) {
147: return def;
148: } else {
149: return info.value[0];
150: }
151:
152: }
153:
154: /* (non-Javadoc)
155: * @see javax.portlet.PortletPreferences#getValues(java.lang.String, java.lang.String[])
156: */
157: public String[] getValues(String key, String[] def) {
158: PrefInfo info = ppiPrefs.get(key);
159: if (info == null) {
160: return def;
161: } else {
162: return info.value;
163: }
164: }
165:
166: /* (non-Javadoc)
167: * @see javax.portlet.PortletPreferences#setValue(java.lang.String, java.lang.String)
168: */
169: @SuppressWarnings("unchecked")
170: public void setValue(String key, String value)
171: throws ReadOnlyException {
172: PrefInfo info = ppiPrefs.get(key);
173: if (info == null) {
174: info = new PrefInfo(key, true, new String[] { value });
175: ppiPrefs.put(key, info);
176: } else {
177: if (info.modifiable) {
178: info.value = new String[] { value };
179: } else {
180: throw new ReadOnlyException(
181: "This property is not modifiable.");
182: }
183: }
184: ppiUserPrefs.put(key, info.copy());
185: }
186:
187: /* (non-Javadoc)
188: * @see javax.portlet.PortletPreferences#setValues(java.lang.String, java.lang.String[])
189: */
190: @SuppressWarnings("unchecked")
191: public void setValues(String key, String[] values)
192: throws ReadOnlyException {
193: PrefInfo info = ppiPrefs.get(key);
194: if (info == null) {
195: info = new PrefInfo(key, true, values);
196: ppiPrefs.put(key, info);
197: } else {
198: if (info.modifiable) {
199: info.value = values;
200: } else {
201: throw new ReadOnlyException(
202: "This property is not modifiable.");
203: }
204: }
205: ppiUserPrefs.put(key, info);
206:
207: }
208:
209: /* (non-Javadoc)
210: * @see javax.portlet.PortletPreferences#getNames()
211: */
212: public Enumeration getNames() {
213: return ppiPrefs.keys();
214: }
215:
216: /* (non-Javadoc)
217: * @see javax.portlet.PortletPreferences#reset(java.lang.String)
218: */
219: @SuppressWarnings("unchecked")
220: public void reset(String key) throws ReadOnlyException {
221: PrefInfo info = (PrefInfo) ppiOriginalPrefs.get(key);
222: if (info != null) {
223: ppiPrefs.put(key, info.copy());
224: } else {
225: ppiPrefs.remove(key);
226: }
227: ppiUserPrefs.remove(key);
228:
229: }
230:
231: /* (non-Javadoc)
232: * @see javax.portlet.PortletPreferences#store()
233: */
234: public void store() throws IOException, ValidatorException {
235: if (ppiValidator != null)
236: ppiValidator.validate(this );
237: ppiPrefStore
238: .store(this , portletRequest.getHttpServletRequest());
239:
240: }
241:
242: public void setPreferencesValidator(PreferencesValidator v) {
243: ppiValidator = v;
244: }
245:
246: @XmlTransient
247: public PreferencesStore getPreferencesStore() {
248: return ppiPrefStore;
249: }
250:
251: public void setPreferencesStore(PreferencesStore st) {
252: this .ppiPrefStore = st;
253: }
254:
255: public Hashtable<String, PrefInfo> getUserPreferences() {
256: return this.ppiUserPrefs;
257: }
258:
259: }
|