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.IEntityGroup;
015: import org.w3c.dom.Document;
016: import org.w3c.dom.Element;
017: import org.w3c.dom.Node;
018: import org.w3c.dom.NodeList;
019:
020: /**
021: * This command deletes an IEntityGroup and removes all of it's associations.
022: * It then gathers all of the xml
023: * nodes for the parent group and removes the child node of the removed member.
024: * @author Don Fracapane
025: * @version $Revision: 34756 $
026: */
027: public class DeleteGroup extends GroupsManagerCommand {
028:
029: public DeleteGroup() {
030: }
031:
032: /**
033: * This is the public method
034: * @param sessionData
035: * @throws Exception
036: */
037: public void execute(CGroupsManagerSessionData sessionData)
038: throws Exception {
039: //ChannelStaticData staticData = sessionData.staticData;
040: ChannelRuntimeData runtimeData = sessionData.runtimeData;
041:
042: Utility.logMessage("DEBUG", "DeleteGroup::execute(): Start");
043: Document model = getXmlDoc(sessionData);
044: String delId = getCommandArg(runtimeData);
045: Element delElem = GroupsManagerXML.getElementByTagNameAndId(
046: model, GROUP_TAGNAME, delId);
047: Element pn = ((Element) delElem.getParentNode());
048: if (pn != null) {
049: sessionData.highlightedGroupID = pn.getAttribute("id");
050: }
051: String delKey = delElem.getAttribute("key");
052: String elemName = delElem.getAttribute("name");
053: String retMsg;
054: Node parentNode;
055: Node deletedNode;
056: Utility.logMessage("DEBUG", "DeleteGroup::execute(): Group: "
057: + elemName + "will be deleted");
058: if (Utility.areEqual(delElem.getAttribute("searchResults"),
059: "true")) {
060: // if it is search results, just delete the node and skip the rest
061: delElem.getParentNode().removeChild(delElem);
062: } else {
063: IEntityGroup delGroup = sessionData.lockedGroup;
064: if (delGroup == null) {
065: retMsg = "Unable to retrieve Group!";
066: sessionData.feedback = retMsg;
067: return;
068: }
069: Utility.logMessage("DEBUG",
070: "DeleteGroup::execute(): About to delete group: "
071: + elemName);
072: // remove permissions associated with group
073: deletePermissions(delGroup);
074: // delete the group
075: delGroup.delete();
076: Utility.logMessage("DEBUG",
077: "DeleteGroup::execute(): About to delete xml nodes for group: "
078: + elemName);
079: // remove all xml nodes for this group
080: Iterator deletedNodes = GroupsManagerXML
081: .getNodesByTagNameAndKey(model, GROUP_TAGNAME,
082: delKey);
083: IEntityGroup parentEntGrp = null;
084: String hasMbrs = "duh";
085: while (deletedNodes.hasNext()) {
086: deletedNode = (Node) deletedNodes.next();
087: parentNode = deletedNode.getParentNode();
088: String nodeKey = ((Element) parentNode)
089: .getAttribute("key");
090: if (parentEntGrp == null
091: || !parentEntGrp.getKey().equals(nodeKey)) {
092: if (!Utility.areEqual(nodeKey, "")) {
093: parentEntGrp = GroupsManagerXML
094: .retrieveGroup(nodeKey);
095: hasMbrs = String.valueOf(parentEntGrp
096: .hasMembers());
097: parentNode.removeChild(deletedNode);
098: } else {
099: //Search elements have a null "key"
100: parentNode.removeChild(deletedNode);
101: NodeList nl = parentNode.getChildNodes();
102: if (nl.getLength() > 0) {
103: hasMbrs = "true";
104: } else {
105: hasMbrs = "false";
106: }
107: }
108: }
109: ((Element) parentNode).setAttribute("hasMembers",
110: hasMbrs);
111: }
112:
113: /** Remove the permission elements in the xmlDoc */
114: Node principalNode = model.getDocumentElement()
115: .getElementsByTagName("principal").item(0);
116: NodeList permElems = model
117: .getElementsByTagName("permission");
118: /** If we delete from the bottom up, the NodeList elements shift down
119: * everytime we delete an element. Since the elements that we are looking
120: * for are sequential and because we increment the counter at the end of
121: * the loop, the element that we should process next slips down into the
122: * slot that we just processed. We therefore end up deleting every other
123: * element. The solution is to delete from the top down.
124: */
125: for (int i = permElems.getLength() - 1; i > -1; i--) {
126: Element permElem = (Element) permElems.item(i);
127: if (permElem.getAttribute("target").equals(delKey)) {
128: principalNode.removeChild(permElem);
129: }
130: }
131: sessionData.mode = BROWSE_MODE;
132: }
133: Utility.logMessage("DEBUG", "DeleteGroup::execute(): Finished");
134: }
135: }
|