001: /* Copyright 2004 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.container.om.entity;
007:
008: import java.io.IOException;
009: import java.io.Serializable;
010: import java.util.Arrays;
011: import java.util.Iterator;
012: import java.util.Locale;
013:
014: import org.apache.commons.collections.IteratorUtils;
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.apache.pluto.om.common.Description;
018: import org.apache.pluto.om.common.ObjectID;
019: import org.apache.pluto.om.common.Preference;
020: import org.apache.pluto.om.common.PreferenceSet;
021: import org.apache.pluto.om.entity.PortletApplicationEntity;
022: import org.apache.pluto.om.entity.PortletEntity;
023: import org.apache.pluto.om.entity.PortletEntityCtrl;
024: import org.apache.pluto.om.portlet.PortletDefinition;
025: import org.apache.pluto.om.window.PortletWindow;
026: import org.apache.pluto.om.window.PortletWindowList;
027: import org.apache.pluto.om.window.PortletWindowListCtrl;
028: import org.jasig.portal.ChannelDefinition;
029: import org.jasig.portal.IPortletPreferencesStore;
030: import org.jasig.portal.PortletPreferencesStoreFactory;
031: import org.jasig.portal.container.om.common.ObjectIDImpl;
032: import org.jasig.portal.container.om.common.PreferenceImpl;
033: import org.jasig.portal.container.om.common.PreferenceSetImpl;
034: import org.jasig.portal.container.om.window.PortletWindowListImpl;
035: import org.jasig.portal.layout.IUserLayout;
036: import org.jasig.portal.layout.node.IUserLayoutChannelDescription;
037: import org.jasig.portal.security.IPerson;
038:
039: /**
040: * Implementation of Apache Pluto object model.
041: * @author Ken Weiner, kweiner@unicon.net
042: * @version $Revision: 41892 $
043: */
044: public class PortletEntityImpl implements PortletEntity,
045: PortletEntityCtrl, Serializable {
046: private static final Log log = LogFactory
047: .getLog(PortletEntityImpl.class);
048: private ObjectID objectId = null;
049: private PreferenceSetImpl originalPreferences = null;
050: private PreferenceSetImpl preferences = null;
051: private PreferenceSetImpl publicationPreferences = null;
052: private PortletDefinition portletDefinition = null;
053: private PortletApplicationEntity portletApplicationEntity = null;
054: private PortletWindowList portletWindows = null;
055: private IPerson person = null;
056: private IUserLayout layout = null;
057: private IUserLayoutChannelDescription channelDescription = null;
058: private ChannelDefinition channelDefinition = null;
059:
060: // PortletEntity methods
061:
062: public PortletEntityImpl() {
063: preferences = new PreferenceSetImpl();
064: publicationPreferences = new PreferenceSetImpl();
065: portletWindows = new PortletWindowListImpl();
066: }
067:
068: public ObjectID getId() {
069: return objectId;
070: }
071:
072: public PreferenceSet getPreferenceSet() {
073: return preferences;
074: }
075:
076: public PortletDefinition getPortletDefinition() {
077: return portletDefinition;
078: }
079:
080: public PortletApplicationEntity getPortletApplicationEntity() {
081: return portletApplicationEntity;
082: }
083:
084: public PortletWindowList getPortletWindowList() {
085: return portletWindows;
086: }
087:
088: public Description getDescription(Locale locale) {
089: return portletDefinition.getDescription(locale);
090: }
091:
092: // PortletEntityCtrl methods
093:
094: public void setId(String id) {
095: this .objectId = ObjectIDImpl.createFromString(id);
096: }
097:
098: public void setPortletDefinition(PortletDefinition portletDefinition) {
099: this .portletDefinition = portletDefinition;
100: }
101:
102: public void store() throws IOException {
103: try {
104: //Remove preferences added from the definition and publishing
105: for (final Iterator allPrefs = preferences.iterator(); allPrefs
106: .hasNext();) {
107: final Preference nextAllPref = (PreferenceImpl) allPrefs
108: .next();
109: final String nextAllPrefName = nextAllPref.getName();
110: final Object[] nextAllPrefVals = IteratorUtils
111: .toArray(nextAllPref.getValues());
112:
113: if (defPrefNameValuesPairExists(nextAllPrefName,
114: nextAllPrefVals)
115: || pubPrefNameValuesPairExists(nextAllPrefName,
116: nextAllPrefVals)) {
117: allPrefs.remove();
118: }
119: }
120:
121: //Store the trimmed prefrences for the entity
122: IPortletPreferencesStore portletPrefsStore = PortletPreferencesStoreFactory
123: .getPortletPreferencesStoreImpl();
124: int userId = person.getID();
125: int layoutId = Integer.parseInt(layout.getId());
126: String channelDescId = channelDescription.getId();
127: portletPrefsStore.setEntityPreferences(userId, layoutId,
128: channelDescId, preferences);
129:
130: //Re-load the all-preferences map
131: this .loadPreferences();
132: } catch (Exception e) {
133: log.error("Could not store portlet entity preferences", e);
134:
135: if (e instanceof IOException)
136: throw (IOException) e;
137: else {
138: IOException ioe = new IOException(
139: "Could not store portlet entity preferences: "
140: + e.getMessage());
141: ioe.initCause(e);
142: throw ioe;
143: }
144: }
145: }
146:
147: /**
148: * Calls {@link #prefNameValuesPairExists(Iterator, String, Object[])} using the portletDefinition preferences iterator.
149: */
150: private boolean defPrefNameValuesPairExists(String name,
151: Object[] values) {
152: return prefNameValuesPairExists(portletDefinition
153: .getPreferenceSet(), name, values);
154: }
155:
156: /**
157: * Calls {@link #prefNameValuesPairExists(Iterator, String, Object[])} using the publicationPreferences iterator.
158: */
159: private boolean pubPrefNameValuesPairExists(String name,
160: Object[] values) {
161: return prefNameValuesPairExists(publicationPreferences, name,
162: values);
163: }
164:
165: /**
166: * Iterates through Preference objects, checking if any have the same name and values
167: * as the name and values arguments. Returns true if so, false if not.
168: */
169: private boolean prefNameValuesPairExists(PreferenceSet prefs,
170: String name, Object[] values) {
171: final Preference pref = prefs.get(name);
172: if (pref != null) {
173: Object[] nextPrefVals = IteratorUtils.toArray(pref
174: .getValues());
175: if (Arrays.equals(values, nextPrefVals)) {
176: return true;
177: }
178: }
179:
180: return false;
181: }
182:
183: public void reset() throws IOException {
184: ((PreferenceSetImpl) preferences).clear();
185: if (originalPreferences != null) {
186: ((PreferenceSetImpl) preferences)
187: .addAll(originalPreferences);
188: }
189: }
190:
191: // Additional methods
192:
193: public void setPerson(final IPerson person) {
194: this .person = person;
195: }
196:
197: public void setUserLayout(final IUserLayout layout) {
198: this .layout = layout;
199: }
200:
201: public void setChannelDescription(
202: final IUserLayoutChannelDescription channelDescription) {
203: this .channelDescription = channelDescription;
204: }
205:
206: public void setPreferences(PreferenceSet preferences) {
207: this .preferences = (PreferenceSetImpl) preferences;
208: }
209:
210: public ChannelDefinition getChannelDefinition() {
211: return this .channelDefinition;
212: }
213:
214: public void setChannelDefinition(ChannelDefinition channelDefinition) {
215: this .channelDefinition = channelDefinition;
216: }
217:
218: /**
219: * re-loads preferences for the publish prefs and the entity prefs
220: */
221: public void loadPreferences() throws IOException {
222: //Load publish time preferences
223: try {
224: IPortletPreferencesStore portletPrefsStore = PortletPreferencesStoreFactory
225: .getPortletPreferencesStoreImpl();
226: PreferenceSet storedPublicationPreferences = portletPrefsStore
227: .getDefinitionPreferences(this .channelDefinition
228: .getId());
229: publicationPreferences.addAll(storedPublicationPreferences);
230: preferences.addAll(publicationPreferences);
231: } catch (Exception e) {
232: log.error("Could not load portlet publication preferences",
233: e);
234: if (e instanceof IOException) {
235: throw (IOException) e;
236: } else {
237: IOException ioe = new IOException(
238: "Could not load portlet publication preferences: "
239: + e.getMessage());
240: ioe.initCause(e);
241: throw ioe;
242: }
243: }
244:
245: //Load entity preferences
246: try {
247: IPortletPreferencesStore portletPrefsStore = PortletPreferencesStoreFactory
248: .getPortletPreferencesStoreImpl();
249: int userId = person.getID();
250: int layoutId = Integer.parseInt(layout.getId());
251: String channelDescId = channelDescription.getId();
252: PreferenceSet subscriptionPreferences = portletPrefsStore
253: .getEntityPreferences(userId, layoutId,
254: channelDescId);
255: preferences.addAll(subscriptionPreferences);
256:
257: // Save preferences as original preferences
258: originalPreferences = new PreferenceSetImpl();
259: originalPreferences.addAll(preferences);
260: } catch (Exception e) {
261: log.error("Could not load portlet entity preferences.", e);
262:
263: if (e instanceof IOException)
264: throw (IOException) e;
265: else {
266: IOException ioe = new IOException(
267: "Could not store portlet entity preferences: "
268: + e.getMessage());
269: ioe.initCause(e);
270: throw ioe;
271: }
272: }
273: }
274:
275: public void removePreferences() throws Exception {
276: try {
277: IPortletPreferencesStore portletPrefsStore = PortletPreferencesStoreFactory
278: .getPortletPreferencesStoreImpl();
279: int userId = person.getID();
280: int layoutId = Integer.parseInt(layout.getId());
281: String channelDescId = channelDescription.getId();
282:
283: portletPrefsStore.deletePortletPreferencesByInstance(
284: userId, layoutId, channelDescId);
285: } catch (Exception e) {
286: log.error("Could not delete portlet entity preferences", e);
287:
288: if (e instanceof IOException)
289: throw (IOException) e;
290: else {
291: IOException ioe = new IOException(
292: "Could not store portlet entity preferences: "
293: + e.getMessage());
294: ioe.initCause(e);
295: throw ioe;
296: }
297: }
298: }
299:
300: public void addPortletWindow(PortletWindow portletWindow) {
301: ((PortletWindowListCtrl) portletWindows).add(portletWindow);
302: }
303:
304: public void setPortletApplicationEntity(
305: PortletApplicationEntity portletApplicationEntity) {
306: this.portletApplicationEntity = portletApplicationEntity;
307: }
308: }
|