using System; using System.Collections.Generic; using System.Linq; using Umbraco.Core.Auditing; using Umbraco.Core.Models; using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.Querying; using Umbraco.Core.Persistence.Repositories; using Umbraco.Core.Persistence.UnitOfWork; namespace Umbraco.Core.Services { /// /// Represents the Localization Service, which is an easy access to operations involving and /// public class LocalizationService : ILocalizationService { private readonly RepositoryFactory _repositoryFactory; private static readonly Guid RootParentId = new Guid("41c7638d-f529-4bff-853e-59a0c2fb1bde"); private readonly IDatabaseUnitOfWork _unitOfWork; private readonly IDictionaryRepository _dictionaryRepository; private readonly ILanguageRepository _languageRepository; public LocalizationService(RepositoryFactory repositoryFactory) : this(new PetaPocoUnitOfWorkProvider(), repositoryFactory) { } public LocalizationService(IDatabaseUnitOfWorkProvider provider, RepositoryFactory repositoryFactory) { _repositoryFactory = repositoryFactory; _unitOfWork = provider.GetUnitOfWork(); _dictionaryRepository = _repositoryFactory.CreateDictionaryRepository(_unitOfWork); _languageRepository = _repositoryFactory.CreateLanguageRepository(_unitOfWork); } /// /// Gets a by its id /// /// Id of the /// public IDictionaryItem GetDictionaryItemById(int id) { var repository = _dictionaryRepository; return repository.Get(id); } /// /// Gets a by its id /// /// Id of the /// public IDictionaryItem GetDictionaryItemById(Guid id) { var repository = _dictionaryRepository; var query = Query.Builder.Where(x => x.Key == id); var items = repository.GetByQuery(query); return items.FirstOrDefault(); } /// /// Gets a by its key /// /// Key of the /// public IDictionaryItem GetDictionaryItemByKey(string key) { var repository = _dictionaryRepository; var query = Query.Builder.Where(x => x.ItemKey == key); var items = repository.GetByQuery(query); return items.FirstOrDefault(); } /// /// Gets a list of children for a /// /// Id of the parent /// An enumerable list of objects public IEnumerable GetDictionaryItemChildren(Guid parentId) { var repository = _dictionaryRepository; var query = Query.Builder.Where(x => x.ParentId == parentId); var items = repository.GetByQuery(query); return items; } /// /// Gets the root/top objects /// /// An enumerable list of objects public IEnumerable GetRootDictionaryItems() { var repository = _dictionaryRepository; var query = Query.Builder.Where(x => x.ParentId == RootParentId); var items = repository.GetByQuery(query); return items; } /// /// Checks if a with given key exists /// /// Key of the /// True if a exists, otherwise false public bool DictionaryItemExists(string key) { var repository = _dictionaryRepository; var query = Query.Builder.Where(x => x.ItemKey == key); var items = repository.GetByQuery(query); return items.Any(); } /// /// Saves a object /// /// to save /// Optional id of the user saving the dictionary item public void Save(IDictionaryItem dictionaryItem, int userId = -1) { var e = new SaveEventArgs(); if (Saving != null) Saving(dictionaryItem, e); if (!e.Cancel) { _dictionaryRepository.AddOrUpdate(dictionaryItem); _unitOfWork.Commit(); if (Saved != null) Saved(dictionaryItem, e); Audit.Add(AuditTypes.Save, "Save DictionaryItem performed by user", userId == -1 ? 0 : userId, dictionaryItem.Id); } } /// /// Deletes a object and its related translations /// as well as its children. /// /// to delete /// Optional id of the user deleting the dictionary item public void Delete(IDictionaryItem dictionaryItem, int userId = -1) { var e = new DeleteEventArgs { Id = dictionaryItem.Id }; if (Deleting != null) Deleting(dictionaryItem, e); if (!e.Cancel) { //NOTE: The recursive delete is done in the repository _dictionaryRepository.Delete(dictionaryItem); _unitOfWork.Commit(); if (Deleted != null) Deleted(dictionaryItem, e); Audit.Add(AuditTypes.Delete, "Delete DictionaryItem performed by user", userId == -1 ? 0 : userId, dictionaryItem.Id); } } /// /// Gets a by its id /// /// Id of the /// public ILanguage GetLanguageById(int id) { var repository = _languageRepository; return repository.Get(id); } /// /// Gets a by its culture code /// /// Culture Code /// public ILanguage GetLanguageByCultureCode(string culture) { var repository = _languageRepository; var query = Query.Builder.Where(x => x.CultureName == culture); var items = repository.GetByQuery(query); return items.FirstOrDefault(); } /// /// Gets all available languages /// /// An enumerable list of objects public IEnumerable GetAllLanguages() { var repository = _languageRepository; var languages = repository.GetAll(); return languages; } /// /// Saves a object /// /// to save /// Optional id of the user saving the language public void Save(ILanguage language, int userId = -1) { var e = new SaveEventArgs(); if (Saving != null) Saving(language, e); if (!e.Cancel) { _languageRepository.AddOrUpdate(language); _unitOfWork.Commit(); if (Saved != null) Saved(language, e); Audit.Add(AuditTypes.Save, "Save Language performed by user", userId == -1 ? 0 : userId, language.Id); } } /// /// Deletes a by removing it (but not its usages) from the db /// /// to delete /// Optional id of the user deleting the language public void Delete(ILanguage language, int userId = -1) { var e = new DeleteEventArgs { Id = language.Id }; if (Deleting != null) Deleting(language, e); if (!e.Cancel) { //NOTE: There isn't any constraints in the db, so possible references aren't deleted _languageRepository.Delete(language); _unitOfWork.Commit(); if (Deleted != null) Deleted(language, e); Audit.Add(AuditTypes.Delete, "Delete Language performed by user", userId == -1 ? 0 : userId, language.Id); } } #region Event Handlers /// /// Occurs before Delete /// public static event EventHandler Deleting; /// /// Occurs after Delete /// public static event EventHandler Deleted; /// /// Occurs before Save /// public static event EventHandler Saving; /// /// Occurs after Save /// public static event EventHandler Saved; #endregion } }