public object Import(IEnumerable <LocalizationResource> newResources, bool importOnlyNewContent) { var count = 0; // if we are overwriting old content - we need to get rid of resources first if (!importOnlyNewContent) { new DeleteAllResources.Command().Execute(); } var allCurrentResources = new GetAllResources.Query(true).Execute().ToDictionary(r => r.ResourceKey); var newInserts = new List <LocalizationResource>(); foreach (var localizationResource in newResources) { if (importOnlyNewContent) { // look for existing resource allCurrentResources.TryGetValue(localizationResource.ResourceKey, out var existingResource); if (existingResource == null) { // resource with this key does not exist - so we can just add it newInserts.Add(localizationResource); //new CreateNewResource.Command(localizationResource).Execute(); count++; } else { // there is a resource with this key - looking for missing translations foreach (var translation in localizationResource.Translations) { new CreateOrUpdateTranslation.Command(existingResource.ResourceKey, new CultureInfo(translation.Language), translation.Value).Execute(); } } } else { // don't care about state in DB // if we are importing all resources once again - all will be gone anyway //new CreateNewResource.Command(localizationResource).Execute(); newInserts.Add(localizationResource); count++; } } new CreateNewResources.Command(newInserts).Execute(); var c = new ClearCache.Command(); c.Execute(); return($"Import successful. Imported {count} resources"); }
private void PopulateCache() { var c = new ClearCache.Command(); c.Execute(); var allResources = new GetAllResources.Query().Execute(); foreach (var resource in allResources) { var key = CacheKeyHelper.BuildKey(resource.ResourceKey); ConfigurationContext.Current.CacheManager.Insert(key, resource); } }
private void PopulateCache() { var c = new ClearCache.Command(); c.Execute(); var allResources = new GetAllResources.Query().Execute(); foreach (var resource in allResources) { var key = CacheKeyHelper.BuildKey(resource.ResourceKey); ConfigurationContext.Current.CacheManager.Insert(key, resource); } }
public object Import(IEnumerable <LocalizationResource> newResources, bool importOnlyNewContent) { var count = 0; using (var db = new LanguageEntities()) { // if we are overwriting old content - we need to get rid of it first if (!importOnlyNewContent) { var existingResources = db.Set <LocalizationResource>(); db.LocalizationResources.RemoveRange(existingResources); db.SaveChanges(); } foreach (var localizationResource in newResources) { if (importOnlyNewContent) { // look for existing resource var existingResource = db.LocalizationResources .Include(r => r.Translations) .FirstOrDefault(r => r.ResourceKey == localizationResource.ResourceKey); if (existingResource == null) { // resource with this key does not exist - so we can just add it AddNewResource(db, localizationResource); count++; } else { // there is a resource with this key - looking for missing translations foreach (var translation in localizationResource.Translations) { var existingTranslation = existingResource.Translations.FirstOrDefault(t => t.Language == translation.Language); if (existingTranslation == null) { // there is no translation in that language - adding one // but before adding that - we need to fix its reference to resource (exported file might have different id) translation.ResourceId = existingResource.Id; db.LocalizationResourceTranslations.Add(translation); } else if (string.IsNullOrEmpty(existingTranslation.Value)) { // we can check - if content of the translation is empty - for us - it's the same as translation would not exist existingTranslation.Value = translation.Value; } } } } else { // don't care about state in DB // if we are importing all resources once again - all will be gone anyway AddNewResource(db, localizationResource); count++; } } db.SaveChanges(); var c = new ClearCache.Command(); c.Execute(); } return($"Import successful. Imported {count} resources"); }
public IEnumerable <string> ImportChanges(ICollection <DetectedImportChange> changes) { var result = new List <string>(); var inserts = 0; var updates = 0; var deletes = 0; var allCurrentResources = new GetAllResources.Query(true).Execute().ToDictionary(r => r.ResourceKey); var newInserts = new List <LocalizationResource>(); // process deletes foreach (var delete in changes.Where(c => c.ChangeType == ChangeType.Delete)) { new DeleteResource.Command(delete.ExistingResource.ResourceKey).Execute(); deletes++; } // process inserts foreach (var insert in changes.Where(c => c.ChangeType == ChangeType.Insert)) { // fix incoming incomplete resource from web insert.ImportingResource.Author = "import"; insert.ImportingResource.IsModified = false; insert.ImportingResource.IsHidden = false; // fix incoming resource translation invariant language (if any) insert.ImportingResource.Translations.ForEach(t => t.Language = t.Language ?? ""); newInserts.Add(insert.ImportingResource); inserts++; } // process updates foreach (var update in changes.Where(c => c.ChangeType == ChangeType.Update)) { // look for existing resource allCurrentResources.TryGetValue(update.ImportingResource.ResourceKey, out var existingResource); if (existingResource == null) { // resource with this key does not exist - so we can just add it update.ImportingResource.Author = "import"; update.ImportingResource.IsModified = false; update.ImportingResource.IsModified = false; newInserts.Add(update.ImportingResource); inserts++; continue; } foreach (var translation in update.ImportingResource.Translations.Where(_ => _.Value != null)) { new CreateOrUpdateTranslation.Command(existingResource.ResourceKey, new CultureInfo(translation.Language), translation.Value).Execute(); } updates++; } new CreateNewResources.Command(newInserts).Execute(); var clearCommand = new ClearCache.Command(); clearCommand.Execute(); if (inserts > 0) { result.Add($"Inserted {inserts} resources."); } if (updates > 0) { result.Add($"Updated {updates} resources."); } if (deletes > 0) { result.Add($"Deleted {deletes} resources."); } return(result); }
public IEnumerable <string> ImportChanges(ICollection <DetectedImportChange> changes) { var result = new List <string>(); var inserts = 0; var updates = 0; var deletes = 0; using (var db = new LanguageEntities()) { // process deletes foreach (var delete in changes.Where(c => c.ChangeType == ChangeType.Delete)) { var existingResource = db.LocalizationResources.FirstOrDefault(r => r.ResourceKey == delete.ExistingResource.ResourceKey); if (existingResource != null) { db.LocalizationResources.Remove(existingResource); } } // process inserts foreach (var insert in changes.Where(c => c.ChangeType == ChangeType.Insert)) { // fix incoming incomplete resource from web insert.ImportingResource.ModificationDate = DateTime.UtcNow; insert.ImportingResource.Author = "import"; insert.ImportingResource.IsModified = false; // fix incoming resource translation invariant language (if any) insert.ImportingResource.Translations.ForEach(t => t.Language = t.Language ?? ""); AddNewResource(db, insert.ImportingResource); inserts++; } // process updates foreach (var update in changes.Where(c => c.ChangeType == ChangeType.Update)) { // look for existing resource var existingResource = db.LocalizationResources .Include(r => r.Translations) .FirstOrDefault(r => r.ResourceKey == update.ImportingResource.ResourceKey); if (existingResource == null) { // resource with this key does not exist - so we can just add it update.ImportingResource.ModificationDate = DateTime.UtcNow; update.ImportingResource.Author = "import"; update.ImportingResource.IsModified = false; AddNewResource(db, update.ImportingResource); inserts++; continue; } foreach (var translation in update.ImportingResource.Translations.Where(_ => _.Value != null)) { var existingTranslation = existingResource.Translations.FirstOrDefault(t => t.Language == translation.Language); if (existingTranslation == null) { // there is no translation in that language - adding one // but before adding that - we need to fix its reference to resource (exported file might have different id) translation.ResourceId = existingResource.Id; db.LocalizationResourceTranslations.Add(translation); } else { existingTranslation.Value = translation.Value; } } updates++; } db.SaveChanges(); var clearCommand = new ClearCache.Command(); clearCommand.Execute(); } if (inserts > 0) { result.Add($"Inserted {inserts} resources."); } if (updates > 0) { result.Add($"Updated {updates} resources."); } if (deletes > 0) { result.Add($"Deleted {deletes} resources."); } return(result); }
public object Import(IEnumerable<LocalizationResource> newResources, bool importOnlyNewContent) { var count = 0; using (var db = new LanguageEntities()) { // if we are overwriting old content - we need to get rid of it first if (!importOnlyNewContent) { var existingResources = db.Set<LocalizationResource>(); db.LocalizationResources.RemoveRange(existingResources); db.SaveChanges(); } foreach (var localizationResource in newResources) { if (importOnlyNewContent) { // look for existing resource var existingResource = db.LocalizationResources .Include(r => r.Translations) .FirstOrDefault(r => r.ResourceKey == localizationResource.ResourceKey); if (existingResource == null) { // resource with this key does not exist - so we can just add it AddNewResource(db, localizationResource); count++; } else { // there is a resource with this key - looking for missing translations foreach (var translation in localizationResource.Translations) { var existingTranslation = existingResource.Translations.FirstOrDefault(t => t.Language == translation.Language); if (existingTranslation == null) { // there is no translation in that language - adding one // but before adding that - we need to fix its reference to resource (exported file might have different id) translation.ResourceId = existingResource.Id; db.LocalizationResourceTranslations.Add(translation); } else if (string.IsNullOrEmpty(existingTranslation.Value)) { // we can check - if content of the translation is empty - for us - it's the same as translation would not exist existingTranslation.Value = translation.Value; } } } } else { // don't care about state in DB // if we are importing all resources once again - all will be gone anyway AddNewResource(db, localizationResource); count++; } } db.SaveChanges(); var c = new ClearCache.Command(); c.Execute(); } return $"Import successful. Imported {count} resources"; }