public ActionResult Process(FormCollection oColl) { HttpStatusCodeResult httpResult; string recipient = string.Empty; Organization org; string sender = string.Empty; string body = string.Empty; string attachmentData = string.Empty; string userIP = string.Empty; string userDomain = string.Empty; string json = string.Empty; string err = string.Empty; string uvFileCnt = string.Empty; string vFileCnt = string.Empty; int fileLen = 0; byte[] fileContent = new byte[0]; string attachmentName = string.Empty; StringBuilder sb = new StringBuilder(); try { recipient = Request.Unvalidated.Form["recipient"]; string[] recipientParts = recipient.Split('@'); using (var db = new StationCADDb()) { string tag = recipientParts[0]; org = db.Organizations .Include("NotificationRules") .Where(x => x.Tag == tag).FirstOrDefault(); if (org == null) throw new InvalidProgramException(string.Format("Invalid Organization tag: {0}", tag)); } sender = Request.Unvalidated.Form["sender"]; body = Request.Unvalidated.Form["body-plain"]; DateTime eventRecieved = DateTime.Now; // Validate the sender userIP = Request.UserHostAddress; userDomain = Request.UserHostName; sb.AppendLine(string.Format("User IP:{1}{0}{0}", Environment.NewLine, userIP)); sb.AppendLine(string.Format("User Domain:{1}{0}{0}", Environment.NewLine, userDomain)); var formkeys = Request.Unvalidated.Form.Keys; sb.AppendLine(string.Format("Keys:{0}", Environment.NewLine)); foreach (var item in formkeys) { string value = Request.Unvalidated.Form[item.ToString()]; sb.AppendLine(string.Format("Keys: {1}; Value: {2}{0}", Environment.NewLine, item.ToString(), value)); } vFileCnt = Request.Files.Count.ToString(); uvFileCnt = Request.Unvalidated.Files.Count.ToString(); sb.AppendLine(string.Format("Attachment Count: {1}, {2}{0}{0} ", Environment.NewLine, vFileCnt, uvFileCnt)); if (Request.Unvalidated.Files.Count > 0) { // for this example; processing just the first file HttpPostedFileBase file = Request.Unvalidated.Files[0]; fileLen = file.ContentLength; attachmentName = file.FileName; sb.AppendLine(string.Format("Length:{0}{1}{0}{0}", Environment.NewLine, fileLen)); if (fileLen >= 0) { // throw an error here if content length is not > 0 // you'll probably want to do something with file.ContentType and file.FileName fileContent = new byte[file.ContentLength]; file.InputStream.Read(fileContent, 0, file.ContentLength); sb.AppendLine(string.Format("File Content Length:{0}{1}{0}{0}", Environment.NewLine, fileContent.Length)); // fileContent now contains the byte[] of your attachment... attachmentData = System.Text.Encoding.Default.GetString(fileContent); sb.AppendLine(string.Format("Attachment Data:{0}{1}{0}{0}", Environment.NewLine, attachmentData)); } } DispatchManager dispMgr = new DispatchManager(); DispatchEvent eventMsg; if (attachmentData.Length > 0) { eventMsg = dispMgr.ProcessEvent(org, attachmentData, DispatchManager.MessageType.Html); eventMsg.FileName = attachmentName; } else eventMsg = dispMgr.ProcessEvent(org, body, DispatchManager.MessageType.Text); json = JsonUtil<DispatchEvent>.ToJson(eventMsg); sb.AppendLine(string.Format("Json Body:{0} {1}{0}{0}", Environment.NewLine, json)); httpResult = new HttpStatusCodeResult(HttpStatusCode.OK); } catch (Exception ex) { string errMsg = string.Format("An error occurred in EventController.Process(). Exception: {0}", ex.Message); base.LogException(errMsg, ex); sb.AppendLine(string.Format("Error:{0}{1}{0}{0}", Environment.NewLine, err)); httpResult = new HttpStatusCodeResult(HttpStatusCode.InternalServerError, string.Format("Error encountered processing the event. Message: {0}", ex.Message)); } finally { base.LogInfo(sb.ToString()); } return httpResult; }
public void TestDispatchEventParsing() { string tag = "CC51-FWFC"; using (var db = new StationCADDb()) { string data; using (StreamReader sr = new StreamReader(@"TestData\UnitDispatchReport-F16001716.htm")) { data = sr.ReadToEnd(); } DispatchManager dispMgr = new DispatchManager(); Organization org = db.Organizations.Where(x => x.Tag == tag).FirstOrDefault(); if (org == null) { org = new Organization(); org.Name = "First West Chester Fire Company"; org.Status = OrganizationStatus.Active; org.Type = OrganizationType.Fire; org.Tag = tag; org.ContactEmail = "*****@*****.**"; org.ContactPhone = "610.883.3253"; db.Organizations.Add(org); try { db.SaveChanges(); } catch (Exception ex) { ex.ToString(); } } // Add UserProfile string email = "*****@*****.**"; User user; UserProfile usrp; user = db.Users .Include("Profile") .Include("Profile.OrganizationAffiliations") .Include("Profile.MobileDevices") .Where(w => w.Email == email) .FirstOrDefault(); if (user == null) { user = new User { Id = Guid.NewGuid().ToString(), UserName = email, Email = email }; usrp = new UserProfile(); usrp.FirstName = string.Format("FirstName_{0}", DateTime.Now.Ticks); usrp.LastName = string.Format("LastName_{0}", DateTime.Now.Ticks); usrp.AccountEmail = email; usrp.IdentificationNumber = DateTime.Now.Ticks.ToString(); //usrp.UserName = string.Format("{0}.{1}", usrp.FirstName, usrp.LastName); usrp.OrganizationAffiliations = new List<OrganizationUserAffiliation>(); usrp.OrganizationAffiliations.Add(new OrganizationUserAffiliation { Status = OrganizationUserStatus.Active, Role = OrganizationUserRole.User }); usrp.NotificationEmail = email; usrp.MobileDevices = new List<UserMobileDevice>(); usrp.MobileDevices.Add(new UserMobileDevice { Carrier = MobileCarrier.ATT, EnableSMS = true, MobileNumber = "6108833253" }); user.Profile = usrp; user.Profile.OrganizationAffiliations = new List<OrganizationUserAffiliation>(); OrganizationUserAffiliation uoa = new OrganizationUserAffiliation(); uoa.CurrentOrganization = org; uoa.Role = OrganizationUserRole.User; uoa.Status = OrganizationUserStatus.Active; user.Profile.OrganizationAffiliations.Add(uoa); db.Users.Add(user); } //usr2 = db.UserProfiles // .Include("OrganizationAffiliations")b // .Include("MobileDevices") // .Where(w => w.NotificationEmail == "*****@*****.**") // .FirstOrDefault(); //if (usr2 == null) //{ // usr2 = new UserProfile(); // usr2.FirstName = "Michael"; // usr2.LastName = "Lam"; // usr2.IdentificationNumber = DateTime.Now.Ticks.ToString(); //// usr2.UserName = string.Format("{0}.{1}", usr2.FirstName, usr2.LastName); // usr2.OrganizationAffiliations = new List<UserOrganizationAffiliation>(); // usr2.OrganizationAffiliations.Add(new UserOrganizationAffiliation { Status = OrganizationUserStatus.Active, Role = OrganizationUserRole.User }); // usr2.NotificationEmail = "*****@*****.**"; // usr2.MobileDevices = new List<UserMobileDevice>(); // usr2.MobileDevices.Add(new UserMobileDevice { Carrier = MobileCarrier.ATT, EnableSMS = true, MobileNumber = "6108833253" }); // db.UserProfiles.Add(usr2); //} db.SaveChanges(); dispMgr.ProcessEvent(org, data, DispatchManager.MessageType.Html); //db.UserProfiles.Remove(usrp); //db.SaveChanges(); } }