initial commit
[CPE_learningsite] / CPE / CPE.App / CPE.App.NotifyConsole / Helpers / AdobeHelper.cs
1 /*
2 Developer: Tyler Allen
3 Date Created: 08/24/2016
4 ---------------------------------------------------
5 */
6
7 using System;
8 using System.Configuration;
9 using System.Linq;
10 using System.Xml;
11 using CPE.App.NotifyConsole.Extensions;
12 using CPE.App.NotifyConsole.Models;
13 using CPE.App.NotifyConsole.Models.Enums;
14 using IBS.Connect.API;
15 using IBS.Connect.API.Enums;
16 using Action = IBS.Connect.API.Action;
17
18 namespace CPE.App.NotifyConsole.Helpers {
19     /// <summary>
20     /// </summary>
21     public class AdobeHelper {
22         private Session _adobeConnectSession;
23
24         /// <summary>
25         /// </summary>
26         /// <returns></returns>
27         private Session getAdobeConnectSession() {
28             if(_adobeConnectSession == null) {
29                 string login = null;
30                 string password = null;
31                 string url = null;
32                 long? accountId = null;
33                 try {
34                     login = ConfigurationManager.AppSettings["Adobe_Login"];
35                     password = ConfigurationManager.AppSettings["Adobe_Password"];
36                     url = ConfigurationManager.AppSettings["Adobe_Url"];
37                     accountId = int.Parse(ConfigurationManager.AppSettings["Adobe_AccountId"]);
38                 } catch (Exception exception) {
39                     exception.Log(LoggingLevels.Fatal);
40                     return null;
41                 }
42                 var connectSession = new Session(new Uri(url)) {
43                     AccountId = accountId,
44                     LoginId = login,
45                     Password = password
46                 };
47                 if(connectSession.Login()) {
48                     _adobeConnectSession = connectSession;
49                 }
50             }
51             return _adobeConnectSession;
52         }
53
54         /// <summary>
55         /// </summary>
56         /// <param name="meetingSession"></param>
57         /// <param name="context"></param>
58         /// <returns></returns>
59         public bool GetAdobeTransactions(MeetingSession meetingSession, CPEWebDataContext context) {
60             var boolResult = false;
61
62             // Test if the session has ended.
63             if(!meetingSession.EndDate.HasValue) {
64                 return false;
65             }
66
67             // Get an Adobe Connect session and test for valid session.
68             var connectSession = getAdobeConnectSession();
69             if(connectSession == null) {
70                 return false;
71             }
72
73
74             // Get the transaction for the meeting session from Adobe Connect
75             var dateCreatedFilter = meetingSession.StartDate.AddHours(-6)
76                                                   .ToString("yyyy-MM-dd");
77             var request = new Request {
78                 Actions = {
79                     new Action("report-bulk-consolidated-transactions") {
80                         Filters = {
81                             new Filter {
82                                 Field = "sco-id",
83                                 Value = meetingSession.SCO_ID
84                             },
85                             new Filter {
86                                 Field = "date-created",
87                                 FilterModifier = FilterModifiers.GreaterThan,
88                                 Value = dateCreatedFilter
89                             }
90                         }
91                     }
92                 }
93             };
94             request.ActionsXml.Log();
95             request.ExecuteUrl.Log();
96             var response = request.Execute(connectSession);
97             response.Status.Log();
98             response.XmlString.Log();
99             if(response.Status == ResponseStatus.OK) {
100                 // Delete the old records that are no longer valid
101                 var oldRecords = context.AdobeTransactions.Where(x => x.MeetingSessionKey == meetingSession.MeetingSessionKey);
102                 context.AdobeTransactions.DeleteAllOnSubmit(oldRecords);
103
104                 var xml = response.Xml;
105                 var nodes = xml.SelectNodes("//row");
106                 foreach (XmlNode node in nodes) {
107                     node.OuterXml.Log();
108                     var dateString = node.SelectSingleNode("date-created")
109                                          .InnerText;
110                     var offSetString = dateString.Substring(dateString.Length - 6, 6);
111                     var offSet = int.Parse(offSetString.Substring(1, 2));
112                     dateString = dateString.Replace(offSetString, "");
113                     var dateStart = DateTime.Parse(dateString)
114                                             .AddHours(offSet);
115                     var transaction = new AdobeTransaction {
116                         Created = DateTime.UtcNow,
117                         Principal_ID = int.Parse(node.Attributes["principal-id"].Value),
118                         MeetingSessionKey = meetingSession.MeetingSessionKey,
119                         StartDate = dateStart,
120                         EndDate = node.SelectSingleNode("date-closed") == null ? (meetingSession.EndDate ?? DateTime.UtcNow) : DateTime.Parse(node.SelectSingleNode("date-closed")
121                                                                                                                                                                                          .InnerText.Replace(offSetString, ""))
122                                                                                                                                                                               .AddHours(offSet)
123                     };
124
125                     context.AdobeTransactions.InsertOnSubmit(transaction);
126                 }
127                 context.SubmitChanges();
128                 boolResult = true;
129             }
130             return boolResult;
131         }
132
133         /// <summary>
134         /// </summary>
135         /// <param name="meetingSession"></param>
136         /// <param name="context"></param>
137         /// <returns></returns>
138         public bool GetAdobeCertificateTransactions(MeetingSession meetingSession, CPEWebDataContext context) {
139             var boolResult = false;
140
141             if(!meetingSession.EndDate.HasValue) {
142                 return false;
143             }
144
145             var connectSession = getAdobeConnectSession();
146             if(connectSession == null) {
147                 return false;
148             }
149
150             var oldRecords = context.AdobeCertificateTransactions.Where(x => x.MeetingSessionKey == meetingSession.MeetingSessionKey);
151             context.AdobeCertificateTransactions.DeleteAllOnSubmit(oldRecords);
152
153             var request = new Request {
154                 Actions = {
155                     new Action("report-bulk-objects") {
156                         Filters = {
157                             new Filter {
158                                 Field = "sco-id",
159                                 Value = meetingSession.SCO_ID
160                             }
161                         }
162                     }
163                 }
164             };
165             request.ActionsXml.Log();
166             request.ExecuteUrl.Log();
167             var response = request.Execute(connectSession);
168             response.Status.Log();
169             response.XmlString.Log();
170             if(response.Status == ResponseStatus.OK) {
171                 var xml = response.Xml;
172                 var nodes = xml.SelectNodes("//row");
173                 foreach (XmlNode node in nodes) {
174                     var description = node.SelectSingleNode("description")
175                                           .InnerText;
176                     var fields = description.Split('|');
177                     var presenter = fields.FirstOrDefault(p => p.Contains("Presenter"));
178                     presenter = presenter?.Substring(presenter.IndexOf('=') + 1);
179                     var fos = fields.FirstOrDefault(f => f.Contains("FOS") || f.Contains("Field of Study"));
180                     fos = fos?.Substring(fos.IndexOf('=') + 1);
181                     var transaction = new AdobeCertificateTransaction {
182                         Created = DateTime.UtcNow,
183                         MeetingSessionKey = meetingSession.MeetingSessionKey,
184                         Fos = fos,
185                         Presenter = presenter
186                     };
187                     context.AdobeCertificateTransactions.InsertOnSubmit(transaction);
188                 }
189                 boolResult = true;
190             }
191             return boolResult;
192         }
193     }
194 }