PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/CmsData/APIFunctions.cs

https://bitbucket.org/mahalowe/bvcms
C# | 369 lines | 364 code | 4 blank | 1 comment | 25 complexity | 4169ffe9bb247ee4c8e4dddc33d6db9d MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, AGPL-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UtilityExtensions;
  6. using System.Text.RegularExpressions;
  7. using System.Data.Linq;
  8. using System.Xml.Linq;
  9. using System.Data.Linq.SqlClient;
  10. using IronPython.Hosting;
  11. using System.IO;
  12. using CmsData.Codes;
  13. using System.Web;
  14. using System.Xml;
  15. using System.Diagnostics;
  16. using Microsoft.Scripting.Hosting;
  17. using System.Xml.Serialization;
  18. namespace CmsData.API
  19. {
  20. public partial class APIFunctions
  21. {
  22. private CMSDataContext Db;
  23. public APIFunctions()
  24. {
  25. Db = new CMSDataContext("Data Source=.;Initial Catalog=CMS_bellevue;Integrated Security=True");
  26. }
  27. public APIFunctions(CMSDataContext Db)
  28. {
  29. this.Db = Db;
  30. }
  31. public static string TestAPI(string init, string script, Dictionary<string, string> namevalues)
  32. {
  33. var shell = @"
  34. from System.Net import WebClient, NetworkCredential
  35. from System.Collections.Specialized import NameValueCollection
  36. from System.Text import Encoding
  37. from System import Convert
  38. {0}
  39. class TestAPI(object):
  40. def Run(self):
  41. {1}";
  42. try
  43. {
  44. var sb = new StringBuilder();
  45. var ss = (script ?? "return").Replace("\r\n", "\n");
  46. foreach (var s in ss.Split('\n'))
  47. sb.AppendFormat("\t\t{0}\n", s);
  48. var engine = Python.CreateEngine();
  49. script = shell.Fmt(init, sb.ToString());
  50. var sc = engine.CreateScriptSourceFromString(script);
  51. var code = sc.Compile();
  52. var scope = engine.CreateScope();
  53. foreach (var i in namevalues)
  54. scope.SetVariable(i.Key, i.Value);
  55. code.Execute(scope);
  56. dynamic TestAPI = scope.GetVariable("TestAPI");
  57. dynamic m = TestAPI();
  58. var ret = m.Run();
  59. return HttpUtility.HtmlEncode(ret);
  60. }
  61. catch (Exception ex)
  62. {
  63. return ex.Message;
  64. }
  65. }
  66. public string Login(Person p)
  67. {
  68. var script = Db.Content("API-LoginInfo");
  69. if (script == null)
  70. {
  71. script = new Content();
  72. script.Body = @"
  73. from System import *
  74. from System.Text import *
  75. class LoginInfo(object):
  76. def Run(self, m, w, p):
  77. w.Start('Login')
  78. w.Attr('PeopleId', p.PeopleId)
  79. w.Attr('PreferredName', p.PreferredName)
  80. w.Attr('Last', p.LastName)
  81. w.Attr('EmailAddress', p.EmailAddress)
  82. w.Attr('IsMember', p.MemberStatusId == 10)
  83. for b in m.QueryBits(p.PeopleId):
  84. w.Add('QueryBit', b);
  85. w.End()
  86. return w.ToString()
  87. ";
  88. }
  89. var engine = Python.CreateEngine();
  90. var sc = engine.CreateScriptSourceFromString(script.Body);
  91. try
  92. {
  93. var code = sc.Compile();
  94. var scope = engine.CreateScope();
  95. code.Execute(scope);
  96. dynamic LoginInfo = scope.GetVariable("LoginInfo");
  97. dynamic m = LoginInfo();
  98. var api = new APIFunctions(Db);
  99. var w = new APIWriter();
  100. return m.Run(api, w, p);
  101. }
  102. catch (Exception ex)
  103. {
  104. return "<login error=\"API-LoginInfo script error: {0}\" />".Fmt(ex.Message);
  105. }
  106. }
  107. public int AttendCount(int orgid, int PeopleId)
  108. {
  109. var q = from a in Db.Attends
  110. where a.OrganizationId == orgid
  111. where a.PeopleId == PeopleId
  112. where a.AttendanceFlag == true
  113. select a;
  114. return q.Count();
  115. }
  116. public List<string> QueryBits(int PeopleId)
  117. {
  118. var q1 = (from f in Db.QueryBitsFlags()
  119. select f).ToList();
  120. var q2 = (from t in Db.TagPeople
  121. where t.PeopleId == PeopleId
  122. where t.Tag.TypeId == 100
  123. select t.Tag.Name).ToList();
  124. var q = from t in q2
  125. join f in q1 on t equals f[0]
  126. select f[1];
  127. var list = q.ToList();
  128. return list;
  129. }
  130. public DateTime LastAttendDate(int orgid, int PeopleId)
  131. {
  132. var q = from a in Db.Attends
  133. where a.OrganizationId == orgid
  134. where a.PeopleId == PeopleId
  135. where a.AttendanceFlag == true
  136. orderby a.MeetingDate descending
  137. select a.MeetingDate;
  138. return q.FirstOrDefault();
  139. }
  140. public int IsMemberOf(int orgid, int PeopleId)
  141. {
  142. var q = from a in Db.OrganizationMembers
  143. where a.OrganizationId == orgid
  144. where a.PeopleId == PeopleId
  145. where a.MemberTypeId != Codes.MemberTypeCode.InActive
  146. select a;
  147. return q.Count();
  148. }
  149. public void DeleteExtraValue(int peopleid, string field)
  150. {
  151. var q = from v in Db.PeopleExtras
  152. where v.Field == field
  153. where v.PeopleId == peopleid
  154. select v;
  155. Db.PeopleExtras.DeleteAllOnSubmit(q);
  156. Db.SubmitChanges();
  157. }
  158. public string ExtraValues(int peopleid, string fields)
  159. {
  160. try
  161. {
  162. var a = (fields ?? "").Split(',');
  163. var nofields = !fields.HasValue();
  164. var q = from v in Db.PeopleExtras
  165. where nofields || a.Contains(v.Field)
  166. where v.PeopleId == peopleid
  167. select v;
  168. var w = new APIWriter();
  169. w.Start("ExtraValues");
  170. w.Attr("Id", peopleid);
  171. foreach (var v in q)
  172. w.Add(v.Field, v.StrValue ?? v.Data ?? v.DateValue.FormatDate() ?? v.IntValue.ToString());
  173. w.End();
  174. return w.ToString();
  175. }
  176. catch (Exception ex)
  177. {
  178. return ex.Message;
  179. }
  180. }
  181. public string AddEditExtraValue(int peopleid, string field, string value, string type = "data")
  182. {
  183. try
  184. {
  185. var q = from v in Db.PeopleExtras
  186. where v.Field == field
  187. where v.PeopleId == peopleid
  188. select v;
  189. var ev = q.SingleOrDefault();
  190. if (ev == null)
  191. {
  192. ev = new PeopleExtra
  193. {
  194. PeopleId = peopleid,
  195. Field = field,
  196. TransactionTime = DateTime.Now
  197. };
  198. Db.PeopleExtras.InsertOnSubmit(ev);
  199. }
  200. else
  201. {
  202. // prepare for new data type
  203. ev.Data = null;
  204. ev.IntValue = null;
  205. ev.DateValue = null;
  206. ev.StrValue = null;
  207. }
  208. switch (type)
  209. {
  210. case "code":
  211. ev.StrValue = value;
  212. break;
  213. case "data":
  214. ev.Data = value;
  215. break;
  216. case "date":
  217. ev.DateValue = value.ToDate();
  218. break;
  219. case "int":
  220. ev.IntValue = value.ToInt2();
  221. break;
  222. }
  223. Db.SubmitChanges();
  224. return "ok";
  225. }
  226. catch (Exception ex)
  227. {
  228. return ex.Message;
  229. }
  230. }
  231. public string FamilyMembers(int familyid)
  232. {
  233. try
  234. {
  235. var q = from p in DbUtil.Db.People
  236. where p.FamilyId == familyid
  237. select p;
  238. var w = new APIWriter();
  239. w.Start("Family");
  240. w.Attr("Id", familyid);
  241. foreach (var m in q)
  242. {
  243. w.Start("Member");
  244. w.Add("peopleid", m.PeopleId);
  245. w.Add("first", m.FirstName);
  246. w.Add("last", m.LastName);
  247. w.Add("goesby", m.NickName);
  248. w.Add("birthday", m.BDate);
  249. w.Add("position", m.PositionInFamilyId);
  250. w.Add("marital", m.MaritalStatusId);
  251. w.Add("suffix", m.SuffixCode);
  252. w.Add("title", m.TitleCode);
  253. w.End();
  254. }
  255. w.End();
  256. return w.ToString();
  257. }
  258. catch (Exception ex)
  259. {
  260. return ex.Message;
  261. }
  262. }
  263. [Serializable()]
  264. public class AccessUsers
  265. {
  266. [XmlElement("Person")]
  267. public AccessUserInfo[] People { get; set; }
  268. }
  269. [Serializable()]
  270. public class AccessUserInfo
  271. {
  272. [XmlAttribute("peopleid")]
  273. public int peopleid { get; set; }
  274. public string first { get; set; }
  275. public string goesby { get; set; }
  276. public string last { get; set; }
  277. public string cphone { get; set; }
  278. public string hphone { get; set; }
  279. public string wphone { get; set; }
  280. public string dob { get; set; }
  281. public int? bmon { get; set; }
  282. public int? bday { get; set; }
  283. public int? byear { get; set; }
  284. public int? married { get; set; }
  285. public int? gender { get; set; }
  286. public string company { get; set; }
  287. public string email { get; set; }
  288. public string email2 { get; set; }
  289. public string username { get; set; }
  290. public string lastactive { get; set; }
  291. public string roles { get; set; }
  292. public List<string> QueryBits { get; set; }
  293. }
  294. public IEnumerable<AccessUserInfo> AccessUsersData(bool includeNoAccess = false)
  295. {
  296. var q = from u in Db.Users
  297. where includeNoAccess || u.UserRoles.Any(rr => rr.Role.RoleName == "Access")
  298. where u.EmailAddress.Length > 0
  299. select new
  300. {
  301. u.PeopleId,
  302. roles = u.UserRoles.Select(uu => uu.Role.RoleName),
  303. lastactive = Db.LastActive(u.UserId),
  304. first = u.Person.FirstName,
  305. goesby = u.Person.NickName,
  306. last = u.Person.LastName,
  307. married = u.Person.MaritalStatusId,
  308. gender = u.Person.GenderId,
  309. cphone = u.Person.CellPhone,
  310. hphone = u.Person.HomePhone,
  311. wphone = u.Person.WorkPhone,
  312. dob = u.Person.DOB,
  313. bday = u.Person.BirthDay,
  314. bmon = u.Person.BirthMonth,
  315. byear = u.Person.BirthYear,
  316. company = u.Person.EmployerOther,
  317. email = u.EmailAddress,
  318. email2 = u.Person.EmailAddress2,
  319. username = u.Username,
  320. };
  321. var list = q.ToList();
  322. var q2 = from i in list
  323. group i by i.PeopleId into g
  324. let i1 = g.OrderByDescending(i => i.lastactive).First()
  325. select new AccessUserInfo
  326. {
  327. peopleid = i1.PeopleId.Value,
  328. first = i1.first,
  329. goesby = i1.goesby,
  330. last = i1.last,
  331. married = i1.married,
  332. gender = i1.gender,
  333. cphone = i1.cphone.GetDigits(),
  334. hphone = i1.hphone.GetDigits(),
  335. wphone = i1.wphone,
  336. dob = i1.dob,
  337. bday = i1.bday,
  338. bmon = i1.bmon,
  339. byear = i1.byear,
  340. company = i1.company,
  341. email = i1.email,
  342. email2 = i1.email2,
  343. username = i1.username,
  344. lastactive = i1.lastactive.ToString2("s"),
  345. roles = string.Join(",", i1.roles),
  346. #if DEBUG
  347. #else
  348. QueryBits = (from qb in QueryBits(i1.PeopleId.Value) select qb).ToList()
  349. #endif
  350. };
  351. return q2;
  352. }
  353. public string AccessUsersXml(bool includeNoAccess = false)
  354. {
  355. var xs = new XmlSerializer(typeof(AccessUsers));
  356. var sw = new StringWriter();
  357. var a = new AccessUsers { People = AccessUsersData(includeNoAccess).ToArray() };
  358. xs.Serialize(sw, a);
  359. return sw.ToString();
  360. }
  361. }
  362. }