PageRenderTime 23ms CodeModel.GetById 1ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/CmsData/APIPerson.cs

https://bitbucket.org/mahalowe/bvcms
C# | 431 lines | 428 code | 3 blank | 0 comment | 68 complexity | fa4f161e71b093434c3e16fa39f48650 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Xml;
  4using System.Text;
  5using System.Linq;
  6using System.Xml.Linq;
  7using UtilityExtensions;
  8using IronPython.Hosting;
  9using System.Data.Linq;
 10using System.Xml.Serialization;
 11using System.IO;
 12using System.Runtime.Serialization;
 13using System.Net;
 14
 15namespace CmsData.API
 16{
 17	public class APIPerson
 18	{
 19		private CMSDataContext Db;
 20
 21		public APIPerson(CMSDataContext Db)
 22		{
 23			this.Db = Db;
 24		}
 25		[Serializable]
 26		public class People
 27		{
 28			[XmlElement("Person")]
 29			public Person[] List { get; set; }
 30		}
 31		[Serializable]
 32		public class Address
 33		{
 34			[XmlElementAttribute(IsNullable = true)]
 35			public string AddressLineOne { get; set; }
 36			[XmlElementAttribute(IsNullable = true)]
 37			public string AddressLineTwo { get; set; }
 38			[XmlElementAttribute(IsNullable = true)]
 39			public string CityName { get; set; }
 40			[XmlElementAttribute(IsNullable = true)]
 41			public string StateCode { get; set; }
 42			[XmlElementAttribute(IsNullable = true)]
 43			public string ZipCode { get; set; }
 44			[XmlElementAttribute(IsNullable = true)]
 45			public string CountryName { get; set; }
 46			public DateTime? AddressFromDate { get; set; }
 47			public DateTime? AddressToDate { get; set; }
 48			public bool? BadAddressFlag { get; set; }
 49		}
 50		[Serializable]
 51		public class AddrItem
 52		{
 53			public static AddrItem New(XElement e)
 54			{
 55				return new AddrItem
 56				{
 57					Text = e != null ? e.Value : ""
 58				};
 59			}
 60			[XmlAttribute]
 61			public string Error { get; set; }
 62			[XmlText]
 63			public string Text { get; set; }
 64		}
 65		[Serializable]
 66		public class Address2
 67		{
 68			public static Address2 New(XElement e, int id, string country)
 69			{
 70				return new Address2
 71				{
 72					Id = id,
 73					type = e.Name.ToString(),
 74					AddressLineOne = AddrItem.New(e.Element("AddressLineOne")),
 75					AddressLineTwo = AddrItem.New(e.Element("AddressLineTwo")),
 76					CityName = AddrItem.New(e.Element("CityName")),
 77					StateCode = AddrItem.New(e.Element("StateCode")),
 78					ZipCode = AddrItem.New(e.Element("ZipCode")),
 79					CountryName = country
 80				};
 81			}
 82			[XmlAttribute()]
 83			public int Id { get; set; }
 84			[XmlAttribute()]
 85			public string type { get; set; }
 86			[XmlAttribute()]
 87			public string Error { get; set; }
 88			public AddrItem AddressLineOne { get; set; }
 89			public AddrItem AddressLineTwo { get; set; }
 90			public AddrItem CityName { get; set; }
 91			public AddrItem StateCode { get; set; }
 92			public AddrItem ZipCode { get; set; }
 93			public string CountryName { get; set; }
 94		}
 95		[Serializable]
 96		public class Person
 97		{
 98			[XmlAttribute]
 99			public int PeopleId { get; set; }
100			public int FamilyId { get; set; }
101			[XmlElementAttribute(IsNullable = true)]
102			public string NickName { get; set; }
103			[XmlElementAttribute(IsNullable = true)]
104			public string TitleCode { get; set; }
105			[XmlElementAttribute(IsNullable = true)]
106			public string FirstName { get; set; }
107			[XmlElementAttribute(IsNullable = true)]
108			public string MiddleName { get; set; }
109			[XmlElementAttribute(IsNullable = true)]
110			public string LastName { get; set; }
111			[XmlElementAttribute(IsNullable = true)]
112			public string SuffixCode { get; set; }
113			[XmlElementAttribute(IsNullable = true)]
114			public string AltName { get; set; }
115			[XmlElementAttribute(IsNullable = true)]
116			public string MaidenName { get; set; }
117			public int GenderId { get; set; }
118			[XmlElementAttribute(IsNullable = true)]
119			public string HomePhone { get; set; }
120			[XmlElementAttribute(IsNullable = true)]
121			public string CellPhone { get; set; }
122			[XmlElementAttribute(IsNullable = true)]
123			public string WorkPhone { get; set; }
124			[XmlElementAttribute(IsNullable = true)]
125			public string EmailAddress { get; set; }
126			public bool? SendEmailAddress1 { get; set; }
127			[XmlElementAttribute(IsNullable = true)]
128			public string EmailAddress2 { get; set; }
129			public bool? SendEmailAddress2 { get; set; }
130			[XmlElementAttribute(IsNullable = true)]
131			public string SchoolOther { get; set; }
132			public int? Grade { get; set; }
133			[XmlElementAttribute(IsNullable = true)]
134			public string EmployerOther { get; set; }
135			[XmlElementAttribute(IsNullable = true)]
136			public string OccupationOther { get; set; }
137			public int? MaritalStatusId { get; set; }
138			public DateTime? WeddingDate { get; set; }
139			[XmlElementAttribute(IsNullable = true)]
140			public string DOB { get; set; }
141			public bool? DoNotCallFlag { get; set; }
142			public bool? DoNotMailFlag { get; set; }
143			public bool? DoNotVisitFlag { get; set; }
144			public int PositionInFamilyId { get; set; }
145			[XmlElementAttribute(IsNullable = true)]
146			public string SpouseName { get; set; }
147			public int? CampusId { get; set; }
148			public DateTime? DeceasedDate { get; set; }
149			public int? MemberStatusId { get; set; }
150			public DateTime? JoinDate { get; set; }
151			public int? DecisionTypeId { get; set; }
152			public DateTime? DecisionDate { get; set; }
153			public int? BaptismTypeId { get; set; }
154			public DateTime? BaptismDate { get; set; }
155			public DateTime? BaptismSchedDate { get; set; }
156			[XmlElementAttribute(IsNullable = true)]
157			public string OtherPreviousChurch { get; set; }
158			public int? JoinCodeId { get; set; }
159			public int? DropCodeId { get; set; }
160			public DateTime? DropDate { get; set; }
161			[XmlElementAttribute(IsNullable = true)]
162			public string OtherNewChurch { get; set; }
163			public int? NewMemberClassStatusId { get; set; }
164			public DateTime? NewMemberClassDate { get; set; }
165			public Address FamilyAddress { get; set; }
166			public Address PersonalAddress { get; set; }
167			public int AddressTypeId { get; set; }
168			public List<string> Usernames { get; set; }
169		}
170		public Person GetPersonData(int id)
171		{
172			return GetPeopleData(id).SingleOrDefault();
173		}
174		public IEnumerable<Person> GetPeopleData(int? id, int? famid = null, string first = null, string last = null)
175		{
176			var q = from p in Db.People
177					where id == null || id == p.PeopleId
178					where famid == null || famid == p.FamilyId
179					where first == null || (first == p.FirstName || first == p.NickName)
180					where last == null || (last == p.LastName || last == p.MaidenName)
181					let sp = p.Family.People.SingleOrDefault(pp => pp.PeopleId == p.SpouseId)
182					select new Person
183					{
184						PeopleId = p.PeopleId,
185						FamilyId = p.FamilyId,
186						NickName = p.NickName,
187						TitleCode = p.TitleCode,
188						FirstName = p.FirstName,
189						MiddleName = p.MiddleName,
190						LastName = p.LastName,
191						SuffixCode = p.SuffixCode,
192						AltName = p.AltName,
193						MaidenName = p.MaidenName,
194						GenderId = p.GenderId,
195						HomePhone = p.HomePhone,
196						CellPhone = p.CellPhone,
197						WorkPhone = p.WorkPhone,
198						EmailAddress = p.EmailAddress,
199						SendEmailAddress1 = p.SendEmailAddress1,
200						EmailAddress2 = p.EmailAddress2,
201						SendEmailAddress2 = p.SendEmailAddress2,
202						SchoolOther = p.SchoolOther,
203						Grade = p.Grade,
204						EmployerOther = p.EmployerOther,
205						OccupationOther = p.OccupationOther,
206						MaritalStatusId = p.MaritalStatusId,
207						WeddingDate = p.WeddingDate,
208						DOB = p.DOB,
209						DoNotCallFlag = p.DoNotCallFlag,
210						DoNotMailFlag = p.DoNotMailFlag,
211						DoNotVisitFlag = p.DoNotVisitFlag,
212						PositionInFamilyId = p.PositionInFamilyId,
213						SpouseName = sp.Name,
214						CampusId = p.CampusId,
215						DeceasedDate = p.DeceasedDate,
216						MemberStatusId = p.MemberStatusId,
217						JoinDate = p.JoinDate,
218						BaptismDate = p.BaptismDate,
219						BaptismSchedDate = p.BaptismSchedDate,
220						BaptismTypeId = p.BaptismTypeId,
221						DecisionDate = p.DecisionDate,
222						DecisionTypeId = p.DecisionTypeId,
223						DropCodeId = p.DropCodeId,
224						DropDate = p.DropDate,
225						JoinCodeId = p.JoinCodeId,
226						NewMemberClassDate = p.NewMemberClassDate,
227						NewMemberClassStatusId = p.NewMemberClassStatusId,
228						OtherNewChurch = p.OtherNewChurch,
229						OtherPreviousChurch = p.OtherPreviousChurch,
230						AddressTypeId = p.AddressTypeId,
231						FamilyAddress = new Address
232						{
233							AddressLineOne = p.Family.AddressLineOne,
234							AddressLineTwo = p.Family.AddressLineTwo,
235							CityName = p.Family.CityName,
236							StateCode = p.Family.StateCode,
237							ZipCode = p.Family.ZipCode,
238							CountryName = p.Family.CountryName,
239							BadAddressFlag = p.Family.BadAddressFlag,
240							AddressFromDate = p.Family.AddressFromDate,
241							AddressToDate = p.Family.AddressToDate,
242						},
243						PersonalAddress = new Address
244						{
245							AddressLineOne = p.AddressLineOne,
246							AddressLineTwo = p.AddressLineTwo,
247							CityName = p.CityName,
248							StateCode = p.StateCode,
249							ZipCode = p.ZipCode,
250							CountryName = p.CountryName,
251							BadAddressFlag = p.BadAddressFlag,
252							AddressFromDate = p.AddressFromDate,
253							AddressToDate = p.AddressToDate
254						},
255						Usernames = (from u in p.Users
256									 select u.Username).ToList()
257					};
258			return q.Take(100);
259		}
260		public string GetPeopleXml(int? id, int? famid, string first, string last)
261		{
262			var xs = new XmlSerializer(typeof(People));
263			var sw = new StringWriter();
264			var a = new People { List = GetPeopleData(id, famid, first, last).ToArray() };
265			xs.Serialize(sw, a);
266			return sw.ToString();
267		}
268		public string GetPersonXml(int id)
269		{
270			var xs = new XmlSerializer(typeof(Person));
271			var sw = new StringWriter();
272			var p = GetPersonData(id);
273			if (p == null)
274				return "<Person error=\"Not found\" />";
275			xs.Serialize(sw, p);
276			return sw.ToString();
277		}
278		public string UpdatePersonXml(string xml)
279		{
280			var x = XDocument.Parse(xml);
281			var p = Db.LoadPersonById(x.Root.Attribute("PeopleId").Value.ToInt());
282			var u = new APIPerson.Update(p);
283
284			Address2 addr = null;
285			foreach (var e in x.Root.Elements())
286				switch (e.Name.ToString())
287				{
288					case "PersonalAddress":
289						foreach (var pa in e.Elements())
290							u.UpdatePerson(pa);
291						addr = Address2.New(e, p.PeopleId, p.CountryName);
292						addr = ValidateAddress(addr);
293						if (addr.Error.HasValue())
294							return serialize(addr);
295						break;
296					case "FamilyAddress":
297						foreach (var fa in e.Elements())
298							u.UpdateFamily(fa);
299						addr = Address2.New(e, p.PeopleId, p.Family.CountryName);
300						addr = ValidateAddress(addr);
301						if (addr.Error.HasValue())
302							return serialize(addr);
303						break;
304					default:
305						break;
306				}
307			foreach (var e in x.Root.Elements())
308				switch (e.Name.ToString())
309				{
310					case "PersonalAddress":
311					case "FamilyAddress":
312					case "SpouseName":
313					case "FamilyId":
314						break;
315					case "HomePhone":
316						u.UpdateFamily(e);
317						break;
318					default:
319						u.UpdatePerson(e);
320						break;
321				}
322			p.LogChanges(DbUtil.Db, u.sb, Util.UserPeopleId.Value);
323			p.Family.LogChanges(DbUtil.Db, u.fsb, p.PeopleId, Util.UserPeopleId.Value);
324			Db.SubmitChanges();
325			return "<Success />";
326		}
327		private Address2 ValidateAddress(Address2 a)
328		{
329			var sw = new StringWriter();
330			bool addrok = a.CityName.Text.HasValue() && a.StateCode.Text.HasValue();
331			if (!addrok && a.ZipCode.Text.HasValue())
332				addrok = true;
333			if (!addrok && !a.CityName.Text.HasValue() && !a.StateCode.Text.HasValue() && !a.ZipCode.Text.HasValue())
334				addrok = true;
335			if (!addrok)
336			{
337				a.Error = "city/state required or zip required (or \"na\" in all)";
338				return a;
339			}
340			if (a.AddressLineOne.Text.HasValue() && (a.CityName.Text.HasValue() || a.StateCode.Text.HasValue() || a.ZipCode.Text.HasValue())
341				&& (a.CountryName == "United States" || !a.CountryName.HasValue()))
342			{
343				var r = AddressVerify.LookupAddress(a.AddressLineOne.Text, a.AddressLineTwo.Text, a.CityName.Text, a.StateCode.Text, a.ZipCode.Text);
344				if (r.Line1 == "error")
345				{
346					a.Error = r.address;
347					return a;
348				}
349				if (!r.found)
350				{
351					a.Error = r.address + ", if your address will not validate, change the country to 'USA, Not Validated'";
352					return a;
353				}
354				if (r.Line1 != a.AddressLineOne.Text)
355				{
356					a.AddressLineOne.Error = "address changed from '{0}'".Fmt(a.AddressLineOne.Text);
357					a.Error = "changes";
358					a.AddressLineOne.Text = r.Line1;
359				}
360				if (r.Line2 != (a.AddressLineTwo.Text ?? ""))
361				{
362					a.AddressLineTwo.Error = "address2 changed from '{0}'".Fmt(a.AddressLineTwo.Text);
363					a.Error = "changes";
364					a.AddressLineTwo.Text = r.Line2;
365				}
366				if (r.City != (a.CityName.Text ?? ""))
367				{
368					a.CityName.Error = "city changed from '{0}'".Fmt(a.CityName.Text);
369					a.Error = "changes";
370					a.CityName.Text = r.City;
371				}
372				if (r.State != (a.StateCode.Text ?? ""))
373				{
374					a.StateCode.Error = "state changed from '{0}'".Fmt(a.StateCode.Text);
375					a.Error = "changes";
376					a.StateCode.Text = r.State;
377				}
378				if (r.Zip != (a.ZipCode.Text ?? ""))
379				{
380					a.ZipCode.Error = "zip changed from '{0}'".Fmt(a.ZipCode.Text);
381					a.Error = "changes";
382					a.ZipCode.Text = r.Zip;
383				}
384			}
385			return a;
386		}
387		private string serialize(Address2 addr)
388		{
389			var sw = new StringWriter();
390			new XmlSerializer(typeof(Address2)).Serialize(sw, addr);
391			return sw.ToString();
392		}
393		private class Update
394		{
395			private CmsData.Person person;
396			private CmsData.Family family;
397			public StringBuilder sb { get; set; }
398			public StringBuilder fsb { get; set; }
399			private XNamespace xsi = "xsi";
400			public Update(CmsData.Person p)
401			{
402				person = p;
403				family = p.Family;
404				sb = new StringBuilder();
405				fsb = new StringBuilder();
406			}
407			public void UpdatePerson(XElement e)
408			{
409				var nil = e.Attribute(xsi + "nil");
410				var name = e.Name.ToString();
411				if (nil != null && nil.Value == "true")
412					person.UpdateValue(sb, name, null);
413				else if (name == "CellPhone" || name == "WorkPhone")
414					person.UpdateValueFromText(fsb, name, e.Value.GetDigits());
415				else
416					person.UpdateValueFromText(sb, name, e.Value);
417			}
418			public void UpdateFamily(XElement e)
419			{
420				var nil = e.Attribute(xsi + "nil");
421				var name = e.Name.ToString();
422				if (nil != null && nil.Value == "true")
423					family.UpdateValue(fsb, name, null);
424				else if (name == "HomePhone")
425					family.UpdateValueFromText(fsb, name, e.Value.GetDigits());
426				else
427					family.UpdateValueFromText(fsb, name, e.Value);
428			}
429		}
430	}
431}