initial commit
[CPE_learningsite] / CPE / CPE.App / CPE.App.Web / Helpers / adobe.cs
1 using System;
2 using System.Linq;
3 using System.Xml;
4 using CPE.App.Web.Connect;
5 using CPE.App.Web.Models;
6 using CPE.App.Web.Code;
7
8 namespace CPE.App.Web
9 {
10     public static class adobe
11     {
12         public static void GetAdobeTransactions(MeetingSession meetingSession, CPEWebDataContext context)
13         {
14             Extensions.LogServiceCall("[adobe][GetAdobeTransactions]", String.Format("Parameters: meetingSession.MeetingSessionKey = {0} meetingSession.EndDate.HasValue = {1}", meetingSession.MeetingSessionKey, meetingSession.EndDate.HasValue));
15
16             int adobeTransactionsAdded = 0;
17
18             if (!meetingSession.EndDate.HasValue)
19                 return;
20
21             string login = "support@intesolv.com";
22             string password = "Cp3!nC";
23             string url = "http://cpeonline.adobeconnect.com";
24             int accountId = 1050997608;
25             Session session = Login.UserLogin(login, password, url, accountId);
26
27             if (session == null)
28             {
29                 Extensions.LogServiceCall("[adobe][GetAdobeTransactions]", String.Format("session == null; meetingSession.MeetingSessionKey = {0}", meetingSession.MeetingSessionKey));
30                 return;
31             }
32
33             var oldRecords = context.AdobeTransactions.Where(x => x.MeetingSessionKey == meetingSession.MeetingSessionKey);
34
35             //string dateCreatedFilter = meetingSession.StartDate.AddHours(-6).ToString("yyyy-MM-ddThh:mm:00.000-04:00"); // Striping the time is way easier than trying to use the correct time zone, even though the results may be different
36             //string dateCreatedFilter = meetingSession.StartDate.AddHours(-6).ToString("yyyy-MM-ddThh:mm"); // This sometimes fails, so adobe asked us to format the date differently
37             string dateCreatedFilter = meetingSession.StartDate.AddHours(-6).ToString("yyyy-MM-dd");
38
39             // https://cpeonline.adobeconnect.com/api/xml?action=report-bulk-consolidated-transactions&filter-sco-id=1091486528&filter-gt-date-created=2014-06-23T8:00
40             var request = new Connect.Request(session, "report-bulk-consolidated-transactions");
41             request.Parameters.Add("filter-sco-id", meetingSession.SCO_ID.ToString());
42             request.Parameters.Add("filter-gt-date-created", dateCreatedFilter);
43
44             Extensions.LogServiceCall("[adobe][GetAdobeTransactions]", String.Format("Before report-bulk-consolidated-transactions.  meetingSession.MeetingSessionKey = {0} ScoIdFilter = {1} dateCreatedFilter = {2}", meetingSession.MeetingSessionKey, meetingSession.SCO_ID.ToString(), dateCreatedFilter));
45
46             if (request.Execute() && request.Status == Status.OK)
47             {
48                 // Tyler Allen -- 08/27/2016
49                 // The transaction is getting locked due to other transaction working on the same row.
50                 try
51                 {
52                     context.AdobeTransactions.DeleteAllOnSubmit(oldRecords);
53
54                     var xml = request.XmlResults;
55                     var nodes = xml.SelectNodes("//row");
56                     foreach (XmlNode node in nodes)
57                     {
58                         var dateString = node.SelectSingleNode("date-created")
59                                              .InnerText;
60                         var offSetString = dateString.Substring(dateString.Length - 6, 6);
61                         var offSet = int.Parse(offSetString.Substring(1, 2));
62                         dateString = dateString.Replace(offSetString, "");
63                         var dateStart = DateTime.Parse(dateString)
64                                                 .AddHours(offSet);
65                         var transaction = new AdobeTransaction()
66                         {
67                             Created = DateTime.UtcNow,
68
69                             Principal_ID = int.Parse(node.Attributes["principal-id"].Value),
70                             MeetingSessionKey = meetingSession.MeetingSessionKey,
71
72                             StartDate = dateStart,
73                             EndDate = (node.SelectSingleNode("date-closed") == null) ? ((meetingSession.EndDate.HasValue) ? meetingSession.EndDate.Value : DateTime.UtcNow) : DateTime.Parse(node.SelectSingleNode("date-closed").InnerText.Replace(offSetString, "")).AddHours(offSet)
74                         };
75                         string dateClosedForLogging = "null";
76                         if (node.SelectSingleNode("date-closed") != null)
77                         {
78                             dateClosedForLogging = node.SelectSingleNode("date-closed").InnerText;
79                         }
80                         Extensions.LogServiceCall("[adobe][GetAdobeTransactions]", String.Format("Before Insert.  meetingSession.MeetingSessionKey = {0} Principal_ID = {1} dateCreated = {2} dateClosed = {3}", meetingSession.MeetingSessionKey, transaction.Principal_ID, node.SelectSingleNode("date-created").InnerText, dateClosedForLogging));
81                         context.AdobeTransactions.InsertOnSubmit(transaction);
82                         adobeTransactionsAdded++;
83                     }
84                     context.SubmitChanges();
85                 } catch (Exception exception) {
86                  Console.WriteLine(exception);
87                 }
88                 var adobeTransactionsForMeeting = context.AdobeTransactions.Where(at => at.MeetingSessionKey == meetingSession.MeetingSessionKey).OrderByDescending(at => at.AdobeTransactionKey).ToList();
89                 if (adobeTransactionsForMeeting.Count > 1)
90                 {
91                     Extensions.LogServiceCall("[adobe][GetAdobeTransactions]", String.Format("After Insert.  meetingSession.MeetingSessionKey = {0} Max AdobeTransactionKey = {1}", meetingSession.MeetingSessionKey, adobeTransactionsForMeeting.First().AdobeTransactionKey));
92                 }
93                 else
94                 {
95                     Extensions.LogServiceCall("[adobe][GetAdobeTransactions]", String.Format("After Insert.  meetingSession.MeetingSessionKey = {0} No AdobeTransactions found.", meetingSession.MeetingSessionKey));
96                 }
97             }
98             else
99             {
100                 Extensions.LogServiceCall("[adobe][GetAdobeTransactions]", String.Format("Execute failed.  meetingSession.MeetingSessionKey = {0} request.Status = {1} request.XmlResults = [{2}] request.Url = {3}", meetingSession.MeetingSessionKey, request.Status, request.XmlResults.InnerXml, request.Url));
101
102                 // Retry?
103             }
104
105             Extensions.LogServiceCall("[adobe][GetAdobeTransactions]", String.Format("End meetingSession.MeetingSessionKey = {0} adobeTransactionsAdded = {1}", meetingSession.MeetingSessionKey, adobeTransactionsAdded));
106         }
107
108         public static void GetAdobeCertificateTransactions(MeetingSession meetingSession, CPEWebDataContext context)
109         {
110             Extensions.LogServiceCall("[adobe][GetAdobeCertificateTransactions]", String.Format("Parameters: meetingSession.MeetingSessionKey = {0} meetingSession.EndDate.HasValue = {1}", meetingSession.MeetingSessionKey, meetingSession.EndDate.HasValue));
111
112             int adobeCertificateTransactionsAdded = 0;
113
114             if (!meetingSession.EndDate.HasValue)
115                 return;
116
117             string login = "support@intesolv.com";
118             string password = "Cp3!nC";
119             string url = "http://cpeonline.adobeconnect.com";
120             int accountId = 1050997608;
121             Session session = Login.UserLogin(login, password, url, accountId);
122
123             if (session == null)
124                 return;
125
126             var oldRecords = context.AdobeCertificateTransactions.Where(x => x.MeetingSessionKey == meetingSession.MeetingSessionKey);
127             context.AdobeCertificateTransactions.DeleteAllOnSubmit(oldRecords);
128
129             // https://cpeonline.adobeconnect.com/api/xml?action=report-bulk-objects&filter-sco-id=1091486528
130             var request = new Connect.Request(session, "report-bulk-objects");
131             request.Parameters.Add("filter-sco-id", meetingSession.SCO_ID.ToString());
132
133             if (request.Execute() && request.Status == Status.OK)
134             {
135                 var xml = request.XmlResults;
136                 var nodes = xml.SelectNodes("//row");
137                 foreach (XmlNode node in nodes)
138                 {
139                     var description = node.SelectSingleNode("description")
140                                          .InnerText;
141                     string[] fields = description.Split('|');
142                     string presenter = fields.FirstOrDefault(p => p.Contains("Presenter"));
143                     presenter = presenter?.Substring(presenter.IndexOf('=') + 1);
144                     string fos = fields.FirstOrDefault(f => f.Contains("FOS") || f.Contains("Field of Study"));
145                     fos = fos?.Substring(fos.IndexOf('=') + 1);
146                     var transaction = new AdobeCertificateTransaction()
147                     {
148                         Created = DateTime.UtcNow,
149                         MeetingSessionKey = meetingSession.MeetingSessionKey,
150                         Fos = fos,
151                         Presenter = presenter
152                     };
153                     context.AdobeCertificateTransactions.InsertOnSubmit(transaction);
154                     adobeCertificateTransactionsAdded++;
155                 }
156             }
157
158             Extensions.LogServiceCall("[adobe][GetAdobeCertificateTransactions]", String.Format("End meetingSession.MeetingSessionKey = {0} adobeCertificateTransactionsAdded = {1}", meetingSession.MeetingSessionKey, adobeCertificateTransactionsAdded));
159         }
160     }
161 }