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 structure transformation
018: * @author Peter Kharchenko
019: * @version $Revision: 35879 $
020: */
021:
022: // structure stylesheet preferences will remain to be more complex then
023: // preferences of the second stylesheet, hence the derivation
024: public class StructureStylesheetUserPreferences extends
025: ThemeStylesheetUserPreferences {
026:
027: private static final Log log = LogFactory
028: .getLog(StructureStylesheetUserPreferences.class);
029:
030: protected Hashtable folderAttributeNumbers;
031: protected Hashtable folderAttributeValues;
032: protected ArrayList defaultFolderAttributeValues;
033:
034: public StructureStylesheetUserPreferences() {
035: super ();
036: folderAttributeNumbers = new Hashtable();
037: folderAttributeValues = new Hashtable();
038: defaultFolderAttributeValues = new ArrayList();
039: }
040:
041: public StructureStylesheetUserPreferences(
042: StructureStylesheetUserPreferences fsup) {
043: super (fsup);
044:
045: // fields added by this class may be null if we are dealing with an
046: // instance of DistributeUserPreferences wrapping a
047: // ThemeStylesheetUserPreferences object since the theme transform does
048: // not use folder preferences. So check for null before instantiating
049: // these fields.
050:
051: if (fsup.folderAttributeNumbers != null)
052: this .folderAttributeNumbers = new Hashtable(
053: fsup.folderAttributeNumbers);
054: if (fsup.folderAttributeValues != null)
055: this .folderAttributeValues = new Hashtable(
056: fsup.folderAttributeValues);
057: if (fsup.defaultFolderAttributeValues != null)
058: this .defaultFolderAttributeValues = new ArrayList(
059: fsup.defaultFolderAttributeValues);
060: }
061:
062: /**
063: * If instantiated with a theme stylesheet this object will be used only
064: * for theme type work and so don't instantiate the variables added by
065: * this class. Used in DLM.
066: */
067: public StructureStylesheetUserPreferences(
068: ThemeStylesheetUserPreferences tsup) {
069: super (tsup);
070: }
071:
072: /**
073: * Provides a copy of this object with all fields instantiated to reflect
074: * the values of this object. This allows subclasses to override to add
075: * correct copying behavior for their added fields.
076: *
077: * @return a copy of this object
078: */
079: public Object newInstance() {
080: return new StructureStylesheetUserPreferences(this );
081: }
082:
083: public String getFolderAttributeValue(String folderID,
084: String attributeName) {
085: Integer attributeNumber = (Integer) folderAttributeNumbers
086: .get(attributeName);
087: if (attributeNumber == null) {
088: log
089: .error("StructureStylesheetUserPreferences::getFolderAttributeValue() : Attempting to obtain a non-existing attribute \""
090: + attributeName + "\".");
091: return null;
092: }
093: String value = null;
094: List l = (List) folderAttributeValues.get(folderID);
095: if (l == null) {
096: // log.error("StructureStylesheetUserPreferences::getFolderAttributeValue() : Attempting to obtain an attribute for a non-existing folder \""+folderID+"\".");
097: // return null;
098: return (String) defaultFolderAttributeValues
099: .get(attributeNumber.intValue());
100: } else {
101: if (attributeNumber.intValue() < l.size()) {
102: value = (String) l.get(attributeNumber.intValue());
103: }
104: if (value == null) {
105: try {
106: value = (String) defaultFolderAttributeValues
107: .get(attributeNumber.intValue());
108: } catch (IndexOutOfBoundsException e) {
109: log
110: .error("StructureStylesheetUserPreferences::getFolderAttributeValue() : internal error - attribute name is registered, but no default value is provided.");
111: return null;
112: }
113: }
114: }
115: return value;
116: }
117:
118: /**
119: * Returns folder attribute value only if it has been assigned specifically.
120: * @param folderID folder id
121: * @param attributeName name of the attribute
122: * @return attribute value or null if the value is determined by the attribute default
123: */
124: public String getDefinedFolderAttributeValue(String folderID,
125: String attributeName) {
126: Integer attributeNumber = (Integer) folderAttributeNumbers
127: .get(attributeName);
128: if (attributeNumber == null) {
129: log
130: .error("ThemeStylesheetUserPreferences::hasDefinedFolderAttributeValue() : Attempting to obtain a non-existing attribute \""
131: + attributeName + "\".");
132: return null;
133: }
134: List l = (List) folderAttributeValues.get(folderID);
135: if (l == null) {
136: return null;
137: } else {
138: if (attributeNumber.intValue() < l.size())
139: return (String) l.get(attributeNumber.intValue());
140: else
141: return null;
142: }
143: }
144:
145: // this should be modified to throw exceptions
146: public void setFolderAttributeValue(String folderID,
147: String attributeName, String attributeValue) {
148: Integer attributeNumber = (Integer) folderAttributeNumbers
149: .get(attributeName);
150: if (attributeNumber == null) {
151: log
152: .error("StructureStylesheetUserPreferences::setFolderAttribute() : Attempting to set a non-existing folder attribute \""
153: + attributeName + "\".");
154: return;
155: }
156: List l = (List) folderAttributeValues.get(folderID);
157: if (l == null)
158: l = this .createFolder(folderID);
159: try {
160: l.set(attributeNumber.intValue(), attributeValue);
161: } catch (IndexOutOfBoundsException e) {
162: // bring up the array to the right size
163: for (int i = l.size(); i < attributeNumber.intValue(); i++) {
164: l.add((String) null);
165: }
166: l.add(attributeValue);
167: }
168: }
169:
170: public void addFolderAttribute(String attributeName,
171: String defaultValue) {
172: if (folderAttributeNumbers.get(attributeName) != null) {
173: log
174: .error("StructureStylesheetUserPreferences::addFolderAttribute() : Attempting to re-add an existing folder attribute \""
175: + attributeName + "\".");
176: } else {
177: folderAttributeNumbers.put(attributeName, new Integer(
178: defaultFolderAttributeValues.size()));
179: // append to the end of the default value array
180: defaultFolderAttributeValues.add(defaultValue);
181: }
182: }
183:
184: public void setFolderAttributeDefaultValue(String attributeName,
185: String defaultValue) {
186: Integer attributeNumber = (Integer) folderAttributeNumbers
187: .get(attributeName);
188: defaultFolderAttributeValues.set(attributeNumber.intValue(),
189: defaultValue);
190: }
191:
192: public void removeFolderAttribute(String attributeName) {
193: Integer attributeNumber;
194: if ((attributeNumber = (Integer) folderAttributeNumbers
195: .get(attributeName)) == null) {
196: log
197: .error("StructureStylesheetUserPreferences::removeFolderAttribute() : Attempting to remove a non-existing folder attribute \""
198: + attributeName + "\".");
199: } else {
200: folderAttributeNumbers.remove(attributeName);
201: // do not touch the arraylists
202: }
203: }
204:
205: public Enumeration getFolderAttributeNames() {
206: return folderAttributeNumbers.keys();
207: }
208:
209: public void addFolder(String folderID) {
210: // check if the folder is there. In general it might be ok to use this functon to default
211: // all of the folder's parameters
212:
213: ArrayList l = new ArrayList(defaultFolderAttributeValues.size());
214:
215: if (folderAttributeValues.put(folderID, l) != null)
216: log
217: .debug("StructureStylesheetUserPreferences::addFolder() : Readding an existing folder (folderID=\""
218: + folderID
219: + "\"). All values will be set to default.");
220: }
221:
222: public void removeFolder(String folderID) {
223: if (folderAttributeValues.remove(folderID) == null)
224: log
225: .error("StructureStylesheetUserPreferences::removeFolder() : Attempting to remove an non-existing folder (folderID=\""
226: + folderID + "\").");
227: }
228:
229: public Enumeration getFolders() {
230: return folderAttributeValues.keys();
231: }
232:
233: public boolean hasFolder(String folderID) {
234: return folderAttributeValues.containsKey(folderID);
235: }
236:
237: private ArrayList createFolder(String folderID) {
238: ArrayList l = new ArrayList(defaultFolderAttributeValues.size());
239: folderAttributeValues.put(folderID, l);
240: return l;
241: }
242:
243: private Hashtable copyFolderAttributeNames() {
244: return folderAttributeNumbers;
245: }
246:
247: public String getCacheKey() {
248: StringBuffer sbKey = new StringBuffer();
249: // if no folder values then skip adding to prevent null pointer ex.
250: if (folderAttributeValues != null) {
251: for (Enumeration e = folderAttributeValues.keys(); e
252: .hasMoreElements();) {
253: String folderId = (String) e.nextElement();
254: sbKey.append("(folder:").append(folderId).append(':');
255: List l = (List) folderAttributeValues.get(folderId);
256: for (int i = 0; i < l.size(); i++) {
257: String value = (String) l.get(i);
258: if (value == null)
259: value = (String) defaultFolderAttributeValues
260: .get(i);
261: sbKey.append(value).append(",");
262: }
263: sbKey.append(")");
264: }
265: }
266: return super.getCacheKey().concat(sbKey.toString());
267: }
268:
269: }
|