Esempio n. 1
0
        public NotificationDetails(Notification notification)
            : this()
        {
            this.NotificationKey = notification.Key;
            this.Message = notification.Message;
            this.NotificationTypeId = (int)notification.NotificationType;
            this.StartDate = notification.StartDate;
            this.DueDate = notification.DueDate;

            if (notification.Contacts.Count > 0)
            {
                foreach (IContact contact in notification.Contacts)
                    Selection.Add(new AccountContactSelectedDetails(contact.Key, AccountContactSelectedTypes.Contact, contact.FullName + " (" + contact.GetBSN + ")"));
            }

            if (notification.Accounts.Count > 0)
            {
                foreach (IAccountTypeCustomer account in notification.Accounts)
                    Selection.Add(new AccountContactSelectedDetails(account.Key, AccountContactSelectedTypes.Account, account.FullName + " (" + account.Number + ")"));
            }
        }
        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;
        }