/// <summary> /// Saves a <see cref="IDictionaryItem"/> object /// </summary> /// <param name="dictionaryItem"><see cref="IDictionaryItem"/> to save</param> /// <param name="userId">Optional id of the user saving the dictionary item</param> public void Save(IDictionaryItem dictionaryItem, int userId = 0) { if (SavingDictionaryItem.IsRaisedEventCancelled(new SaveEventArgs <IDictionaryItem>(dictionaryItem), this)) { return; } var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateDictionaryRepository(uow)) { repository.AddOrUpdate(dictionaryItem); uow.Commit(); SavedDictionaryItem.RaiseEvent(new SaveEventArgs <IDictionaryItem>(dictionaryItem, false), this); } Audit.Add(AuditTypes.Save, "Save DictionaryItem performed by user", userId, dictionaryItem.Id); }
/// <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) { var uow = UowProvider.GetUnitOfWork(); using (var repository = RepositoryFactory.CreateDictionaryRepository(uow)) { //validate the parent if (parentId.HasValue && parentId.Value != Guid.Empty) { var parent = GetDictionaryItemById(parentId.Value); if (parent == null) { throw new ArgumentException("No parent dictionary item was found with id " + parentId.Value); } } var item = new DictionaryItem(parentId, key); if (defaultValue.IsNullOrWhiteSpace() == false) { var langs = GetAllLanguages(); var translations = langs.Select(language => new DictionaryTranslation(language, defaultValue)) .Cast <IDictionaryTranslation>() .ToList(); item.Translations = translations; } if (SavingDictionaryItem.IsRaisedEventCancelled(new SaveEventArgs <IDictionaryItem>(item), this)) { return(item); } repository.AddOrUpdate(item); uow.Commit(); SavedDictionaryItem.RaiseEvent(new SaveEventArgs <IDictionaryItem>(item), this); return(item); } }