/* This is called when the user submits the page's changes.
         * It is called once for each checkbox change being submitted.
         * If necessary, a workspace is launched for the relevant subprocess.
         * If the subprocess already contains a workspace but it's owned
         * by some other user, the change is not made and the user
         * is alerted, but the process of executing the set of changes is
         * not aborted.
         *
         * mimic this to create the automatic workspace creation:
         * AutoRemoveEntitlementStatus in GuidedEditor/...
         */

        public void OnAfterCallback(object sender, EventArgs oArgs)
        {
            if (this.boolMustAbort)
            {
                Grid1.CallbackParameter = "OPERATION ABORTED:\n" + messages;
                return;
            }


            Dictionary <int, HELPERS.infoEASet> readyToUseWorkspaces = new Dictionary <int, HELPERS.infoEASet>();
            Dictionary <int, HELPERS.infoEASet> lockedWorkspaces     = new Dictionary <int, HELPERS.infoEASet>();

            if (messages.Length > 0)
            {
                messages += "\n-------------------------\n\n";
            }

            HELPERS.AutoGenWorkspacesInBulk
                (session.idUser, ref messages, MAPsubprToEASet, readyToUseWorkspaces, lockedWorkspaces,
                "facilitate bulk role assignment/unassignment for a particular entitlement");

            // If any problem occurred at all, we abort!
            if (lockedWorkspaces.Count > 0)
            {
                Grid1.CallbackParameter = messages;
                return;
            }


            // We now proceed with the actual edits.
            IEntAssignment ENGINEentass = new IEntAssignment(HELPERS.NewOdbcConn());

            while (QUEUEidsBusrolesToREMOVE.Count > 0)
            {
                int idBusrole = QUEUEidsBusrolesToREMOVE.Dequeue();
                int idSubpr   = MAPbroleToSubproc[idBusrole];
                messages += "Detaching from this role: " + MAPbroleIdToName[idBusrole] + "\n";
                HELPERS.infoEASet wstouse = readyToUseWorkspaces[idSubpr];

                returnListEntAssignmentByEntAssignmentSet[] theEAss =
                    ENGINEentass.ListEntAssignmentByEntAssignmentSet
                        (null, "\"BusRole\" = ? AND \"Entitlement\" = ? ", new string[] { idBusrole.ToString(), IDentitlement.ToString() }, "", wstouse.idEntAssSet);

                ENGINEentass.SetEntAssignment
                    (theEAss[0].ID, theEAss[0].EntAssignmentSetID, theEAss[0].BusRoleID,
                    theEAss[0].EntitlementID, "X");
            }
            while (QUEUEidsBusrolesToADD.Count > 0)
            {
                int idBusrole = QUEUEidsBusrolesToADD.Dequeue();
                int idSubpr   = MAPbroleToSubproc[idBusrole];
                messages += "Attaching to this role: " + MAPbroleIdToName[idBusrole] + "\n";
                HELPERS.infoEASet wstouse = readyToUseWorkspaces[idSubpr];

                returnListEntAssignmentByEntAssignmentSet[] theEAss =
                    ENGINEentass.ListEntAssignmentByEntAssignmentSet
                        (null, "\"BusRole\" = ? AND \"Entitlement\" = ? ", new string[] { idBusrole.ToString(), IDentitlement.ToString() }, "", wstouse.idEntAssSet);

                if (theEAss.GetLength(0) > 1)
                {
                    throw new Exception("Workspace " + wstouse.idEntAssSet + " contains multiple entitlement assignment records for business role " + idBusrole);
                }
                else if (theEAss.GetLength(0) == 1)
                {
                    switch (theEAss[0].Status)
                    {
                    case "A":
                        messages += "Nothing to do. Workspace already has this match in place.\n";
                        break;

                    case "N":
                        messages += "Nothing to do. Workspace already has this match in place.\n";
                        break;

                    case "X":
                        ENGINEentass.SetEntAssignment
                            (theEAss[0].ID, theEAss[0].EntAssignmentSetID, theEAss[0].BusRoleID,
                            theEAss[0].EntitlementID, "N");
                        break;
                    }
                }
                else
                {
                    int baby = ENGINEentass.NewEntAssignment
                                   (wstouse.idEntAssSet, idBusrole, IDentitlement, "N");
                }
            }



            if (messages.Length == 0)
            {
                messages = "NO MESSAGES TO REPORT.";
            }
            Grid1.CallbackParameter = messages;
        }