Example #1
0
        public void CreateIncident()
        {
            Incident inc = new Incident();
            inc.OrganizationId = 1;
            inc.CADIdentifier = 1;
            inc.CallerAddress = "Caller Address 1";
            inc.CallerName = "Caller Name 1";
            inc.CallerPhone = "Caller Phone 1";
            inc.ConsoleID = "Console 1";
            inc.DispatchedDateTime = DateTime.Now;
            inc.IncidentIdentifier = Guid.NewGuid();
            inc.Title = "Title";

            IncidentAddress add = new IncidentAddress();
            add.Number = "1";
            add.Street = "street";
            add.City = "city";
            inc.LocationAddresses = new List<IncidentAddress>();
            inc.LocationAddresses.Add(add);

            IncidentUnit unit = new IncidentUnit();
            unit.UnitID = "Unit 1";
            unit.EnteredDateTime = DateTime.Now;
            unit.Disposition = "DP";
            inc.Units = new List<IncidentUnit>();
            inc.Units.Add(unit);

            IncidentNote cmt = new IncidentNote();
            cmt.Message = "Comment 1";
            cmt.EnteredDateTime = DateTime.Now;
            cmt.Author = "Author";
            inc.Notes = new List<IncidentNote>();
            inc.Notes.Add(cmt);

            using (var db = new StationCADDb())
            {
                Incident existing = db.Incidents.Where(x => x.Title == "Title").FirstOrDefault();
                if (existing != null)
                {
                    db.Incidents.Remove(existing);
                    db.SaveChanges();
                }
                db.Incidents.Add(inc);
                db.SaveChanges();

                Incident saved = db.Incidents.Where(x => x.Title == "Title").FirstOrDefault();
                Assert.IsNotNull(saved);

                db.Incidents.Remove(saved);
                db.SaveChanges();
            }
        }
        public static List<OrganizationUserNotification> CreateNotifications(Incident incident)
        {
            List<OrganizationUserNotification> results = new List<OrganizationUserNotification>();
            ConcurrentBag<OrganizationUserNotification> resultsBag = new ConcurrentBag<OrganizationUserNotification>();
            List<OrganizationUserAffiliation> uoas;
            List<OrganizationUserNotification> notifications;
            try
            {
                using (var db = new StationCADDb())
                {
                    uoas = db.OrganizationUserAffiliations
                        .Include("CurrentUserProfile")
                        .Include("CurrentUserProfile.MobileDevices")
                        .Include("CurrentOrganization")
                        .Where(x => x.CurrentOrganization.Id == incident.OrganizationId && x.Status == OrganizationUserStatus.Active).ToList();

                    if (uoas == null)
                        throw new InvalidProgramException("Unable to find valid UserProfile-Org Affiliations.");

                    ParallelOptions opts = new ParallelOptions();
                    opts.MaxDegreeOfParallelism = ParallelismFactor;
                    ParallelLoopResult ptlseResult = Parallel.ForEach(
                        uoas,
                        opts,
                        current =>
                        {
                            OrganizationUserNotification item = new OrganizationUserNotification();
                            SMSEmailNotification notification = incident.GetSMSEmailNotification(current);
                            item.NotifcationType = OrganizationUserNotifcationType.TextMessage;
                            item.Notification = notification;
                            item.MessageTitle = notification.MessageSubject;
                            item.MessageBody = notification.MessageBody;
                            item.Affilitation = current;
                            resultsBag.Add(item);
                        });

                    ParallelLoopResult ptleResult = Parallel.ForEach(
                        uoas,
                        opts,
                        current =>
                        {
                            OrganizationUserNotification item = new OrganizationUserNotification();
                            EmailNotification notification = incident.GetEmailNotification(current);
                            item.NotifcationType = OrganizationUserNotifcationType.Email;
                            item.Notification = notification;
                            item.MessageTitle = notification.MessageSubject;
                            item.MessageBody = notification.MessageBody;
                            item.Affilitation = current;
                            resultsBag.Add(item);
                        });
                    notifications = resultsBag.ToList<OrganizationUserNotification>();
                    db.OrganizationUserNotifcations.AddRange(notifications);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                string errMsg = string.Format("An error occurred in NotificationManager.CreateNotifications(). Exception: {0}", ex.Message);
                LogException(errMsg, ex);
                throw ex;
            }
            return notifications;
        }