Пример #1
0
 public void updateFacebookUserStatus(Domain.Myfashion.Domain.FacebookAccount fbaccount)
 {
     //Creates a database connection and opens up a session
     using (NHibernate.ISession session = SessionFactory.GetNewSession())
     {
         //After Session creation, start Transaction.
         using (NHibernate.ITransaction transaction = session.BeginTransaction())
         {
             try
             {
                 // Proceed action to Update Data.
                 // And Set the reuired paremeters to find the specific values.
                 session.CreateQuery("Update FacebookAccount set IsActive=:status where FbUserId = :fbuserid and UserId = :userid")
                 .SetParameter("fbuserid", fbaccount.FbUserId)
                 .SetParameter("userid", fbaccount.UserId)
                 .SetParameter("status", fbaccount.IsActive)
                 .ExecuteUpdate();
                 transaction.Commit();
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.StackTrace);
                 // return 0;
             }
         } //End Transaction
     }     //End session
 }
Пример #2
0
        /// <getUserDetails>
        /// Get User's all Detail from FacebookAccount by FbUserId.
        /// </summary>
        /// <param name="FbUserId">FbUserId of FacebookAccount(string)</param>
        /// <returns>Return a object of FacebookAccount with  value of each member.</returns>
        public Domain.Myfashion.Domain.FacebookAccount getUserDetails(string FbUserId)
        {
            //Creates a database connection and opens up a session
            using (NHibernate.ISession session = SessionFactory.GetNewSession())
            {
                //After Session creation, start Transaction.
                using (NHibernate.ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        //Proceed action to, Get User's all Detail from FacebookAccount by FbUserId.
                        NHibernate.IQuery query = session.CreateQuery("from FacebookAccount where FbUserId = :fbuserid");

                        query.SetParameter("fbuserid", FbUserId);
                        List <Domain.Myfashion.Domain.FacebookAccount> lst = new List <Domain.Myfashion.Domain.FacebookAccount>();

                        foreach (Domain.Myfashion.Domain.FacebookAccount item in query.Enumerable())
                        {
                            lst.Add(item);
                            break;
                        }
                        Domain.Myfashion.Domain.FacebookAccount fbacc = lst[0];
                        return(fbacc);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                        return(null);
                    }
                } //End Transaction
            }     //End session
        }
Пример #3
0
 /// <addFacebookUser>
 /// Add new facebok user in  database.
 /// </summary>
 /// <param name="fbaccount">Set Values in a FacebookAccount Class Property and Pass the same Object of FacebookAccount Class.(Domain.FacebookAccount)</param>
 public void addFacebookUser(Domain.Myfashion.Domain.FacebookAccount fbaccount)
 {
     //Creates a database connection and opens up a session
     using (NHibernate.ISession session = SessionFactory.GetNewSession())
     {
         //After Session creation, start Transaction.
         using (NHibernate.ITransaction transaction = session.BeginTransaction())
         {
             //proceed action, to save data.
             session.Save(fbaccount);
             transaction.Commit();
         } //End Transaction
     }     //End session
 }
Пример #4
0
 /// <summary>
 /// Get all Facebook Account of User by UserId(Guid) and FbUserId(string).
 /// </summary>
 /// <param name="Fbuserid">Fbuserid of User(String)</param>
 /// <param name="userId">UserId of User(Guid)</param>
 /// <returns>Return a object of FacebookAccount with  value of each member.</returns>
 public Domain.Myfashion.Domain.FacebookAccount getFacebookAccountDetailsById(string Fbuserid, Guid userId)
 {
     //Creates a database connection and opens up a session
     using (NHibernate.ISession session = SessionFactory.GetNewSession())
     {
         //After Session creation, start Transaction.
         using (NHibernate.ITransaction transaction = session.BeginTransaction())
         {
             // proceed action, to get all Facebook Account of User by UserId(Guid) and FbUserId(string).
             NHibernate.IQuery query = session.CreateQuery("from FacebookAccount where FbUserId = :Fbuserid and UserId=:userId");
             query.SetParameter("Fbuserid", Fbuserid);
             query.SetParameter("userId", userId);
             Domain.Myfashion.Domain.FacebookAccount result = (Domain.Myfashion.Domain.FacebookAccount)query.UniqueResult();
             return(result);
         } //End Transaction
     }     //End session
 }
