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.jetspeed.om.preference.impl;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import javax.portlet.PreferencesValidator;
026:
027: import org.apache.jetspeed.om.common.preference.PreferenceSetComposite;
028: import org.apache.jetspeed.om.page.Fragment;
029: import org.apache.pluto.om.common.Preference;
030:
031: /**
032: * This is a per-request wrapper for a PreferenceSet that allows
033: * the use of fragment-specified Preferences within a portlet instance
034: * in a page.
035: *
036: * @author <href a="mailto:weaver@apache.org">Scott T. Weaver</a>
037: *
038: */
039: public class FragmentPortletPreferenceSet implements
040: PreferenceSetComposite {
041: private final PreferenceSetComposite preferenceSet;
042: private final Map prefs;
043:
044: public FragmentPortletPreferenceSet(
045: PreferenceSetComposite preferenceSet, Fragment fragment) {
046: // save mutable preference set and read only fragment
047: this .preferenceSet = preferenceSet;
048: // construct merged portlet definition prefs map;
049: // note that user specific preferences accessed via
050: // the portlet entity should override these defaults
051: int prefsSize = preferenceSet.size() + 1;
052: if (fragment != null && fragment.getPreferences() != null) {
053: prefsSize += fragment.getPreferences().size();
054: }
055: this .prefs = new HashMap(prefsSize);
056:
057: // add global portlet definition defaults to prefs
058: Iterator iterator = preferenceSet.iterator();
059: while (iterator.hasNext()) {
060: Preference pref = (Preference) iterator.next();
061: prefs.put(pref.getName(), pref);
062: }
063:
064: // add/override global portlet definition defaults
065: // using more specific fragment preferences
066: if (fragment != null && fragment.getPreferences() != null) {
067: Iterator itr = fragment.getPreferences().iterator();
068: while (itr.hasNext()) {
069: Preference pref = (Preference) itr.next();
070: prefs.put(pref.getName(), pref);
071: }
072: }
073: }
074:
075: public Preference add(String arg0, List arg1) {
076: Preference pref = preferenceSet.add(arg0, arg1);
077: prefs.put(arg0, pref);
078: return pref;
079: }
080:
081: public Preference get(String name) {
082: return (Preference) prefs.get(name);
083: }
084:
085: public Set getNames() {
086: return prefs.keySet();
087: }
088:
089: public PreferencesValidator getPreferencesValidator() {
090: return preferenceSet.getPreferencesValidator();
091: }
092:
093: public Iterator iterator() {
094: return prefs.values().iterator();
095: }
096:
097: public void remove(Preference pref) {
098: prefs.remove(pref.getName());
099: preferenceSet.remove(pref);
100: }
101:
102: public Preference remove(String name) {
103: Preference pref = (Preference) prefs.remove(name);
104: preferenceSet.remove(name);
105: return pref;
106: }
107:
108: public int size() {
109: return prefs.size();
110: }
111: }
|