001: /* Copyright 2005 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;
007:
008: import org.jasig.portal.PortalException;
009: import org.jasig.portal.layout.IUserLayoutStore;
010: import org.jasig.portal.layout.UserLayoutStoreFactory;
011: import org.jasig.portal.security.IPerson;
012: import org.w3c.dom.Document;
013: import org.w3c.dom.Element;
014:
015: /**
016: * Utility functions for applying user changes to the user's plf prior
017: * to persisting.
018: *
019: * @version $Revision: 36294 $Date$
020: * @since uPortal 2.5
021: */
022: public class HandlerUtils {
023: public static final String RCS_ID = "@(#) $Header$";
024:
025: /**
026: This method returns the PLF version of the passed in compViewNode. If
027: create is false and a node with the same id is not found in the PLF then
028: null is returned. If the create is true then an attempt is made to
029: create the node along with any necessary ancestor nodes needed to
030: represent the path along the tree.
031: */
032: public static Element getPLFNode(Element compViewNode,
033: IPerson person, boolean create, boolean includeChildNodes)
034: throws PortalException {
035: Document plf = (Document) person.getAttribute(Constants.PLF);
036: String ID = compViewNode.getAttribute(Constants.ATT_ID);
037: Element plfNode = plf.getElementById(ID);
038:
039: if (plfNode != null)
040: return plfNode;
041:
042: // make sure that the root isn't what we are trying to get. Root is not
043: // registered with document under an id.
044:
045: if (compViewNode.getNodeName().equals("layout"))
046: return plf.getDocumentElement();
047:
048: // if not found then create if indicated.
049: if (create == true)
050: return createPlfNodeAndPath(compViewNode,
051: includeChildNodes, person);
052: return null;
053: }
054:
055: /**
056: Creates a copy of the passed in ILF node in the PLF if not already
057: there as well as creating any ancestor nodes along the path from this
058: node up to the layout root if they are not there.
059: */
060: public static Element createPlfNodeAndPath(Element compViewNode,
061: boolean includeChildNodes, IPerson person)
062: throws PortalException {
063: // first attempt to get parent
064: Element compViewParent = (Element) compViewNode.getParentNode();
065: Element plfParent = getPLFNode(compViewParent, person, true,
066: false);
067: Document plf = (Document) person.getAttribute(Constants.PLF);
068:
069: // if ilf copy being created we can append to parent and use the
070: // position set to place it.
071:
072: if (compViewNode.getAttribute(Constants.ATT_ID).startsWith(
073: Constants.FRAGMENT_ID_USER_PREFIX))
074: return createILFCopy(compViewNode, compViewParent,
075: includeChildNodes, plf, plfParent, person);
076: return createOrMovePLFOwnedNode(compViewNode, compViewParent,
077: true, // create if not found
078: includeChildNodes, plf, plfParent, person);
079: }
080:
081: /**
082: Creates a copy of an ilf node in the plf and sets up necessary storage
083: attributes.
084: */
085: private static Element createILFCopy(Element compViewNode,
086: Element compViewParent, boolean includeChildNodes,
087: Document plf, Element plfParent, IPerson person)
088: throws PortalException {
089: Element plfNode = (Element) plf.importNode(compViewNode,
090: includeChildNodes);
091: // make sure that we don't include ILF restriction params in the PLF if
092: // this ILF node contained any.
093: plfNode.removeAttributeNS(Constants.NS_URI,
094: Constants.LCL_ADD_CHILD_ALLOWED);
095: plfNode.removeAttributeNS(Constants.NS_URI,
096: Constants.LCL_DELETE_ALLOWED);
097: plfNode.removeAttributeNS(Constants.NS_URI,
098: Constants.LCL_EDIT_ALLOWED);
099: plfNode.removeAttributeNS(Constants.NS_URI,
100: Constants.LCL_MOVE_ALLOWED);
101:
102: String ID = plfNode.getAttribute(Constants.ATT_ID);
103: plfNode.setIdAttribute(Constants.ATT_ID, true);
104:
105: IUserLayoutStore uls = null;
106: uls = UserLayoutStoreFactory.getUserLayoutStoreImpl();
107:
108: if (plfNode.getAttribute(Constants.ATT_PLF_ID).equals("")) {
109: String plfID = null;
110:
111: try {
112: if (!plfNode.getAttribute(Constants.ATT_CHANNEL_ID)
113: .equals("")) // dealing with a channel
114: plfID = uls.generateNewChannelSubscribeId(person);
115: else
116: plfID = uls.generateNewFolderId(person);
117: } catch (Exception e) {
118: throw new PortalException(
119: "Exception encountered while "
120: + "generating new user layout node "
121: + "Id for userId=" + person.getID(), e);
122: }
123:
124: plfNode.setAttributeNS(Constants.NS_URI,
125: Constants.ATT_PLF_ID, plfID);
126: plfNode.setAttributeNS(Constants.NS_URI,
127: Constants.ATT_ORIGIN, ID);
128: }
129:
130: plfParent.appendChild(plfNode);
131: PositionManager.updatePositionSet(compViewParent, plfParent,
132: person);
133: return plfNode;
134: }
135:
136: /**
137: Creates or moves the plf copy of a node in the composite view and
138: inserting it before its next highest sibling so that if dlm is not used
139: then the model ends up exactly like the original non-dlm persistance
140: version. The position set is also updated and if no ilf copy nodes are
141: found in the sibling list the set is cleared if it exists.
142: */
143: static Element createOrMovePLFOwnedNode(Element compViewNode,
144: Element compViewParent, boolean createIfNotFound,
145: boolean createChildNodes, Document plf, Element plfParent,
146: IPerson person) throws PortalException {
147: Element child = (Element) compViewParent.getFirstChild();
148: Element nextOwnedSibling = null;
149: boolean insertionPointFound = false;
150:
151: while (child != null) {
152: if (insertionPointFound
153: && nextOwnedSibling == null
154: && !child.getAttribute(Constants.ATT_ID)
155: .startsWith(
156: Constants.FRAGMENT_ID_USER_PREFIX))
157: nextOwnedSibling = child;
158:
159: if (child == compViewNode)
160: insertionPointFound = true;
161: child = (Element) child.getNextSibling();
162: }
163:
164: if (insertionPointFound == false)
165: return null;
166:
167: String nextSibID = null;
168: Element nextPlfSib = null;
169:
170: if (nextOwnedSibling != null) {
171: nextSibID = nextOwnedSibling.getAttribute(Constants.ATT_ID);
172: nextPlfSib = plf.getElementById(nextSibID);
173: }
174:
175: String plfNodeID = compViewNode.getAttribute(Constants.ATT_ID);
176: Element plfNode = plf.getElementById(plfNodeID);
177:
178: if (plfNode == null) {
179: if (createIfNotFound == true) {
180: plfNode = (Element) plf.importNode(compViewNode,
181: createChildNodes);
182: plfNode.setIdAttribute(Constants.ATT_ID, true);
183: } else
184: return null;
185: }
186:
187: if (nextPlfSib == null)
188: plfParent.appendChild(plfNode);
189: else
190: plfParent.insertBefore(plfNode, nextPlfSib);
191:
192: PositionManager.updatePositionSet(compViewParent, plfParent,
193: person);
194: return (Element) plfNode;
195: }
196: }
|