Пример #5
0
 public Domain.Myfashion.Domain.FacebookAccount getFacebookAccountDetailsById(string Fbuserid)
 {
     Domain.Myfashion.Domain.FacebookAccount result = new Domain.Myfashion.Domain.FacebookAccount();
     //Creates a database connection and opens up a session
     using (NHibernate.ISession session = SessionFactory.GetNewSession())
     {
         //After Session creation, start Transaction.
         using (NHibernate.ITransaction transaction = session.BeginTransaction())
         {
             // proceed action, to get all Facebook Account of User by UserId(Guid) and FbUserId(string).
             List <Domain.Myfashion.Domain.FacebookAccount> objlstfb = session.CreateQuery("from FacebookAccount where FbUserId = :Fbuserid ")
                                                                       .SetParameter("Fbuserid", Fbuserid)
                                                                       .List <Domain.Myfashion.Domain.FacebookAccount>().ToList <Domain.Myfashion.Domain.FacebookAccount>();
             if (objlstfb.Count > 0)
             {
                 result = objlstfb[0];
             }
             return(result);
         } //End Transaction
     }     //End session
 }
 private static string GetFbPageData(string ret, FacebookAccount itemFb)
 {
     Api.Facebook.Facebook ApiobjFacebook = new Api.Facebook.Facebook();
     ret = ApiobjFacebook.GetFacebookPageData(itemFb.FbUserId, itemFb.UserId.ToString());
     return ret;
 }
        public string AddFacebookAccountWithlogin(string accessToken, string UserId, string GroupId)
        {
            string ret = string.Empty;
            string client_id = ConfigurationManager.AppSettings["ClientId"];
            string redirect_uri = ConfigurationManager.AppSettings["RedirectUrl"];
            string client_secret = ConfigurationManager.AppSettings["ClientSecretKey"];
            long friendscount = 0;
            try
            {
                FacebookClient fb = new FacebookClient();
                string profileId = string.Empty;
                Dictionary<string, object> parameters = new Dictionary<string, object>();
                parameters.Add("client_id", client_id);
                parameters.Add("redirect_uri", redirect_uri);
                parameters.Add("client_secret", client_secret);
                if (accessToken != null)
                {
                    fb.AccessToken = accessToken;
                    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                    dynamic profile = fb.Get("v2.0/me");
                    dynamic friends = fb.Get("v2.0/me/friends");
                    try
                    {
                        friendscount = Convert.ToInt16(friends["summary"]["total_count"].ToString());
                    }
                    catch (Exception ex)
                    {
                        //try
                        //{
                        //    dynamic frndscount = fb.Get("fql", new { q = "SELECT friend_count FROM user WHERE uid=me()" });

                        //    foreach (var friend in frndscount.data)
                        //    {
                        //        frndscount = friend.friend_count;
                        //    }
                        //    friendscount = Convert.ToInt16(frndscount);
                        //}
                        //catch (Exception ex)
                        //{
                        //    friendscount = 0;
                        //    logger.Error(ex.Message);
                        //    logger.Error(ex.StackTrace);
                        //}

                        friendscount = 0;
                        logger.Error("friendscount >> " + ex.Message);
                        logger.Error("friendscount >> " + ex.StackTrace);
                        logger.Error("friendscount >> " + friends.ToString());
                        logger.Error("friendscount >> " + friends["paging"]["next"].ToString());
                    }
                    if (!objFacebookAccountRepository.checkFacebookUserExists(Convert.ToString(profile["id"]), Guid.Parse(UserId)))
                    {
                        #region Add FacebookAccount
                        objFacebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
                        objFacebookAccount.Id = Guid.NewGuid();
                        objFacebookAccount.FbUserId = (Convert.ToString(profile["id"]));
                        objFacebookAccount.FbUserName = (Convert.ToString(profile["name"]));
                        objFacebookAccount.AccessToken = accessToken;
                        objFacebookAccount.Friends = Convert.ToInt16(friendscount);
                        try
                        {
                            objFacebookAccount.EmailId = (Convert.ToString(profile["email"]));
                        }
                        catch (Exception ex)
                        {

                        }
                        objFacebookAccount.Type = "account";
                        objFacebookAccount.ProfileUrl = (Convert.ToString(profile["link"]));
                        objFacebookAccount.IsActive = 1;
                        objFacebookAccount.UserId = Guid.Parse(UserId);
                        objFacebookAccountRepository.addFacebookUser(objFacebookAccount);
                        #endregion
                        #region Add TeamMemberProfile
                        Domain.Myfashion.Domain.Team objTeam = objTeamRepository.GetTeamByGroupId(Guid.Parse(GroupId));
                        Domain.Myfashion.Domain.TeamMemberProfile objTeamMemberProfile = new Domain.Myfashion.Domain.TeamMemberProfile();
                        objTeamMemberProfile.Id = Guid.NewGuid();
                        objTeamMemberProfile.TeamId = objTeam.Id;
                        objTeamMemberProfile.Status = 1;
                        objTeamMemberProfile.ProfileType = "facebook";
                        objTeamMemberProfile.StatusUpdateDate = DateTime.Now;
                        objTeamMemberProfile.ProfileId = Convert.ToString(profile["id"]);
                        objTeamMemberProfile.ProfileName = (Convert.ToString(profile["name"]));
                        objTeamMemberProfile.ProfilePicUrl = "http://graph.facebook.com/" + objTeamMemberProfile.ProfileId + "/picture?type=small";
                        objTeamMemberProfileRepository.addNewTeamMember(objTeamMemberProfile);
                        #endregion
                        #region SocialProfile
                        Domain.Myfashion.Domain.SocialProfile objSocialProfile = new Domain.Myfashion.Domain.SocialProfile();
                        objSocialProfile.Id = Guid.NewGuid();
                        objSocialProfile.ProfileType = "facebook";
                        objSocialProfile.ProfileId = (Convert.ToString(profile["id"]));
                        objSocialProfile.UserId = Guid.Parse(UserId);
                        objSocialProfile.ProfileDate = DateTime.Now;
                        objSocialProfile.ProfileStatus = 1;
                        #endregion
                        if (!objSocialProfilesRepository.checkUserProfileExist(objSocialProfile))
                        {
                            objSocialProfilesRepository.addNewProfileForUser(objSocialProfile);
                        }
                        #region Add Facebook Feeds
                        AddFacebookFeeds(UserId, fb, profile);
                        #endregion
                        #region Add Facebook User Home
                        AddFacebookUserHome(UserId, fb, profile);
                        #endregion
                        #region Add Facebook User Inbox Message
                        //AddFacebookMessage(UserId, fb, profile);
                        AddFacebookMessageWithPagination(UserId, fb, profile);
                        #endregion
                        #region Add Facebook Stats
                        AddFacebookStats(UserId, fb, profile);
                        #endregion
                        //getUserNotifications(UserId, fb, profile);
                        ret = "Account Added Successfully";

                        ret = new JavaScriptSerializer().Serialize(objFacebookAccount);

                    }
                    else
                    {
                        objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(Convert.ToString(profile["id"]), Guid.Parse(UserId));
                        if (objFacebookAccount.IsActive == 2)
                        {
                            objFacebookAccount.IsActive = 1;
                            objFacebookAccount.AccessToken = accessToken;
                            objFacebookAccountRepository.updateFacebookUser(objFacebookAccount);

                            Domain.Myfashion.Domain.SocialProfile objSocialProfile = new Domain.Myfashion.Domain.SocialProfile();
                            objSocialProfile.Id = Guid.NewGuid();
                            objSocialProfile.ProfileType = "facebook";
                            objSocialProfile.ProfileId = objFacebookAccount.FbUserId;
                            objSocialProfile.UserId = Guid.Parse(UserId);
                            objSocialProfile.ProfileDate = DateTime.Now;
                            objSocialProfile.ProfileStatus = 1;
                            objSocialProfilesRepository.updateSocialProfileStatus(objSocialProfile);
                            ret = "Account Updated successfully !";
                        }
                        else
                        {
                            ret = "Account already Exist !";
                        }

                    }
                }
                //return new JavaScriptSerializer().Serialize(ret);
                return ret;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                return "Something Went Wrong";
            }
        }
        public string AddFacebookPagesInfo(string userid, string profileId, string accessToken, string groupId, string email)
        {
            string ret = string.Empty;
            try
            {
                FacebookClient fb = new FacebookClient();
                fb.AccessToken = accessToken;
                int fancountPage = 0;
                try
                {
                    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                    //dynamic fancount = fb.Get("fql", new { q = " SELECT fan_count FROM page WHERE page_id =" + profileId });
                    //foreach (var friend in fancount.data)
                    //{
                    //    fancountPage = Convert.ToInt32(friend.fan_count);
                    //}

                    // dynamic friends = fb.Get("v2.0/me/friends");
                    dynamic friends = fb.Get("v2.0/" + profileId);
                    //fancountPage = Convert.ToInt16(friends["summary"]["total_count"].ToString());
                    fancountPage = Convert.ToInt16(friends["likes"].ToString());

                }
                catch (Exception)
                {
                    fancountPage = 0;
                }

                if (accessToken != null)
                {
                    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;

                    dynamic profile = fb.Get("v2.0/me");

                    #region Add FacebookAccount
                    objFacebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
                    objFacebookAccount.Id = Guid.NewGuid();
                    objFacebookAccount.FbUserId = (Convert.ToString(profile["id"]));
                    objFacebookAccount.FbUserName = (Convert.ToString(profile["name"]));
                    objFacebookAccount.AccessToken = accessToken;
                    objFacebookAccount.Friends = Convert.ToInt32(fancountPage);
                    objFacebookAccount.EmailId = email;
                    objFacebookAccount.Type = "Page";
                    objFacebookAccount.ProfileUrl = (Convert.ToString(profile["link"]));
                    objFacebookAccount.IsActive = 1;
                    objFacebookAccount.UserId = Guid.Parse(userid);
                    if (!objFacebookAccountRepository.checkFacebookUserExists(objFacebookAccount.FbUserId, objFacebookAccount.UserId))
                    {
                        objFacebookAccountRepository.addFacebookUser(objFacebookAccount);
                    }

                    #endregion
                    #region SocialProfile
                    Domain.Myfashion.Domain.SocialProfile objSocialProfile = new Domain.Myfashion.Domain.SocialProfile();
                    objSocialProfile.Id = Guid.NewGuid();
                    objSocialProfile.ProfileType = "facebook_page";
                    objSocialProfile.ProfileId = (Convert.ToString(profile["id"]));
                    objSocialProfile.UserId = Guid.Parse(userid);
                    objSocialProfile.ProfileDate = DateTime.Now;
                    objSocialProfile.ProfileStatus = 1;
                    if (!objSocialProfilesRepository.checkUserProfileExist(objSocialProfile))
                    {
                        objSocialProfilesRepository.addNewProfileForUser(objSocialProfile);
                    }
                    #endregion
                    #region Add TeamMemberProfile
                    Domain.Myfashion.Domain.Team objTeam = objTeamRepository.GetTeamByGroupId(Guid.Parse(groupId));
                    Domain.Myfashion.Domain.TeamMemberProfile objTeamMemberProfile = new Domain.Myfashion.Domain.TeamMemberProfile();
                    objTeamMemberProfile.Id = Guid.NewGuid();
                    objTeamMemberProfile.TeamId = objTeam.Id;
                    objTeamMemberProfile.Status = 1;
                    objTeamMemberProfile.ProfileType = "facebook_page";
                    objTeamMemberProfile.StatusUpdateDate = DateTime.Now;
                    objTeamMemberProfile.ProfileId = Convert.ToString(profile["id"]);

                    objTeamMemberProfile.ProfileName = objFacebookAccount.FbUserName;
                    objTeamMemberProfile.ProfilePicUrl = "http://graph.facebook.com/" + objTeamMemberProfile.ProfileId + "/picture?type=small";

                    if (!objTeamMemberProfileRepository.checkTeamMemberProfile(objTeamMemberProfile.TeamId, objTeamMemberProfile.ProfileId))
                    {
                        objTeamMemberProfileRepository.addNewTeamMember(objTeamMemberProfile);
                    }

                    #endregion
                    #region Add Facebook Feeds
                    AddFacebookFeeds(userid, fb, profile);
                    #endregion
                    #region AddFacebook FanPge
                    Domain.Myfashion.Domain.FacebookFanPage objFacebookFanPage = new Domain.Myfashion.Domain.FacebookFanPage();
                    FacebookFanPageRepository objFacebookFanPageRepository = new FacebookFanPageRepository();
                    objFacebookFanPage.Id = Guid.NewGuid();
                    objFacebookFanPage.UserId = Guid.Parse(userid);
                    objFacebookFanPage.ProfilePageId = (Convert.ToString(profile["id"]));
                    objFacebookFanPage.FanpageCount = fancountPage.ToString();
                    objFacebookFanPage.EntryDate = DateTime.Now;
                    objFacebookFanPageRepository.addFacebookUser(objFacebookFanPage);
                    #endregion

                    AddFbPagePost(userid, accessToken, profileId);
                    getPageConversations(userid, fb, profile);

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            return ret;
        }
        public string GetFacebookData(string FbId, string UserId)
        {
            string ret = string.Empty;
            long friendscount = 0;
            try
            {
                Domain.Myfashion.Domain.FacebookAccount objFacebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
                if (objFacebookAccountRepository.checkFacebookUserExists(FbId, Guid.Parse(UserId)))
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FbId, Guid.Parse(UserId));
                }
                else
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FbId);
                }


                FacebookClient fb = new FacebookClient();
                string profileId = string.Empty;
                string accessToken = objFacebookAccount.AccessToken;
                dynamic profile = null;
                dynamic friends = null;
                if (accessToken != null)
                {
                    fb.AccessToken = accessToken;
                    try
                    {
                        profile = fb.Get("v2.0/me");

                    }
                    catch (Exception ex)
                    {
                        string errormssg = ex.Message;
                        if (errormssg.Contains("changed the password"))
                        {
                            UpdateSocialprofileStatus(UserId, FbId);
                            objFacebookAccount.IsActive = 2;
                            UpdateFacebookAccount(objFacebookAccount);
                        }
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                        return "Token Expired";
                    }
                    try
                    {
                        friends = fb.Get("v2.0/me/friends");

                    }
                    catch (Exception ex)
                    {
                        string errormssg = ex.Message;
                        if (errormssg.Contains("changed the password"))
                        {
                            UpdateSocialprofileStatus(UserId, FbId);
                        }
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                        return "Token Expired";
                    }

                    try
                    {
                        friendscount = Convert.ToInt16(friends["summary"]["total_count"].ToString());
                    }
                    catch (Exception ex)
                    {
                        //try
                        //{
                        //    dynamic frndscount = fb.Get("fql", new { q = "SELECT friend_count FROM user WHERE uid=me()" });

                        //    foreach (var friend in frndscount.data)
                        //    {
                        //        frndscount = friend.friend_count;
                        //    }
                        //    friendscount = Convert.ToInt16(frndscount);
                        //}
                        //catch (Exception exx)
                        //{
                        //    friendscount = 0;
                        //    logger.Error(exx.Message);
                        //    logger.Error(exx.StackTrace);
                        //}
                        friendscount = 0;
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                    }
                    if (objFacebookAccountRepository.checkFacebookUserExists(Convert.ToString(profile["id"]), Guid.Parse(UserId)))
                    {
                        #region Update FacebookAccount
                        UpdateFacebookAccount(objFacebookAccount);
                        #endregion
                        #region UpdateTeammemberprofile
                        Domain.Myfashion.Domain.TeamMemberProfile objTeamMemberProfile = new Domain.Myfashion.Domain.TeamMemberProfile();
                        objTeamMemberProfile.ProfileName = objFacebookAccount.FbUserName;
                        objTeamMemberProfile.ProfilePicUrl = "http://graph.facebook.com/" + objFacebookAccount.FbUserId + "/picture?type=small";
                        objTeamMemberProfile.ProfileId = objFacebookAccount.FbUserId;
                        objTeamMemberProfileRepository.updateTeamMemberbyprofileid(objTeamMemberProfile);
                        #endregion

                        #region UpdateFacebook FanPge
                        Domain.Myfashion.Domain.FacebookFanPage objFacebookFanPage = new Domain.Myfashion.Domain.FacebookFanPage();
                        FacebookFanPageRepository objFacebookFanPageRepository = new FacebookFanPageRepository();
                        objFacebookFanPage.Id = Guid.NewGuid();
                        objFacebookFanPage.UserId = Guid.Parse(UserId);
                        objFacebookFanPage.ProfilePageId = FbId;
                        objFacebookFanPage.FanpageCount = friendscount.ToString();
                        objFacebookFanPage.EntryDate = DateTime.Now;
                        objFacebookFanPageRepository.addFacebookUser(objFacebookFanPage);
                        #endregion

                        #region Add Facebook Feeds
                        //AddFacebookFeeds(UserId, fb, profile);
                        AddFacebookFeedsWithPagination(UserId, fb, profile);
                        #endregion
                        #region Add Facebook User Home
                        AddFacebookUserHome(UserId, fb, profile);
                        #endregion
                        #region Add Facebook User Inbox Message
                        AddFacebookMessageWithPagination(UserId, fb, profile);
                        #endregion
                        #region Add Facebook User Notifications
                       // getUserNotifications(UserId, fb, profile);
                        #endregion
                        ret = "Facebook info Updated Successfully";
                    }
                    else
                    {
                        ret = "Account already Exist !";
                    }
                }
                return new JavaScriptSerializer().Serialize(ret);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                Console.WriteLine(ex.StackTrace);
                return "Something Went Wrong";
            }
        }
        public string AddNewFacebookWallPosts(string FbId, string UserId)
        {
            string ret = string.Empty;
            long friendscount = 0;

            List<Domain.Myfashion.Domain.FacebookMessage> lstFacebookMessage = new List<Domain.Myfashion.Domain.FacebookMessage>();

            try
            {
                Domain.Myfashion.Domain.FacebookAccount objFacebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
                if (objFacebookAccountRepository.checkFacebookUserExists(FbId, Guid.Parse(UserId)))
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FbId, Guid.Parse(UserId));
                }
                else
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FbId);
                }


                FacebookClient fb = new FacebookClient();
                string profileId = string.Empty;
                string accessToken = objFacebookAccount.AccessToken;
                dynamic profile = null;
                dynamic friends = null;
                if (accessToken != null)
                {
                    fb.AccessToken = accessToken;
                    try
                    {
                        profile = fb.Get("v2.0/me");

                    }
                    catch (Exception ex)
                    {
                        string errormssg = ex.Message;
                        if (errormssg.Contains("changed the password"))
                        {
                            UpdateSocialprofileStatus(UserId, FbId);
                            objFacebookAccount.IsActive = 2;
                            UpdateFacebookAccount(objFacebookAccount);
                        }
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                        return "Token Expired";
                    }
                    try
                    {
                        friends = fb.Get("v2.0/me/friends");

                    }
                    catch (Exception ex)
                    {
                        string errormssg = ex.Message;
                        if (errormssg.Contains("changed the password"))
                        {
                            UpdateSocialprofileStatus(UserId, FbId);
                        }
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                        return "Token Expired";
                    }

                    try
                    {
                        //friendscount = Convert.ToInt16(friends["summary"]["total_count"].ToString());
                    }
                    catch (Exception ex)
                    {
                        //try
                        //{
                        //    dynamic frndscount = fb.Get("fql", new { q = "SELECT friend_count FROM user WHERE uid=me()" });

                        //    foreach (var friend in frndscount.data)
                        //    {
                        //        frndscount = friend.friend_count;
                        //    }
                        //    friendscount = Convert.ToInt16(frndscount);
                        //}
                        //catch (Exception exx)
                        //{
                        //    friendscount = 0;
                        //    logger.Error(exx.Message);
                        //    logger.Error(exx.StackTrace);
                        //}
                        friendscount = 0;
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                    }
                    if (objFacebookAccountRepository.checkFacebookUserExists(Convert.ToString(profile["id"]), Guid.Parse(UserId)))
                    {
                        //#region Add Facebook Feeds
                        //lstNewFacebookFeeds = AddFacebookFeeds(UserId, fb, profile);
                        //#endregion

                        //#region Add Facebook User Home
                        lstFacebookMessage = AddNewFacebookUserHome(UserId, fb, profile);
                        //#endregion
                        //#region Add Facebook User Inbox Message
                        //AddFacebookMessageWithPagination(UserId, fb, profile);
                        //#endregion
                        //ret = "Facebook info Updated Successfully";
                    }
                    else
                    {
                        //ret = "Account already Exist !";
                    }
                }
                return new JavaScriptSerializer().Serialize(lstFacebookMessage);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                Console.WriteLine(ex.StackTrace);
                return "Something Went Wrong";
            }
        }
        public string AddAllFbPagePost(string userid, string accesstoken, string profileid)
        {
            logger.Error("AddFbPagePost");
            logger.Error(userid + ", " + accesstoken + " , " + profileid);
            string ret = string.Empty;
            Api.Myfashionmarketer.Models.FacebookAccount _FacebookAccount = new FacebookAccount();
            Domain.Myfashion.Domain.FacebookAccount _facebookAccount = new Domain.Myfashion.Domain.FacebookAccount();

            try
            {
                string _nextPageDataUrl = string.Empty;
                FacebookClient fb = new FacebookClient();

                if (string.IsNullOrEmpty(accesstoken))
                {
                    try
                    {
                        _facebookAccount = (Domain.Myfashion.Domain.FacebookAccount)(new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(_FacebookAccount.getFacebookAccountDetailsById(userid, profileid), typeof(Domain.Myfashion.Domain.FacebookAccount)));

                        _facebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
                        Api.Myfashionmarketer.Models.FacebookAccountRepository _FacebookAccountRepository = new FacebookAccountRepository();

                        System.Collections.ArrayList lstFacebookAccounts = _FacebookAccountRepository.getAllFacebookAccounts();

                        Random _random = new Random();
                        var rnum = _random.Next(0, lstFacebookAccounts.Count - 1);
                        _facebookAccount = (Domain.Myfashion.Domain.FacebookAccount)lstFacebookAccounts[rnum];
                        fb.AccessToken = _facebookAccount.AccessToken;
                    }
                    catch { };
                }
                else
                    fb.AccessToken = accesstoken;


                dynamic post = null;
                try
                {
                    post = fb.Get("v2.0/" + profileid + "/posts");
                }
                catch (Exception ex)
                {
                    logger.Error("profileid +posts");
                    logger.Error(ex.Message);
                    logger.Error(ex.StackTrace);
                }
            //dynamic post1 = fb.Get("me/posts");
            _NextPageDataUrl:

                if (!string.IsNullOrEmpty(_nextPageDataUrl))
                {
                    fb = new FacebookClient();
                    post = fb.Get(_nextPageDataUrl);
                    _nextPageDataUrl = string.Empty;
                }
                foreach (var item in post["data"])
                {
                    #region
                    objFbPagePost.Id = Guid.NewGuid();
                    objFbPagePost.UserId = Guid.Parse(userid);
                    objFbPagePost.PageId = profileid;
                    objFbPagePost.PostId = item["id"].ToString();

                    objFbPagePost.PostDate = Convert.ToDateTime(item["created_time"]);
                    objFbPagePost.EntryDate = DateTime.Now;
                    try
                    {
                        objFbPagePost.Post = item["message"].ToString();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }
                    try
                    {
                        objFbPagePost.PictureUrl = item["picture"].ToString();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }

                    try
                    {
                        objFbPagePost.LinkUrl = item["link"].ToString();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }
                    try
                    {
                        objFbPagePost.IconUrl = item["icon"].ToString();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }
                    try
                    {
                        objFbPagePost.StatusType = item["status_type"].ToString();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }
                    try
                    {
                        objFbPagePost.Type = item["type"].ToString();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }
                    try
                    {
                        objFbPagePost.FromId = item["from"]["id"];

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }
                    try
                    {
                        objFbPagePost.FromName = item["from"]["name"];

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }
                    try
                    {
                        dynamic like = fb.Get("v2.0/" + objFbPagePost.PostId + "/likes?summary=1&limit=0");

                        objFbPagePost.Likes = Convert.ToInt32(like["summary"]["total_count"]);

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }

                    try
                    {
                        dynamic comment = fb.Get("v2.0/" + objFbPagePost.PostId + "/comments?summary=1&limit=0");

                        objFbPagePost.Comments = Convert.ToInt32(comment["summary"]["total_count"]);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }
                    try
                    {
                        dynamic shares = fb.Get("v2.0/" + objFbPagePost.PostId);
                        objFbPagePost.Shares = Convert.ToInt32(shares["shares"]["count"]);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                        logger.Error(ex.StackTrace);
                    }
                    try
                    {
                        if (!objFbPagePostRepository.IsPostExist(objFbPagePost))
                            objFbPagePostRepository.addFbPagePost(objFbPagePost);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                        logger.Error(ex.StackTrace);
                    }

                    try
                    {
                        AddAllFbPagePostComments(objFbPagePost.PostId, accesstoken, userid);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.StackTrace);
                        Console.WriteLine(ex.StackTrace);
                    }
                    try
                    {
                        AddAllFbPagePostLiker(objFbPagePost.PostId, accesstoken, userid);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.StackTrace);
                        Console.WriteLine(ex.StackTrace);
                    }
                    #endregion

                }


                try
                {
                    _nextPageDataUrl = post["paging"]["next"];
                    if (!string.IsNullOrEmpty(_nextPageDataUrl))
                        goto _NextPageDataUrl;

                }
                catch { };


            }
            catch (Exception ex)
            {
                logger.Error(ex.StackTrace);
                Console.WriteLine(ex.StackTrace);
            }
            return ret;
        }
        public string FacebookProfileDetails(string UserId, string ProfileId)
        {
            objFacebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
            Domain.Myfashion.Domain.FacebookAccount FacebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
            //objFacebookAccountRepository.getFacebookAccountDetailsById(ProfileId, Guid.Parse(UserId));
            if (objFacebookAccountRepository.checkFacebookUserExists(ProfileId, Guid.Parse(UserId)))
            {
                FacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(ProfileId, Guid.Parse(UserId));
            }
            else
            {
                FacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(ProfileId);
            }

            //long friendscount = 0;
            //FacebookClient fb = new FacebookClient();
            //fb.AccessToken = FacebookAccount.AccessToken;
            //System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
            //dynamic profile = fb.Get("me");
            //dynamic friends = fb.Get("me/friends");
            //try
            //{
            //    friendscount = Convert.ToInt16(friends["summary"]["total_count"].ToString());
            //}
            //catch (Exception)
            //{
            //    friendscount = 0;
            //}
            //objFacebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
            //objFacebookAccount.FbUserId = (Convert.ToString(profile["id"]));
            //objFacebookAccount.FbUserName = (Convert.ToString(profile["name"]));
            //objFacebookAccount.Friends = Convert.ToInt16(friendscount);
            //objFacebookAccount.EmailId = (Convert.ToString(profile["email"]));
            //objFacebookAccount.ProfileUrl = (Convert.ToString(profile["link"]));

            return new JavaScriptSerializer().Serialize(FacebookAccount);
        }
        public string AddFacebookPagesByUrl(string userid, string profileId, string groupId, string name)
        {
            logger.Error("AddFacebookPagesByUrllllllll");
            logger.Error(userid + ", " + profileId + ", " + groupId + ", " + name);
            string ret = string.Empty;
            FacebookAccount _FacebookAccount = new FacebookAccount();
            // string token = _FacebookAccount.getFbToken();
            string token = "CAAKYvwDVmnUBACyqUsvADWoAfBYTxi0kbz2gcw0sDWbBVJCXmIUG6rGez4BFSCE4hKV8eNE86eCD2iOwEWADvYuNlYupZCL4WUAGhFmRIZA6nTkdUOFeiUVHuri571QxhZA3YfSk5YkjhYy81pYtPj9FNM2mENtjCWRr5tN9zWZAKpUkw3gzsXRuEH9ZBTBwZD";
            try
            {
                #region fancount
                FacebookClient fb = new FacebookClient();
                fb.AccessToken = token;
                int fancountPage = 0;
                try
                {
                    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                    //dynamic fancount = fb.Get("fql", new { q = " SELECT fan_count FROM page WHERE page_id =" + profileId });
                    //foreach (var friend in fancount.data)
                    //{
                    //    fancountPage = Convert.ToInt32(friend.fan_count);
                    //}

                    //dynamic friends = fb.Get("v2.0/me/friends");
                    //fancountPage = Convert.ToInt16(friends["summary"]["total_count"].ToString());
                    dynamic friends = fb.Get("v2.0/" + profileId);
                    fancountPage = Convert.ToInt32(friends["likes"].ToString());

                }
                catch (Exception)
                {
                    fancountPage = 0;
                    fb.AccessToken = "CAAKYvwDVmnUBAFvCcZCQDL53q82jfR5mvgF2whNsFHgR4NmeSSUeRVpdEUpcVVgK1ERs2GZCNhJAwRHtq6MEWiRtBQnxBmZAML6dnwgpsCbjUmyT7ws6EKZBxuWbxhJqjeNCsxhac00b3L9Bf7LLlYa3PG94Uouj7vXZAZC6djZCme5BuszE3vibNFLKQqaLcgZD";
                    try
                    {
                        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                        //dynamic fancount = fb.Get("fql", new { q = " SELECT fan_count FROM page WHERE page_id =" + profileId });
                        //foreach (var friend in fancount.data)
                        //{
                        //    fancountPage = Convert.ToInt32(friend.fan_count);
                        //}
                        //dynamic friends = fb.Get("v2.0/me/friends");
                        //fancountPage = Convert.ToInt16(friends["summary"]["total_count"].ToString());
                        dynamic friends = fb.Get("v2.0/" + profileId);
                        fancountPage = Convert.ToInt32(friends["likes"].ToString());
                    }
                    catch (Exception ex)
                    {
                        logger.Error("fancount : " + ex.Message);
                    }
                }
                #endregion


                #region Add FacebookAccount
                objFacebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
                objFacebookAccount.Id = Guid.NewGuid();
                objFacebookAccount.FbUserId = profileId;
                objFacebookAccount.FbUserName = name;
                objFacebookAccount.AccessToken = "";
                objFacebookAccount.Friends = Convert.ToInt32(fancountPage);
                objFacebookAccount.EmailId = "";
                objFacebookAccount.Type = "Page";
                objFacebookAccount.ProfileUrl = "";
                objFacebookAccount.IsActive = 1;
                objFacebookAccount.UserId = Guid.Parse(userid);
                if (!objFacebookAccountRepository.checkFacebookUserExists(objFacebookAccount.FbUserId, objFacebookAccount.UserId))
                {
                    objFacebookAccountRepository.addFacebookUser(objFacebookAccount);
                }

                #endregion
                #region SocialProfile
                Domain.Myfashion.Domain.SocialProfile objSocialProfile = new Domain.Myfashion.Domain.SocialProfile();
                objSocialProfile.Id = Guid.NewGuid();
                objSocialProfile.ProfileType = "facebook_page";
                objSocialProfile.ProfileId = profileId;
                objSocialProfile.UserId = Guid.Parse(userid);
                objSocialProfile.ProfileDate = DateTime.Now;
                objSocialProfile.ProfileStatus = 1;
                if (!objSocialProfilesRepository.checkUserProfileExist(objSocialProfile))
                {
                    objSocialProfilesRepository.addNewProfileForUser(objSocialProfile);
                }
                #endregion
                #region Add TeamMemberProfile
                Domain.Myfashion.Domain.Team objTeam = objTeamRepository.GetTeamByGroupId(Guid.Parse(groupId));
                Domain.Myfashion.Domain.TeamMemberProfile objTeamMemberProfile = new Domain.Myfashion.Domain.TeamMemberProfile();
                objTeamMemberProfile.Id = Guid.NewGuid();
                objTeamMemberProfile.TeamId = objTeam.Id;
                objTeamMemberProfile.Status = 1;
                objTeamMemberProfile.ProfileType = "facebook_page";
                objTeamMemberProfile.StatusUpdateDate = DateTime.Now;
                objTeamMemberProfile.ProfileId = profileId;
                if (!objTeamMemberProfileRepository.checkTeamMemberProfile(objTeamMemberProfile.TeamId, objTeamMemberProfile.ProfileId))
                {
                    objTeamMemberProfileRepository.addNewTeamMember(objTeamMemberProfile);
                }

                #endregion

                try
                {
                    logger.Error(" Token:" + token);
                    if (token != null)
                    {
                        FacebookClient _FacebookClient = new FacebookClient();
                        _FacebookClient.AccessToken = token;
                        dynamic profile = null;

                        try
                        {
                            profile = fb.Get("v2.0/" + profileId);
                            logger.Error("AddFacebookPagesByUrl Token 1");
                        }
                        catch (Exception ex)
                        {
                            logger.Error("fb.Get(profileId)");
                            logger.Error(ex.StackTrace);
                            Console.WriteLine(ex.StackTrace);
                            try
                            {
                                fb.AccessToken = "CAAKYvwDVmnUBAFvCcZCQDL53q82jfR5mvgF2whNsFHgR4NmeSSUeRVpdEUpcVVgK1ERs2GZCNhJAwRHtq6MEWiRtBQnxBmZAML6dnwgpsCbjUmyT7ws6EKZBxuWbxhJqjeNCsxhac00b3L9Bf7LLlYa3PG94Uouj7vXZAZC6djZCme5BuszE3vibNFLKQqaLcgZD";
                                profile = fb.Get("v2.0/" + profileId);
                                logger.Error("AddFacebookPagesByUrl Token 2");
                            }
                            catch (Exception ex2)
                            {
                                try
                                {
                                    fb.AccessToken = "CAAKYvwDVmnUBAAR2O9hxFkHzfNG8H6KbQLaiGFMRshJkbttdzhDeprklcb1yaV0rwtC7N8Xz1rsL1cykiRv2ouXtBUFxvOZCNnpFELnQGFV8jGUWjm1GYsZA40IKAORLGoAcSaa2lJkuuSoLBksB8LFPHI4cqW7VVqxgDwZCRwObxqR4Qp9QEDHxa7j1yoZD";
                                    profile = fb.Get("v2.0/" + profileId);
                                    logger.Error("AddFacebookPagesByUrl Token 3");
                                }
                                catch (Exception ex3)
                                {
                                    try
                                    {
                                        fb.AccessToken = "CAAKYvwDVmnUBAFtZB8pvVrqYQonmq7MD90oNdoipDc0Te4onP2XlbZAYT4bzOZAKTr8jdhw0P1PclgLOtVxJ9g2qx4vxZAzh2CXqXAZBZAZBwkgWIVjc2B4rcXAp6O5B3gXqd8Ko5ITL9VCZCMOkMZCPc1hBsp0n8zgPt6e3Dd0vaodPBS8nMz7RD";
                                        profile = fb.Get("v2.0/" + profileId);
                                        logger.Error("AddFacebookPagesByUrl Token 4");
                                    }
                                    catch (Exception ex4)
                                    {
                                        try
                                        {
                                            fb.AccessToken = "CAAKYvwDVmnUBALvjTAKIrVKnL719aVDB7BmMRn7e08ySJQwHYtLDZBBjx5yBZBaMeJ04lIT8bCzX2A685YLXR9d8PukZCBZA2LiwZAmj6qhMZC8F0od7NBircdMZAOZAD1xukXDhd24RQRvVk9GyJNRmmGTiZAhMJzXBVczH3TlYb37qi8FRXfTGDRTZAyxjyYSt8ZD";
                                            profile = fb.Get("v2.0/" + profileId);
                                            logger.Error("AddFacebookPagesByUrl Token 5");
                                        }
                                        catch (Exception ex5)
                                        {
                                            logger.Error("Finally :" + fb.AccessToken);
                                            logger.Error(ex5.Message);
                                            logger.Error(ex5.Message);
                                        }
                                    }
                                }
                            }
                        }

                        try
                        {
                            //Edited by Sumit Gupta [10/27/2014]
                            //AddFacebookStats(userid, _FacebookClient, profile);
                            AddFacebookStats(userid, fb, profile);
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.StackTrace);
                            Console.WriteLine(ex.StackTrace);
                        }

                        //Edited by Sumit Gupta [10/29/2014]
                        AddFbPagePost(userid, fb.AccessToken, profileId); // AddFbPagePost(userid, token, profileId);


                    }
                }
                catch (Exception ex)
                {
                    logger.Error("dynamic profile");
                    logger.Error(ex.StackTrace);
                    Console.WriteLine(ex.StackTrace);
                }



            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                Console.WriteLine(ex.StackTrace);
            }
            return ret;
        }
        public string AddFacebookGroupsInfo(string userid, string fbgroupid, string accessToken, string groupId, string email, string fbgroupname)
        {
            string ret = string.Empty;
            try
            {
                FacebookClient fb = new FacebookClient();

                if (accessToken != null)
                {
                    fb.AccessToken = accessToken;
                    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                    dynamic profile = fb.Get("v2.0/me");

                    #region Add FacebookAccount
                    objFacebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
                    objFacebookAccount.Id = Guid.NewGuid();
                    objFacebookAccount.FbUserId = fbgroupid;
                    objFacebookAccount.FbUserName = fbgroupname;
                    objFacebookAccount.AccessToken = accessToken;
                    objFacebookAccount.Friends = 0;
                    objFacebookAccount.EmailId = email;
                    objFacebookAccount.Type = "group";
                    objFacebookAccount.ProfileUrl = (Convert.ToString(profile["link"]));
                    objFacebookAccount.IsActive = 2;
                    objFacebookAccount.UserId = Guid.Parse(userid);
                    if (!objFacebookAccountRepository.checkFacebookUserExists(objFacebookAccount.FbUserId, objFacebookAccount.UserId))
                    {
                        objFacebookAccountRepository.addFacebookUser(objFacebookAccount);
                    }
                    //objFacebookAccountRepository.addFacebookUser(objFacebookAccount);
                    #endregion
                    #region SocialProfile
                    Domain.Myfashion.Domain.SocialProfile objSocialProfile = new Domain.Myfashion.Domain.SocialProfile();
                    objSocialProfile.Id = Guid.NewGuid();
                    objSocialProfile.ProfileType = "facebook_group";
                    objSocialProfile.ProfileId = fbgroupid;
                    objSocialProfile.UserId = Guid.Parse(userid);
                    objSocialProfile.ProfileDate = DateTime.Now;
                    objSocialProfile.ProfileStatus = 1;
                    if (!objSocialProfilesRepository.checkUserProfileExist(objSocialProfile))
                    {
                        objSocialProfilesRepository.addNewProfileForUser(objSocialProfile);
                    }
                    #endregion
                    #region Add TeamMemberProfile
                    Domain.Myfashion.Domain.Team objTeam = objTeamRepository.GetTeamByGroupId(Guid.Parse(groupId));
                    Domain.Myfashion.Domain.TeamMemberProfile objTeamMemberProfile = new Domain.Myfashion.Domain.TeamMemberProfile();
                    objTeamMemberProfile.Id = Guid.NewGuid();
                    objTeamMemberProfile.TeamId = objTeam.Id;
                    objTeamMemberProfile.Status = 1;
                    objTeamMemberProfile.ProfileType = "facebook_group";
                    objTeamMemberProfile.StatusUpdateDate = DateTime.Now;
                    objTeamMemberProfile.ProfileId = fbgroupid;
                    if (!objTeamMemberProfileRepository.checkTeamMemberProfile(objTeamMemberProfile.TeamId, objTeamMemberProfile.ProfileId))
                    {
                        objTeamMemberProfileRepository.addNewTeamMember(objTeamMemberProfile);
                    }

                    #endregion
                    #region Add Facebook Feeds
                    AddFacebookFeeds(userid, fb, profile);
                    #endregion

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            return ret;
        }
        public string GetFacebookPageData(string FbId, string UserId)
        {
            string ret = string.Empty;
            try
            {
                Domain.Myfashion.Domain.FacebookAccount objFacebookAccount = new Domain.Myfashion.Domain.FacebookAccount();
                if (objFacebookAccountRepository.checkFacebookUserExists(FbId, Guid.Parse(UserId)))
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FbId, Guid.Parse(UserId));
                }
                else
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FbId);
                }
                FacebookClient fb = new FacebookClient();
                string profileId = string.Empty;
                string accessToken = objFacebookAccount.AccessToken;
                dynamic profile = null;
                dynamic friends = null;
                if (accessToken != null)
                {
                    fb.AccessToken = accessToken;
                    int fancountPage = 0;
                    try
                    {
                        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                        friends = fb.Get("v2.0/" + FbId);
                        fancountPage = Convert.ToInt16(friends["likes"].ToString());
                        objFacebookAccount.Friends = fancountPage;
                    }
                    catch (Exception)
                    {
                        fancountPage = 0;
                    }
                    try
                    {
                        profile = fb.Get("v2.0/me");

                    }
                    catch (Exception ex)
                    {
                        string errormssg = ex.Message;
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                        return "Token Expired";
                    }
                    if (objFacebookAccountRepository.checkFacebookUserExists(Convert.ToString(profile["id"]), Guid.Parse(UserId)))
                    {
                        #region Update FacebookAccount
                        UpdateFacebookAccount(objFacebookAccount);
                        #endregion
                        #region UpdateTeammemberprofile
                        Domain.Myfashion.Domain.TeamMemberProfile objTeamMemberProfile = new Domain.Myfashion.Domain.TeamMemberProfile();
                        objTeamMemberProfile.ProfileName = objFacebookAccount.FbUserName;
                        objTeamMemberProfile.ProfilePicUrl = "http://graph.facebook.com/" + objFacebookAccount.FbUserId + "/picture?type=small";
                        objTeamMemberProfile.ProfileId = objFacebookAccount.FbUserId;
                        objTeamMemberProfileRepository.updateTeamMemberbyprofileid(objTeamMemberProfile);
                        #endregion

                        //getUserNotifications(UserId,fb,profile);

                        ret = "Facebook page info Updated Successfully";
                    }
                    else
                    {
                        ret = "Account already Exist !";
                    }
                }
                return new JavaScriptSerializer().Serialize(ret);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                Console.WriteLine(ex.StackTrace);
                return "Something Went Wrong";
            }
        }
        public Domain.Myfashion.Domain.FacebookAccount getFacebookAccountDetailsById(string Fbuserid)
        {

            Domain.Myfashion.Domain.FacebookAccount result = new Domain.Myfashion.Domain.FacebookAccount();
            //Creates a database connection and opens up a session
            using (NHibernate.ISession session = SessionFactory.GetNewSession())
            {
                //After Session creation, start Transaction.
                using (NHibernate.ITransaction transaction = session.BeginTransaction())
                {

                    // proceed action, to get all Facebook Account of User by UserId(Guid) and FbUserId(string).
                    List<Domain.Myfashion.Domain.FacebookAccount> objlstfb = session.CreateQuery("from FacebookAccount where FbUserId = :Fbuserid ")
                            .SetParameter("Fbuserid", Fbuserid)
                       .List<Domain.Myfashion.Domain.FacebookAccount>().ToList<Domain.Myfashion.Domain.FacebookAccount>();
                    if (objlstfb.Count > 0)
                    {
                        result = objlstfb[0];
                    }
                    return result;
                }//End Transaction
            }//End session
        }
        public string SheduleFacebookGroupMessage(string FacebookId, string UserId, string sscheduledmsgguid)
        {
            string str = string.Empty;
            try
            {
                objScheduledMessage = objScheduledMessageRepository.GetScheduledMessageDetails(Guid.Parse(sscheduledmsgguid));
                GroupScheduleMessageRepository grpschedulemessagerepo = new GroupScheduleMessageRepository();
                Domain.Myfashion.Domain.GroupScheduleMessage _GroupScheduleMessage = grpschedulemessagerepo.GetScheduleMessageId(objScheduledMessage.Id);
                if (objFacebookAccountRepository.checkFacebookUserExists(FacebookId, Guid.Parse(UserId)))
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FacebookId, Guid.Parse(UserId));
                }
                else
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FacebookId);
                }
                if (objFacebookAccount != null)
                {
                    FacebookClient fbclient = new FacebookClient(objFacebookAccount.AccessToken);
                    var args = new Dictionary<string, object>();
                    args["message"] = objScheduledMessage.ShareMessage;
                    string imagepath = objScheduledMessage.PicUrl;

                    var facebookpost = "";
                    try
                    {
                        if (!string.IsNullOrEmpty(imagepath))
                        {
                            var media = new FacebookMediaObject
                            {
                                FileName = "filename",
                                ContentType = "image/jpeg"
                            };
                            byte[] img = System.IO.File.ReadAllBytes(imagepath);
                            media.SetValue(img);
                            args["source"] = media;
                            facebookpost = fbclient.Post("v2.0/" + _GroupScheduleMessage.GroupId + "/photos", args).ToString();
                        }
                        else
                        {
                            facebookpost = fbclient.Post("v2.0/" + _GroupScheduleMessage.GroupId + "/feed", args).ToString();
                        }
                    }
                    catch (Exception ex)
                    {
                        objFacebookAccountRepository = new FacebookAccountRepository();
                        objFacebookAccount.IsActive = 2;
                        objFacebookAccountRepository.updateFacebookUserStatus(objFacebookAccount);
                        Domain.Myfashion.Domain.SocialProfile objSocialProfile = new Domain.Myfashion.Domain.SocialProfile();
                        SocialProfilesRepository objSocialProfilesRepository = new SocialProfilesRepository();
                        string errormsg = ex.Message;
                        if (errormsg.Contains("access token"))
                        {
                            objSocialProfile.UserId = objFacebookAccount.UserId;
                            objSocialProfile.ProfileId = objFacebookAccount.FbUserId;
                            objSocialProfile.ProfileStatus = 2;
                            objSocialProfilesRepository.updateSocialProfileStatus(objSocialProfile);
                        }
                        Console.WriteLine(ex.Message);
                        str = ex.Message;
                    }

                    if (!string.IsNullOrEmpty(facebookpost))
                    {
                        str = "Message post on facebook for Id :" + objFacebookAccount.FbUserId + " and Message: " + objScheduledMessage.ShareMessage;
                        ScheduledMessage schmsg = new ScheduledMessage();
                        schmsg.UpdateScheduledMessageByMsgId(Guid.Parse(sscheduledmsgguid));
                    }
                }
                else
                {
                    str = "facebook account not found for id" + objScheduledMessage.ProfileId;
                }
            }
            catch (Exception ex)
            {
                str = ex.Message;
            }
            return str;
        }