private void IntGetGroupMemebers(TWikiGroup tWikiGroup, Dictionary<string, TWikiUser> members, 
     Dictionary<string, TWikiGroup> visitedGroups)
 {
     foreach (string member in tWikiGroup.Members)
     {
         TWikiUser user = null;
         if (_convertedUsers.TryGetValue(member, out user))
         {
             if (user != null)
             {
                 members[member] = user;
             }
         }
         TWikiGroup subGroup = null;
         if (visitedGroups.ContainsKey(member))
         {
             continue;
         }
         if (_convertedGroups.TryGetValue(member, out subGroup))
         {
             if (subGroup != null)
             {
                 visitedGroups[member] = subGroup;
                 IntGetGroupMemebers(subGroup, members, visitedGroups);
             }
         }
     }
 }
        private void LoadGroups(string tMainWebWikiDataPath)
        {
            foreach (string groupFileName in Directory.GetFiles(tMainWebWikiDataPath, "*Group.txt"))
            {
                string groupName = Path.GetFileNameWithoutExtension(Path.GetFileName(groupFileName));
                
                string groupContent = File.ReadAllText(groupFileName);

                TWikiGroup group = null;
                _convertedGroups.TryGetValue(groupName, out group);
                if (group == null)
                {
                    group = new TWikiGroup(true, groupName);
                }

                string[] members = ExtractNamesList(groupContent, "GROUP");

                foreach (string member in members)
                {
                    group.AddMemeber(member);
                }

                _convertedGroups[groupName] = group;
            }

            DreamMessage msg = _dekiPlug.At("groups").With("limit", int.MaxValue).GetAsync().Wait();
            if (msg.Status != DreamStatus.Ok)
            {
                WriteErrorResponse(msg, "Error while reciving groups from Deki. Groups not converted.");
                return;
            }
            Dictionary<string, string> dekiGroups = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);

            XDoc groupsDoc = msg.AsDocument();
            foreach (XDoc groupDoc in groupsDoc["//group"])
            {
                string dekiGroupName = groupDoc["groupname"].AsText;
                dekiGroups[dekiGroupName.ToLower()] = null;
            }

            foreach (string groupName in _convertedGroups.Keys)
            {
                TWikiGroup group = _convertedGroups[groupName];
                if (group.IsNewGroup)
                {
                    int groupNum = 0;
                    string dekiGroupName = groupName;
                    while (dekiGroups.ContainsKey(dekiGroupName))
                    {
                        groupNum++;
                        dekiGroupName = groupName + groupNum.ToString();
                    }
                    if (dekiGroupName != groupName)
                    {
                        WriteLineToConsole("TWiki group \"" + groupName + "\" converted as \"" + dekiGroupName + "\" becouse of existing same group in Deki");
                    }
                    group.DekiName = dekiGroupName;

                    XDoc newGroupDoc = new XDoc("group");
                    newGroupDoc.Elem("name", group.DekiName);

                    TWikiUser[] members = GetAllGroupMembers(groupName);
                    if (members.Length > 0)
                    {
                        newGroupDoc.Start("users");
                        foreach (TWikiUser member in members)
                        {
                            newGroupDoc.Start("user").Attr("id", member.DekiId).End();
                        }
                        newGroupDoc.End();
                    }

                    DreamMessage res = _dekiPlug.At("groups").PostAsync(newGroupDoc).Wait();
                    WriteErrorResponse(res, "Error converting group\"" + groupName + "\"");
                }
                else
                {
                    XDoc updateGroupDoc = new XDoc("users");
                    TWikiUser[] members = GetAllGroupMembers(groupName);
                    if (members.Length > 0)
                    {
                        foreach (TWikiUser member in members)
                        {
                            updateGroupDoc.Start("user").Attr("id", member.DekiId).End();
                        }
                    }
                    DreamMessage res = _dekiPlug.At("groups", "=" + group.DekiName, "users").PutAsync(updateGroupDoc).Wait();
                    WriteErrorResponse(res, "Error updating group \"" + groupName + "\" users.");
                }
            }
        }
        private void LoadConvertedUsersAndGroups()
        {
            if (!File.Exists(ConvertedUsersFileName))
            {
                return;
            }

            WriteLineToConsole("Loading converted users from " + ConvertedUsersFileName);

            XDoc convertedUsers = XDocFactory.LoadFrom(ConvertedUsersFileName, MimeType.XML);

            if ((convertedUsers == null) || (convertedUsers.IsEmpty))
            {
                return;
            }
            foreach (XDoc userDoc in convertedUsers["//" + ConvertedUserTagName])
            {
                string tWikiUserName = userDoc["@" + TWikiUserNameAttribute].AsText;
                if (tWikiUserName == null)
                {
                    continue;
                }
                string dekiUserName = userDoc["@" + DekiUserNameAttribute].AsText;
                if (dekiUserName == null)
                {
                    continue;
                }

                DreamMessage dekiUserMessage = _dekiPlug.At("users", "=" + XUri.DoubleEncodeSegment(dekiUserName)).GetAsync().Wait();
                if (dekiUserMessage.Status != DreamStatus.Ok)
                {
                    if (dekiUserMessage.Status == DreamStatus.NotFound)
                    {
                        WriteLineToConsole("User \"" + dekiUserName+"\" specified in \""+ ConvertedUsersFileName+ "\" but not found in Deki. New user created." );
                    }
                    else
                    {
                        WriteErrorResponse(dekiUserMessage, "Error reciving user data from Deki.");
                    }
                    continue;
                }
                XDoc dekiUserDoc = dekiUserMessage.AsDocument();
                int dekiUserId = dekiUserDoc["@id"].AsInt.Value;

                _convertedUsers[tWikiUserName] = new TWikiUser(tWikiUserName, dekiUserName, dekiUserId);
            }
            foreach (XDoc groupDoc in convertedUsers["//" + ConvertedGroupTagName])
            {
                string tWikiGroupName = groupDoc["@" + TWikiGroupNameAttribute].AsText;
                if (tWikiGroupName == null)
                {
                    continue;
                }
                string dekiGroupName = groupDoc["@" + DekiGroupNameAttribute].AsText;
                if (dekiGroupName == null)
                {
                    continue;
                }

                _convertedGroups[tWikiGroupName] = new TWikiGroup(dekiGroupName, false, tWikiGroupName);
            }
            WriteLineToConsole("Users loaded!!!");
            WriteLineToConsole();
        }