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:
010: import org.jasig.portal.ChannelRuntimeData;
011: import org.jasig.portal.channels.groupsmanager.CGroupsManagerSessionData;
012: import org.jasig.portal.channels.groupsmanager.GroupsManagerXML;
013: import org.jasig.portal.channels.groupsmanager.Utility;
014: import org.jasig.portal.groups.GroupsException;
015: import org.jasig.portal.groups.IEntityGroup;
016: import org.jasig.portal.groups.IGroupMember;
017: import org.jasig.portal.groups.ILockableEntityGroup;
018: import org.w3c.dom.Document;
019: import org.w3c.dom.Element;
020: import org.w3c.dom.Node;
021: import org.w3c.dom.NodeList;
022:
023: /**
024: * This command removes the association of a member element to an IEntityGroup
025: * It then gathers all of the xml nodes for the parent group and removes the child
026: * node of the removed member.
027: * @author Don Fracapane
028: * @version $Revision: 34756 $
029: */
030: public class RemoveMember extends GroupsManagerCommand {
031:
032: /** Creates new RemoveMember */
033: public RemoveMember() {
034: }
035:
036: /**
037: * This is the public method
038: * @param sessionData
039: * @throws Exception
040: */
041: public void execute(CGroupsManagerSessionData sessionData)
042: throws Exception {
043: //ChannelStaticData staticData = sessionData.staticData;
044: ChannelRuntimeData runtimeData = sessionData.runtimeData;
045: Utility.logMessage("DEBUG", "RemoveMember::execute(): Start");
046: Document model = getXmlDoc(sessionData);
047: String cmdIds = getCommandArg(runtimeData);
048: Object parentGroup = null;
049: IGroupMember childGm = null;
050: String hasMbrs = "duh";
051: Utility
052: .logMessage("DEBUG",
053: "RemoveMember::execute(): About to get parent and child keys");
054: String parentID = Utility.parseStringDelimitedBy("parent.",
055: cmdIds, "|");
056: String childID = Utility.parseStringDelimitedBy("child.",
057: cmdIds, "|");
058: Utility.logMessage("DEBUG",
059: "RemoveMember::execute(): Uid of parent element = "
060: + parentID + " child element = " + childID);
061: Element parentElem = GroupsManagerXML.getElementByTagNameAndId(
062: model, GROUP_TAGNAME, parentID);
063: if (parentElem == null) {
064: Utility
065: .logMessage("ERROR",
066: "RemoveMember::execute(): Unable to retrieve parent element!");
067: return;
068: }
069: Utility.logMessage("DEBUG",
070: "RemoveMember::execute(): About to get child element = "
071: + childID);
072: Element childElem = GroupsManagerXML.getElementById(model,
073: childID);
074: if (childElem == null) {
075: Utility
076: .logMessage("ERROR",
077: "RemoveMember::execute(): Unable to retrieve Child element!");
078: return;
079: }
080: // The child will always be an IGroupMember
081: childGm = GroupsManagerXML.retrieveGroupMemberForElementId(
082: model, childID);
083: parentGroup = sessionData.lockedGroup;
084: removeChildFromGroup(parentGroup, childGm);
085: hasMbrs = String.valueOf(((IEntityGroup) parentGroup)
086: .hasMembers());
087: Utility.logMessage("DEBUG",
088: "RemoveMember::execute(): Got the parent group ");
089: Utility
090: .logMessage("DEBUG",
091: "RemoveMember::execute(): about to remove child elements");
092: // remove property elements for child gm and clear the Entity Properties cache.
093: GroupsManagerXML.removePropertyElements(model, childGm, true);
094:
095: // Removes EntityProperites for the child GroupMember from the Entity Property cache.
096: GroupsManagerXML.clearPropertiesCache(childGm);
097:
098: // remove this member from all parent group elements.
099: Iterator parentNodes = GroupsManagerXML
100: .getNodesByTagNameAndKey(model, GROUP_TAGNAME,
101: parentElem.getAttribute("key"));
102: Node parentNode;
103: NodeList childNodes;
104: Node childNode;
105: while (parentNodes.hasNext()) {
106: parentNode = (Node) parentNodes.next();
107: childNodes = parentNode.getChildNodes();
108: for (int i = 0; i < childNodes.getLength(); i++) {
109: childNode = childNodes.item(i);
110: if (((Element) childNode).getAttribute("key").equals(
111: childElem.getAttribute("key"))) {
112: parentNode.removeChild(childNode);
113: ((Element) parentNode).setAttribute("hasMembers",
114: hasMbrs);
115: }
116: }
117: }
118: Utility.logMessage("DEBUG",
119: "RemoveMember::execute(): Completed");
120: }
121:
122: /**
123: * This section removes the selected member from an IEntityGroup.
124: * @param parentGroup
125: * @param childGm
126: * @exception Exception
127: */
128: private void removeChildFromGroup(Object parentGroup,
129: IGroupMember childGm) throws Exception {
130: Utility
131: .logMessage("DEBUG",
132: "RemoveMember::removeChildrenFromGroup(): about to remove child");
133: if (parentGroup != null && childGm != null) {
134: try {
135: ((IEntityGroup) parentGroup).removeMember(childGm);
136: ((ILockableEntityGroup) parentGroup)
137: .updateMembersAndRenewLock();
138: } catch (GroupsException ge) {
139: String aMsg = "Unable to remove child from parent/n"
140: + ge;
141: Utility.logMessage("ERROR", aMsg, ge);
142: }
143: } else {
144: String suspect = (parentGroup == null ? "Parent" : "Child");
145: throw new Exception(suspect + " group member was not found");
146: }
147: }
148: }
|