public void Execute(Command command)
            {
                if (string.IsNullOrEmpty(command.Key))
                {
                    throw new ArgumentNullException(nameof(command.Key));
                }

                using (var db = new LanguageEntities())
                {
                    var existingResource = db.LocalizationResources.FirstOrDefault(r => r.ResourceKey == command.Key);

                    if (existingResource == null)
                    {
                        return;
                    }

                    if (existingResource.FromCode)
                    {
                        throw new InvalidOperationException($"Cannot delete resource `{command.Key}` that is synced with code");
                    }

                    db.LocalizationResources.Remove(existingResource);
                    db.SaveChanges();
                }

                ConfigurationContext.Current.CacheManager.Remove(CacheKeyHelper.BuildKey(command.Key));
            }
        public void Execute(RemoveTranslation.Command command)
        {
            using (var db = new LanguageEntities())
            {
                var resource = db.LocalizationResources.Include(r => r.Translations).FirstOrDefault(r => r.ResourceKey == command.Key);
                if (resource == null)
                {
                    return;
                }

                if (!resource.IsModified.HasValue || !resource.IsModified.Value)
                {
                    throw new InvalidOperationException($"Cannot delete translation for unmodified resource `{command.Key}`");
                }

                var t = resource.Translations.FirstOrDefault(_ => _.Language == command.Language.Name);
                if (t != null)
                {
                    db.LocalizationResourceTranslations.Remove(t);
                    db.SaveChanges();
                }
            }

            ConfigurationContext.Current.CacheManager.Remove(CacheKeyHelper.BuildKey(command.Key));
        }
            public void Execute(Command command)
            {
                if (string.IsNullOrEmpty(command.Key))
                {
                    throw new ArgumentNullException(nameof(command.Key));
                }

                using (var db = new LanguageEntities())
                {
                    var existingResource = db.LocalizationResources.FirstOrDefault(r => r.ResourceKey == command.Key);
                    if (existingResource != null)
                    {
                        throw new InvalidOperationException($"Resource with key `{command.Key}` already exists");
                    }

                    db.LocalizationResources.Add(new LocalizationResource(command.Key)
                    {
                        ModificationDate = DateTime.UtcNow,
                        FromCode         = command.FromCode,
                        IsModified       = false,
                        Author           = command.UserName
                    });

                    db.SaveChanges();
                }
            }
Exemple #4
0
        private void RegisterDiscoveredModels(LanguageEntities db, IEnumerable <Type> types)
        {
            var properties = types.SelectMany(type => TypeDiscoveryHelper.GetAllProperties(type, contextAwareScanning: false));

            foreach (var property in properties)
            {
                RegisterIfNotExist(db, property.Key, property.Translation);
                db.SaveChanges();
            }
        }
Exemple #5
0
        private void RegisterDiscoveredResources(LanguageEntities db, IEnumerable <Type> types)
        {
            var properties = types.SelectMany(type => TypeDiscoveryHelper.GetAllProperties(type));

            foreach (var property in properties)
            {
                RegisterIfNotExist(db, property.Key, property.Translation);
            }

            db.SaveChanges();
        }
        public void RegisterManually(IEnumerable<ManualResource> resources)
        {
            using (var db = new LanguageEntities())
            {
                foreach (var resource in resources)
                {
                    RegisterIfNotExist(db, resource.Key, resource.Translation, author: "manual");
                }

                db.SaveChanges();
            }
        }
        private void RegisterDiscoveredResources(IEnumerable<Type> types)
        {
            var helper = new TypeDiscoveryHelper();
            var properties = types.SelectMany(type => helper.ScanResources(type)).DistinctBy(r => r.Key);

            using (var db = new LanguageEntities())
            {
                foreach (var property in properties)
                    RegisterIfNotExist(db, property.Key, property.Translation);

                db.SaveChanges();
            }
        }
Exemple #8
0
        public void RegisterManually(IEnumerable <ManualResource> resources)
        {
            using (var db = new LanguageEntities())
            {
                var defaultCulture = DetermineDefaultCulture();

                foreach (var resource in resources)
                {
                    RegisterIfNotExist(db, resource.Key, resource.Translation, defaultCulture, "manual");
                }

                db.SaveChanges();
            }
        }
Exemple #9
0
        private void RegisterManuallySingle(IEnumerable <ManualResource> resources, string connectionString)
        {
            using (var db = new LanguageEntities(connectionString))
            {
                var defaultCulture = new DetermineDefaultCulture.Query().Execute();

                foreach (var resource in resources)
                {
                    RegisterIfNotExist(db, resource.Key, resource.Translation, defaultCulture, "manual");
                }

                db.SaveChanges();
            }
        }
            public void Execute(Command command)
            {
                using (var db = new LanguageEntities())
                {
                    var resource = db.LocalizationResources.Include(r => r.Translations).FirstOrDefault(r => r.ResourceKey == command.Key);

                    if (resource == null)
                    {
                        // TODO: return some status response obj
                        return;
                    }

                    var translation = resource.Translations.FirstOrDefault(t => t.Language == command.Language.Name);

                    if (translation != null)
                    {
                        // update existing translation
                        translation.Value = command.Translation;
                    }
                    else
                    {
                        var newTranslation = new LocalizationResourceTranslation
                        {
                            Value      = command.Translation,
                            Language   = command.Language.Name,
                            ResourceId = resource.Id
                        };

                        db.LocalizationResourceTranslations.Add(newTranslation);
                    }

                    resource.ModificationDate = DateTime.UtcNow;
                    resource.IsModified       = true;
                    db.SaveChanges();
                }

                ConfigurationContext.Current.CacheManager.Remove(CacheKeyHelper.BuildKey(command.Key));
            }
        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");
        }
Exemple #12
0
        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";
        }