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.layout.alm.channels;
007:
008: import java.util.Iterator;
009: import java.util.Map;
010: import java.util.Collection;
011: import java.util.HashMap;
012:
013: import org.jasig.portal.IPrivileged;
014: import org.jasig.portal.PortalControlStructures;
015: import org.jasig.portal.PortalException;
016: import org.jasig.portal.ThemeStylesheetUserPreferences;
017: import org.jasig.portal.channels.BaseChannel;
018: import org.jasig.portal.layout.IUserLayoutManager;
019: import org.jasig.portal.layout.TransientUserLayoutManagerWrapper;
020: import org.jasig.portal.layout.alm.ALFolder;
021: import org.jasig.portal.layout.alm.ALFragment;
022: import org.jasig.portal.layout.alm.ALNode;
023: import org.jasig.portal.layout.alm.IAggregatedUserLayoutManager;
024: import org.jasig.portal.layout.alm.ILayoutFragment;
025: import org.jasig.portal.utils.XSLT;
026: import org.w3c.dom.Document;
027: import org.w3c.dom.Node;
028: import org.w3c.dom.Element;
029:
030: /**
031: * An abstract class containing the basic business-logic and components for
032: * CFragmentManager and CContentSubscriber.
033: *
034: * Prior to uPortal 2.5, this channel existed in the org.jasig.portal.layout.channels
035: * package. It was moved to its present package to reflect that it is a part of
036: * Aggregated Layouts.
037: *
038: * @author Michael Ivanov, mvi@immagic.com
039: * @version $Revision: 36811 $
040: * @since uPortal 2.5
041: */
042: public abstract class FragmentManager extends BaseChannel implements
043: IPrivileged {
044:
045: protected IAggregatedUserLayoutManager alm;
046: protected ThemeStylesheetUserPreferences themePrefs;
047: protected Map fragments;
048:
049: protected String getFragmentRootId(String fragmentId)
050: throws PortalException {
051: if (fragments != null && !fragments.isEmpty()) {
052: ALFragment fragment = (ALFragment) fragments
053: .get(fragmentId);
054: ALFolder rootFolder = (ALFolder) fragment.getNode(fragment
055: .getRootId());
056: return rootFolder.getFirstChildNodeId();
057: }
058: return null;
059: }
060:
061: /**
062: * Passes portal control structure to the channel.
063: * @see PortalControlStructures
064: */
065: public void setPortalControlStructures(PortalControlStructures pcs)
066: throws PortalException {
067: themePrefs = pcs.getUserPreferencesManager()
068: .getUserPreferences()
069: .getThemeStylesheetUserPreferences();
070: IUserLayoutManager ulm = pcs.getUserPreferencesManager()
071: .getUserLayoutManager();
072: if (ulm instanceof TransientUserLayoutManagerWrapper)
073: ulm = ((TransientUserLayoutManagerWrapper) ulm)
074: .getOriginalLayoutManager();
075: if (ulm instanceof IAggregatedUserLayoutManager)
076: alm = (IAggregatedUserLayoutManager) ulm;
077: }
078:
079: protected void getFragmentList(Document document)
080: throws PortalException {
081: getFragmentList(document, document);
082: }
083:
084: protected void getFragmentList(Document document, Node node)
085: throws PortalException {
086: Element fragmentsNode = document.createElement("fragments");
087: node.appendChild(fragmentsNode);
088: Element category = document.createElement("category");
089: category.setAttribute("name", "Fragments");
090: category.setAttribute("view", "expanded");
091: category.setAttribute("ID", "fragcat.1");
092: fragmentsNode.appendChild(category);
093: boolean updateList = false;
094: if (fragments != null) {
095: for (Iterator ids = fragments.keySet().iterator(); ids
096: .hasNext();) {
097: String fragmentId = (String) ids.next();
098: ALFragment fragment = (ALFragment) fragments
099: .get(fragmentId);
100: String fragmentRootId = getFragmentRootId(fragmentId);
101: // if the fragment root ID is NULL then the fragment must be deleted
102: // since it does not have any content
103: if (fragmentRootId == null) {
104: alm.deleteFragment(fragmentId);
105: if (!updateList)
106: updateList = true;
107: continue;
108: }
109: Element fragmentNode = document
110: .createElement("fragment");
111: fragmentNode.setAttribute("ID", fragmentId);
112: category.appendChild(fragmentNode);
113: Element rootId = document.createElement("rootNodeID");
114: rootId.appendChild(document
115: .createTextNode(fragmentRootId));
116: rootId.setAttribute("immutable", fragment.getNode(
117: fragmentRootId).getNodeDescription()
118: .isImmutable() ? "Y" : "N");
119: fragmentNode.appendChild(rootId);
120: Element type = document.createElement("type");
121: type.appendChild(document.createTextNode(fragment
122: .isPushedFragment() ? "pushed" : "pulled"));
123: fragmentNode.appendChild(type);
124: Element fname = document.createElement("fname");
125: fname.appendChild(document.createTextNode(fragment
126: .getFunctionalName()));
127: fragmentNode.appendChild(fname);
128: Element name = document.createElement("name");
129: name
130: .appendChild(document
131: .createTextNode(fragmentRootId != null ? ((ALNode) fragment
132: .getNode(fragmentRootId))
133: .getNodeDescription().getName()
134: : fragment.getFunctionalName()));
135: fragmentNode.appendChild(name);
136: Element desc = document.createElement("description");
137: desc.appendChild(document.createTextNode(fragment
138: .getDescription()));
139: fragmentNode.appendChild(desc);
140: }
141:
142: // If there were any fragments withno rootID and these fragments were deleted - need to update the fragment list
143: if (updateList)
144: refreshFragmentMap();
145: }
146:
147: }
148:
149: protected void refreshFragmentMap() throws PortalException {
150: Collection fragmentIds = getFragments();
151: fragments = new HashMap();
152: for (Iterator ids = fragmentIds.iterator(); ids.hasNext();) {
153: String fragmentId = (String) ids.next();
154: ILayoutFragment layoutFragment = alm
155: .getFragment(fragmentId);
156: if (layoutFragment == null
157: || !(layoutFragment instanceof ALFragment))
158: throw new PortalException("The fragment must be "
159: + ALFragment.class.getName() + " type!");
160: fragments.put(fragmentId, layoutFragment);
161: }
162: }
163:
164: protected abstract Collection getFragments() throws PortalException;
165:
166: protected static final class ErrorMessage {
167:
168: // Instance Members.
169: private final String title;
170: private final String description;
171:
172: /*
173: * Public API.
174: */
175:
176: public ErrorMessage(String title, String description) {
177:
178: // Assertions.
179: if (title == null) {
180: String msg = "Argument 'title' cannot be null.";
181: throw new IllegalArgumentException(msg);
182: }
183: if (description == null) {
184: String msg = "Argument 'description' cannot be null.";
185: throw new IllegalArgumentException(msg);
186: }
187:
188: // Instance Members.
189: this .title = title;
190: this .description = description;
191:
192: }
193:
194: public String getTitle() {
195: return title;
196: }
197:
198: public String getDescription() {
199: return description;
200: }
201:
202: public Element toElement(Document doc) {
203:
204: // Assertions.
205: if (doc == null) {
206: String msg = "Argument 'doc' cannot be null.";
207: throw new IllegalArgumentException(msg);
208: }
209:
210: // Title.
211: Element t = doc.createElement("title");
212: t.appendChild(doc.createTextNode(title));
213:
214: // Description.
215: Element d = doc.createElement("body");
216: d.appendChild(doc.createTextNode(description));
217:
218: Element rslt = doc.createElement("error-message");
219: rslt.appendChild(t);
220: rslt.appendChild(d);
221:
222: return rslt;
223: }
224: }
225: }
|