Exemplo n.º 1
0
        public TranslationUserAccount LoadUserAccount(SPWeb spWeb, string sourceLanguage)
        {
            string                 login                  = string.Empty;
            string                 password               = string.Empty;
            string                 url                    = string.Empty;
            List <string>          userProfileList        = new List <string>();
            TranslationUserAccount translationUserAccount = new TranslationUserAccount();
            SPUser                 currentUser            = spWeb.CurrentUser;

            string lang = string.Empty;

            if (string.IsNullOrEmpty(sourceLanguage))
            {
                if (HttpContext.Current != null)
                {
                    lang = Utilities.GetLanguageCode(HttpContext.Current);
                }
                else
                {
                    lang = sourceLanguage;
                }
            }

            if (HttpRuntime.Cache.Get("OceanikTranslationAccount " + spWeb.ID.ToString() + currentUser.LoginName + "-" + lang) == null)
            {
                try
                {
                    SPUser rootSiteCurrentUser = spWeb.SiteUsers.GetByID(currentUser.ID);
                    string systranAccount      = string.Empty;

                    //if (HttpRuntime.Cache.Get("OceanikTranslationAccount " + currentUser.LoginName) == null)
                    //{
                    if (spWeb.Lists["Translation Accounts"] != null)
                    {
                        SPList accountsSystranList = spWeb.Lists["Translation Accounts"];

                        if (HttpRuntime.Cache.Get("OceanikTranslationAccountId " + spWeb.ID.ToString() + currentUser.LoginName) == null)
                        {
                            foreach (SPItem account in accountsSystranList.Items)
                            {
                                if (account["Groups"] != null)
                                {
                                    var o = account["Groups"];

                                    SPFieldUserValueCollection groupsForThisAccount = (SPFieldUserValueCollection)account["Groups"];

                                    foreach (SPFieldUserValue groupForThisAccount in groupsForThisAccount)
                                    {
                                        try
                                        {
                                            foreach (SPGroup groupForTheRootSiteCurrentUser in rootSiteCurrentUser.Groups)
                                            {
                                                if (groupForTheRootSiteCurrentUser.Name == groupForThisAccount.LookupValue)
                                                {
                                                    if (accountsSystranList.Fields.ContainsField("Account"))
                                                    {
                                                        systranAccount = account["Account"].ToString();
                                                        HttpRuntime.Cache.Remove("OceanikTranslationAccountId " + spWeb.ID.ToString() + currentUser.LoginName);
                                                        HttpRuntime.Cache.Add("OceanikTranslationAccountId " + spWeb.ID.ToString() + currentUser.LoginName, account.ID, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);



                                                        //if (accountsSystranList.Fields.ContainsField("Profiles"))
                                                        //{
                                                        //    string profilesFieldValue = account["Profiles"].ToString();
                                                        //    if (profilesFieldValue != string.Empty)
                                                        //    {
                                                        //        Regex lookupRegex = new Regex("[0-9]+;#(?<value>([^;]+))");
                                                        //        foreach (Match profileName in lookupRegex.Matches(profilesFieldValue))
                                                        //        {
                                                        //            userProfileArrayList.Add(profileName.Groups["value"].Value);
                                                        //        }
                                                        //    }
                                                        //}
                                                    }

                                                    break;
                                                }
                                            }

                                            if (userProfileList.Count > 0)
                                            {
                                                break;
                                            }
                                        }
                                        catch
                                        {
                                        }
                                    }

                                    if (userProfileList.Count > 0)
                                    {
                                        break;
                                    }
                                }
                            }
                        }

                        if (accountsSystranList.Fields.ContainsField("Account") && accountsSystranList.Fields.ContainsField("Password") && accountsSystranList.Fields.ContainsField("Url"))
                        {
                            string     accountId   = Convert.ToString(HttpRuntime.Cache.Get("OceanikTranslationAccountId " + spWeb.ID.ToString() + currentUser.LoginName));
                            SPListItem accountItem = accountsSystranList.Items.Cast <SPListItem>().First(sPListItemTmp => sPListItemTmp.ID.ToString() == accountId);

                            if (accountItem["Account"] != null && accountItem["Password"] != null && accountItem["Url"] != null)
                            {
                                login    = accountItem["Account"].ToString();
                                password = EncryptionUtility.Encrypt(accountItem["Password"].ToString());
                                url      = accountItem["Url"].ToString();

                                string languageItemSource = string.Empty;

                                if (string.IsNullOrEmpty(sourceLanguage))
                                {
                                    if (HttpContext.Current != null)
                                    {
                                        languageItemSource = Utilities.GetLanguageCode(HttpContext.Current);
                                    }
                                    else
                                    {
                                        languageItemSource = sourceLanguage;
                                    }
                                }

                                //foreach (LanguageItem languageItemSource in Dictionaries.Instance.VisibleLanguages)
                                {
                                    foreach (LanguageItem languageItemDestination in Dictionaries.Instance.VisibleLanguages)
                                    {
                                        if (languageItemSource != languageItemDestination.LanguageDestination)
                                        {
                                            List <string> userProfileListTmp = GetTemplates(ConvertLanguageCode2LettersTo3Letters(languageItemSource),
                                                                                            ConvertLanguageCode2LettersTo3Letters(languageItemDestination.LanguageDestination), url, login, password);

                                            foreach (string languageItemName in userProfileListTmp)
                                            {
                                                if (!userProfileList.Contains(languageItemName))
                                                {
                                                    userProfileList.Add(languageItemName);
                                                }
                                            }
                                        }
                                    }
                                }

                                //userProfileList = GetTemplates("eng", "fra", url, login, password);
                            }
                        }
                    }
                    //}
                }
                catch
                { }

                translationUserAccount.Login    = login;
                translationUserAccount.Password = password;
                translationUserAccount.Url      = url;
                translationUserAccount.Profiles = userProfileList;

                HttpRuntime.Cache.Remove("OceanikTranslationAccount " + spWeb.ID.ToString() + currentUser.LoginName + "-" + lang);
                HttpRuntime.Cache.Add("OceanikTranslationAccount " + spWeb.ID.ToString() + currentUser.LoginName + "-" + lang, translationUserAccount, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            else
            {
                translationUserAccount = (TranslationUserAccount)(HttpRuntime.Cache.Get("OceanikTranslationAccount " + spWeb.ID.ToString() + currentUser.LoginName + "-" + lang));
            }

            return(translationUserAccount);
        }