/// <summary>
        /// add notes to InnerItem from CaseCommunicationViewModel
        /// </summary>
        private void GetNotesToCaseFromCaseCommunications(ICustomerRepository repository)
        {
            var adaptor = new CommunicationAdaptor();
            if (CaseCommunications != null && CaseCommunications.Items != null)
            {
                var result = new List<Note>();

                result = result.Union(CaseCommunications.Items
                                                        .Where(n => n.Type == CommunicationItemType.Note)
                                                        .Where(
                                                            n =>
                                                            n.State !=
                                                            CommunicationItemState.Deleted)
                                                        .Select(
                                                            n =>
                                                            adaptor.NoteCommunicationViewModel2Note(
                                                                n as CommunicationItemNoteViewModel))).ToList();

                foreach (var note in result)
                {
                    var noteForUpdate = CaseNotes.SingleOrDefault(caseNote => caseNote.NoteId == note.NoteId);
                    if (noteForUpdate == null)
                    {
                        note.CaseId = InnerItem.CaseId;
                        CaseNotes.Add(note);
                        repository.Add(note);
                    }
                }

                //if messsage was writed in textbox, but Enter was not pressed
                //then create new noteItem and save it;
                if (!string.IsNullOrEmpty(CaseCommunications.NewBody))
                {
                    var activeCommand = CaseCommunications.ToolBarCommmands.SingleOrDefault(c => c.IsActive);

                    if ((CommunicationItemType)activeCommand.CommandParametr == CommunicationItemType.Note)
                    {
                        var note = new Note
                            {
                                AuthorName = _authorName,
                                Body = CaseCommunications.NewBody,
                                Title = InnerItem.Title,
                                Created = (DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local)).ToUniversalTime(),
                                CaseId = InnerItem.CaseId
                            };
                        note.LastModified = note.Created;
                        CaseNotes.Add(note);
                        repository.Add(note);
                    }

                    OnUIThread(() =>
                    {
                        if (activeCommand != null)
                        {
                            activeCommand.Command.Execute(CaseCommunications.NewBody);
                        }
                    });
                }
            }
        }
        private Task<List<CommunicationItemViewModel>> RefreshCaseCommunicationItemsAsync(CommunicationAdaptor adaptor)
        {
            Task<List<CommunicationItemViewModel>> taskResult = Task.Run(() =>
                {
                    var result = new List<CommunicationItemViewModel>();

                    // Append publicReplyItems
                    if (CaseCommunicationItems.Count > 0)
                    {
                        result = result.Union(CaseCommunicationItems
                                                       .Where(item => item is PublicReplyItem)
                                                       .Select(
                                                           item =>
                                                           adaptor.PublicReply2PublicReplyViewModel(
                                                               item.DeepClone(
                                                                   _entityFactory as IKnownSerializationTypes)
                                                               as PublicReplyItem)))
                                       .ToList();
                    }
                    // Append Notes
                    if (CaseNotes.Count > 0)
                    {
                        result =
                            result.Union(
                                CaseNotes.Select(
                                    n =>
                                    adaptor.Note2NoteCommunicationViewModel(
                                        n.DeepClone(_entityFactory as IKnownSerializationTypes)))).ToList();
                    }

                    return result;
                });

            return taskResult;
        }
        /// <summary>
        /// Add communication item to CurrentCase
        /// </summary>
        /// <returns></returns>
        private void GetCommunicationItemsFromCaseCommunications(ICustomerRepository repository)
        {
            var adaptor = new CommunicationAdaptor();

            if (CaseCommunications != null && CaseCommunications.Items != null)
            {
                var result = new List<CommunicationItem>();

                result = result.Union(CaseCommunications.Items
                                                        .Where(item => item.Type == CommunicationItemType.PublicReply)
                                                        .Where(item => item.State != CommunicationItemState.Deleted)
                                                        .Select(
                                                            item =>
                                                            adaptor.PublicReplyViewModel2PublicReplyItem(
                                                                item as CommunicationItemPublicReplyViewModel))).ToList();

                foreach (var item in result)
                {
                    var itemForUpdate =
                        CaseCommunicationItems.SingleOrDefault(
                            comItem => comItem.CommunicationItemId == item.CommunicationItemId);
                    if (itemForUpdate == null)
                    {
                        item.CaseId = InnerItem.CaseId;
                        CaseCommunicationItems.Add(item);
                        repository.Add(item);
                    }
                }

                //if messsage was writed in textbox, but Enter was not pressed
                //then create new publicReplyItem and save it;
                if (!string.IsNullOrEmpty(CaseCommunications.NewBody))
                {
                    var activeCommand = CaseCommunications.ToolBarCommmands.SingleOrDefault(c => c.IsActive);

                    if ((CommunicationItemType)activeCommand.CommandParametr == CommunicationItemType.PublicReply)
                    {
                        PublicReplyItem publicReplyItem = new PublicReplyItem();
                        publicReplyItem.AuthorName = _authorName;
                        publicReplyItem.Body = CaseCommunications.NewBody;
                        publicReplyItem.Title = InnerItem.Title;
                        publicReplyItem.Created =
                            (DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local)).ToUniversalTime();
                        publicReplyItem.LastModified = publicReplyItem.Created;
                        publicReplyItem.CaseId = InnerItem.CaseId;
                        CaseCommunicationItems.Add(publicReplyItem);
                        repository.Add(publicReplyItem);
                    }

                    OnUIThread(() =>
                        {
                            if (activeCommand != null)
                            {
                                activeCommand.Command.Execute(CaseCommunications.NewBody);
                            }
                        });
                }
            }
        }
        /// <summary>
        /// refresh case communication Items (Public Reply)
        /// refresh case communication Items (Notes)
        /// </summary>
        private async void RefreshCaseCommunicationItems()
        {
            var adaptor = new CommunicationAdaptor();

            try
            {
                CaseCommunications.IsInitialized = false;
                var communicationItems = await RefreshCaseCommunicationItemsAsync(adaptor);

                OnUIThread(() =>
                    {
                        CaseCommunications.AppendCommunucationItems(communicationItems);
                        CaseCommunications.IsInitialized = true;
                        IsModified = false;
                    });

            }
            catch (Exception ex)
            {
                ShowErrorDialog(ex, string.Format("An error occurred when trying to refresh case notes: {0}".Localize(), ex.InnerException.Message));
            }
        }
        private Task<List<CommunicationItemViewModel>> RefreshCustomerNotesItemAsync(CommunicationAdaptor adaptor)
        {
            Task<List<CommunicationItemViewModel>> taskResult = Task.Run(() =>
                {
                    var result = new List<CommunicationItemViewModel>();
                    if (CurrentCustomer != null && ContactNotes.Count > 0)
                    {
                        result =
                            result.Union(
                                ContactNotes.Select(
                                    n =>
                                    adaptor.Note2NoteCommunicationViewModel(
                                        n.DeepClone(_entityFactory as IKnownSerializationTypes)))).ToList();
                    }
                    return result;
                });

            return taskResult;
        }
        /// <summary>
        /// refresh customer notes
        /// </summary>
        private async void RefreshCustomerNotesItems()
        {
            var adaptor = new CommunicationAdaptor();

            try
            {
                CustomerNotes.IsInitialized = false;
                var notes = await RefreshCustomerNotesItemAsync(adaptor);
                OnUIThread(() =>
                {
                    CustomerNotes.AppendCommunucationItems(notes);
                    CustomerNotes.IsInitialized = true;
                    ShowLoadingAnimation = false;
                    IsModified = false;
                });

            }
            catch (Exception ex)
            {
                ShowErrorDialog(ex, string.Format("An error occurred when trying to refresh contact notes: {0}".Localize(), ex.InnerException.Message));
            }
        }
		private void CommitSelectedKnowledgeGroupArticles()
		{
			var adaptor = new CommunicationAdaptor();
			var selectedGroupId = (_selectedKnowledgeGroup as KnowledgeGroupViewModel) != null ? (_selectedKnowledgeGroup as KnowledgeGroupViewModel).OriginalItem.KnowledgeBaseGroupId : null;
			var communicationControl = KnowledgeArticleCommunicationControl as KnowledgeArticleCommunicationControlViewModel;
			var itemsForDelete = communicationControl.GetItemsByState(CommunicationItemState.Deleted);
			var itemsForAppend = communicationControl.GetItemsByState(CommunicationItemState.Appended);
			var itemsForModify = communicationControl.GetItemsByState(CommunicationItemState.Modified);

			Task.Factory.StartNew(() =>
			{
				var repository = _repositoryFactory.GetRepositoryInstance();
				ShowLoadingAnimation = true;
				// delete
				if (itemsForDelete != null && itemsForDelete.Count() > 0)
				{
					foreach (CommunicationItemViewModel item in itemsForDelete)
					{
						KnowledgeBaseArticle original = adaptor.KnowledgeBaseArticleCommunicationViewModel2KnowledgeBaseArticle(item as CommunicationItemKnowledgeBaseArticleViewModel);
						//SyncAttach(original, null);
						repository.Attach(original);
						repository.Remove(original);
					}
				}
				// append
				if (itemsForAppend != null && itemsForAppend.Count() > 0 && selectedGroupId != null)
				{
					foreach (CommunicationItemViewModel item in itemsForAppend)
					{
						KnowledgeBaseArticle article = adaptor.KnowledgeBaseArticleCommunicationViewModel2KnowledgeBaseArticle(item as CommunicationItemKnowledgeBaseArticleViewModel);
						article.GroupId = selectedGroupId;
						//SyncAttach(null, article);
						repository.Add(article);
					}
				}
				// modify
				if (itemsForModify != null && itemsForModify.Count() > 0)
				{
					foreach (CommunicationItemViewModel item in itemsForModify)
					{
						var article = adaptor.KnowledgeBaseArticleCommunicationViewModel2KnowledgeBaseArticle(item as CommunicationItemKnowledgeBaseArticleViewModel);
						article.GroupId = selectedGroupId;
						var original = repository.KnowledgeBaseArticles.Where(x => x.KnowledgeBaseArticleId == article.KnowledgeBaseArticleId).SingleOrDefault();
						//SyncAttach(original, article);
						repository.Attach(original);
						OnUIThread(()=>original.InjectFrom(article));
					}
				}
				int i = repository.UnitOfWork.Commit();
			})
		   .ContinueWith(t =>
		   {
			   ShowLoadingAnimation = false;
		   });
		}
		/// <summary>
		/// refresh articles for selected KnowledgeBaseGroup
		/// </summary>
		private void RefreshSelectedKnowledgeGroupArticles()
		{
			var adaptor = new CommunicationAdaptor();

			Task.Factory.StartNew(() =>
			{
				KnowledgeArticleCommunicationControl.IsInitialized = false;
				List<CommunicationItemViewModel> result = null;
				var selectedKnowledgeGroup = SelectedKnowledgeGroup as KnowledgeGroupViewModel;
				var articles = GetKnowledgeBaseArticles(selectedKnowledgeGroup);
				if (articles != null && articles.Count() > 0)
				{
					result = new List<CommunicationItemViewModel>();
					foreach (var i in articles)
					{
						result.Add(adaptor.KnowledgeBaseArticle2KnowledgeBaseArticleCommunicationViewModel(i));
					}
					//result = articles.Select(n => (adaptor.KnowledgeBaseArticle2KnowledgeBaseArticleCommunicationViewModel(n) as CommunicationItemViewModel)).OfType<CommunicationItemViewModel>().ToList();
				}
				return result;
			}).ContinueWith(t =>
			{
				OnUIThread(() =>
				{
					if (t.Exception == null)
					{
						KnowledgeArticleCommunicationControl.ClearAllCommunicationItems();
						KnowledgeArticleCommunicationControl.AppendCommunucationItems(t.Result);
					}
					KnowledgeArticleCommunicationControl.IsInitialized = true;
				});
			});
		}