Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. namespace Terrasoft.Configuration.FacebookWebhookService
  2. {
  3. using System;
  4. using System.CodeDom.Compiler;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.Data;
  8. using System.IO;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.ServiceModel;
  12. using System.ServiceModel.Web;
  13. using System.ServiceModel.Activation;
  14. using System.ServiceModel.Channels;
  15. using System.Web;
  16. using Newtonsoft.Json;
  17. using Newtonsoft.Json.Linq;
  18. using Terrasoft.Common;
  19. using Terrasoft.Common.Json;
  20. using Terrasoft.Core;
  21. using Terrasoft.Core.DB;
  22. using Terrasoft.Core.Entities;
  23. using Terrasoft.Core.Store;
  24. using Terrasoft.Nui.ServiceModel;
  25. using Terrasoft.Nui.ServiceModel.Extensions;
  26. using Terrasoft.UI.WebControls;
  27.  
  28. [ServiceContract]
  29. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
  30. public class FacebookWebhookService
  31. {
  32. //obtain user credentials from system settings
  33. private string GetUserCredentialValueFromSysSettings(UserConnection userConnection, string settingCode) {
  34. Select sysSettingsSelect = new Select(userConnection).Top(1)
  35. .Column("TextValue")
  36. .Column("SysSettingsAlias", "ValueTypeName").As("ValueTypeName")
  37. .From("SysSettingsValue").As("SysSettingsValueAlias")
  38. .Join(JoinType.Inner, "SysSettings").As("SysSettingsAlias")
  39. .On("SysSettingsAlias","Id").IsEqual("SysSettingsValueAlias", "SysSettingsId")
  40. .Where("Code").IsEqual(new QueryParameter(settingCode)) as Select;
  41. using (DBExecutor dbExecutor = userConnection.EnsureDBConnection()) {
  42. using (IDataReader dataReader = sysSettingsSelect.ExecuteReader(dbExecutor)) {
  43. if (dataReader.Read()) {
  44. string valueTypeNameValue = dataReader.GetValue(dataReader.GetOrdinal("ValueTypeName")) as string;
  45. if (valueTypeNameValue == "SecureText") {
  46. DataValueType dbDataValueType = userConnection.DataValueTypeManager.GetInstanceByName(valueTypeNameValue);
  47. object secureTextColumnValue = dataReader.GetValue(dataReader.GetOrdinal("TextValue"));
  48. return dbDataValueType.GetValueForLoad(userConnection, secureTextColumnValue) as string;
  49. } else {
  50. return dataReader.GetValue(dataReader.GetOrdinal("TextValue")) as string;
  51. }
  52. }
  53. }
  54. }
  55. return null;
  56. }
  57.  
  58. //obtain UserConnection, we'll need it for DB oprations
  59. private UserConnection _userConnection;
  60. private UserConnection UserConnection {
  61. get {
  62. if (_userConnection != null) {
  63. return _userConnection;
  64. }
  65. if (HttpContext.Current.Session != null) {
  66. _userConnection = HttpContext.Current.Session["UserConnection"] as UserConnection;
  67. }
  68. if (_userConnection == null) {
  69. var appConnection = HttpContext.Current.Application["AppConnection"] as AppConnection;
  70. var systemUserConnection = appConnection.SystemUserConnection;
  71. string WebLeadUserName = GetUserCredentialValueFromSysSettings(systemUserConnection, "WebLeadUserName");
  72. if (!string.IsNullOrEmpty(WebLeadUserName)) {
  73. string WebLeadUserPassword = GetUserCredentialValueFromSysSettings(systemUserConnection, "WebLeadUserPassword");
  74. string workspace = appConnection.SystemUserConnection.Workspace.Name;
  75. _userConnection = new UserConnection(appConnection);
  76. _userConnection.Initialize();
  77. _userConnection.Login(WebLeadUserName, WebLeadUserPassword, workspace, TimeZoneInfo.Utc);
  78. }
  79. }
  80. return _userConnection;
  81. }
  82. }
  83.  
  84. //enable Headers
  85. [OperationContract]
  86. [WebInvoke(Method = "OPTIONS", UriTemplate = "SaveFacebookLeadData")]
  87. public void GetWebFormLeadDataRequestOptions() {
  88. var OutgoingResponseHeaders = WebOperationContext.Current.OutgoingResponse.Headers;
  89. OutgoingResponseHeaders.Add("Access-Control-Allow-Origin", "*");
  90. OutgoingResponseHeaders.Add("Access-Control-Allow-Methods", "POST");
  91. OutgoingResponseHeaders.Add("Access-Control-Allow-Headers", "Content-Type, Accept");
  92. }
  93.  
  94.  
  95. //proceed request
  96. [OperationContract]
  97. [WebInvoke(Method = "POST", UriTemplate = "SaveFacebookLeadData",
  98. BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json,
  99. ResponseFormat = WebMessageFormat.Json)]
  100. public string SaveFacebookLeadData(FormData formData) // request data
  101. {
  102. if (UserConnection == null) {
  103. return string.Empty;
  104. }
  105. string requestURL = HttpContext.Current.Request.UrlReferrer.AbsoluteUri;
  106.  
  107. //
  108. // do smth with formData
  109. //
  110.  
  111. //result answer
  112. WebFormSavingResult resultObject = new WebFormSavingResult();
  113. resultObject.resultMessage = "Data were catched";
  114. resultObject.resultCode = 0;
  115. var CurrentWebOperationContext = WebOperationContext.Current;
  116. var OutgoingResponseHeaders = CurrentWebOperationContext.OutgoingResponse.Headers;
  117. var incomingRequestOrigin = CurrentWebOperationContext.IncomingRequest.Headers["Origin"];
  118. OutgoingResponseHeaders.Add("Access-Control-Allow-Origin", incomingRequestOrigin);
  119.  
  120. string result = Terrasoft.Common.Json.Json.Serialize(resultObject);
  121. return result;
  122. }
  123.  
  124. //classes
  125. public class FormData
  126. {
  127. public string formId
  128. {
  129. get;
  130. set;
  131. }
  132.  
  133. public FormFieldsData[] formFieldsData
  134. {
  135. get;
  136. set;
  137. }
  138. }
  139.  
  140. public class FormFieldsData
  141. {
  142. public string name {
  143. get;
  144. set;
  145. }
  146.  
  147. public string value {
  148. get;
  149. set;
  150. }
  151. }
  152.  
  153. public class WebFormSavingResult
  154. {
  155. public string resultMessage
  156. {
  157. get;
  158. set;
  159. }
  160.  
  161. public int resultCode
  162. {
  163. get;
  164. set;
  165. }
  166. }
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement