Implements ILocalizationService for U4-1075
Refactoring DictionaryItem, DictionaryTranslation and Language to use the same interface implementations as the rest of the db driven repos and services.
This commit is contained in:
@@ -11,22 +11,22 @@ namespace Umbraco.Core.Models
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract(IsReference = true)]
|
||||
public class DictionaryItem : Entity, IAggregateRoot
|
||||
public class DictionaryItem : Entity, IDictionaryItem
|
||||
{
|
||||
private Guid _parentId;
|
||||
private string _itemKey;
|
||||
private IEnumerable<DictionaryTranslation> _translations;
|
||||
private IEnumerable<IDictionaryTranslation> _translations;
|
||||
|
||||
public DictionaryItem(Guid parentId, string itemKey)
|
||||
{
|
||||
_parentId = parentId;
|
||||
_itemKey = itemKey;
|
||||
_translations = new List<DictionaryTranslation>();
|
||||
_translations = new List<IDictionaryTranslation>();
|
||||
}
|
||||
|
||||
private static readonly PropertyInfo ParentIdSelector = ExpressionHelper.GetPropertyInfo<DictionaryItem, Guid>(x => x.ParentId);
|
||||
private static readonly PropertyInfo ItemKeySelector = ExpressionHelper.GetPropertyInfo<DictionaryItem, string>(x => x.ItemKey);
|
||||
private static readonly PropertyInfo TranslationsSelector = ExpressionHelper.GetPropertyInfo<DictionaryItem, IEnumerable<DictionaryTranslation>>(x => x.Translations);
|
||||
private static readonly PropertyInfo TranslationsSelector = ExpressionHelper.GetPropertyInfo<DictionaryItem, IEnumerable<IDictionaryTranslation>>(x => x.Translations);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or Sets the Parent Id of the Dictionary Item
|
||||
@@ -60,7 +60,7 @@ namespace Umbraco.Core.Models
|
||||
/// Gets or sets a list of translations for the Dictionary Item
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
public IEnumerable<DictionaryTranslation> Translations
|
||||
public IEnumerable<IDictionaryTranslation> Translations
|
||||
{
|
||||
get { return _translations; }
|
||||
set
|
||||
|
||||
@@ -10,32 +10,32 @@ namespace Umbraco.Core.Models
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract(IsReference = true)]
|
||||
public class DictionaryTranslation : Entity
|
||||
public class DictionaryTranslation : Entity, IDictionaryTranslation
|
||||
{
|
||||
private Language _language;
|
||||
private ILanguage _language;
|
||||
private string _value;
|
||||
|
||||
public DictionaryTranslation(Language language, string value)
|
||||
public DictionaryTranslation(ILanguage language, string value)
|
||||
{
|
||||
_language = language;
|
||||
_value = value;
|
||||
}
|
||||
|
||||
public DictionaryTranslation(Language language, string value, Guid uniqueId)
|
||||
public DictionaryTranslation(ILanguage language, string value, Guid uniqueId)
|
||||
{
|
||||
_language = language;
|
||||
_value = value;
|
||||
Key = uniqueId;
|
||||
}
|
||||
|
||||
private static readonly PropertyInfo LanguageSelector = ExpressionHelper.GetPropertyInfo<DictionaryTranslation, Language>(x => x.Language);
|
||||
private static readonly PropertyInfo LanguageSelector = ExpressionHelper.GetPropertyInfo<DictionaryTranslation, ILanguage>(x => x.Language);
|
||||
private static readonly PropertyInfo ValueSelector = ExpressionHelper.GetPropertyInfo<DictionaryTranslation, string>(x => x.Value);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="Language"/> for the translation
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
public Language Language
|
||||
public ILanguage Language
|
||||
{
|
||||
get { return _language; }
|
||||
set
|
||||
|
||||
28
src/Umbraco.Core/Models/IDictionaryItem.cs
Normal file
28
src/Umbraco.Core/Models/IDictionaryItem.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
using Umbraco.Core.Models.EntityBase;
|
||||
|
||||
namespace Umbraco.Core.Models
|
||||
{
|
||||
public interface IDictionaryItem : IAggregateRoot
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or Sets the Parent Id of the Dictionary Item
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
Guid ParentId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Key for the Dictionary Item
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
string ItemKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a list of translations for the Dictionary Item
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
IEnumerable<IDictionaryTranslation> Translations { get; set; }
|
||||
}
|
||||
}
|
||||
20
src/Umbraco.Core/Models/IDictionaryTranslation.cs
Normal file
20
src/Umbraco.Core/Models/IDictionaryTranslation.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Runtime.Serialization;
|
||||
using Umbraco.Core.Models.EntityBase;
|
||||
|
||||
namespace Umbraco.Core.Models
|
||||
{
|
||||
public interface IDictionaryTranslation : IEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="Language"/> for the translation
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
ILanguage Language { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the translated text
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
string Value { get; set; }
|
||||
}
|
||||
}
|
||||
27
src/Umbraco.Core/Models/ILanguage.cs
Normal file
27
src/Umbraco.Core/Models/ILanguage.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Globalization;
|
||||
using System.Runtime.Serialization;
|
||||
using Umbraco.Core.Models.EntityBase;
|
||||
|
||||
namespace Umbraco.Core.Models
|
||||
{
|
||||
public interface ILanguage : IAggregateRoot
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the Iso Code for the Language
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
string IsoCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Culture Name for the Language
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
string CultureName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="CultureInfo"/> object for the current Language
|
||||
/// </summary>
|
||||
[IgnoreDataMember]
|
||||
CultureInfo CultureInfo { get; }
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ namespace Umbraco.Core.Models
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract(IsReference = true)]
|
||||
public class Language : Entity, IAggregateRoot
|
||||
public class Language : Entity, ILanguage
|
||||
{
|
||||
private string _isoCode;
|
||||
private string _cultureName;
|
||||
|
||||
@@ -4,11 +4,11 @@ using Umbraco.Core.Models.Rdbms;
|
||||
|
||||
namespace Umbraco.Core.Persistence.Factories
|
||||
{
|
||||
internal class DictionaryItemFactory : IEntityFactory<DictionaryItem, DictionaryDto>
|
||||
internal class DictionaryItemFactory : IEntityFactory<IDictionaryItem, DictionaryDto>
|
||||
{
|
||||
#region Implementation of IEntityFactory<DictionaryItem,DictionaryDto>
|
||||
|
||||
public DictionaryItem BuildEntity(DictionaryDto dto)
|
||||
public IDictionaryItem BuildEntity(DictionaryDto dto)
|
||||
{
|
||||
return new DictionaryItem(dto.Parent, dto.Key)
|
||||
{
|
||||
@@ -17,7 +17,7 @@ namespace Umbraco.Core.Persistence.Factories
|
||||
};
|
||||
}
|
||||
|
||||
public DictionaryDto BuildDto(DictionaryItem entity)
|
||||
public DictionaryDto BuildDto(IDictionaryItem entity)
|
||||
{
|
||||
return new DictionaryDto
|
||||
{
|
||||
@@ -31,7 +31,7 @@ namespace Umbraco.Core.Persistence.Factories
|
||||
|
||||
#endregion
|
||||
|
||||
private List<LanguageTextDto> BuildLanguageTextDtos(DictionaryItem entity)
|
||||
private List<LanguageTextDto> BuildLanguageTextDtos(IDictionaryItem entity)
|
||||
{
|
||||
var list = new List<LanguageTextDto>();
|
||||
foreach (var translation in entity.Translations)
|
||||
|
||||
@@ -4,12 +4,12 @@ using Umbraco.Core.Models.Rdbms;
|
||||
|
||||
namespace Umbraco.Core.Persistence.Factories
|
||||
{
|
||||
internal class DictionaryTranslationFactory : IEntityFactory<DictionaryTranslation, LanguageTextDto>
|
||||
internal class DictionaryTranslationFactory : IEntityFactory<IDictionaryTranslation, LanguageTextDto>
|
||||
{
|
||||
private readonly Guid _uniqueId;
|
||||
private Language _language;
|
||||
private ILanguage _language;
|
||||
|
||||
public DictionaryTranslationFactory(Guid uniqueId, Language language)
|
||||
public DictionaryTranslationFactory(Guid uniqueId, ILanguage language)
|
||||
{
|
||||
_uniqueId = uniqueId;
|
||||
_language = language;
|
||||
@@ -17,13 +17,13 @@ namespace Umbraco.Core.Persistence.Factories
|
||||
|
||||
#region Implementation of IEntityFactory<DictionaryTranslation,LanguageTextDto>
|
||||
|
||||
public DictionaryTranslation BuildEntity(LanguageTextDto dto)
|
||||
public IDictionaryTranslation BuildEntity(LanguageTextDto dto)
|
||||
{
|
||||
return new DictionaryTranslation(_language, dto.Value, _uniqueId)
|
||||
{Id = dto.PrimaryKey};
|
||||
}
|
||||
|
||||
public LanguageTextDto BuildDto(DictionaryTranslation entity)
|
||||
public LanguageTextDto BuildDto(IDictionaryTranslation entity)
|
||||
{
|
||||
var text = new LanguageTextDto
|
||||
{
|
||||
|
||||
@@ -4,16 +4,16 @@ using Umbraco.Core.Models.Rdbms;
|
||||
|
||||
namespace Umbraco.Core.Persistence.Factories
|
||||
{
|
||||
internal class LanguageFactory : IEntityFactory<Language, LanguageDto>
|
||||
internal class LanguageFactory : IEntityFactory<ILanguage, LanguageDto>
|
||||
{
|
||||
#region Implementation of IEntityFactory<Language,LanguageDto>
|
||||
|
||||
public Language BuildEntity(LanguageDto dto)
|
||||
public ILanguage BuildEntity(LanguageDto dto)
|
||||
{
|
||||
return new Language(dto.IsoCode){CultureName = dto.CultureName, Id = dto.Id};
|
||||
}
|
||||
|
||||
public LanguageDto BuildDto(Language entity)
|
||||
public LanguageDto BuildDto(ILanguage entity)
|
||||
{
|
||||
var dto = new LanguageDto{ CultureName = entity.CultureName, IsoCode = entity.IsoCode};
|
||||
if (entity.HasIdentity)
|
||||
|
||||
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Models.EntityBase;
|
||||
using Umbraco.Core.Models.Rdbms;
|
||||
using Umbraco.Core.Persistence.Caching;
|
||||
using Umbraco.Core.Persistence.Factories;
|
||||
@@ -14,7 +15,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// <summary>
|
||||
/// Represents a repository for doing CRUD operations for <see cref="DictionaryItem"/>
|
||||
/// </summary>
|
||||
internal class DictionaryRepository : PetaPocoRepositoryBase<int, DictionaryItem>, IDictionaryRepository
|
||||
internal class DictionaryRepository : PetaPocoRepositoryBase<int, IDictionaryItem>, IDictionaryRepository
|
||||
{
|
||||
private readonly ILanguageRepository _languageRepository;
|
||||
|
||||
@@ -31,7 +32,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
|
||||
#region Overrides of RepositoryBase<int,DictionaryItem>
|
||||
|
||||
protected override DictionaryItem PerformGet(int id)
|
||||
protected override IDictionaryItem PerformGet(int id)
|
||||
{
|
||||
var sql = GetBaseQuery(false);
|
||||
sql.Append(GetBaseWhereClause(id));
|
||||
@@ -43,7 +44,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
var factory = new DictionaryItemFactory();
|
||||
var entity = factory.BuildEntity(dto);
|
||||
|
||||
var list = new List<DictionaryTranslation>();
|
||||
var list = new List<IDictionaryTranslation>();
|
||||
foreach (var textDto in dto.LanguageTextDtos)
|
||||
{
|
||||
var language = _languageRepository.Get(textDto.LanguageId);
|
||||
@@ -55,7 +56,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
return entity;
|
||||
}
|
||||
|
||||
protected override IEnumerable<DictionaryItem> PerformGetAll(params int[] ids)
|
||||
protected override IEnumerable<IDictionaryItem> PerformGetAll(params int[] ids)
|
||||
{
|
||||
if (ids.Any())
|
||||
{
|
||||
@@ -74,10 +75,10 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
}
|
||||
}
|
||||
|
||||
protected override IEnumerable<DictionaryItem> PerformGetByQuery(IQuery<DictionaryItem> query)
|
||||
protected override IEnumerable<IDictionaryItem> PerformGetByQuery(IQuery<IDictionaryItem> query)
|
||||
{
|
||||
var sqlClause = GetBaseQuery(false);
|
||||
var translator = new SqlTranslator<DictionaryItem>(sqlClause, query);
|
||||
var translator = new SqlTranslator<IDictionaryItem>(sqlClause, query);
|
||||
var sql = translator.Translate();
|
||||
|
||||
var dtos = Database.Fetch<DictionaryDto, LanguageTextDto>(sql);
|
||||
@@ -125,9 +126,9 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
|
||||
#region Unit of Work Implementation
|
||||
|
||||
protected override void PersistNewItem(DictionaryItem entity)
|
||||
protected override void PersistNewItem(IDictionaryItem entity)
|
||||
{
|
||||
entity.AddingEntity();
|
||||
((Entity)entity).AddingEntity();
|
||||
|
||||
var factory = new DictionaryItemFactory();
|
||||
var dto = factory.BuildDto(entity);
|
||||
@@ -142,12 +143,12 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
translation.Id = Convert.ToInt32(Database.Insert(textDto));
|
||||
}
|
||||
|
||||
entity.ResetDirtyProperties();
|
||||
((Entity)entity).ResetDirtyProperties();
|
||||
}
|
||||
|
||||
protected override void PersistUpdatedItem(DictionaryItem entity)
|
||||
protected override void PersistUpdatedItem(IDictionaryItem entity)
|
||||
{
|
||||
entity.UpdatingEntity();
|
||||
((Entity)entity).UpdatingEntity();
|
||||
|
||||
var factory = new DictionaryItemFactory();
|
||||
var dto = factory.BuildDto(entity);
|
||||
@@ -168,10 +169,10 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
}
|
||||
}
|
||||
|
||||
entity.ResetDirtyProperties();
|
||||
((Entity)entity).ResetDirtyProperties();
|
||||
}
|
||||
|
||||
protected override void PersistDeletedItem(DictionaryItem entity)
|
||||
protected override void PersistDeletedItem(IDictionaryItem entity)
|
||||
{
|
||||
RecursiveDelete(entity.Key);
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Umbraco.Core.Persistence.Repositories
|
||||
{
|
||||
internal interface IDictionaryRepository : IRepositoryQueryable<int, DictionaryItem>
|
||||
internal interface IDictionaryRepository : IRepositoryQueryable<int, IDictionaryItem>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Umbraco.Core.Persistence.Repositories
|
||||
{
|
||||
public interface ILanguageRepository : IRepositoryQueryable<int, Language>
|
||||
public interface ILanguageRepository : IRepositoryQueryable<int, ILanguage>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Models.EntityBase;
|
||||
using Umbraco.Core.Models.Rdbms;
|
||||
using Umbraco.Core.Persistence.Caching;
|
||||
using Umbraco.Core.Persistence.Factories;
|
||||
@@ -13,7 +14,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// <summary>
|
||||
/// Represents a repository for doing CRUD operations for <see cref="Language"/>
|
||||
/// </summary>
|
||||
internal class LanguageRepository : PetaPocoRepositoryBase<int, Language>, ILanguageRepository
|
||||
internal class LanguageRepository : PetaPocoRepositoryBase<int, ILanguage>, ILanguageRepository
|
||||
{
|
||||
public LanguageRepository(IUnitOfWork work) : base(work)
|
||||
{
|
||||
@@ -26,7 +27,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
|
||||
#region Overrides of RepositoryBase<int,Language>
|
||||
|
||||
protected override Language PerformGet(int id)
|
||||
protected override ILanguage PerformGet(int id)
|
||||
{
|
||||
var sql = GetBaseQuery(false);
|
||||
sql.Append(GetBaseWhereClause(id));
|
||||
@@ -40,7 +41,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
return entity;
|
||||
}
|
||||
|
||||
protected override IEnumerable<Language> PerformGetAll(params int[] ids)
|
||||
protected override IEnumerable<ILanguage> PerformGetAll(params int[] ids)
|
||||
{
|
||||
if (ids.Any())
|
||||
{
|
||||
@@ -59,10 +60,10 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
}
|
||||
}
|
||||
|
||||
protected override IEnumerable<Language> PerformGetByQuery(IQuery<Language> query)
|
||||
protected override IEnumerable<ILanguage> PerformGetByQuery(IQuery<ILanguage> query)
|
||||
{
|
||||
var sqlClause = GetBaseQuery(false);
|
||||
var translator = new SqlTranslator<Language>(sqlClause, query);
|
||||
var translator = new SqlTranslator<ILanguage>(sqlClause, query);
|
||||
var sql = translator.Translate();
|
||||
|
||||
var dtos = Database.Fetch<LanguageDto>(sql);
|
||||
@@ -111,9 +112,9 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
|
||||
#region Unit of Work Implementation
|
||||
|
||||
protected override void PersistNewItem(Language entity)
|
||||
protected override void PersistNewItem(ILanguage entity)
|
||||
{
|
||||
entity.AddingEntity();
|
||||
((Entity)entity).AddingEntity();
|
||||
|
||||
var factory = new LanguageFactory();
|
||||
var dto = factory.BuildDto(entity);
|
||||
@@ -121,19 +122,19 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
var id = Convert.ToInt32(Database.Insert(dto));
|
||||
entity.Id = id;
|
||||
|
||||
entity.ResetDirtyProperties();
|
||||
((Entity)entity).ResetDirtyProperties();
|
||||
}
|
||||
|
||||
protected override void PersistUpdatedItem(Language entity)
|
||||
protected override void PersistUpdatedItem(ILanguage entity)
|
||||
{
|
||||
entity.UpdatingEntity();
|
||||
((Entity)entity).UpdatingEntity();
|
||||
|
||||
var factory = new LanguageFactory();
|
||||
var dto = factory.BuildDto(entity);
|
||||
|
||||
Database.Update(dto);
|
||||
|
||||
entity.ResetDirtyProperties();
|
||||
((Entity)entity).ResetDirtyProperties();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -88,7 +88,10 @@
|
||||
<Compile Include="Models\DictionaryTranslation.cs" />
|
||||
<Compile Include="Models\File.cs" />
|
||||
<Compile Include="Models\IDataTypeDefinition.cs" />
|
||||
<Compile Include="Models\IDictionaryItem.cs" />
|
||||
<Compile Include="Models\IDictionaryTranslation.cs" />
|
||||
<Compile Include="Models\IFile.cs" />
|
||||
<Compile Include="Models\ILanguage.cs" />
|
||||
<Compile Include="Models\Language.cs" />
|
||||
<Compile Include="Models\Media.cs" />
|
||||
<Compile Include="Models\MediaType.cs" />
|
||||
|
||||
@@ -94,7 +94,7 @@ namespace Umbraco.Tests.Persistence
|
||||
var uow = new PetaPocoUnitOfWork();
|
||||
|
||||
// Act
|
||||
var repository = RepositoryResolver.ResolveByType<IDictionaryRepository, DictionaryItem, int>(uow);
|
||||
var repository = RepositoryResolver.ResolveByType<IDictionaryRepository, IDictionaryItem, int>(uow);
|
||||
|
||||
// Assert
|
||||
Assert.That(repository, Is.Not.Null);
|
||||
@@ -107,7 +107,7 @@ namespace Umbraco.Tests.Persistence
|
||||
var uow = new PetaPocoUnitOfWork();
|
||||
|
||||
// Act
|
||||
var repository = RepositoryResolver.ResolveByType<ILanguageRepository, Language, int>(uow);
|
||||
var repository = RepositoryResolver.ResolveByType<ILanguageRepository, ILanguage, int>(uow);
|
||||
|
||||
// Assert
|
||||
Assert.That(repository, Is.Not.Null);
|
||||
|
||||
@@ -1,10 +1,104 @@
|
||||
namespace Umbraco.Web.Services
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Umbraco.Core.Models;
|
||||
|
||||
namespace Umbraco.Web.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the Localization Service, which is an easy access to operations involving Languages and Dictionary
|
||||
/// </summary>
|
||||
public interface ILocalizationService : IService
|
||||
{
|
||||
|
||||
//Possible to-do list:
|
||||
//Import DictionaryItem (?)
|
||||
//RemoveByLanguage (translations)
|
||||
//Add/Set Text (Insert/Update)
|
||||
//Remove Text (in translation)
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IDictionaryItem"/> by its <see cref="Int32"/> id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the <see cref="IDictionaryItem"/></param>
|
||||
/// <returns><see cref="IDictionaryItem"/></returns>
|
||||
IDictionaryItem GetDictionaryItemById(int id);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IDictionaryItem"/> by its <see cref="Guid"/> id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the <see cref="IDictionaryItem"/></param>
|
||||
/// <returns><see cref="IDictionaryItem"/></returns>
|
||||
IDictionaryItem GetDictionaryItemById(Guid id);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IDictionaryItem"/> by its key
|
||||
/// </summary>
|
||||
/// <param name="key">Key of the <see cref="IDictionaryItem"/></param>
|
||||
/// <returns><see cref="IDictionaryItem"/></returns>
|
||||
IDictionaryItem GetDictionaryItemByKey(string key);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of children for a <see cref="IDictionaryItem"/>
|
||||
/// </summary>
|
||||
/// <param name="parentId">Id of the parent</param>
|
||||
/// <returns>An enumerable list of <see cref="IDictionaryItem"/> objects</returns>
|
||||
IEnumerable<IDictionaryItem> GetDictionaryItemChildren(Guid parentId);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the root/top <see cref="IDictionaryItem"/> objects
|
||||
/// </summary>
|
||||
/// <returns>An enumerable list of <see cref="IDictionaryItem"/> objects</returns>
|
||||
IEnumerable<IDictionaryItem> GetRootDictionaryItems();
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a <see cref="IDictionaryItem"/> with given key exists
|
||||
/// </summary>
|
||||
/// <param name="key">Key of the <see cref="IDictionaryItem"/></param>
|
||||
/// <returns>True if a <see cref="IDictionaryItem"/> exists, otherwise false</returns>
|
||||
bool DictionaryItemExists(string key);
|
||||
|
||||
/// <summary>
|
||||
/// Saves a <see cref="IDictionaryItem"/> object
|
||||
/// </summary>
|
||||
/// <param name="dictionaryItem"><see cref="IDictionaryItem"/> to save</param>
|
||||
void Save(IDictionaryItem dictionaryItem);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a <see cref="IDictionaryItem"/> object and its related translations
|
||||
/// as well as its children.
|
||||
/// </summary>
|
||||
/// <param name="dictionaryItem"><see cref="IDictionaryItem"/> to delete</param>
|
||||
void Delete(IDictionaryItem dictionaryItem);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="ILanguage"/> by its id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the <see cref="ILanguage"/></param>
|
||||
/// <returns><see cref="ILanguage"/></returns>
|
||||
ILanguage GetLanguageById(int id);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="ILanguage"/> by its culture code
|
||||
/// </summary>
|
||||
/// <param name="culture">Culture Code</param>
|
||||
/// <returns><see cref="ILanguage"/></returns>
|
||||
ILanguage GetLanguageByCultureCode(string culture);
|
||||
|
||||
/// <summary>
|
||||
/// Gets all available languages
|
||||
/// </summary>
|
||||
/// <returns>An enumerable list of <see cref="ILanguage"/> objects</returns>
|
||||
IEnumerable<ILanguage> GetAllLanguages();
|
||||
|
||||
/// <summary>
|
||||
/// Saves a <see cref="ILanguage"/> object
|
||||
/// </summary>
|
||||
/// <param name="language"><see cref="ILanguage"/> to save</param>
|
||||
void Save(ILanguage language);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a <see cref="ILanguage"/> by removing it and its usages from the db
|
||||
/// </summary>
|
||||
/// <param name="language"><see cref="ILanguage"/> to delete</param>
|
||||
void Delete(ILanguage language);
|
||||
}
|
||||
}
|
||||
215
src/Umbraco.Web/Services/LocalizationService.cs
Normal file
215
src/Umbraco.Web/Services/LocalizationService.cs
Normal file
@@ -0,0 +1,215 @@
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the Localization Service, which is an easy access to operations involving <see cref="Language"/> and <see cref="DictionaryItem"/>
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IDictionaryItem"/> by its <see cref="Int32"/> id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the <see cref="IDictionaryItem"/></param>
|
||||
/// <returns><see cref="IDictionaryItem"/></returns>
|
||||
public IDictionaryItem GetDictionaryItemById(int id)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IDictionaryRepository, IDictionaryItem, int>(unitOfWork);
|
||||
return repository.Get(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IDictionaryItem"/> by its <see cref="Guid"/> id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the <see cref="IDictionaryItem"/></param>
|
||||
/// <returns><see cref="DictionaryItem"/></returns>
|
||||
public IDictionaryItem GetDictionaryItemById(Guid id)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IDictionaryRepository, IDictionaryItem, int>(unitOfWork);
|
||||
|
||||
var query = Query<IDictionaryItem>.Builder.Where(x => x.Key == id);
|
||||
var items = repository.GetByQuery(query);
|
||||
|
||||
return items.FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IDictionaryItem"/> by its key
|
||||
/// </summary>
|
||||
/// <param name="key">Key of the <see cref="IDictionaryItem"/></param>
|
||||
/// <returns><see cref="IDictionaryItem"/></returns>
|
||||
public IDictionaryItem GetDictionaryItemByKey(string key)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IDictionaryRepository, IDictionaryItem, int>(unitOfWork);
|
||||
|
||||
var query = Query<IDictionaryItem>.Builder.Where(x => x.ItemKey == key);
|
||||
var items = repository.GetByQuery(query);
|
||||
|
||||
return items.FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of children for a <see cref="IDictionaryItem"/>
|
||||
/// </summary>
|
||||
/// <param name="parentId">Id of the parent</param>
|
||||
/// <returns>An enumerable list of <see cref="IDictionaryItem"/> objects</returns>
|
||||
public IEnumerable<IDictionaryItem> GetDictionaryItemChildren(Guid parentId)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IDictionaryRepository, IDictionaryItem, int>(unitOfWork);
|
||||
|
||||
var query = Query<IDictionaryItem>.Builder.Where(x => x.ParentId == parentId);
|
||||
var items = repository.GetByQuery(query);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the root/top <see cref="IDictionaryItem"/> objects
|
||||
/// </summary>
|
||||
/// <returns>An enumerable list of <see cref="IDictionaryItem"/> objects</returns>
|
||||
public IEnumerable<IDictionaryItem> GetRootDictionaryItems()
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IDictionaryRepository, IDictionaryItem, int>(unitOfWork);
|
||||
|
||||
var query = Query<IDictionaryItem>.Builder.Where(x => x.ParentId == RootParentId);
|
||||
var items = repository.GetByQuery(query);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a <see cref="IDictionaryItem"/> with given key exists
|
||||
/// </summary>
|
||||
/// <param name="key">Key of the <see cref="IDictionaryItem"/></param>
|
||||
/// <returns>True if a <see cref="IDictionaryItem"/> exists, otherwise false</returns>
|
||||
public bool DictionaryItemExists(string key)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IDictionaryRepository, IDictionaryItem, int>(unitOfWork);
|
||||
|
||||
var query = Query<IDictionaryItem>.Builder.Where(x => x.ItemKey == key);
|
||||
var items = repository.GetByQuery(query);
|
||||
|
||||
return items.Any();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a <see cref="IDictionaryItem"/> object
|
||||
/// </summary>
|
||||
/// <param name="dictionaryItem"><see cref="IDictionaryItem"/> to save</param>
|
||||
public void Save(IDictionaryItem dictionaryItem)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IDictionaryRepository, IDictionaryItem, int>(unitOfWork);
|
||||
|
||||
repository.AddOrUpdate(dictionaryItem);
|
||||
unitOfWork.Commit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a <see cref="IDictionaryItem"/> object and its related translations
|
||||
/// as well as its children.
|
||||
/// </summary>
|
||||
/// <param name="dictionaryItem"><see cref="IDictionaryItem"/> to delete</param>
|
||||
public void Delete(IDictionaryItem dictionaryItem)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IDictionaryRepository, IDictionaryItem, int>(unitOfWork);
|
||||
|
||||
//NOTE: The recursive delete is done in the repository
|
||||
repository.Delete(dictionaryItem);
|
||||
unitOfWork.Commit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="Language"/> by its id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the <see cref="Language"/></param>
|
||||
/// <returns><see cref="Language"/></returns>
|
||||
public ILanguage GetLanguageById(int id)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<ILanguageRepository, ILanguage, int>(unitOfWork);
|
||||
return repository.Get(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="Language"/> by its culture code
|
||||
/// </summary>
|
||||
/// <param name="culture">Culture Code</param>
|
||||
/// <returns><see cref="Language"/></returns>
|
||||
public ILanguage GetLanguageByCultureCode(string culture)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<ILanguageRepository, ILanguage, int>(unitOfWork);
|
||||
|
||||
var query = Query<ILanguage>.Builder.Where(x => x.CultureName == culture);
|
||||
var items = repository.GetByQuery(query);
|
||||
|
||||
return items.FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all available languages
|
||||
/// </summary>
|
||||
/// <returns>An enumerable list of <see cref="ILanguage"/> objects</returns>
|
||||
public IEnumerable<ILanguage> GetAllLanguages()
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<ILanguageRepository, ILanguage, int>(unitOfWork);
|
||||
var languages = repository.GetAll();
|
||||
return languages;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a <see cref="ILanguage"/> object
|
||||
/// </summary>
|
||||
/// <param name="language"><see cref="ILanguage"/> to save</param>
|
||||
public void Save(ILanguage language)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<ILanguageRepository, ILanguage, int>(unitOfWork);
|
||||
|
||||
repository.AddOrUpdate(language);
|
||||
unitOfWork.Commit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a <see cref="ILanguage"/> by removing it (but not its usages) from the db
|
||||
/// </summary>
|
||||
/// <param name="language"><see cref="ILanguage"/> to delete</param>
|
||||
public void Delete(ILanguage language)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<ILanguageRepository, ILanguage, int>(unitOfWork);
|
||||
|
||||
//NOTE: There isn't any constraints in the db, so possible references aren't deleted
|
||||
repository.Delete(language);
|
||||
unitOfWork.Commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -324,6 +324,7 @@
|
||||
<Compile Include="Services\ILocalizationService.cs" />
|
||||
<Compile Include="Services\IMediaService.cs" />
|
||||
<Compile Include="Services\IService.cs" />
|
||||
<Compile Include="Services\LocalizationService.cs" />
|
||||
<Compile Include="Services\MediaService.cs" />
|
||||
<Compile Include="Templates\TemplateUtilities.cs" />
|
||||
<Compile Include="umbraco.presentation\Default.aspx.cs">
|
||||
|
||||
Reference in New Issue
Block a user