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.io.Serializable;
009: import java.util.Enumeration;
010: import java.util.HashSet;
011:
012: import org.w3c.dom.Document;
013: import org.w3c.dom.Element;
014: import org.w3c.dom.NodeList;
015:
016: /**
017: * Object managing user preferences.
018: * @author Peter Kharchenko
019: * @version $Revision: 35884 $
020: */
021:
022: public class UserPreferences implements Serializable {
023:
024: protected UserProfile profile;
025:
026: protected StructureStylesheetUserPreferences fsup;
027: protected ThemeStylesheetUserPreferences ssup;
028:
029: /*
030: * Copy-constructor
031: */
032: public UserPreferences(UserPreferences up) {
033: fsup = (StructureStylesheetUserPreferences) up
034: .getStructureStylesheetUserPreferences().newInstance();
035: ssup = (ThemeStylesheetUserPreferences) up
036: .getThemeStylesheetUserPreferences().newInstance();
037: profile = up.getProfile();
038: }
039:
040: public void setProfile(UserProfile p) {
041: profile = p;
042: }
043:
044: public UserProfile getProfile() {
045: return profile;
046: }
047:
048: public UserPreferences(UserProfile p) {
049: this .profile = p;
050: }
051:
052: public void setStructureStylesheetUserPreferences(
053: StructureStylesheetUserPreferences up) {
054: this .fsup = up;
055: }
056:
057: public void setThemeStylesheetUserPreferences(
058: ThemeStylesheetUserPreferences up) {
059: this .ssup = up;
060: }
061:
062: public StructureStylesheetUserPreferences getStructureStylesheetUserPreferences() {
063: return this .fsup;
064: }
065:
066: public ThemeStylesheetUserPreferences getThemeStylesheetUserPreferences() {
067: return this .ssup;
068: }
069:
070: public void synchronizeWithUserLayoutXML(Document uLayoutXML)
071: throws PortalException {
072: // make a list of channels in the XML Layout
073: NodeList channelNodes = uLayoutXML
074: .getElementsByTagName("channel");
075: HashSet channelSet = new HashSet();
076: for (int i = 0; i < channelNodes.getLength(); i++) {
077: Element el = (Element) channelNodes.item(i);
078: if (el != null) {
079: String channelSubscribeId = el.getAttribute("ID");
080: if (!fsup.hasChannel(channelSubscribeId)) {
081: fsup.addChannel(channelSubscribeId);
082: // log.debug("UserPrefenreces::synchUserPreferencesWithLayout() : StructureStylesheetUserPreferences were missing a channel="+channelSubscribeId);
083: }
084: if (!ssup.hasChannel(channelSubscribeId)) {
085: ssup.addChannel(channelSubscribeId);
086: // log.debug("UserPreferences::synchUserPreferencesWithLayout() : ThemeStylesheetUserPreferences were missing a channel="+channelSubscribeId);
087: }
088: channelSet.add(channelSubscribeId);
089: }
090:
091: }
092:
093: // make a list of categories in the XML Layout
094: NodeList folderNodes = uLayoutXML
095: .getElementsByTagName("folder");
096: HashSet folderSet = new HashSet();
097: for (int i = 0; i < folderNodes.getLength(); i++) {
098: Element el = (Element) folderNodes.item(i);
099: if (el != null) {
100: String caID = el.getAttribute("ID");
101: if (!fsup.hasFolder(caID)) {
102: fsup.addFolder(caID);
103: // log.debug("UserPreferences::synchUserPreferencesWithLayout() : StructureStylesheetUserPreferences were missing a folder="+caID);
104: }
105: folderSet.add(caID);
106: }
107: }
108:
109: // cross check
110: for (Enumeration e = fsup.getChannels(); e.hasMoreElements();) {
111: String chID = (String) e.nextElement();
112: if (!channelSet.contains(chID)) {
113: fsup.removeChannel(chID);
114: // log.debug("UserPreferences::synchUserPreferencesWithLayout() : StructureStylesheetUserPreferences had a non-existent channel="+chID);
115: }
116: }
117:
118: for (Enumeration e = fsup.getFolders(); e.hasMoreElements();) {
119: String caID = (String) e.nextElement();
120: if (!folderSet.contains(caID)) {
121: fsup.removeFolder(caID);
122: // log.debug("UserPreferences::synchUserPreferencesWithLayout() : StructureStylesheetUserPreferences had a non-existent folder="+caID);
123: }
124: }
125:
126: for (Enumeration e = ssup.getChannels(); e.hasMoreElements();) {
127: String chID = (String) e.nextElement();
128: if (!channelSet.contains(chID)) {
129: ssup.removeChannel(chID);
130: // log.debug("UserPreferences::synchUserPreferencesWithLayout() : ThemeStylesheetUserPreferences had a non-existent channel="+chID);
131: }
132: }
133:
134: }
135:
136: /**
137: * Generates a unique state key
138: *
139: * @return a <code>String</code> key
140: */
141: public String getCacheKey() {
142: String key = null;
143: if (profile.isSystemProfile()) {
144: key = "system " + Integer.toString(profile.getProfileId());
145: } else {
146: key = Integer.toString(profile.getProfileId());
147: }
148: key = key.concat(fsup.getCacheKey());
149: key = key.concat(ssup.getCacheKey());
150: return key;
151:
152: }
153:
154: }
|