001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal;
007:
008: import java.util.ArrayList;
009: import java.util.Enumeration;
010: import java.util.Hashtable;
011: import java.util.List;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015:
016: /**
017: * User preferences for stylesheets performing theme transformation
018: * @author Peter Kharchenko <a href="mailto:">pkharchenko@interactivebusiness.com</>a
019: * @version $Revision: 35879 $
020: */
021:
022: public class ThemeStylesheetUserPreferences extends
023: StylesheetUserPreferences {
024:
025: private static final Log log = LogFactory
026: .getLog(ThemeStylesheetUserPreferences.class);
027:
028: protected Hashtable channelAttributeNumbers;
029: protected Hashtable channelAttributeValues;
030: protected ArrayList defaultChannelAttributeValues;
031:
032: public ThemeStylesheetUserPreferences() {
033: super ();
034: channelAttributeNumbers = new Hashtable();
035: channelAttributeValues = new Hashtable();
036: defaultChannelAttributeValues = new ArrayList();
037: }
038:
039: public ThemeStylesheetUserPreferences(
040: ThemeStylesheetUserPreferences ssup) {
041: super (ssup);
042: this .channelAttributeNumbers = new Hashtable(
043: ssup.channelAttributeNumbers);
044: this .channelAttributeValues = new Hashtable(
045: ssup.channelAttributeValues);
046: this .defaultChannelAttributeValues = new ArrayList(
047: ssup.defaultChannelAttributeValues);
048: }
049:
050: /**
051: * Provides a copy of this object with all fields instantiated to reflect
052: * the values of this object. This allows subclasses to override to add
053: * correct copying behavior for their added fields.
054: *
055: * @return a copy of this object
056: */
057: public Object newInstance() {
058: return new ThemeStylesheetUserPreferences(this );
059: }
060:
061: public String getChannelAttributeValue(String channelSubscribeId,
062: String attributeName) {
063: Integer attributeNumber = (Integer) channelAttributeNumbers
064: .get(attributeName);
065: if (attributeNumber == null) {
066: log
067: .error("ThemeStylesheetUserPreferences::getChannelAttributeValue() : Attempting to obtain a non-existing attribute \""
068: + attributeName + "\".");
069: return null;
070: }
071: String value = null;
072: List l = (List) channelAttributeValues.get(channelSubscribeId);
073: if (l == null) {
074: // log.debug("ThemeStylesheetUserPreferences::getChannelAttributeValue() : Attempting to obtain an attribute for a non-existing channel \""+channelSubscribeId+"\".");
075: // return null;
076: return (String) defaultChannelAttributeValues
077: .get(attributeNumber.intValue());
078: } else {
079: if (attributeNumber.intValue() < l.size()) {
080: value = (String) l.get(attributeNumber.intValue());
081: }
082: if (value == null) {
083: try {
084: value = (String) defaultChannelAttributeValues
085: .get(attributeNumber.intValue());
086: } catch (IndexOutOfBoundsException e) {
087: log
088: .error("ThemeStylesheetUserPreferences::getChannelAttributeValue() : internal error - attribute name is registered, but no default value is provided.");
089: return null;
090: }
091: }
092: }
093: return value;
094: }
095:
096: /**
097: * Returns channel attribute value only if it has been assigned specifically.
098: * @param channelSubscribeId channel id
099: * @param attributeName name of the attribute
100: * @return attribute value or null if the value is determined by the attribute default
101: */
102: public String getDefinedChannelAttributeValue(
103: String channelSubscribeId, String attributeName) {
104: Integer attributeNumber = (Integer) channelAttributeNumbers
105: .get(attributeName);
106: if (attributeNumber == null) {
107: log
108: .error("ThemeStylesheetUserPreferences::hasDefinedChannelAttributeValue() : Attempting to obtain a non-existing attribute \""
109: + attributeName + "\".");
110: return null;
111: }
112: List l = (List) channelAttributeValues.get(channelSubscribeId);
113: if (l == null) {
114: return null;
115: } else {
116: if (attributeNumber.intValue() < l.size())
117: return (String) l.get(attributeNumber.intValue());
118: else
119: return null;
120: }
121: }
122:
123: // this should be modified to throw exceptions
124: public void setChannelAttributeValue(String channelSubscribeId,
125: String attributeName, String attributeValue) {
126: Integer attributeNumber = (Integer) channelAttributeNumbers
127: .get(attributeName);
128: if (attributeNumber == null) {
129: log
130: .error("ThemeStylesheetUserPreferences::setChannelAttribute() : Attempting to set a non-existing channel attribute \""
131: + attributeName + "\".");
132: return;
133: }
134: List l = (List) channelAttributeValues.get(channelSubscribeId);
135: if (l == null)
136: l = this .createChannel(channelSubscribeId);
137: try {
138: l.set(attributeNumber.intValue(), attributeValue);
139: } catch (IndexOutOfBoundsException e) {
140: // bring up the array to the right size
141: for (int i = l.size(); i < attributeNumber.intValue(); i++) {
142: l.add((String) null);
143: }
144: l.add(attributeValue);
145: }
146: }
147:
148: public void addChannelAttribute(String attributeName,
149: String defaultValue) {
150: if (channelAttributeNumbers.get(attributeName) != null) {
151: log
152: .error("ThemeStylesheetUserPreferences::addChannelAttribute() : Attempting to re-add an existing channel attribute \""
153: + attributeName + "\".");
154: } else {
155: channelAttributeNumbers.put(attributeName, new Integer(
156: defaultChannelAttributeValues.size()));
157: // append to the end of the default value array
158: defaultChannelAttributeValues.add(defaultValue);
159: }
160: }
161:
162: public void setChannelAttributeDefaultValue(String attributeName,
163: String defaultValue) {
164: Integer attributeNumber = (Integer) channelAttributeNumbers
165: .get(attributeName);
166: defaultChannelAttributeValues.set(attributeNumber.intValue(),
167: defaultValue);
168: }
169:
170: public void removeChannelAttribute(String attributeName) {
171: Integer attributeNumber;
172: if ((attributeNumber = (Integer) channelAttributeNumbers
173: .get(attributeName)) == null) {
174: log
175: .error("ThemeStylesheetUserPreferences::removeChannelAttribute() : Attempting to remove a non-existing channel attribute \""
176: + attributeName + "\".");
177: } else {
178: channelAttributeNumbers.remove(attributeName);
179: // do not touch the arraylists
180: }
181: }
182:
183: public Enumeration getChannelAttributeNames() {
184: return channelAttributeNumbers.keys();
185: }
186:
187: public void addChannel(String channelSubscribeId) {
188: // check if the channel is there. In general it might be ok to use this functon to default
189: // all of the channel's parameters
190:
191: ArrayList l = new ArrayList(defaultChannelAttributeValues
192: .size());
193:
194: if (channelAttributeValues.put(channelSubscribeId, l) != null
195: && log.isDebugEnabled())
196: log
197: .debug("ThemeStylesheetUserPreferences::addChannel() : Readding an existing channel (channelSubscribeId=\""
198: + channelSubscribeId
199: + "\"). All values will be set to default.");
200: }
201:
202: public void removeChannel(String channelSubscribeId) {
203: if (channelAttributeValues.remove(channelSubscribeId) == null
204: && log.isDebugEnabled())
205: log
206: .error("ThemeStylesheetUserPreferences::removeChannel() : Attempting to remove an non-existing channel (channelSubscribeId=\""
207: + channelSubscribeId + "\").");
208: }
209:
210: public Enumeration getChannels() {
211: return channelAttributeValues.keys();
212: }
213:
214: public boolean hasChannel(String channelSubscribeId) {
215: return channelAttributeValues.containsKey(channelSubscribeId);
216: }
217:
218: private ArrayList createChannel(String channelSubscribeId) {
219: ArrayList l = new ArrayList(defaultChannelAttributeValues
220: .size());
221: channelAttributeValues.put(channelSubscribeId, l);
222: return l;
223: }
224:
225: public String getCacheKey() {
226: StringBuffer sbKey = new StringBuffer();
227: for (Enumeration e = channelAttributeValues.keys(); e
228: .hasMoreElements();) {
229: String channelId = (String) e.nextElement();
230: sbKey.append("(channel:").append(channelId).append(':');
231: List l = (List) channelAttributeValues.get(channelId);
232: for (int i = 0; i < l.size(); i++) {
233: String value = (String) l.get(i);
234: if (value == null)
235: value = (String) defaultChannelAttributeValues
236: .get(i);
237: sbKey.append(value).append(",");
238: }
239: sbKey.append(")");
240: }
241: return super.getCacheKey().concat(sbKey.toString());
242: }
243: }
|