Example #1
0
        private static void NicknameOperations(AppsService service)
        {
            // Create a new nickname.
            NicknameEntry insertedEntry = service.CreateNickname(testUserName, testNickname);
            Console.WriteLine("Created nickname '{0}' for user {1}", insertedEntry.Nickname.Name,
                insertedEntry.Login.UserName);

            // Retrieve the newly-created nickname.
            NicknameEntry entry = service.RetrieveNickname(testNickname);
            Console.WriteLine("Retrieved nickname {0}", entry.Nickname.Name);

            // Retrieve all nicknames for testUserName (really, just this one).
            NicknameFeed feed = service.RetrieveNicknames(testUserName);
            entry = feed.Entries[0] as NicknameEntry;
            Console.WriteLine("Retrieved nickname '{0}' for user {1}", entry.Nickname.Name,
                entry.Login.UserName);

            // Retrieve a page of nicknames.
            feed = service.RetrievePageOfNicknames(testNickname);
            entry = feed.Entries[0] as NicknameEntry;
            Console.WriteLine("Retrieved page of {0} nicknames, beginning with '{1}'",
                feed.Entries.Count, entry.Nickname.Name);

            // Retrieve the feed of all nicknames.
            feed = service.RetrieveAllNicknames();
            entry = feed.Entries[0] as NicknameEntry;
            Console.WriteLine("Retrieved all {0} nicknames in the domain, beginning with '{1}'",
                feed.Entries.Count, entry.Nickname.Name);
        }
        public void NicknameUpdate(AppsService service, SqlDataReader nicknames, string usernameFieldName, string userNicknameField, LogFile log)
        {
            // updates nicknames in gmail from sql data reader containing user ids and nicknames
            // finds the user with the ID listed and returns all the nicknames for that user
            // iterates trhrough the nicknames and delets all those which do not mathch the nickname from the usernickname field
            // if the nickname from the usernickname field does not exist it will be created fro the user listed
            //
            // usernameFieldName    userNicknameField
            // ------------------------------------
            // ID               |   Nickname
            // ------------------------------------
            // SB289312         |   test.user
            //
            //

            int i = 0;
            int nicknamecount = 0;
            bool foundnickname = false;
            try
            {
                while (nicknames.Read())
                {
                    // get all nicknames for user who has the wrong nickname
                    NicknameFeed userNicknames;
                    userNicknames = service.RetrieveNicknames(nicknames[usernameFieldName].ToString());

                    // get the count so we can iterate over them\

                    nicknamecount = userNicknames.Entries.Count;
                    // iterate and delete all nicknames that are not equal to the correct nickname
                    for (i = 0; i < nicknamecount; i++)
                    {
                        try
                        {

                            NicknameEntry nicknameEntry = userNicknames.Entries[nicknamecount] as NicknameEntry;
                            if (nicknameEntry.Nickname.Name.ToString() == nicknames[userNicknameField].ToString())
                            {
                                foundnickname = true;
                            }
                            else
                            {
                                service.DeleteNickname(nicknameEntry.Nickname.Name.ToString());
                                log.addTrn("Deleting user nickname " + nicknameEntry.Nickname.Name.ToString(), "Transaction");
                            }
                        }
                        catch
                        {
                            log.addTrn("Error deleting user nickname " + nicknames[userNicknameField].ToString(), "Error");
                        }
                    }
                    // if the nickname is not found create the new nickname
                    if (foundnickname == false)
                    {
                        try
                        {
                            service.CreateNickname(nicknames[usernameFieldName].ToString(), nicknames[userNicknameField].ToString());
                            log.addTrn("Creating user nickname " + nicknames[userNicknameField].ToString() + " for user " + nicknames[usernameFieldName].ToString(), "Transaction");
                        }
                        catch
                        {
                            log.addTrn("Error adding user nickname " + nicknames[userNicknameField].ToString() + " for user " + nicknames[usernameFieldName].ToString(), "Error");
                        }
                    }
                    // reset all variables
                    foundnickname = false;
                    i = 0;
                    nicknamecount = 0;
                }
            }
            catch (Exception ex)
            {
                log.addTrn("Issue updating nicknames datareader is null " + ex.Message.ToString() + "\n" + ex.StackTrace.ToString(), "Error");
            }
        }
        // Gmail tools
        public string GetNewUserNickname(AppsService service, string studentID, string firstName, string midName, string lastName, int i, bool complete)
        {
            /**
            * Comments By: Arlo Carreon, 12/12/11
            *
            * GetNewUserNickname():
            *  -This method tries different combinations of [email protected]
            *  -In theory it should stop at the first combination it can successfully set for this user.
            *
            * Problems:
            *  -We are not certain whether or not the alias is created even though an exception is thrown.
            *      - We are not aware of all the types of exceptions
            *  -If a nickname already exists we quick the process. Maybe it is taken by someone else? Especially in the
            *   initial attempt of [email protected].
            *
            * Todo:
            *  -Log some of these exceptions.
            *  -Log when user not matched with an alias.
            *
            */
            // this could really get screwed if there are enough duplicates it will be only do first.m.last f.middle.last
            // i can be used to start the process somewhere in the middle of the name

            // Log for trouble shooting purposes
            troubleshootLog.Add("INIT: Following attempts are for user -> " + studentID.ToString());

            string returnvalue = "";
            firstName = firstName.Replace(".", "");
            lastName = lastName.Replace(".", "");
            midName = midName.Replace(".", "");
            while (complete == false)
            {
                int r = midName.Length + 1;

                // First try will be full first and last name
                if (i == 0)
                {
                    returnvalue = firstName + "." + lastName;
                }
                else
                {
                    // Working on combinations for the middle name
                    if (i < r)
                    {
                        returnvalue = firstName + "." + midName.Substring(0, i) + "." + lastName;
                    }
                    else
                    {
                        // Working on combinations for the firstname
                        if (i < (r + firstName.Length))
                        {
                            returnvalue = firstName.Substring(0, (i - r)) + "." + midName + "." + lastName;
                        }
                        else
                        {
                            // We tried them all and we have no available match
                            returnvalue = "failure";
                            complete = true;
                            // Log for trouble shooting purposes
                            troubleshootLog.Add("FAIL: Tried all combinations. No Match.");
                        }
                    }
                }

                try
                {
                    if (complete == false)
                    {

                        // Assuming an exception is thrown only if not successful
                        returnvalue = Regex.Replace(Regex.Replace(returnvalue, @"[^a-z|^A-Z|^0-9|^\.|_|-]|[\^|\|]", ""), @"\.+", ".");

                        // Log for trouble shooting purposes
                        troubleshootLog.Add("ATTEMPT: " + returnvalue);

                        // Attempting to save this combination as Alias
                        service.CreateNickname(studentID, returnvalue);
                        complete = true;
                    }
                }
                catch (AppsException apex)
                {
                    if (apex.ErrorCode == "1300")
                    {
                        // Log for trouble shooting purposes
                        troubleshootLog.Add("GOOGLE EXCEPTION " + apex.ErrorCode.ToString() + ": " + apex.Reason.ToString() + " (Nickname already in use)");
                    }
                    else if (apex.ErrorCode == "1301")
                    {
                        // User does not exist
                        complete = true;

                        // Log for trouble shooting purposes
                        troubleshootLog.Add("GOOGLE EXCEPTION " + apex.ErrorCode.ToString() + ": " + apex.Reason.ToString() + " (Cannot set nickname for nonexistant user)");
                    }
                    else
                    {
                        // Log for trouble shooting purposes
                        troubleshootLog.Add("GOOGLE EXCEPTION " + apex.ErrorCode.ToString() + ": " + apex.Reason.ToString());
                    }
                    i++;
                }
                catch (Exception e)
                {
                    //MessageBox.Show("Nickname issue " + ex.Message.ToString() + "\n" + ex.StackTrace.ToString());
                    i++;
                    complete = false;
                    // Log for trouble shooting purposes
                    troubleshootLog.Add("EXCEPTION: " + e.Message.ToString());
                }
            }
            // Log for trouble shooting purposes
            troubleshootLog.Add("DONE: " + returnvalue.Trim());
            return returnvalue.Trim();
        }