public CommunicationItemNoteViewModel Note2NoteCommunicationViewModel(Note note)
		{
			CommunicationItemNoteViewModel retVal = new CommunicationItemNoteViewModel
			{
				AuthorName = note.AuthorName,
				Body = note.Body,
				Title = note.Title,
				Id = note.NoteId,
				ModifierName = note.ModifierName,
                LastModified = note.LastModified
                
			};

			return retVal;
		}
		public Note NoteCommunicationViewModel2Note(CommunicationItemNoteViewModel viewModel)
		{
			Note retVal = new Note
			{
				AuthorName = viewModel.AuthorName,
				Body = viewModel.Body,
				Title = viewModel.Title,
			};

			if (viewModel.Id != null)
				retVal.NoteId = viewModel.Id;

			return retVal;

		}
		public void AddCommunicationItemsWithAttachmentsToExistingCase()
		{
			var client = GetRepository();

			Case caseFromDb = client.Cases
				.Expand(c => c.Labels).Expand(c => c.Notes).Expand(c => c.CommunicationItems)
				.FirstOrDefault();

			Assert.IsNotNull(caseFromDb);
			client.Attach(caseFromDb);

			var emailItem = new EmailItem();
			emailItem.AuthorName = "Unknown";
			emailItem.Body = "body of emailItem";
			emailItem.From = "*****@*****.**";
			emailItem.Subject = "Subject";
			emailItem.Title = "Hello!!";
			emailItem.To = "*****@*****.**";


			var phoneItem = new PhoneCallItem();
			phoneItem.AuthorName = "bob marley";
			phoneItem.Body = "body of phoneitem";
			phoneItem.Direction = "Inbound";
			phoneItem.PhoneNumber = "789465123";
			phoneItem.Title = "Hey you!";


			var note = new Note();
			note.AuthorName = "Me";
			note.Body = "noteBody";
			note.Title = "note fo case";

			Attachment attach1 = new Attachment();
			attach1.DisplayName = "Attachment1";

			emailItem.Attachments.Add(attach1);

			caseFromDb.CommunicationItems.Add(emailItem);
			caseFromDb.CommunicationItems.Add(phoneItem);
			caseFromDb.Notes.Add(note);

			client.UnitOfWork.Commit();


			client = GetRepository();

			Case caseForCheck = client.Cases.Where(c => c.CaseId == caseFromDb.CaseId)
				.Expand(c => c.Labels).Expand(c => c.Notes).Expand(c => c.CommunicationItems)
				.SingleOrDefault();

			var phoneItemFromDb = caseForCheck.CommunicationItems.Where(ci => ci.CommunicationItemId == phoneItem.CommunicationItemId)
				.OfType<PhoneCallItem>().SingleOrDefault();

			var emailItemFromDb = caseForCheck.CommunicationItems.Where(ci => ci.CommunicationItemId == emailItem.CommunicationItemId)
				.OfType<EmailItem>().SingleOrDefault();

			var noteFromDb = caseForCheck.Notes.Where(n => n.NoteId == note.NoteId).SingleOrDefault();

			Assert.IsNotNull(caseForCheck);
			Assert.IsNotNull(phoneItemFromDb);
			Assert.IsNotNull(emailItemFromDb);
			Assert.IsNotNull(noteFromDb);

		}
		public void AddNotesToCaseAndContactTest()
		{
			var client = GetRepository();


			var contactFromDb = client.Members.OfType<Contact>()
				.Expand(c => c.Notes).Expand(c => c.Cases).FirstOrDefault();

			client.Attach(contactFromDb);

			Note noteForContact = new Note();
			noteForContact.Body = "noteForCase";


			contactFromDb.Notes.Add(noteForContact);
			//noteForContact.ContactId = contactFromDb.MemberId;

			client.UnitOfWork.Commit();

			client = GetRepository();


			var contactForCheck = client.Members.Where(m => m.MemberId == contactFromDb.MemberId).OfType<Contact>()
				.Expand(c => c.Notes).Expand(c => c.Cases).FirstOrDefault();

			Assert.IsNotNull(contactForCheck);
			Assert.IsTrue(contactForCheck.Notes.Count >= 1);
		}
        public void Can_create_Notes_in_Contact()
        {
            var client = GetRepository();


	        var contactWithNewNote = new Contact();
	        contactWithNewNote.FullName = "test";

            var noteForContact = new Note();
            noteForContact.Body = "noteForCase";


			contactWithNewNote.Notes.Add(noteForContact);
			client.Add(contactWithNewNote);

            client.UnitOfWork.Commit();

            client = GetRepository();


			var contactForCheck = client.Members.Where(m => m.MemberId == contactWithNewNote.MemberId).OfType<Contact>()
                                        .Expand(c => c.Notes).SingleOrDefault();

            Assert.NotNull(contactForCheck);
            Assert.True(contactForCheck.Notes.Count >= 1);
        }
        /// <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);
                        }
                    });
                }
            }
        }