public static bool SaveNotificationData(NotificationDetails details) { bool success = false; if (details.Selection == null || details.Selection.Count == 0) throw new ApplicationException("It is mandatory to attach at least one contact or account."); using (IDalSession session = NHSessionFactory.CreateSession()) { Notification notif = null; if (details.NotificationKey != 0) notif = session.GetTypedList<Notification>(details.NotificationKey).FirstOrDefault(); else { notif = new Notification(); notif.CreatedBy = Security.SecurityManager.CurrentUser; } notif.NotificationType = (NotificationTypes)details.NotificationTypeId; notif.Message = details.Message; notif.StartDate = details.StartDate; notif.DueDate = details.DueDate; if (Util.IsNotNullDate(notif.DueDate) && notif.DueDate < DateTime.Today) notif.IsActive = false; else notif.IsActive = true; // First -> delete attached accounts / contacts that are removed if (notif.Key != 0) { for (int i = notif.Accounts.Count; i > 0; i--) { var account = notif.Accounts[i - 1]; if (!details.Selection.Any(x => x.SelectedType == AccountContactSelectedTypes.Account && x.EntityKey == account.Key)) notif.Accounts.Remove(account); } for (int i = notif.Contacts.Count; i > 0; i--) { var contact = notif.Contacts[i - 1]; if (!details.Selection.Any(x => x.SelectedType == AccountContactSelectedTypes.Contact && x.EntityKey == contact.Key)) notif.Contacts.Remove(contact); } } // Add the new attached accounts / contacts foreach (var item in details.Selection) { if (item.SelectedType == AccountContactSelectedTypes.Account) { if (notif.Accounts == null || !notif.Accounts.Any(x => x.Key == item.EntityKey)) { IAccountTypeCustomer account = (IAccountTypeCustomer)AccountMapper.GetAccount(session, item.EntityKey); notif.Accounts.Add(account); } } else if (item.SelectedType == AccountContactSelectedTypes.Contact) { if (notif.Contacts == null || !notif.Contacts.Any(x => x.Key == item.EntityKey)) { IContact contact = ContactMapper.GetContact(session, item.EntityKey); notif.Contacts.Add(contact); } } } success = session.InsertOrUpdate(notif); } return success; }
private bool saveData() { bool success = false; try { Page.Validate(); if (Page.IsValid) { NotificationDetails details = new NotificationDetails(); details.NotificationKey = NotificationID; details.NotificationTypeId = Utility.GetKeyFromDropDownList(ddlNotificationType_Edit); details.Message = txtMessage.Text; details.StartDate = cldStartDate.SelectedDate; details.DueDate = cldDueDate.SelectedDate; details.Selection = ctlAccContSelector.Selection; success = NotificationMaintenanceAdapter.SaveNotificationData(details); } } catch (Exception ex) { lblErrorMessage.Text = Utility.GetCompleteExceptionMessage(ex); } return success; }