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