using System;
using System.Collections.Generic;
using System.Linq;
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.Web.Services
{
///
/// Represents the Localization Service, which is an easy access to operations involving and
///
public class LocalizationService : ILocalizationService
{
private static readonly Guid RootParentId = new Guid("41c7638d-f529-4bff-853e-59a0c2fb1bde");
private readonly IUnitOfWorkProvider _provider;
public LocalizationService() : this(new PetaPocoUnitOfWorkProvider())
{
}
public LocalizationService(IUnitOfWorkProvider provider)
{
_provider = provider;
}
///
/// Gets a by its id
///
/// Id of the
///
public IDictionaryItem GetDictionaryItemById(int id)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType(unitOfWork);
return repository.Get(id);
}
///
/// Gets a by its id
///
/// Id of the
///
public IDictionaryItem GetDictionaryItemById(Guid id)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType(unitOfWork);
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 unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType(unitOfWork);
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 unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType(unitOfWork);
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 unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType(unitOfWork);
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 unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType(unitOfWork);
var query = Query.Builder.Where(x => x.ItemKey == key);
var items = repository.GetByQuery(query);
return items.Any();
}
///
/// Saves a object
///
/// to save
public void Save(IDictionaryItem dictionaryItem)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType(unitOfWork);
repository.AddOrUpdate(dictionaryItem);
unitOfWork.Commit();
}
///
/// Deletes a object and its related translations
/// as well as its children.
///
/// to delete
public void Delete(IDictionaryItem dictionaryItem)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType(unitOfWork);
//NOTE: The recursive delete is done in the repository
repository.Delete(dictionaryItem);
unitOfWork.Commit();
}
///
/// Gets a by its id
///
/// Id of the
///
public ILanguage GetLanguageById(int id)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType(unitOfWork);
return repository.Get(id);
}
///
/// Gets a by its culture code
///
/// Culture Code
///
public ILanguage GetLanguageByCultureCode(string culture)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType(unitOfWork);
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 unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType(unitOfWork);
var languages = repository.GetAll();
return languages;
}
///
/// Saves a object
///
/// to save
public void Save(ILanguage language)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType(unitOfWork);
repository.AddOrUpdate(language);
unitOfWork.Commit();
}
///
/// Deletes a by removing it (but not its usages) from the db
///
/// to delete
public void Delete(ILanguage language)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType(unitOfWork);
//NOTE: There isn't any constraints in the db, so possible references aren't deleted
repository.Delete(language);
unitOfWork.Commit();
}
}
}