/// <summary>
 /// Initializes a new instance of the <see cref="SpsDictionary"/> class.
 /// </summary>
 /// <param name="web">
 /// SPWeb containing the dictionary
 /// </param>
 /// /// <param name="automaticTranslationPlugin">
 /// Set web service to use if loaded
 /// </param>
 public SpsDictionary(SPWeb web, IAutomaticTranslation automaticTranslationPlugin)
 {
     _automaticTranslationPlugin = automaticTranslationPlugin;
     Web    = web;
     WebUrl = Web.Site.Url;
     List   = Web.GetList(ListName);
 }
 public SpsDictionary(IAutomaticTranslation automaticTranslationPlugin)
 {
     _automaticTranslationPlugin = automaticTranslationPlugin;
     WebUrl = SPContext.Current.Site.Url;
     Web    = SPContext.Current.Site.OpenWeb();
     List   = Web.GetList(ListName);
 }
 public HttpApplicationEvents(TranslatorHelper.TranslatorHelper translatorHelper, License license, IAutomaticTranslation automaticTranslationPlugin)
 {
     _license = license;
     _automaticTranslationPlugin   = automaticTranslationPlugin;
     _translatorHelper             = translatorHelper;
     _debugTranslatorHelper        = new DebugTranslatorHelper(_translatorHelper);
     _currentTranslatorHelperToUse = _translatorHelper;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="SpsDictionary"/> class.
        /// </summary>
        /// <param name="webUrl">
        ///   url of the SPWeb containing the dictonary
        /// </param>
        /// <param name="automaticTranslationPlugin">
        /// Set web service to use if loaded
        /// </param>
        public SpsDictionary(string webUrl, IAutomaticTranslation automaticTranslationPlugin)
        {
            _automaticTranslationPlugin = automaticTranslationPlugin;
            using (var sysSite = new SPSite(webUrl))
            {
                Web = sysSite.OpenWeb();
            }

            WebUrl = webUrl;
            List   = Web.GetList(ListName);
        }
        const string _profileIdFieldName = "Profile ID";          //"Profile_X0020_ID";

        public static void CreateClonedMultilingualItem(IAutomaticTranslation automaticTranslationPlugin, SPWeb siteWeb, string listId, string url, string itemId, string lang,
                                                        bool toTranslate, bool discussionBoardEdition)
        {
            try
            {
                using (var currentSite = new SPSite(url))
                    using (SPWeb web = currentSite.OpenWeb())
                    {
                        web.AllowUnsafeUpdates = true;
                        SPList currentList = web.Lists[new Guid(listId)];

                        //SPItem currentItem;

                        //if (currentList.BaseTemplate == SPListTemplateType.DiscussionBoard)
                        //{
                        //    currentItem = currentList.Folders.
                        //}

                        SPListItem currentItem = currentList.GetItemById(Convert.ToInt32(itemId));

                        string currentItemLanguage = string.Empty;

                        if (currentList.Fields.ContainsField("SharePoint_Item_Language"))
                        {
                            currentItemLanguage = (string)currentItem["SharePoint_Item_Language"];
                            currentItemLanguage = currentItemLanguage.Replace("(SPS_LNG_ALL)", string.Empty).Replace("SPS_LNG_", string.Empty);
                        }

                        var displayedLang = new ArrayList();

                        if (lang == "ALL")
                        {
                            foreach (LanguageItem languageItem in BaseStaticOverride <Dictionaries> .Instance.VisibleLanguages)
                            {
                                if (currentItemLanguage != languageItem.LanguageDestination)
                                {
                                    displayedLang.Add(languageItem.LanguageDestination);
                                }
                            }
                        }
                        else
                        {
                            displayedLang.Add(lang);
                        }

                        foreach (string langTmp in displayedLang)
                        {
                            lang = langTmp;

                            if (currentList.Fields.ContainsField(lang + " version"))
                            {
                                bool       isLinkedItemExist = false;
                                SPListItem cloneItem         = null;
                                if (currentItem[lang + " version"] != null)
                                {
                                    string linkedItemId = currentItem[lang + " version"].ToString();
                                    var    query        = new SPQuery
                                    {
                                        Query = "<Where><Eq><FieldRef Name='ID'/>" +
                                                "<Value Type='Number'>" +
                                                linkedItemId.Remove(linkedItemId.IndexOf(";")) +
                                                "</Value></Eq></Where>",
                                        QueryThrottleMode = SPQueryThrottleOption.Override
                                    };

                                    if ((discussionBoardEdition) && (currentItem.Folder == null))
                                    {
                                        query.ViewAttributes = "Scope='Recursive'";
                                    }

                                    SPListItemCollection collListItems = currentList.GetItems(query);
                                    if (collListItems.Count > 0)
                                    {
                                        foreach (SPListItem listItemTmp in collListItems)
                                        {
                                            string listItemTmpLanguage = listItemTmp["SharePoint_Item_Language"].ToString();
                                            if (listItemTmpLanguage == "SPS_LNG_" + lang)
                                            {
                                                isLinkedItemExist = true;
                                                cloneItem         = listItemTmp;
                                            }
                                        }
                                    }
                                }

                                bool toOverwrite = false;
                                if (currentItem["ItemsAutoCreation"] != null)
                                {
                                    if (currentItem["ItemsAutoCreation"].ToString() == "Overwrite/Create items for all languages")
                                    {
                                        toOverwrite = true;
                                    }
                                }

                                if ((!isLinkedItemExist) || toOverwrite)
                                {
                                    string cloneItemName;

                                    if (currentList.BaseType == SPBaseType.DocumentLibrary)
                                    {
                                        cloneItemName = currentItem.File.Name.Remove(currentItem.File.Name.LastIndexOf(".")) + "_" + lang + currentItem.Name.Substring(currentItem.Name.LastIndexOf("."));
                                    }
                                    else
                                    {
                                        cloneItemName = currentItem.Name + "_" + lang;
                                    }

                                    if (!isLinkedItemExist)
                                    {
                                        if (currentList.BaseType == SPBaseType.DocumentLibrary)
                                        {
                                            byte[] fileBytes = currentItem.File.OpenBinary();
                                            SPFile file      = currentList.RootFolder.Files.Add(currentList.RootFolder.Url + "/" + cloneItemName, fileBytes);
                                            cloneItem                      = file.Item;
                                            cloneItem["Title"]             = cloneItemName;
                                            cloneItem["ItemsAutoCreation"] = "None";
                                            cloneItem.SystemUpdate(false);
                                        }
                                        else
                                        if (currentList.BaseTemplate == SPListTemplateType.DiscussionBoard)
                                        {
                                            string discussionParentId = string.Empty;


                                            //if (currentUrl.IndexOf("&DiscussionParentID=") > -1)
                                            //{
                                            //    discussionParentId = currentUrl.Substring(currentUrl.IndexOf("=", currentUrl.IndexOf("&DiscussionParentID=")) + 1);
                                            //    if (discussionParentId.IndexOf("&") > -1)
                                            //        discussionParentId = discussionParentId.Remove(discussionParentId.IndexOf("&"));
                                            //}

                                            //if (discussionParentId == string.Empty)
                                            //if (web.GetFolder(currentItem.Url).ParentFolder == null)
                                            if (currentItem.Folder != null)
                                            {
                                                cloneItem = SPUtility.CreateNewDiscussion(currentList.Items, cloneItemName); //  currentList.Items.Add();
                                            }
                                            else
                                            {
                                                SPFolder   parentFolder     = web.GetFolder(currentItem.Url).ParentFolder;
                                                SPListItem parentDiscussion = web.GetFolder(currentItem.Url).ParentFolder.Item;

                                                //SPListItem parentDiscussion = currentList.GetItemById(Convert.ToInt32(discussionParentId));

                                                if ((parentDiscussion["SharePoint_Group_Language"] != null) && (Convert.ToInt32(parentDiscussion["SharePoint_Group_Language"]) != 0))
                                                {
                                                    var query = new SPQuery();
                                                    query.Query = "<Where><And><Eq><FieldRef Name=\"SharePoint_Item_Language\" /><Value Type=\"Text\">SPS_LNG_" + lang + "</Value></Eq>" +
                                                                  "<Eq><FieldRef Name='SharePoint_Group_Language'/>" +
                                                                  "<Value Type='Number'>" + parentDiscussion["SharePoint_Group_Language"].ToString() + "</Value></Eq></And></Where>";

                                                    SPListItemCollection clonedDiscussionsCollection = currentList.GetItems(query);

                                                    foreach (SPListItem clonedDiscussion in clonedDiscussionsCollection)
                                                    {
                                                        cloneItem = SPUtility.CreateNewDiscussionReply(clonedDiscussion);
                                                        break;
                                                    }
                                                }
                                            }

                                            cloneItem[SPBuiltInFieldId.Body]        = cloneItemName;
                                            cloneItem[SPBuiltInFieldId.TrimmedBody] = currentItem[SPBuiltInFieldId.TrimmedBody];

                                            cloneItem["Title"]             = cloneItemName;
                                            cloneItem["ItemsAutoCreation"] = "None";

                                            cloneItem.SystemUpdate(false);
                                        }
                                        else
                                        {
                                            cloneItem = currentList.Items.Add();

                                            cloneItem["Title"]             = cloneItemName;
                                            cloneItem["ItemsAutoCreation"] = "None";

                                            cloneItem.SystemUpdate(false);
                                        }
                                    }

                                    var oQuery = new SPQuery
                                    {
                                        QueryThrottleMode = SPQueryThrottleOption.Override
                                    };

                                    if (currentList.BaseType == SPBaseType.DocumentLibrary)
                                    {
                                        oQuery.Query = "<Where><Eq><FieldRef Name='FileLeafRef'/>" +
                                                       "<Value Type='File'>" + cloneItemName + "</Value></Eq></Where>";
                                    }
                                    else
                                    {
                                        oQuery.Query = "<Where><Eq><FieldRef Name='Title'/>" +
                                                       "<Value Type='Text'>" + cloneItemName + "</Value></Eq></Where>";
                                    }

                                    if (currentList.Fields.ContainsField("MetadataToDuplicate"))
                                    {
                                        if (currentItem["MetadataToDuplicate"] != null)
                                        {
                                            string[] metadataToUpdateArray = ((string)currentItem["MetadataToDuplicate"]).Split(';');
                                            foreach (string t in metadataToUpdateArray)
                                            {
                                                if (currentList.Fields.ContainsField(t))
                                                {
                                                    cloneItem[t] = currentItem[t];
                                                    cloneItem.SystemUpdate(false);
                                                }
                                            }
                                        }
                                    }

                                    bool isBodyExist = currentList.Fields.Cast <SPField>().Any(sPFieldTmp => sPFieldTmp.InternalName == "Body");

                                    bool isTitleExist = currentList.Fields.Cast <SPField>().Any(sPFieldTmp => sPFieldTmp.InternalName == "Title");

                                    bool isProfileExist = currentList.Fields.Cast <SPField>().Any(sPFieldTmp => sPFieldTmp.ToString() == _profileFieldName);

                                    string profileId = string.Empty;

                                    if (isProfileExist)
                                    {
                                        profileId = GetProfileId(web, currentItem);
                                    }

                                    TranslationUserAccount translationUserAccount = null;

                                    if (automaticTranslationPlugin != null)
                                    {
                                        translationUserAccount = automaticTranslationPlugin.LoadUserAccount(web.Site.RootWeb, currentItemLanguage);
                                    }

                                    if (isBodyExist && (currentItem["Body"] != null))
                                    {
                                        string bodyContentToTranslate = currentItem["Body"].ToString();
                                        string bodyContentTranslated  = bodyContentToTranslate;

                                        if ((currentItem["AutoTranslation"] != null) && (currentItemLanguage != lang))
                                        {
                                            if ((currentItem["AutoTranslation"].ToString() == "Yes") || toTranslate)
                                            {
                                                if (automaticTranslationPlugin != null)
                                                {
                                                    bodyContentTranslated  = automaticTranslationPlugin.TranslateText(bodyContentToTranslate, currentItemLanguage, lang, profileId, translationUserAccount, true);
                                                    bodyContentTranslated += " " + HttpRuntime.Cache["AlphamosaikMessageForAutotranslateText"];
                                                }
                                                else
                                                {
                                                    bodyContentTranslated = bodyContentToTranslate;
                                                }

                                                if ((currentList.BaseTemplate == SPListTemplateType.DiscussionBoard) && (cloneItem[SPBuiltInFieldId.TrimmedBody] != null))
                                                {
                                                    if (automaticTranslationPlugin != null)
                                                    {
                                                        cloneItem[SPBuiltInFieldId.TrimmedBody] = automaticTranslationPlugin.TranslateText(currentItem[SPBuiltInFieldId.TrimmedBody].ToString(), currentItemLanguage,
                                                                                                                                           lang, profileId, translationUserAccount, true);

                                                        if (!string.IsNullOrEmpty(cloneItem[SPBuiltInFieldId.TrimmedBody].ToString()))
                                                        {
                                                            cloneItem[SPBuiltInFieldId.TrimmedBody] += " " + HttpRuntime.Cache["AlphamosaikMessageForAutotranslateText"];
                                                        }
                                                    }
                                                    else
                                                    {
                                                        cloneItem[SPBuiltInFieldId.TrimmedBody] = currentItem[SPBuiltInFieldId.TrimmedBody];
                                                    }
                                                }
                                            }
                                        }

                                        if (currentList.BaseTemplate == SPListTemplateType.DiscussionBoard)
                                        {
                                            CopyDiscussionAttachments(currentItem, cloneItem);
                                        }

                                        cloneItem["Body"] = bodyContentTranslated;
                                        cloneItem.SystemUpdate(false);
                                    }

                                    if (currentList.BaseType != SPBaseType.DocumentLibrary)
                                    {
                                        if (isTitleExist && (currentItem["Title"] != null))
                                        {
                                            string titleContentToTranslate = currentItem["Title"].ToString();
                                            string titleContentTranslated  = titleContentToTranslate;
                                            if (currentItem["AutoTranslation"] != null)
                                            {
                                                if (automaticTranslationPlugin != null && ((currentItem["AutoTranslation"].ToString() == "Yes") || toTranslate))
                                                {
                                                    titleContentTranslated = automaticTranslationPlugin.TranslateText(titleContentTranslated, currentItemLanguage, lang, profileId, translationUserAccount, false);
                                                }
                                            }

                                            cloneItem["Title"] = titleContentTranslated;
                                            cloneItem.SystemUpdate(false);
                                        }
                                    }
                                    else
                                    {
                                        if (currentItem["AutoTranslation"] != null)
                                        {
                                            if ((currentItem["AutoTranslation"].ToString() == "Yes") || toTranslate)
                                            {
                                                if (currentList.BaseTemplate == (SPListTemplateType)(850))
                                                {
                                                    string pageContentToTranslate = string.Empty;

                                                    if (currentItem["PublishingPageContent"] != null)
                                                    {
                                                        pageContentToTranslate = currentItem["PublishingPageContent"].ToString();
                                                    }

                                                    if (currentList.Fields.ContainsField("PublishingPageContent"))
                                                    {
                                                        if (automaticTranslationPlugin != null)
                                                        {
                                                            cloneItem["PublishingPageContent"] = automaticTranslationPlugin.TranslateText(pageContentToTranslate, currentItemLanguage,
                                                                                                                                          lang, profileId, translationUserAccount, true);
                                                        }
                                                        else
                                                        {
                                                            cloneItem["PublishingPageContent"] = pageContentToTranslate;
                                                        }

                                                        cloneItem.SystemUpdate(false);
                                                    }
                                                }
                                                else if (automaticTranslationPlugin != null && automaticTranslationPlugin.SupportFileTranslation())
                                                {
                                                    cloneItem.SystemUpdate(false);
                                                    lock (cloneItem)
                                                    {
                                                        byte[] fileBytes = currentItem.File.OpenBinary();
                                                        fileBytes = automaticTranslationPlugin.TranslateFile(fileBytes, currentItemLanguage, lang, profileId, translationUserAccount, currentItem.File.Name);
                                                        cloneItem.File.SaveBinary(fileBytes);
                                                    }
                                                }
                                            }
                                        }

                                        if (currentList.BaseTemplate == (SPListTemplateType)(850))
                                        {
                                            CreatePublishingPage(currentItem, cloneItem, web, toOverwrite);
                                        }
                                    }


                                    url = url.Replace("?SPS_Trans_Code=CreateClonedMultilingualItem&", "?").Replace("&SPS_Trans_Code=CreateClonedMultilingualItem", string.Empty);
                                    Utilities.SetItemLanguage(siteWeb, listId, url, cloneItem.ID.ToString(), lang);
                                    Utilities.LinkItemWith(siteWeb, listId, url, itemId, cloneItem.ID.ToString());
                                }
                                else
                                {
                                    if (HttpContext.Current != null)
                                    {
                                        HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri.Replace("?SPS_Trans_Code=CreateClonedMultilingualItem&", "?")
                                                                              .Replace("&SPS_Trans_Code=CreateClonedMultilingualItem", string.Empty), false);
                                    }
                                }
                            }
                            else
                            {
                                if (HttpContext.Current != null)
                                {
                                    HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri.Replace("?SPS_Trans_Code=CreateClonedMultilingualItem&", "?")
                                                                          .Replace("&SPS_Trans_Code=CreateClonedMultilingualItem", string.Empty), false);
                                }
                            }
                        }
                    }

                if (HttpContext.Current != null)
                {
                    HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri.Replace("?SPS_Trans_Code=CreateClonedMultilingualItem&", "?")
                                                          .Replace("&SPS_Trans_Code=CreateClonedMultilingualItem", string.Empty), false);
                }
            }
            catch (Exception exc)
            {
                Utilities.LogException("Error in Translator2010.TranslatorAutoTranslation.CreateClonedMultilingualItem: " + exc.Message, EventLogEntryType.Warning);
            }
        }
 public abstract string AddToDictionary(string url, string defaultLang, string term, IAutomaticTranslation automaticTranslationPlugin);
        private void InitializeApplication(HttpContext context)
        {
            try
            {
                lock (LockThis)
                {
                    if (!_init)
                    {
                        string absoluteUri = Alphamosaik.Common.SharePoint.Library.Utilities.GetAbsoluteUri(context.ApplicationInstance);

                        if (_license == null)
                        {
                            // We already use around 60
                            Regex.CacheSize = 90;

                            string value = ConfigStore.Instance.GetValue("Oceanik", Environment.MachineName, absoluteUri);

                            if (string.IsNullOrEmpty(value))
                            {
                                byte[] bytes = Encoding.ASCII.GetBytes(ServerPrivateKey);
                                ConfigStore.Instance.AddValue(Environment.MachineName, "Oceanik", "ServerPrivateKey", string.Empty, "license.key", bytes, false, absoluteUri);

                                UpdateOldLicensing(absoluteUri);
                            }

                            string license = ConfigStore.Instance.GetSpecificStringAttachment("Oceanik", Environment.MachineName, "license.dat", absoluteUri);

                            _license = new License();

                            if (!string.IsNullOrEmpty(ConfigStore.Instance.GetValue("Oceanik", "ActivateStatisticsLogDetails", absoluteUri)))
                            {
                                _activateStatisticsLogDetails = Convert.ToInt32(ConfigStore.Instance.GetValue("Oceanik", "ActivateStatisticsLogDetails", absoluteUri));
                            }

                            if (!string.IsNullOrEmpty(license))
                            {
                                _license = new License(ServerPrivateKey, license);
                            }

                            string automaticTranslationPlugin = ConfigStore.Instance.GetValue("Oceanik", "automaticTranslationPlugin", absoluteUri);

                            if (!string.IsNullOrEmpty(automaticTranslationPlugin) && HttpRuntime.Cache[OceanikAutomaticTranslation] == null)
                            {
                                _automaticTranslationPlugin = PlugInUtilities.GetPlugin <IAutomaticTranslation>(automaticTranslationPlugin);

                                if (_automaticTranslationPlugin != null)
                                {
                                    HttpRuntime.Cache.Add(OceanikAutomaticTranslation, _automaticTranslationPlugin, null,
                                                          Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
                                                          CacheItemPriority.NotRemovable, null);
                                }
                            }
                        }

                        if ((_automaticTranslationPlugin == null) && (HttpRuntime.Cache[OceanikAutomaticTranslation] != null))
                        {
                            _automaticTranslationPlugin = (IAutomaticTranslation)HttpRuntime.Cache[OceanikAutomaticTranslation];
                        }

                        _translatorHelper = _activateStatisticsLogDetails > 0 ? new StatisticsStandardTranslatorHelper() : new StandardTranslatorHelper();

                        _httpApplicationEvents = new HttpApplicationEvents(_translatorHelper, _license, _automaticTranslationPlugin);

                        if (_activateStatisticsLogDetails > 0)
                        {
                            _httpApplicationEvents = new StatisticsHttpApplicationEvents(_httpApplicationEvents, _activateStatisticsLogDetails, absoluteUri);
                        }

                        _init = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Utilities.LogException("Error in Oceanik.Init: " + ex.Message, EventLogEntryType.Warning);
            }
        }
Exemplo n.º 8
0
 public override string AddToDictionary(string url, string defaultLang, string term, IAutomaticTranslation automaticTranslationPlugin)
 {
     return(_currentHelper.AddToDictionary(url, defaultLang, term, automaticTranslationPlugin));
 }
Exemplo n.º 9
0
 public override string AddToDictionary(string url, string defaultLang, string term, IAutomaticTranslation automaticTranslationPlugin)
 {
     using (new Statistic(HttpContext.Current != null ? (StatisticsTracer)HttpContext.Current.Items[StatisticsSlotName] : null, "AddToDictionary", FullDetail))
     {
         return(base.AddToDictionary(url, defaultLang, term, automaticTranslationPlugin));
     }
 }