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.jasig.portal.groups.IGroupMember;
016: import org.jasig.portal.groups.ILockableEntityGroup;
017: import org.jasig.portal.services.GroupService;
018: import org.w3c.dom.Document;
019: import org.w3c.dom.Element;
020:
021: /**
022: * We will only be creating groups. We do not create entities. Once we create
023: * the new group, it will be added to a parent and default permissions will
024: * be assigned if the user is not in the portal adminstrators groups that automatically
025: * has full access.
026: * All of the xml nodes for the parent group will be found and if the node is
027: * expanded, the new child node will be added.
028: * @author Don Fracapane
029: * @version $Revision: 35496 $
030: */
031: public class CreateGroup extends GroupsManagerCommand {
032:
033: /** Creates new CreateGroup */
034: public CreateGroup() {
035: }
036:
037: /**
038: * This is the public method
039: * @param sessionData
040: * @throws Exception
041: */
042: public void execute(CGroupsManagerSessionData sessionData)
043: throws Exception {
044: ChannelRuntimeData runtimeData = sessionData.runtimeData;
045:
046: Utility.logMessage("DEBUG", "CreateGroup::execute(): Start");
047: Document model = getXmlDoc(sessionData);
048: String parentID = getCommandArg(runtimeData);
049: String newGrpName = runtimeData.getParameter("grpNewName");
050: Utility.logMessage("DEBUG", "CreateGroup::execute(): New grp: "
051: + newGrpName + " will be added to parent element = "
052: + parentID);
053: ILockableEntityGroup parentGroup = null;
054: Class parentEntityType;
055: Element parentElem = GroupsManagerXML.getElementByTagNameAndId(
056: model, GROUP_TAGNAME, parentID);
057: String parentKey = parentElem.getAttribute("key");
058: String retMsg;
059: Iterator parentNodes;
060: if (parentElem == null) {
061: retMsg = "Unable to find Parent element!";
062: sessionData.feedback = retMsg;
063: Utility.logMessage("ERROR", "CreateGroup::execute(): "
064: + retMsg);
065: return;
066: }
067: Utility.logMessage("DEBUG",
068: "CreateGroup::execute(): Parent element was found!");
069:
070: //parentGroup = GroupsManagerXML.retrieveGroup(parentKey);
071: parentGroup = sessionData.lockedGroup;
072: if (parentGroup == null) {
073: retMsg = "Unable to retrieve Parent Entity Group!";
074: sessionData.feedback = retMsg;
075: return;
076: }
077: parentEntityType = parentGroup.getLeafType();
078: //parentEntityType = (Class) GroupsManagerXML.getEntityTypes().get("Person");
079: Utility.logMessage("DEBUG",
080: "CreateGroup::execute(): About to create new group: "
081: + newGrpName + " Type: "
082: + parentEntityType.getName());
083: String userID = sessionData.user.getEntityIdentifier().getKey();
084: Utility.logMessage("DEBUG", "CreateGroup::execute(): userID = "
085: + userID);
086: IEntityGroup childEntGrp = GroupService
087: .newGroup(parentEntityType);
088: childEntGrp.setName(newGrpName);
089: childEntGrp.setCreatorID(userID);
090: childEntGrp.update();
091: Utility.logMessage("DEBUG",
092: "CreateGroup::execute(): About to add new group: "
093: + newGrpName);
094: parentGroup.addMember((IGroupMember) childEntGrp);
095: parentGroup.updateMembersAndRenewLock();
096: parentNodes = GroupsManagerXML.getNodesByTagNameAndKey(model,
097: GROUP_TAGNAME, parentKey);
098: // add new group to all parent group xml nodes
099: while (parentNodes.hasNext()) {
100: Element parentNode = (Element) parentNodes.next();
101: GroupsManagerXML.getGroupMemberXml(
102: (IGroupMember) parentGroup, true, parentNode,
103: sessionData.getUnrestrictedData());
104: ((Element) parentNode).setAttribute("hasMembers", "true");
105: }
106:
107: /** Grant all permissions for the new group to the creator only if the user is
108: * not in the portal administrators group
109: */
110: if (!sessionData.isAdminUser) {
111: GroupsManagerXML
112: .createPermissions(sessionData, childEntGrp);
113: }
114: // Parent was locked so no other thread or process could have changed it, but
115: // child members could have changed.
116: GroupsManagerXML.refreshAllNodesRecursivelyIfRequired(
117: sessionData.getUnrestrictedData(), parentElem);
118:
119: Utility.logMessage("DEBUG", "CreateGroup::execute(): Finished");
120: }
121: }
|