Beispiel #1
0
        private void RegisterDiscoveredResources(IEnumerable <Type> types, IEnumerable <LocalizationResource> allResources)
        {
            var helper     = new TypeDiscoveryHelper();
            var properties = types.SelectMany(type => helper.ScanResources(type)).DistinctBy(r => r.Key);

            // split work queue by 400 resources each
            var groupedProperties = properties.SplitByCount(400);

            Parallel.ForEach(groupedProperties,
                             group =>
            {
                var sb = new StringBuilder();
                sb.AppendLine("declare @resourceId int");

                var refactoredResources = group.Where(r => !string.IsNullOrEmpty(r.OldResourceKey));
                foreach (var refactoredResource in refactoredResources)
                {
                    sb.Append($@"
if exists(select 1 from localizationresources with(nolock) where resourcekey = '{refactoredResource.OldResourceKey}')
begin
    update dbo.localizationresources set resourcekey = '{refactoredResource.Key}', fromcode = 1 where resourcekey = '{refactoredResource.OldResourceKey}'
end
");
                }

                foreach (var property in group)
                {
                    var existingResource = allResources.FirstOrDefault(r => r.ResourceKey == property.Key);

                    if (existingResource == null)
                    {
                        sb.Append($@"
set @resourceId = isnull((select id from localizationresources where [resourcekey] = '{property.Key}'), -1)
if (@resourceId = -1)
begin
    insert into localizationresources ([resourcekey], modificationdate, author, fromcode, ismodified, ishidden)
    values ('{property.Key}', getutcdate(), 'type-scanner', 1, 0, {Convert.ToInt32(property.IsHidden)})
    set @resourceId = SCOPE_IDENTITY()");

                        // add all translations
                        foreach (var propertyTranslation in property.Translations)
                        {
                            sb.Append($@"
    insert into localizationresourcetranslations (resourceid, [language], [value]) values (@resourceId, '{propertyTranslation.Culture}', N'{
                                                               propertyTranslation.Translation.Replace("'", "''")
                                                           }')
");
                        }

                        sb.Append(@"
end
");
                    }

                    if (existingResource != null)
                    {
                        sb.AppendLine($"update localizationresources set fromcode = 1, ishidden = {Convert.ToInt32(property.IsHidden)} where [id] = {existingResource.Id}");

                        if (existingResource.IsModified.HasValue && !existingResource.IsModified.Value)
                        {
                            foreach (var propertyTranslation in property.Translations)
                            {
                                AddTranslationScript(existingResource, sb, propertyTranslation);
                            }
                        }
                    }
                }

                using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings[ConfigurationContext.Current.ConnectionName].ConnectionString))
                {
                    var cmd = new SqlCommand(sb.ToString(), conn)
                    {
                        CommandTimeout = 60
                    };

                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            });
        }