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.channels.DLMUserPreferences;
007:
008: import org.jasig.portal.*;
009: import org.w3c.dom.Document;
010: import org.xml.sax.ContentHandler;
011:
012: import org.jasig.portal.layout.IUserLayoutManager;
013: import org.jasig.portal.layout.IUserLayoutStore;
014: import org.jasig.portal.layout.UserLayoutStoreFactory;
015: import org.jasig.portal.security.IPerson;
016: import org.apache.commons.logging.Log;
017: import org.apache.commons.logging.LogFactory;
018:
019: /** <p>Manages User Layout, user preferences and profiles </p>
020: * @author Peter Kharchenko, peterk@interactivebusiness.com
021: * @author Ken Weiner, kweiner@interactivebusiness.com
022: * @version $Revision: 36441 $
023: */
024: public class CUserPreferences implements IPrivilegedChannel {
025: private static final Log log = LogFactory
026: .getLog(CUserPreferences.class);
027: IUserPreferencesManager upm;
028: ChannelRuntimeData runtimeData = null;
029: ChannelStaticData staticData = null;
030: StylesheetSet set = null;
031: private static final String sslLocation = "/org/jasig/portal/channels/DLMUserPreferences/CUserPreferences.ssl";
032:
033: private UserPreferences up = null;
034: public static final int MANAGE_PREFERENCES = 1;
035: public static final int MANAGE_PROFILES = 2;
036: IPrivilegedChannel internalState = null;
037: IPrivilegedChannel managePreferences = null;
038: IPrivilegedChannel manageProfiles = null;
039: protected IUserLayoutStore ulsdb;
040: private boolean initialized = false;
041: UserProfile editedProfile = null;
042:
043: public CUserPreferences() throws PortalException {
044: this .runtimeData = new ChannelRuntimeData();
045: this .set = new StylesheetSet(this .getClass().getResource(
046: sslLocation).toString());
047: this .set.setMediaProps("/properties/media.properties");
048:
049: manageProfiles = new ManageProfilesState(this );
050: ulsdb = UserLayoutStoreFactory.getUserLayoutStoreImpl();
051: }
052:
053: protected IUserPreferencesManager getUserPreferencesManager() {
054: return upm;
055: }
056:
057: protected IUserLayoutManager getUserLayoutManager() {
058: return getUserPreferencesManager().getUserLayoutManager();
059: }
060:
061: protected UserPreferences getCurrentUserPreferences() {
062: return up;
063: }
064:
065: protected ChannelRuntimeData getRuntimeData() {
066: return runtimeData;
067: }
068:
069: protected StylesheetSet getStylesheetSet() {
070: return set;
071: }
072:
073: public void setPortalControlStructures(PortalControlStructures pcs)
074: throws PortalException {
075: if (upm == null)
076: upm = pcs.getUserPreferencesManager();
077: if (up == null)
078: up = upm.getUserPreferencesCopy();
079: // instantiate the browse state here
080:
081: if (!initialized) {
082: instantiateManagePreferencesState(up.getProfile());
083: // Initial state should be manage preferences
084: internalState = managePreferences;
085: internalState.setStaticData(staticData);
086: editedProfile = up.getProfile();
087: initialized = true;
088: }
089: if (internalState != null) {
090: internalState.setPortalControlStructures(pcs);
091: }
092: }
093:
094: /**
095: * Instantiates appropriate managePreferences object.
096: *
097: * @param profile profile for which preferences are to be edited
098: */
099: private void instantiateManagePreferencesState(UserProfile profile) {
100: try {
101: ThemeStylesheetDescription tsd = ulsdb
102: .getThemeStylesheetDescription(profile
103: .getThemeStylesheetId());
104: if (tsd != null) {
105: String cupmClass = tsd
106: .getCustomUserPreferencesManagerClass();
107: managePreferences = (IPrivilegedChannel) Class.forName(
108: cupmClass).newInstance();
109: ((BaseState) managePreferences).setContext(this );
110: } else {
111: log
112: .error("Unable to retrieve theme stylesheet description. stylesheetId="
113: + profile.getThemeStylesheetId());
114: managePreferences = new GPreferencesState(this );
115: }
116: } catch (Exception e) {
117: log.error("Error instantiating user profile [" + profile
118: + "]", e);
119: managePreferences = new GPreferencesState(this );
120: }
121: }
122:
123: /** Returns channel runtime properties
124: * @return handle to runtime properties
125: */
126: public ChannelRuntimeProperties getRuntimeProperties() {
127: // Channel will always render, so the default values are ok
128: return new ChannelRuntimeProperties();
129: }
130:
131: /** Processes layout-level events coming from the portal
132: * @param ev a portal layout event
133: */
134: public void receiveEvent(PortalEvent ev) {
135: // no events for this channel
136: if (internalState != null) {
137: internalState.receiveEvent(ev);
138: }
139: }
140:
141: /** Receive static channel data from the portal
142: * @param sd static channel data
143: */
144: public void setStaticData(ChannelStaticData sd)
145: throws PortalException {
146: this .staticData = sd;
147: }
148:
149: /** CUserPreferences listens for an HttpRequestParameter "userPreferencesAction"
150: * and based on its value changes state between profile management and layout/stylesheet
151: * preferences.
152: * @param rd handle to channel runtime data
153: */
154: public void setRuntimeData(ChannelRuntimeData rd)
155: throws PortalException {
156: this .runtimeData = rd;
157: String action = runtimeData
158: .getParameter("userPreferencesAction");
159: if (action != null) {
160: Integer profileId = null;
161: try {
162: profileId = new Integer(runtimeData
163: .getParameter("profileId"));
164: } catch (NumberFormatException nfe) {
165: }
166: ;
167: boolean systemProfile = false;
168: if (profileId != null) {
169: String profileType = runtimeData
170: .getParameter("profileType");
171: if (profileType != null && profileType.equals("system"))
172: systemProfile = true;
173: }
174:
175: if (action.equals("manageProfiles")) {
176: this .internalState = manageProfiles;
177: } else if (action.equals("managePreferences")) {
178: if (profileId != null) {
179: // find the profile mapping
180: try {
181: if (systemProfile) {
182: UserProfile newProfile = ulsdb
183: .getSystemProfileById(profileId
184: .intValue());
185: if (newProfile != null
186: && (!(editedProfile
187: .isSystemProfile() && editedProfile
188: .getProfileId() == newProfile
189: .getProfileId()))) {
190: // new profile has been selected
191: editedProfile = newProfile;
192: instantiateManagePreferencesState(editedProfile);
193: }
194: } else {
195: UserProfile newProfile = ulsdb
196: .getUserProfileById(
197: upm.getPerson(), profileId
198: .intValue());
199: if (newProfile != null
200: && (editedProfile.isSystemProfile() || (editedProfile
201: .getProfileId() != newProfile
202: .getProfileId()))) {
203: // new profile has been selected
204: editedProfile = newProfile;
205: instantiateManagePreferencesState(editedProfile);
206: }
207: }
208: } catch (Exception e) {
209: throw new PortalException(e);
210: }
211: }
212:
213: if (editedProfile == null) {
214: editedProfile = up.getProfile();
215: }
216:
217: // managePreferences.setRuntimeData(rd);
218: this .internalState = managePreferences;
219: }
220: }
221:
222: if (internalState != null) {
223: internalState.setRuntimeData(rd);
224: }
225: }
226:
227: /**
228: * Output channel content to the portal
229: * @param out a sax document handler
230: */
231: public void renderXML(ContentHandler out) throws PortalException {
232: internalState.renderXML(out);
233: }
234:
235: protected UserPreferences getUserPreferencesFromStore(
236: UserProfile profile) throws Exception {
237: up = ulsdb.getUserPreferences(getUserPreferencesManager()
238: .getPerson(), profile);
239: IUserLayoutStore uls = UserLayoutStoreFactory
240: .getUserLayoutStoreImpl();
241: IPerson person = getUserPreferencesManager().getPerson();
242: UserProfile currProfile = getCurrentUserPreferences()
243: .getProfile();
244: Document layout = uls.getUserLayout(person, currProfile);
245: up.synchronizeWithUserLayoutXML(layout);
246: return up;
247: }
248:
249: protected UserProfile getEditedUserProfile() {
250: return editedProfile;
251: }
252: }
|