001: /* Copyright 2006 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.dlm.publisher;
007:
008: import java.io.FileOutputStream;
009: import java.util.Enumeration;
010:
011: import javax.xml.transform.OutputKeys;
012: import javax.xml.transform.Result;
013: import javax.xml.transform.Source;
014: import javax.xml.transform.Transformer;
015: import javax.xml.transform.TransformerFactory;
016: import javax.xml.transform.dom.DOMSource;
017: import javax.xml.transform.stream.StreamResult;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: import org.jasig.portal.PortalException;
023: import org.jasig.portal.StructureStylesheetUserPreferences;
024: import org.jasig.portal.UserProfile;
025: import org.jasig.portal.i18n.LocaleManager;
026: import org.jasig.portal.layout.IUserLayout;
027: import org.jasig.portal.layout.IUserLayoutManager;
028: import org.jasig.portal.layout.IUserLayoutStore;
029: import org.jasig.portal.layout.TransientUserLayoutManagerWrapper;
030: import org.jasig.portal.layout.UserLayoutManagerFactory;
031: import org.jasig.portal.layout.dlm.RDBMDistributedLayoutStore;
032: import org.jasig.portal.layout.node.IUserLayoutChannelDescription;
033: import org.jasig.portal.layout.node.IUserLayoutFolderDescription;
034: import org.jasig.portal.layout.node.IUserLayoutNodeDescription;
035: import org.jasig.portal.layout.node.UserLayoutFolderDescription;
036: import org.jasig.portal.security.IPerson;
037: import org.jasig.portal.security.PersonFactory;
038: import org.jasig.portal.utils.DocumentFactory;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041:
042: public class DlmLayoutExporter {
043:
044: // Static Members.
045: private static final Log log = LogFactory
046: .getLog(DlmLayoutExporter.class);
047: private static final int PROFILE_ID = 1;
048: private static final int LAYOUT_ID = 1;
049: private static final int STYLESHEET_ID = 4;
050: private static final int THEME_ID = 3;
051:
052: public static void main(String[] args) {
053:
054: // PrintWriter out = new PrintWriter(System.out);
055: log.info("DLM Layout Exporter Tool");
056: log.info("");
057:
058: switch (args.length) {
059: case 2:
060:
061: try {
062:
063: // Set up the layout store...
064: IUserLayoutStore store = new RDBMDistributedLayoutStore();
065:
066: // The first argument should be a valid username...
067: String uName = (args[0]);
068: System.out.print("Exporting layout for user '" + uName
069: + "'...");
070: int uId = DlmLayoutPublisher.lookupUserId(uName);
071: IPerson usr = PersonFactory.createGuestPerson();
072: usr.setAttribute(IPerson.USERNAME, uName);
073: usr.setID(uId);
074:
075: // Construct the layout manager...
076: UserProfile prf = new UserProfile(PROFILE_ID,
077: "User Profile for " + uName, "User Profile",
078: LAYOUT_ID, STYLESHEET_ID, THEME_ID);
079: LocaleManager lm = new LocaleManager(usr);
080: prf.setLocaleManager(lm);
081: IUserLayoutManager dlm = ((TransientUserLayoutManagerWrapper) UserLayoutManagerFactory
082: .getUserLayoutManager(usr, prf))
083: .getOriginalLayoutManager();
084:
085: // Retrieve the layout...
086: StructureStylesheetUserPreferences ssup = store
087: .getStructureStylesheetUserPreferences(usr,
088: PROFILE_ID, STYLESHEET_ID);
089: IUserLayout layout = dlm.getUserLayout();
090: String rootId = layout.getRootId();
091: IUserLayoutNodeDescription root = layout
092: .getNodeDescription(rootId);
093:
094: // Set up the Document...
095: Document doc = DocumentFactory.getNewDocument();
096: Element layoutElement = doc.createElement("layout");
097: doc.appendChild(layoutElement);
098: layoutElement.setAttribute("user", uName);
099: Element rootFolderElement = doc
100: .createElement("root-folder");
101: layoutElement.appendChild(rootFolderElement);
102:
103: // Analyze the layout...
104: populateChildren(rootFolderElement,
105: (IUserLayoutFolderDescription) root, dlm, ssup);
106:
107: // Write to the indicated file...
108: // JAXP serializing
109: TransformerFactory xformFactory = TransformerFactory
110: .newInstance();
111: Transformer idTransform = xformFactory.newTransformer();
112: Source input = new DOMSource(doc);
113: Result output = new StreamResult(new FileOutputStream(
114: args[1]));
115: idTransform.setOutputProperty(OutputKeys.INDENT, "yes");
116: idTransform.setOutputProperty(
117: "{http://xml.apache.org/xslt}indent-amount",
118: "4");
119: idTransform.transform(input, output);
120:
121: } catch (Throwable t) {
122: log.error("The exporter tool terminated unexpectedly.",
123: t);
124: System.out
125: .println("The exporter tool terminated unexpectedly.");
126: t.printStackTrace(System.out);
127: System.exit(7);
128: }
129:
130: break;
131:
132: default:
133:
134: log
135: .info("Usage: java org.jasig.portal.layout.dlm.publisher <username> <file_path>");
136: log.info("");
137: log.info("where <username> is a valid portal user and ");
138: log
139: .info("<file_path> is the name of a file NOT in existance.");
140: break;
141:
142: }
143:
144: log.info("The publisher tool completed successfully.");
145: System.exit(0);
146:
147: }
148:
149: private static void populateChildren(Element folderElement,
150: IUserLayoutFolderDescription folder,
151: IUserLayoutManager dlm,
152: StructureStylesheetUserPreferences ssup) throws Exception {
153:
154: // Assertions.
155: if (folderElement == null) {
156: String msg = "Argument 'folderElement' cannot be null.";
157: throw new IllegalArgumentException(msg);
158: }
159: if (folder == null) {
160: String msg = "Argument 'folder' cannot be null.";
161: throw new IllegalArgumentException(msg);
162: }
163: if (dlm == null) {
164: String msg = "Argument 'dlm' cannot be null.";
165: throw new IllegalArgumentException(msg);
166: }
167: if (ssup == null) {
168: String msg = "Argument 'ssup' cannot be null.";
169: throw new IllegalArgumentException(msg);
170: }
171:
172: // Child Nodes...
173: for (Enumeration e = dlm.getChildIds(folder.getId()); e
174: .hasMoreElements();) {
175:
176: String chldId = (String) e.nextElement();
177: IUserLayoutNodeDescription chldNd = null;
178: try {
179: chldNd = dlm.getNode(chldId);
180: } catch (PortalException pe) {
181: // NB: This really isn't a very satisfactory way to handle the
182: // issue here, which is that DLM embeds "dlm:editSet" elements
183: // in layouts to persist customization of layout fragments (I
184: // think). Anyhow uPortal (even via DLM) can't give you a node
185: // description for one of these b/c it's neither a folder nor a
186: // channel. For now, we'll drop the editSet data on layout export.
187: continue;
188: }
189:
190: Document doc = folderElement.getOwnerDocument();
191: switch (chldNd.getType()) {
192:
193: case IUserLayoutNodeDescription.FOLDER:
194:
195: Element childFolderElement = doc
196: .createElement("folder");
197: folderElement.appendChild(childFolderElement);
198:
199: // Type...
200: switch (((IUserLayoutFolderDescription) chldNd)
201: .getFolderType()) {
202: case UserLayoutFolderDescription.HEADER_TYPE:
203: childFolderElement.setAttribute("type", "header");
204: break;
205: case UserLayoutFolderDescription.FOOTER_TYPE:
206: childFolderElement.setAttribute("type", "footer");
207: break;
208: case UserLayoutFolderDescription.REGULAR_TYPE:
209: childFolderElement.setAttribute("type", "regular");
210: break;
211: }
212:
213: // Removable...
214: if (chldNd.isUnremovable()) {
215: childFolderElement.setAttribute("removable",
216: "False");
217: } else {
218: childFolderElement
219: .setAttribute("removable", "True");
220: }
221:
222: // Mutable...
223: if (chldNd.isEditAllowed()) {
224: childFolderElement.setAttribute("mutable", "True");
225: } else {
226: childFolderElement.setAttribute("mutable", "False");
227: }
228:
229: // Width... where applicable...
230: String width = ssup.getDefinedFolderAttributeValue(
231: chldNd.getId(), "width");
232: if (width != null && width.trim().length() != 0) {
233: childFolderElement.setAttribute("width", width);
234: }
235:
236: // Name...
237: Element nameElement = doc.createElement("name");
238: childFolderElement.appendChild(nameElement);
239:
240: nameElement.appendChild(doc.createTextNode(chldNd
241: .getName()));
242:
243: populateChildren(childFolderElement,
244: (IUserLayoutFolderDescription) chldNd, dlm,
245: ssup);
246:
247: break;
248:
249: case IUserLayoutNodeDescription.CHANNEL:
250:
251: Element channelElement = doc.createElement("channel");
252: folderElement.appendChild(channelElement);
253: channelElement.setAttribute("fName",
254: ((IUserLayoutChannelDescription) chldNd)
255: .getFunctionalName());
256:
257: break;
258:
259: }
260:
261: }
262:
263: }
264:
265: }
|