/// <summary>
 /// gets the Application User representation of "me"
 /// </summary>
 /// <param name="i_AccessToken"></param>
 /// <returns></returns>
 public static ApplicationUser GetUser(string i_AccessToken)
 {
     ApplicationUser result = new ApplicationUser();
     FacebookClient fbClient = new FacebookClient(i_AccessToken); ;
     dynamic me = fbClient.Query(
      "SELECT uid, name, first_name, last_name, pic_small, pic_big, pic_square, pic, email, birthday_date FROM user WHERE uid = me()");
     result = createUserFromDynamicUser(me[0], result);
     result.AccessToken = i_AccessToken;
     return result;
 }
 protected void Page_Load(object sender, EventArgs e)
 {
     if (!string.IsNullOrEmpty(Request.QueryString["access_token"]))
     {
         m_AccessToken = Request.QueryString["access_token"];
         m_ApplicationUser = FacebookUtilities.GetUser(m_AccessToken);
         m_UserFriends = FacebookUtilities.GetUsersFriends(m_AccessToken);
         populateTableWithControls();
     }
     else
     {
         Response.Write("problem with access token. Please try again later");
     }
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["access_token"]))
            {
                m_AccessToken = Request.QueryString["access_token"];
                m_ApplicationUser = FacebookUtilities.GetUser(m_AccessToken);
                m_UserFriends = m_DataBaseHandlerObject.GetUserFriendsThatAreInDataBase(m_ApplicationUser);

                // Checks if the user has friends in database
                if (m_UserFriends.Count == 0)
                {
                    Response.Write("User doesn't appear to have friends in database!");

                    // Returns to the welcomeStage
                    Response.Redirect(string.Format("welcomeStage.aspx?access_token={0}", m_AccessToken));
                }

                populateTableWithFriends();
            }
            else
            {
                Response.Write("problem with access token. Please try again later");
            }
        }
 public BirthdayMessageSender(ApplicationUser applicationUser, List<Friend> list)
 {
     // TODO: Complete member initialization
     this.applicationUser = applicationUser;
     this.list = list;
 }
 private string getInsertCommandForBirthdayMessageIntoDataBase(FacebookUser i_FacebookUser, ApplicationUser i_ApplicationUser)
 {
     return string.Format(
                         "INSERT INTO {0} VALUES ({1}, {2})",
                         eTabelsInDataBase.Birthday_Messages.ToString(),
                         getValuesOfTableForUser(eTabelsInDataBase.Birthday_Messages, i_FacebookUser),
                         i_ApplicationUser.Id);
 }
        /// <summary>
        /// checks if a given messgae i.e. combination of appuser-friend is in db
        /// </summary>
        /// <param name="i_ApplicationUser"></param>
        /// <param name="i_Friend"></param>
        /// <returns></returns>
        private bool messageIsInDatabaseWithoutConnection(ApplicationUser i_ApplicationUser, Friend i_Friend)
        {
            bool result = false;
            SqlDataReader dbReader = null;
            string query = string.Format(
                "SELECT * FROM {0} WHERE {1} = {2} AND {3} = {4}",
                   eTabelsInDataBase.Birthday_Messages.ToString(),
                   eTbale_Birthday_Messages_Columns.From_Application_User_ID.ToString(),
                   i_ApplicationUser.Id,
                   eTbale_Birthday_Messages_Columns.To_Friend_ID.ToString(),
                   i_Friend.Id);

            dbReader = commandDataBase(query, true);
            if (dbReader.HasRows)
            {
                result = true;
            }

            closeDataBaseReader(dbReader);
            return result;
        }
 /// <summary>
 /// this method updates the message from an applicatoin user to his friend.
 /// this method assumes that there was a message before and both the user and friend are in the db
 /// </summary>
 /// <param name="i_ApplicationUser"></param>
 /// <param name="i_Friend"></param>
 public void UpdateBirthdayMessage(ApplicationUser i_ApplicationUser, Friend i_Friend)
 {
     openConnection();
     if (checkIfUserIsInDataBaseWithOutConnection(i_ApplicationUser) && checkIfUserIsInDataBaseWithOutConnection(i_Friend) && messageIsInDatabaseWithoutConnection(i_ApplicationUser,i_Friend))
     {
         string command = string.Format(
             "UPDATE {0} SET {1} = '{2}' WHERE {3} = {4} AND {5} = {6}",
             eTabelsInDataBase.Birthday_Messages.ToString(),
             eTbale_Birthday_Messages_Columns.Birthday_Greet.ToString(),
             i_Friend.BirthdayMessage,
             eTbale_Birthday_Messages_Columns.To_Friend_ID.ToString(),
             i_Friend.Id,
             eTbale_Birthday_Messages_Columns.From_Application_User_ID.ToString(),
             i_ApplicationUser.Id);
         commandDataBase(command, false);
     }
     closeConnection();
 }
 /// <summary>
 /// Inserts a single application user to the data base
 /// </summary>
 /// <param name="i_ApplicationUser"></param>
 public void InsertSingleApplicationUser(ApplicationUser i_ApplicationUser)
 {
     if (!IsUserInDataBase(i_ApplicationUser))
     {
         openConnection();
         commandDataBase(getInsertCommandForFacebookUserInDataBase(i_ApplicationUser), false);
         commandDataBase(getInsertCommandForApplicationUserIntoDataBase(i_ApplicationUser), false);
         closeConnection();
     }
 }
        /// <summary>
        /// this methods inserts a list of friends into the db.
        /// will only work if the application user is already in db.
        /// </summary>
        /// <param name="i_ApplicationUser"></param>
        /// <param name="i_FriendsToInsert"></param>
        public void InsertFriendsIntoDataBase(ApplicationUser i_ApplicationUser, ICollection<Friend> i_FriendsToInsert)
        {
            string[] insertFacebookUsersCommands = new string[i_FriendsToInsert.Count];
            string[] insertFriendsCommands = new string[i_FriendsToInsert.Count];
            string[] insertBirthdayMessageCommands = new string[i_FriendsToInsert.Count];
            int i = 0;

            //building queries foreach friend
            foreach (Friend friend in i_FriendsToInsert)
            {
                insertFacebookUsersCommands[i] = (getInsertCommandForFacebookUserInDataBase(friend));
                insertFriendsCommands[i] = (getInsertCommandForFriendIntoDataBase(friend));
                insertBirthdayMessageCommands[i] = (getInsertCommandForBirthdayMessageIntoDataBase(friend, i_ApplicationUser));
                i++;
            }

            openConnection();

            i = 0;
            //inserting users
            FacebookUser tempFacebookUser = new FacebookUser();
            foreach (Friend friend in i_FriendsToInsert)
            {

                //check if the facebook user isn't in db
                tempFacebookUser.Id = friend.Id;
                if (!checkIfUserIsInDataBaseWithOutConnection(tempFacebookUser))
                {
                    commandDataBase(insertFacebookUsersCommands[i], false);
                }

                //check if the friend isn't in db
                if (!checkIfUserIsInDataBaseWithOutConnection(friend))
                {
                    commandDataBase(insertFriendsCommands[i], false);
                }

                //check if message isn't in db
                if (!messageIsInDatabaseWithoutConnection(i_ApplicationUser, friend))
                {
                    commandDataBase(insertBirthdayMessageCommands[i], false);
                }
                i++;
            }

            closeConnection();
        }
        /// <summary>
        /// This method returns the friends of the user that are already in the database.
        /// Returns a dictionary by {ID,Friend}.
        /// if the user dosn't have any friends in the DB, The count field of the dictionary will 
        /// be 0
        /// Query:
        /// SELECT FacebookUser.*,Birthday_Greet
        /// FROM Friend, Birthday_Messages,FacebookUser
        /// WHERE From_Application_User_ID = '2' AND Friend_ID = To_Friend_ID and ID = Friend_ID
        /// </summary>
        /// <param name="i_User"></param>
        /// <returns></returns>
        public Dictionary<string, Friend> GetUserFriendsThatAreInDataBase(ApplicationUser i_User)
        {
            Dictionary<string, Friend> result = new Dictionary<string, Friend>();

            //build query
            string query = string.Format(
                "SELECT {0}, {1} FROM {2}, {3}, {4} WHERE {5} = {6} AND {7} = {8} AND {9} = {10}",
                addDotAndStarToString(eTabelsInDataBase.FacebookUser.ToString()),
                eTbale_Birthday_Messages_Columns.Birthday_Greet.ToString(),
                eTabelsInDataBase.Friend.ToString(),
                eTabelsInDataBase.Birthday_Messages.ToString(),
                eTabelsInDataBase.FacebookUser.ToString(),
                eTbale_Birthday_Messages_Columns.From_Application_User_ID.ToString(),
                i_User.Id,
                eTable_Friend.Friend_ID.ToString(),
                eTbale_Birthday_Messages_Columns.To_Friend_ID.ToString(),
                eTable_FacebookUser.ID.ToString(),
                eTable_Friend.Friend_ID.ToString());

            openConnection();
            SqlDataReader dbReader = commandDataBase(query, true);

            //create friends from result
            while (dbReader.Read())
            {
                Friend tempFriend = new Friend();
                tempFriend.Id = dbReader[eTable_FacebookUser.ID.ToString()].ToString();
                tempFriend.FirstName = (string)dbReader[eTable_FacebookUser.First_Name.ToString()];
                tempFriend.LastName = (string)dbReader[eTable_FacebookUser.Last_Name.ToString()];
                tempFriend.FullName = (string)dbReader[eTable_FacebookUser.Full_Name.ToString()];
                tempFriend.BirthdayDateTime = (DateTime)dbReader[eTable_FacebookUser.Birthday.ToString()];
                tempFriend.BirthdayMessage = (string)dbReader[eTbale_Birthday_Messages_Columns.Birthday_Greet.ToString()];

                //build pictures
                Dictionary<string, string> tempFriendPics = new Dictionary<string, string>();
                tempFriendPics[ePictureTypes.pic.ToString()] = (string)dbReader[eTable_FacebookUser.Pic.ToString()];
                tempFriendPics[ePictureTypes.pic_big.ToString()] = (string)dbReader[eTable_FacebookUser.Pic_Big.ToString()];
                tempFriendPics[ePictureTypes.pic_small.ToString()] = (string)dbReader[eTable_FacebookUser.Pic_Small.ToString()];
                tempFriendPics[ePictureTypes.pic_square.ToString()] = (string)dbReader[eTable_FacebookUser.Pic_Square.ToString()];
                tempFriend.Pictures = tempFriendPics;
                result[tempFriend.Id] = tempFriend;
            }
            closeReaderAndConnection(dbReader);
            return result;
        }
 public EmailSender(ApplicationUser applicationUser, List<Friend> listOfFriendsThatGotGreetedSuccessfully)
 {
     // TODO: Complete member initialization
     this.applicationUser = applicationUser;
     this.listOfFriendsThatGotGreetedSuccessfully = listOfFriendsThatGotGreetedSuccessfully;
 }