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.groupsmanager.commands;
007:
008: import java.util.Iterator;
009: import java.util.Vector;
010:
011: import org.jasig.portal.ChannelStaticData;
012: import org.jasig.portal.channels.groupsmanager.CGroupsManagerSessionData;
013: import org.jasig.portal.channels.groupsmanager.GroupsManagerXML;
014: import org.jasig.portal.channels.groupsmanager.Utility;
015: import org.jasig.portal.groups.IGroupMember;
016: import org.jasig.portal.groups.ILockableEntityGroup;
017: import org.w3c.dom.Document;
018: import org.w3c.dom.Element;
019: import org.w3c.dom.NodeList;
020:
021: /** A select cycle could be started in Servant mode or it could be started by
022: * the AddMembers command. The AddMembers command sets the id of the parent
023: * group (ie. the group to which child members will be added). Control is then
024: * passed to a selection view where the child group members will be selected
025: * for addition. When the selection has been completed by the user, the
026: * DoneWithSelection command will be invoked where the selected members are
027: * added to a collection of IAuthorizationPrincipal. If a parent group had
028: * been set the children groupmembers are actually added to the parent group.
029: * If in Servant mode, the collection is simply returned to the master channel.
030: * Alternatively, the CancelSelection command have been invoked by the user to
031: * cancel the selection process and reset the mode and view control parameters.
032: * @author Don Fracapane
033: * @version $Revision: 34756 $
034: */
035: public class DoneWithSelection extends GroupsManagerCommand {
036:
037: /** Creates new AddMember */
038: public DoneWithSelection() {
039: }
040:
041: /**
042: * This is the public method
043: * @param sessionData
044: * @throws Exception
045: */
046: public void execute(CGroupsManagerSessionData sessionData)
047: throws Exception {
048: ChannelStaticData staticData = sessionData.staticData;
049: //ChannelRuntimeData runtimeData= sessionData.runtimeData;
050:
051: Utility.logMessage("DEBUG",
052: "DoneWithSelection::execute(): Start");
053: String parentId = null;
054: boolean hasParentId = hasParentId(staticData);
055: Utility.logMessage("DEBUG",
056: "DoneWithSelection::execute(): Parent ID is set: "
057: + hasParentId);
058: IGroupMember[] princResults = null;
059: Document model = getXmlDoc(sessionData);
060: Element rootElem = model.getDocumentElement();
061: NodeList nGroupList = rootElem
062: .getElementsByTagName(GROUP_TAGNAME);
063: NodeList nEntityList = rootElem
064: .getElementsByTagName(ENTITY_TAGNAME);
065: Vector gmCollection = new Vector();
066: Element parentElem = null;
067: Utility.logMessage("DEBUG",
068: "DoneWithSelection::execute(): Starting group process");
069: addGroupMemberToCollection(gmCollection, nGroupList);
070: Utility
071: .logMessage("DEBUG",
072: "DoneWithSelection::execute(): Starting entity process");
073: addGroupMemberToCollection(gmCollection, nEntityList);
074: // check if selections were made
075: if (gmCollection.size() < 1) {
076: sessionData.feedback = sessionData.feedback
077: + "\n No groups or people were selected! ";
078: return;
079: }
080:
081: /** Presence of parentID means the process is not in servant mode. That is,
082: * the master channel is the Groups Manager channel and AddMembers had
083: * been selected
084: */
085: if (hasParentId) {
086: parentId = getParentId(staticData);
087: Utility.logMessage("DEBUG",
088: "DoneWithSelection::execute(): Parent ID is set to: "
089: + parentId);
090: parentElem = GroupsManagerXML.getElementByTagNameAndId(
091: model, GROUP_TAGNAME, parentId);
092: if (parentElem == null) {
093: Utility
094: .logMessage("ERROR",
095: "DoneWithSelection::execute: Error parent element not found");
096: return;
097: }
098: addChildrenToGroup(gmCollection, sessionData, parentElem,
099: model);
100: clearSelected(sessionData);
101: sessionData.mode = sessionData.returnToMode;
102: ;
103: sessionData.highlightedGroupID = parentId;
104: sessionData.rootViewGroupID = null;
105: // Parent was locked so no other thread or process could have changed it, but
106: // child members could have changed.
107: GroupsManagerXML.refreshAllNodesRecursivelyIfRequired(
108: sessionData.getUnrestrictedData(), parentElem);
109: sessionData.staticData.remove("groupParentId");
110: } else {
111: princResults = (IGroupMember[]) gmCollection
112: .toArray(new IGroupMember[0]);
113: if (princResults.length > 0) {
114: staticData.put("princResults", princResults);
115: staticData.setParameter("groupManagerFinished", "true");
116: }
117: }
118: }
119:
120: /**
121: * This method processes the xml document looking for selected groupmembers.
122: * It then creates an instance of IGroupMember for each selected
123: * member and passes the collection back.
124: * @param gmCollection
125: * @param nList
126: * @throws Exception
127: */
128: private void addGroupMemberToCollection(Vector gmCollection,
129: NodeList nList) throws Exception {
130: boolean addit;
131: for (int i = 0; i < nList.getLength(); i++) {
132: Element elem = (org.w3c.dom.Element) nList.item(i);
133: if (Utility.areEqual(elem.getAttribute("selected"), "true")) {
134: addit = true;
135: Iterator gmItr = gmCollection.iterator();
136: while (gmItr.hasNext()) {
137: IGroupMember ggmm = (IGroupMember) gmItr.next();
138: if ((ggmm.getKey().equals(elem.getAttribute("key")))
139: && (ggmm.getType().equals(elem
140: .getAttribute("type")))) {
141: addit = false;
142: break;
143: }
144: }
145: if (addit) {
146: IGroupMember gm = Utility
147: .retrieveGroupMemberForKeyAndType(elem
148: .getAttribute("key"), elem
149: .getAttribute("type"));
150: gmCollection.add(gm);
151: Utility.logMessage("DEBUG",
152: "DoneWithSelection::addGroupMemberToCollection(): "
153: + "- adding group member"
154: + elem.getAttribute("key"));
155: }
156: }
157: }
158: }
159:
160: /**
161: * This section adds the selected members to an IEntityGroup.
162: * @param gmCollection
163: * @param sessionData
164: * @param parentElem
165: * @param model
166: * @throws Exception
167: */
168: private void addChildrenToGroup(Vector gmCollection,
169: CGroupsManagerSessionData sessionData, Element parentElem,
170: Document model) throws Exception {
171: //ChannelRuntimeData runtimeData = sessionData.runtimeData;
172: Element parent;
173: ILockableEntityGroup parentGroup = null;
174: IGroupMember childGm = null;
175: Element childElem;
176: parentGroup = sessionData.lockedGroup;
177: Iterator gmItr = gmCollection.iterator();
178: while (gmItr.hasNext()) {
179: childGm = (IGroupMember) gmItr.next();
180: Utility.logMessage("DEBUG",
181: "DoneWithSelection::execute: About to add child");
182: // add to parent group
183: parentGroup.addMember(childGm);
184: // remove property elements for child gm and clear the Entity Properties cache.
185: GroupsManagerXML.removePropertyElements(model, childGm,
186: true);
187: // update parent group
188: parentGroup.updateMembersAndRenewLock();
189: // get parent element(s) and add element for child group member
190: Iterator parentNodes = GroupsManagerXML
191: .getNodesByTagNameAndKey(model, GROUP_TAGNAME,
192: parentElem.getAttribute("key"));
193: while (parentNodes.hasNext()) {
194: parent = (Element) parentNodes.next();
195:
196: childElem = GroupsManagerXML.getGroupMemberXml(childGm,
197: false, null, sessionData.getUnrestrictedData());
198: parent.appendChild(childElem);
199: parent.setAttribute("hasMembers", "true");
200: }
201: }
202: }
203: }
|