2017-07-20 11:21:28 +02:00
|
|
|
|
using System;
|
2012-10-24 12:22:52 -02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2012-12-15 10:43:03 +05:00
|
|
|
|
using Umbraco.Core.Events;
|
2015-01-09 18:53:08 +11:00
|
|
|
|
using Umbraco.Core.Logging;
|
2012-10-24 12:22:52 -02:00
|
|
|
|
using Umbraco.Core.Models;
|
2016-04-28 08:48:59 +02:00
|
|
|
|
using Umbraco.Core.Persistence.Repositories;
|
2012-10-24 12:22:52 -02:00
|
|
|
|
using Umbraco.Core.Persistence.UnitOfWork;
|
|
|
|
|
|
|
2012-11-12 07:40:11 -01:00
|
|
|
|
namespace Umbraco.Core.Services
|
2012-10-24 12:22:52 -02:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents the Localization Service, which is an easy access to operations involving <see cref="Language"/> and <see cref="DictionaryItem"/>
|
|
|
|
|
|
/// </summary>
|
2017-05-12 14:49:44 +02:00
|
|
|
|
public class LocalizationService : ScopeRepositoryService, ILocalizationService
|
2012-10-24 12:22:52 -02:00
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
public LocalizationService(IScopeUnitOfWorkProvider provider, ILogger logger, IEventMessagesFactory eventMessagesFactory)
|
2016-05-02 12:17:30 +02:00
|
|
|
|
: base(provider, logger, eventMessagesFactory)
|
2017-05-12 14:49:44 +02:00
|
|
|
|
{ }
|
2012-10-24 12:22:52 -02:00
|
|
|
|
|
2015-01-12 19:39:30 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Adds or updates a translation for a dictionary item and language
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="item"></param>
|
|
|
|
|
|
/// <param name="language"></param>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// This does not save the item, that needs to be done explicitly
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public void AddOrUpdateDictionaryValue(IDictionaryItem item, ILanguage language, string value)
|
|
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
if (item == null) throw new ArgumentNullException(nameof(item));
|
|
|
|
|
|
if (language == null) throw new ArgumentNullException(nameof(language));
|
2015-01-12 19:39:30 +11:00
|
|
|
|
|
|
|
|
|
|
var existing = item.Translations.FirstOrDefault(x => x.Language.Id == language.Id);
|
|
|
|
|
|
if (existing != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
existing.Value = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
item.Translations = new List<IDictionaryTranslation>(item.Translations)
|
|
|
|
|
|
{
|
|
|
|
|
|
new DictionaryTranslation(language, value)
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-01-12 18:52:30 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates and saves a new dictionary item and assigns a value to all languages if defaultValue is specified.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key"></param>
|
|
|
|
|
|
/// <param name="parentId"></param>
|
|
|
|
|
|
/// <param name="defaultValue"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public IDictionaryItem CreateDictionaryItemWithIdentity(string key, Guid? parentId, string defaultValue = null)
|
|
|
|
|
|
{
|
2016-05-02 12:24:13 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork())
|
2015-01-12 18:52:30 +11:00
|
|
|
|
{
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<IDictionaryRepository>();
|
2017-05-12 14:49:44 +02:00
|
|
|
|
|
2015-01-12 18:52:30 +11:00
|
|
|
|
//validate the parent
|
|
|
|
|
|
if (parentId.HasValue && parentId.Value != Guid.Empty)
|
|
|
|
|
|
{
|
|
|
|
|
|
var parent = GetDictionaryItemById(parentId.Value);
|
|
|
|
|
|
if (parent == null)
|
2017-05-12 14:49:44 +02:00
|
|
|
|
throw new ArgumentException($"No parent dictionary item was found with id {parentId.Value}.");
|
2015-01-12 18:52:30 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
var item = new DictionaryItem(parentId, key);
|
2015-01-12 18:52:30 +11:00
|
|
|
|
|
|
|
|
|
|
if (defaultValue.IsNullOrWhiteSpace() == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
var langs = GetAllLanguages();
|
|
|
|
|
|
var translations = langs.Select(language => new DictionaryTranslation(language, defaultValue))
|
|
|
|
|
|
.Cast<IDictionaryTranslation>()
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
item.Translations = translations;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-18 15:33:13 +02:00
|
|
|
|
var saveEventArgs = new SaveEventArgs<IDictionaryItem>(item);
|
|
|
|
|
|
if (uow.Events.DispatchCancelable(SavingDictionaryItem, this, saveEventArgs))
|
2017-05-12 14:49:44 +02:00
|
|
|
|
{
|
|
|
|
|
|
uow.Complete();
|
|
|
|
|
|
return item;
|
|
|
|
|
|
}
|
2015-01-12 18:52:30 +11:00
|
|
|
|
|
|
|
|
|
|
repository.AddOrUpdate(item);
|
2016-05-02 12:12:21 +02:00
|
|
|
|
uow.Complete();
|
2016-01-07 18:44:04 +01:00
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
// ensure the lazy Language callback is assigned
|
|
|
|
|
|
EnsureDictionaryItemLanguageCallback(item);
|
2017-09-18 15:33:13 +02:00
|
|
|
|
saveEventArgs.CanCancel = false;
|
|
|
|
|
|
uow.Events.Dispatch(SavedDictionaryItem, this, saveEventArgs);
|
2015-01-12 18:52:30 +11:00
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
return item;
|
|
|
|
|
|
}
|
2015-01-12 18:52:30 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-24 12:22:52 -02:00
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
|
2012-12-14 13:06:58 -01:00
|
|
|
|
{
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<IDictionaryRepository>();
|
2016-01-07 18:44:04 +01:00
|
|
|
|
var item = repository.Get(id);
|
|
|
|
|
|
//ensure the lazy Language callback is assigned
|
|
|
|
|
|
EnsureDictionaryItemLanguageCallback(item);
|
|
|
|
|
|
return item;
|
2012-12-14 13:06:58 -01:00
|
|
|
|
}
|
2012-10-24 12:22:52 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
|
2012-12-14 13:06:58 -01:00
|
|
|
|
{
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<IDictionaryRepository>();
|
2016-01-07 18:44:04 +01:00
|
|
|
|
var item = repository.Get(id);
|
|
|
|
|
|
//ensure the lazy Language callback is assigned
|
|
|
|
|
|
EnsureDictionaryItemLanguageCallback(item);
|
|
|
|
|
|
return item;
|
2012-12-14 13:06:58 -01:00
|
|
|
|
}
|
2012-10-24 12:22:52 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
|
2012-12-14 13:06:58 -01:00
|
|
|
|
{
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<IDictionaryRepository>();
|
2016-01-07 18:44:04 +01:00
|
|
|
|
var item = repository.Get(key);
|
|
|
|
|
|
//ensure the lazy Language callback is assigned
|
|
|
|
|
|
EnsureDictionaryItemLanguageCallback(item);
|
|
|
|
|
|
return item;
|
2012-12-14 13:06:58 -01:00
|
|
|
|
}
|
2012-10-24 12:22:52 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
|
2012-12-14 13:06:58 -01:00
|
|
|
|
{
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<IDictionaryRepository>();
|
2017-09-22 18:28:21 +02:00
|
|
|
|
var query = Query<IDictionaryItem>().Where(x => x.ParentId == parentId);
|
2016-01-07 18:44:04 +01:00
|
|
|
|
var items = repository.GetByQuery(query).ToArray();
|
|
|
|
|
|
//ensure the lazy Language callback is assigned
|
2017-05-12 14:49:44 +02:00
|
|
|
|
foreach (var item in items)
|
|
|
|
|
|
EnsureDictionaryItemLanguageCallback(item);
|
2012-12-14 13:06:58 -01:00
|
|
|
|
return items;
|
|
|
|
|
|
}
|
2012-10-24 12:22:52 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-22 12:10:21 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a list of descendants for a <see cref="IDictionaryItem"/>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="parentId">Id of the parent, null will return all dictionary items</param>
|
|
|
|
|
|
/// <returns>An enumerable list of <see cref="IDictionaryItem"/> objects</returns>
|
|
|
|
|
|
public IEnumerable<IDictionaryItem> GetDictionaryItemDescendants(Guid? parentId)
|
|
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
|
2015-07-22 12:10:21 +02:00
|
|
|
|
{
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<IDictionaryRepository>();
|
2016-01-07 18:44:04 +01:00
|
|
|
|
var items = repository.GetDictionaryItemDescendants(parentId).ToArray();
|
|
|
|
|
|
//ensure the lazy Language callback is assigned
|
2017-05-12 14:49:44 +02:00
|
|
|
|
foreach (var item in items)
|
|
|
|
|
|
EnsureDictionaryItemLanguageCallback(item);
|
2016-01-07 18:44:04 +01:00
|
|
|
|
return items;
|
2015-07-22 12:10:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-24 12:22:52 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the root/top <see cref="IDictionaryItem"/> objects
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>An enumerable list of <see cref="IDictionaryItem"/> objects</returns>
|
|
|
|
|
|
public IEnumerable<IDictionaryItem> GetRootDictionaryItems()
|
|
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
|
2012-12-14 13:06:58 -01:00
|
|
|
|
{
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<IDictionaryRepository>();
|
2017-09-22 18:28:21 +02:00
|
|
|
|
var query = Query<IDictionaryItem>().Where(x => x.ParentId == null);
|
2016-01-07 18:44:04 +01:00
|
|
|
|
var items = repository.GetByQuery(query).ToArray();
|
|
|
|
|
|
//ensure the lazy Language callback is assigned
|
2017-05-12 14:49:44 +02:00
|
|
|
|
foreach (var item in items)
|
|
|
|
|
|
EnsureDictionaryItemLanguageCallback(item);
|
2012-12-14 13:06:58 -01:00
|
|
|
|
return items;
|
|
|
|
|
|
}
|
2012-10-24 12:22:52 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
|
2012-12-14 13:06:58 -01:00
|
|
|
|
{
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<IDictionaryRepository>();
|
2016-05-18 10:55:19 +02:00
|
|
|
|
var item = repository.Get(key);
|
|
|
|
|
|
return item != null;
|
2012-12-14 13:06:58 -01:00
|
|
|
|
}
|
2012-10-24 12:22:52 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Saves a <see cref="IDictionaryItem"/> object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dictionaryItem"><see cref="IDictionaryItem"/> to save</param>
|
2012-12-10 14:53:36 -01:00
|
|
|
|
/// <param name="userId">Optional id of the user saving the dictionary item</param>
|
2013-01-25 08:58:21 -01:00
|
|
|
|
public void Save(IDictionaryItem dictionaryItem, int userId = 0)
|
2012-10-24 12:22:52 -02:00
|
|
|
|
{
|
2016-05-02 12:24:13 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork())
|
2015-01-19 18:37:48 +11:00
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
if (uow.Events.DispatchCancelable(SavingDictionaryItem, this, new SaveEventArgs<IDictionaryItem>(dictionaryItem)))
|
|
|
|
|
|
{
|
|
|
|
|
|
uow.Complete();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<IDictionaryRepository>();
|
2015-01-19 18:37:48 +11:00
|
|
|
|
repository.AddOrUpdate(dictionaryItem);
|
2012-12-15 21:04:17 +05:00
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
// ensure the lazy Language callback is assigned
|
|
|
|
|
|
EnsureDictionaryItemLanguageCallback(dictionaryItem);
|
|
|
|
|
|
|
|
|
|
|
|
uow.Events.Dispatch(SavedDictionaryItem, this, new SaveEventArgs<IDictionaryItem>(dictionaryItem, false));
|
|
|
|
|
|
Audit(uow, AuditType.Save, "Save DictionaryItem performed by user", userId, dictionaryItem.Id);
|
2014-01-10 14:48:31 +01:00
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
uow.Complete();
|
|
|
|
|
|
}
|
2012-10-24 12:22:52 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2012-12-10 14:53:36 -01:00
|
|
|
|
/// <param name="userId">Optional id of the user deleting the dictionary item</param>
|
2013-01-25 08:58:21 -01:00
|
|
|
|
public void Delete(IDictionaryItem dictionaryItem, int userId = 0)
|
2012-10-24 12:22:52 -02:00
|
|
|
|
{
|
2016-05-02 12:24:13 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork())
|
2015-01-19 18:37:48 +11:00
|
|
|
|
{
|
2017-09-18 15:33:13 +02:00
|
|
|
|
var deleteEventArgs = new DeleteEventArgs<IDictionaryItem>(dictionaryItem);
|
|
|
|
|
|
if (uow.Events.DispatchCancelable(DeletingDictionaryItem, this, deleteEventArgs))
|
2017-05-12 14:49:44 +02:00
|
|
|
|
{
|
|
|
|
|
|
uow.Complete();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<IDictionaryRepository>();
|
2017-05-12 14:49:44 +02:00
|
|
|
|
repository.Delete(dictionaryItem);
|
2017-09-18 15:33:13 +02:00
|
|
|
|
deleteEventArgs.CanCancel = false;
|
|
|
|
|
|
uow.Events.Dispatch(DeletedDictionaryItem, this, deleteEventArgs);
|
2017-05-12 14:49:44 +02:00
|
|
|
|
|
|
|
|
|
|
Audit(uow, AuditType.Delete, "Delete DictionaryItem performed by user", userId, dictionaryItem.Id);
|
|
|
|
|
|
|
2016-05-02 12:12:21 +02:00
|
|
|
|
uow.Complete();
|
2015-01-19 18:37:48 +11:00
|
|
|
|
}
|
2012-10-24 12:22:52 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
|
2012-12-14 13:06:58 -01:00
|
|
|
|
{
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<ILanguageRepository>();
|
2017-05-12 14:49:44 +02:00
|
|
|
|
return repository.Get(id);
|
2012-12-14 13:06:58 -01:00
|
|
|
|
}
|
2012-10-24 12:22:52 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a <see cref="Language"/> by its culture code
|
|
|
|
|
|
/// </summary>
|
2014-01-10 14:48:31 +01:00
|
|
|
|
/// <param name="cultureName">Culture Name - also refered to as the Friendly name</param>
|
|
|
|
|
|
/// <returns><see cref="Language"/></returns>
|
|
|
|
|
|
public ILanguage GetLanguageByCultureCode(string cultureName)
|
|
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
|
2014-01-10 14:48:31 +01:00
|
|
|
|
{
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<ILanguageRepository>();
|
2017-05-12 14:49:44 +02:00
|
|
|
|
return repository.GetByCultureName(cultureName);
|
2014-01-10 14:48:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a <see cref="Language"/> by its iso code
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="isoCode">Iso Code of the language (ie. en-US)</param>
|
2012-10-24 12:22:52 -02:00
|
|
|
|
/// <returns><see cref="Language"/></returns>
|
2014-01-10 14:48:31 +01:00
|
|
|
|
public ILanguage GetLanguageByIsoCode(string isoCode)
|
2012-10-24 12:22:52 -02:00
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
|
2012-12-14 13:06:58 -01:00
|
|
|
|
{
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<ILanguageRepository>();
|
2017-05-12 14:49:44 +02:00
|
|
|
|
return repository.GetByIsoCode(isoCode);
|
2012-12-14 13:06:58 -01:00
|
|
|
|
}
|
2012-10-24 12:22:52 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets all available languages
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>An enumerable list of <see cref="ILanguage"/> objects</returns>
|
|
|
|
|
|
public IEnumerable<ILanguage> GetAllLanguages()
|
|
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
|
2012-12-14 13:06:58 -01:00
|
|
|
|
{
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<ILanguageRepository>();
|
2017-05-12 14:49:44 +02:00
|
|
|
|
return repository.GetAll();
|
2012-12-14 13:06:58 -01:00
|
|
|
|
}
|
2012-10-24 12:22:52 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Saves a <see cref="ILanguage"/> object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="language"><see cref="ILanguage"/> to save</param>
|
2012-12-10 14:53:36 -01:00
|
|
|
|
/// <param name="userId">Optional id of the user saving the language</param>
|
2013-01-25 08:58:21 -01:00
|
|
|
|
public void Save(ILanguage language, int userId = 0)
|
2012-10-24 12:22:52 -02:00
|
|
|
|
{
|
2016-05-02 12:24:13 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork())
|
2015-01-19 18:37:48 +11:00
|
|
|
|
{
|
2017-09-18 15:33:13 +02:00
|
|
|
|
var saveEventArgs = new SaveEventArgs<ILanguage>(language);
|
|
|
|
|
|
if (uow.Events.DispatchCancelable(SavingLanguage, this, saveEventArgs))
|
2017-05-12 14:49:44 +02:00
|
|
|
|
{
|
|
|
|
|
|
uow.Complete();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<ILanguageRepository>();
|
2015-01-19 18:37:48 +11:00
|
|
|
|
repository.AddOrUpdate(language);
|
2017-09-18 15:33:13 +02:00
|
|
|
|
saveEventArgs.CanCancel = false;
|
|
|
|
|
|
uow.Events.Dispatch(SavedLanguage, this, saveEventArgs);
|
2017-05-12 14:49:44 +02:00
|
|
|
|
|
|
|
|
|
|
Audit(uow, AuditType.Save, "Save Language performed by user", userId, language.Id);
|
2014-01-10 14:48:31 +01:00
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
uow.Complete();
|
|
|
|
|
|
}
|
2012-10-24 12:22:52 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2012-12-10 14:53:36 -01:00
|
|
|
|
/// <param name="userId">Optional id of the user deleting the language</param>
|
2013-01-25 08:58:21 -01:00
|
|
|
|
public void Delete(ILanguage language, int userId = 0)
|
2012-10-24 12:22:52 -02:00
|
|
|
|
{
|
2016-05-02 12:24:13 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork())
|
2015-01-19 18:37:48 +11:00
|
|
|
|
{
|
2017-09-18 15:33:13 +02:00
|
|
|
|
var deleteEventArgs = new DeleteEventArgs<ILanguage>(language);
|
|
|
|
|
|
if (uow.Events.DispatchCancelable(DeletingLanguage, this, deleteEventArgs))
|
2017-05-12 14:49:44 +02:00
|
|
|
|
{
|
|
|
|
|
|
uow.Complete();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-28 08:48:59 +02:00
|
|
|
|
var repository = uow.CreateRepository<ILanguageRepository>();
|
2015-01-19 18:37:48 +11:00
|
|
|
|
//NOTE: There isn't any constraints in the db, so possible references aren't deleted
|
|
|
|
|
|
repository.Delete(language);
|
2012-12-16 07:43:03 +05:00
|
|
|
|
|
2017-09-18 15:33:13 +02:00
|
|
|
|
deleteEventArgs.CanCancel = false;
|
|
|
|
|
|
uow.Events.Dispatch(DeletedLanguage, this, deleteEventArgs);
|
2014-01-10 14:48:31 +01:00
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
Audit(uow, AuditType.Delete, "Delete Language performed by user", userId, language.Id);
|
2015-01-19 15:12:34 +11:00
|
|
|
|
|
2016-05-02 12:12:21 +02:00
|
|
|
|
uow.Complete();
|
2015-01-19 15:12:34 +11:00
|
|
|
|
}
|
2012-10-24 12:22:52 -02:00
|
|
|
|
}
|
2012-12-10 14:53:36 -01:00
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
private void Audit(IScopeUnitOfWork uow, AuditType type, string message, int userId, int objectId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var repo = uow.CreateRepository<IAuditRepository>();
|
|
|
|
|
|
repo.AddOrUpdate(new AuditItem(objectId, message, type, userId));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-01-07 18:44:04 +01:00
|
|
|
|
/// <summary>
|
2017-07-20 11:21:28 +02:00
|
|
|
|
/// This is here to take care of a hack - the DictionaryTranslation model contains an ILanguage reference which we don't want but
|
2016-01-07 18:44:04 +01:00
|
|
|
|
/// we cannot remove it because it would be a large breaking change, so we need to make sure it's resolved lazily. This is because
|
2017-07-20 11:21:28 +02:00
|
|
|
|
/// if developers have a lot of dictionary items and translations, the caching and cloning size gets much much larger because of
|
2016-01-07 18:44:04 +01:00
|
|
|
|
/// the large object graphs. So now we don't cache or clone the attached ILanguage
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void EnsureDictionaryItemLanguageCallback(IDictionaryItem d)
|
|
|
|
|
|
{
|
|
|
|
|
|
var item = d as DictionaryItem;
|
|
|
|
|
|
if (item == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
item.GetLanguage = GetLanguageById;
|
|
|
|
|
|
foreach (var trans in item.Translations.OfType<DictionaryTranslation>())
|
|
|
|
|
|
trans.GetLanguage = GetLanguageById;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-18 15:33:13 +02:00
|
|
|
|
public Dictionary<string, Guid> GetDictionaryItemKeyMap()
|
|
|
|
|
|
{
|
2017-09-19 15:51:47 +02:00
|
|
|
|
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
|
2017-09-18 15:33:13 +02:00
|
|
|
|
{
|
2017-09-19 15:51:47 +02:00
|
|
|
|
var repository = uow.CreateRepository<IDictionaryRepository>();
|
2017-09-18 15:33:13 +02:00
|
|
|
|
return repository.GetDictionaryItemKeyMap();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-12-10 14:53:36 -01:00
|
|
|
|
#region Event Handlers
|
2015-01-19 18:37:48 +11:00
|
|
|
|
/// <summary>
|
2012-12-10 14:53:36 -01:00
|
|
|
|
/// Occurs before Delete
|
|
|
|
|
|
/// </summary>
|
2015-01-19 18:37:48 +11:00
|
|
|
|
public static event TypedEventHandler<ILocalizationService, DeleteEventArgs<ILanguage>> DeletingLanguage;
|
2012-12-10 14:53:36 -01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Occurs after Delete
|
|
|
|
|
|
/// </summary>
|
2015-01-19 18:37:48 +11:00
|
|
|
|
public static event TypedEventHandler<ILocalizationService, DeleteEventArgs<ILanguage>> DeletedLanguage;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Occurs before Delete
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static event TypedEventHandler<ILocalizationService, DeleteEventArgs<IDictionaryItem>> DeletingDictionaryItem;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Occurs after Delete
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static event TypedEventHandler<ILocalizationService, DeleteEventArgs<IDictionaryItem>> DeletedDictionaryItem;
|
2012-12-10 14:53:36 -01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Occurs before Save
|
|
|
|
|
|
/// </summary>
|
2015-01-19 18:37:48 +11:00
|
|
|
|
public static event TypedEventHandler<ILocalizationService, SaveEventArgs<IDictionaryItem>> SavingDictionaryItem;
|
2012-12-10 14:53:36 -01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Occurs after Save
|
|
|
|
|
|
/// </summary>
|
2015-01-19 18:37:48 +11:00
|
|
|
|
public static event TypedEventHandler<ILocalizationService, SaveEventArgs<IDictionaryItem>> SavedDictionaryItem;
|
2012-12-15 21:04:17 +05:00
|
|
|
|
|
2015-01-19 18:37:48 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Occurs before Save
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static event TypedEventHandler<ILocalizationService, SaveEventArgs<ILanguage>> SavingLanguage;
|
2012-12-15 21:04:17 +05:00
|
|
|
|
|
2015-01-19 18:37:48 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Occurs after Save
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static event TypedEventHandler<ILocalizationService, SaveEventArgs<ILanguage>> SavedLanguage;
|
2012-12-10 14:53:36 -01:00
|
|
|
|
#endregion
|
2012-10-24 12:22:52 -02:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|