Exemplo n.º 1
0
        public virtual int ImportResourcesFromXml(
            Language language,
            XmlDocument xmlDocument,
            string rootKey              = null,
            bool sourceIsPlugin         = false,
            ImportModeFlags mode        = ImportModeFlags.Insert | ImportModeFlags.Update,
            bool updateTouchedResources = false)
        {
            using (var scope = new DbContextScope(autoDetectChanges: false, proxyCreation: false, validateOnSave: false, autoCommit: false, forceNoTracking: true, hooksEnabled: false))
            {
                var toAdd    = new List <LocaleStringResource>();
                var toUpdate = new List <LocaleStringResource>();
                var nodes    = xmlDocument.SelectNodes(@"//Language/LocaleResource");

                var resources = language.LocaleStringResources.ToDictionarySafe(x => x.ResourceName, StringComparer.OrdinalIgnoreCase);

                LocaleStringResource resource;

                foreach (var xel in nodes.Cast <XmlElement>())
                {
                    string name      = xel.GetAttribute("Name").TrimSafe();
                    string value     = "";
                    var    valueNode = xel.SelectSingleNode("Value");
                    if (valueNode != null)
                    {
                        value = valueNode.InnerText;
                    }

                    if (String.IsNullOrEmpty(name))
                    {
                        continue;
                    }

                    if (rootKey.HasValue())
                    {
                        if (!xel.GetAttributeText("AppendRootKey").IsCaseInsensitiveEqual("false"))
                        {
                            name = "{0}.{1}".FormatWith(rootKey, name);
                        }
                    }

                    resource = null;

                    // do not use "Insert"/"Update" methods because they clear cache
                    // let's bulk insert
                    //var resource = language.LocaleStringResources.Where(x => x.ResourceName.Equals(name, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                    if (resources.TryGetValue(name, out resource))
                    {
                        if (mode.HasFlag(ImportModeFlags.Update))
                        {
                            if (updateTouchedResources || !resource.IsTouched.GetValueOrDefault())
                            {
                                if (value != resource.ResourceValue)
                                {
                                    resource.ResourceValue = value;
                                    resource.IsTouched     = null;
                                    toUpdate.Add(resource);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (mode.HasFlag(ImportModeFlags.Insert))
                        {
                            toAdd.Add(
                                new LocaleStringResource
                            {
                                LanguageId    = language.Id,
                                ResourceName  = name,
                                ResourceValue = value,
                                IsFromPlugin  = sourceIsPlugin
                            });
                        }
                    }
                }

                //_lsrRepository.AutoCommitEnabled = true;

                if (toAdd.Any() || toUpdate.Any())
                {
                    var segmentKeys = new HashSet <string>();

                    toAdd.Each(x => segmentKeys.Add(GetSegmentKeyPart(x.ResourceName)));
                    toUpdate.Each(x => segmentKeys.Add(GetSegmentKeyPart(x.ResourceName)));

                    _lsrRepository.InsertRange(toAdd);
                    toAdd.Clear();

                    _lsrRepository.UpdateRange(toUpdate);
                    toUpdate.Clear();

                    int num = _lsrRepository.Context.SaveChanges();

                    // clear cache
                    foreach (var segmentKey in segmentKeys)
                    {
                        ClearCacheSegment(segmentKey, language.Id);
                    }

                    return(num);
                }

                return(0);
            }
        }
        /// <summary>
        /// Import language resources from XML file
        /// </summary>
        /// <param name="language">Language</param>
        /// <param name="xmlDocument">XML document</param>
        /// <param name="rootKey">Prefix for resource key name</param>
        public virtual void ImportResourcesFromXml(
            Language language,
            XmlDocument xmlDocument,
            string rootKey              = null,
            bool sourceIsPlugin         = false,
            ImportModeFlags mode        = ImportModeFlags.Insert | ImportModeFlags.Update,
            bool updateTouchedResources = false)
        {
            using (var scope = new DbContextScope(autoDetectChanges: false, proxyCreation: false, validateOnSave: false, autoCommit: false))
            {
                var toAdd    = new List <LocaleStringResource>();
                var toUpdate = new List <LocaleStringResource>();
                var nodes    = xmlDocument.SelectNodes(@"//Language/LocaleResource");

                foreach (var xel in nodes.Cast <XmlElement>())
                {
                    string name      = xel.GetAttribute("Name").TrimSafe();
                    string value     = "";
                    var    valueNode = xel.SelectSingleNode("Value");
                    if (valueNode != null)
                    {
                        value = valueNode.InnerText;
                    }

                    if (String.IsNullOrEmpty(name))
                    {
                        continue;
                    }

                    if (rootKey.HasValue())
                    {
                        if (!xel.GetAttributeText("AppendRootKey").IsCaseInsensitiveEqual("false"))
                        {
                            name = "{0}.{1}".FormatWith(rootKey, name);
                        }
                    }

                    // do not use "Insert"/"Update" methods because they clear cache
                    // let's bulk insert
                    var resource = language.LocaleStringResources.Where(x => x.ResourceName.Equals(name, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                    if (resource != null)
                    {
                        if (mode.HasFlag(ImportModeFlags.Update))
                        {
                            if (updateTouchedResources || !resource.IsTouched.GetValueOrDefault())
                            {
                                resource.ResourceValue = value;
                                resource.IsTouched     = null;
                                toUpdate.Add(resource);
                            }
                        }
                    }
                    else
                    {
                        if (mode.HasFlag(ImportModeFlags.Insert))
                        {
                            toAdd.Add(
                                new LocaleStringResource()
                            {
                                LanguageId    = language.Id,
                                ResourceName  = name,
                                ResourceValue = value,
                                IsFromPlugin  = sourceIsPlugin
                            });
                        }
                    }
                }

                _lsrRepository.AutoCommitEnabled = true;
                _lsrRepository.InsertRange(toAdd, 500);
                toAdd.Clear();

                _lsrRepository.UpdateRange(toUpdate);
                toUpdate.Clear();

                //clear cache
                _cacheManager.RemoveByPattern(LOCALESTRINGRESOURCES_PATTERN_KEY);
            }
        }