All pastes #1346073 Raw Edit

Someone

public text v1 · immutable
#1346073 ·published 2009-02-24 19:07 UTC
rendered paste body
using System;
using System.Collections;
using System.Collections.Generic;

namespace Groups
{

    // --------------------------------------------------------------------
    public class GroupMember
    {
        // define the information needed to describe a group member
        public GroupMember(Guid memberGuid, string memberTitle, DateTime memberSince)
        {
            //_MemberName = memberName;  Removed - Name/Guid translation should be elsewhere, we only need store one
            _MemberGuid = memberGuid;
            _MemberTitle = memberTitle;
            _MemberSince = memberSince;
        }

        public void WriteToConsole()
        {
            Console.WriteLine("Member GUID={0}, Title {1}, Last Login {2}", _MemberGuid, _MemberTitle, _MemberSince);
        }


        //public string _MemberName;
        public Guid _MemberGuid;
        public string _MemberTitle;
        public DateTime _MemberSince;
    }

    // --------------------------------------------------------------------
    public class GroupRole
    {
        // define the information needed to describe a group role
        public GroupRole(string roleName, string permissions)
        {
            _RoleName = roleName;
            _RoleGuid = Guid.NewGuid();
            _Permissions = permissions; //This should be a bit field of some form TBD
        }

        List<GroupRole> GroupRoles = new List<GroupRole>();

        public string _RoleName;
        public Guid _RoleGuid;
        public string _Permissions;
    
    }

    // --------------------------------------------------------------------
    public class Group
    {
        // Constructor
        public Group(string name, string description)
        {
            this._Name = name;
            this._Description = description;
            this._PermittedRoles = new ArrayList();
            this._Members = new ArrayList();

        }

        // Write Group Details on the Console
        public void WriteToConsole()
        {
            Console.WriteLine("Name={0}, Description={1}, #of Members {2}", _Name, _Description, _Members.Count);
            foreach (Group GroupMember in _Members) GroupMember.WriteToConsole();
        }

        // Type definitions for the Group Structure
        public string _Name;
        public string _Description;
        public ArrayList _PermittedRoles;
        public ArrayList _Members;

    }


    // --------------------------------------------------------------------
    
    public class GroupDictionary
    {
        // The GroupDictionary structure
        Dictionary<Guid, Group> _OSGroups = new Dictionary<Guid,Group> ();

        //TODO - Locking - this is not thread safe

        // Private Methods

        // Method: Determine if a group exists in the GroupDictionary, search on GroupGuid
        // Parameters:  in GroupName
        // Return:      true if the group exists
        //              false if the group does not exist
        //
        private Boolean GroupExists(Guid GroupGuid)
        {
            if (_OSGroups.ContainsKey(GroupGuid))
            {
                return true;
            }
            return false;
        }

        // Public Methods

        // Method: Determine if a group name exists in the GroupDictionary and return the Guid
        // Parameters:  in GroupName
        // Return:      Guid if a group of that name exists
        //              Guid.Empty if a group of that name does not exist
        //
        public Guid GetGuidFromGroupName(string GroupName)
        {
            foreach (KeyValuePair<Guid, Group> pair in _OSGroups)
            {
                if (pair.Value._Name == GroupName)
                {
                    return pair.Key;
                }
            }
            return Guid.Empty;
        }


        // Method: to add a new group to the GroupDictionary
        // Parameters:  in Group
        // Return:      Guid if group is successfully added
        //              Guid.Empty if a group of the same name already exists
        //
        public Guid Add (Group GroupDetail)
        {
            Guid GroupGuid = Guid.NewGuid();

            if ((GetGuidFromGroupName (GroupDetail._Name) != Guid.Empty)) return Guid.Empty;

            _OSGroups.Add(GroupGuid, GroupDetail);

            return GroupGuid;
        }


        // Method: to delete a group from the GroupDictionary
        // Parameters:  in GroupGuid
        // Return:      true if group is successfully deleted
        //              false if the group cannot be located
        //
        public Boolean Delete(Guid GroupGuid)
        {
            if (GroupExists(GroupGuid))
            {
                _OSGroups.Remove(GroupGuid);
                return true;
            }
            return false;
        }


        // Method: to delete a group from the GroupDictionary
        // Parameters:  in GroupName
        // Return:      true if group is successfully deleted
        //              false if the group cannot be located
        //
        public Boolean Delete(string GroupName)
        {
            return Delete(GetGuidFromGroupName(GroupName));
        }



        // Method: to update a group in the GroupDictionary
        // Parameters:  in GroupName
        // Return:      true if group is successfully updated
        //              false if the group cannot be located
        //
        public Boolean UpdateDescription(Guid GroupGuid, string GroupDescription)
        {
            if (GroupExists(GroupGuid))
            {
                _OSGroups [GroupGuid]._Description = GroupDescription;
                return true;
            }
            return false;
        }

        // Method: to update a group in the GroupDictionary
        // Parameters:  in Group
        // Return:      true if group is successfully updated
        //              false if the group cannot be located
        //
        public Boolean UpdateDescription(string GroupName, string GroupDescription)
        {
            return UpdateDescription(GetGuidFromGroupName(GroupName), GroupDescription);
        }

        //Method: to update the Members List for a Group
        // ToDo

        //Method: To update the PermittedRoles for a Group
        // ToDo


        // Method: to write details of all groups in the GroupDictionary to the Console
        // Parameters:  None
        // Return:      None
        //
        public void WriteGroupDictionaryToConsole()
        {
            foreach (KeyValuePair<Guid, Group> pair in _OSGroups)
            {
                pair.Value.WriteToConsole();
            }
        }
    }
}

using System;
using Groups;

namespace Testing
{

    // --------------------------------------------------------------------
    class MainClass
    {
        public static void Main(string[] args)
        {
            GroupDictionary AllGroups = new GroupDictionary();

            // Test of Method AddToDictionary
            if (AllGroups.Add(new Group("Group 1", "Test Group 1")) != Guid.Empty) Console.WriteLine("Test 1 Passed");
            if (AllGroups.Add(new Group("Group 2", "Test Group 2")) != Guid.Empty) Console.WriteLine("Test 2 Passed");
            if (AllGroups.Add(new Group("Group 2", "Test Group 2")) == Guid.Empty) Console.WriteLine("Test 3 Passed");
            if (AllGroups.Add(new Group("Group 3", "Test Group 3")) != Guid.Empty) Console.WriteLine("Test 4 Passed");


            // Test of Method CheckNameInDictionary
            if (AllGroups.GetGuidFromGroupName("Group 1") != Guid.Empty) Console.WriteLine("Test 5 Passed");
            if (AllGroups.GetGuidFromGroupName("Group 16") == Guid.Empty) Console.WriteLine("Test 6 Passed");

            // Test of Method DeleteFromDictionary
            Console.WriteLine("Groups 1, 2 and 3 should be listed below");
            AllGroups.WriteGroupDictionaryToConsole();

            if (AllGroups.Delete("Group 3"))
            {
                if (!AllGroups.Delete("Group 3"))
                {
                    Console.WriteLine("Test 7 Passed");
                }
            }

            if (AllGroups.Delete("Group 1")) Console.WriteLine("Test 8 Passed");

            Console.WriteLine("Groups 2 only should be listed below");
            AllGroups.WriteGroupDictionaryToConsole();

            // Test of Method UpdateInDictionary
            if (AllGroups.UpdateDescription("Group 2", "Changed Description")) Console.WriteLine("Test 9 Passed"); 
            Console.WriteLine("Group 2 should have a changed description field");
            AllGroups.WriteGroupDictionaryToConsole();
        }
    }
}