Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.60 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Xml.Linq;
  4. using System.Net;
  5.  
  6. namespace NiceSoftInc.ForumToDynamicPlugin
  7. {
  8.     public class DynamicsLeadPostExportPlugin : IExportPost, IExportTopic
  9.     {
  10.         #region Private members
  11.  
  12.         private readonly Axapta axapta;
  13.  
  14.         private string userName;
  15.         private string password;
  16.         private string categoryName;
  17.  
  18.         #endregion
  19.  
  20.         public DynamicsLeadPostExportPlugin()
  21.         {
  22.             this.axapta = new Axapta();
  23.  
  24.             this.ReadSettings();
  25.         }
  26.  
  27.         #region Xml reader
  28.  
  29.         private void ReadSettings()
  30.         {
  31.             var xml = XDocument.Load("NiceSoftInc.ForumToDynamicPlugin.Config.xml");
  32.  
  33.             userName = (from settings in xml.Descendants("settings")
  34.                         select settings.Element("username")).First().ToString();
  35.  
  36.             password = (from settings in xml.Descendants("settings")
  37.                         select settings.Element("password")).First().ToString();
  38.  
  39.             categoryName = (from settings in xml.Descendants("settings")
  40.                             select settings.Element("categoryname")).First().ToString();
  41.         }
  42.  
  43.         #endregion
  44.  
  45.         #region Axapta Authentication
  46.  
  47.         private void Logon()
  48.         {
  49.             this.axapta.LogonAs(userName, "",
  50.                 new NetworkCredential(this.userName, this.password),
  51.                 null, null, null, null);
  52.         }
  53.  
  54.         private void Logoff()
  55.         {
  56.             this.axapta.Logoff();
  57.         }
  58.  
  59.         #endregion
  60.  
  61.         #region Export topic
  62.  
  63.         public void Export(Topic topic)
  64.         {
  65.             if (topic == null || topic.Title == null ||
  66.                 topic.Category.Name != this.categoryName)
  67.                 return;
  68.  
  69.             this.Logon();
  70.             try
  71.             {
  72.                 this.axapta.TTSBegin();
  73.                 if (this.GetLeadId(topic.Title) == null)
  74.                 {
  75.                     this.AddNewLead(topic);
  76.                 }
  77.                 this.axapta.TTSCommit();
  78.             }
  79.             catch (Exception)
  80.             {
  81.                 this.axapta.TTSAbort();
  82.                 throw;
  83.             }
  84.             finally
  85.             {
  86.                 this.Logoff();
  87.             }
  88.         }
  89.  
  90.         private string AddNewLead(Topic topic)
  91.         {
  92.             using (var axaptaRecord = this.axapta.CreateAxaptaRecord("smmLeadTable"))
  93.             {
  94.                 axaptaRecord.Clear();
  95.                 axaptaRecord.InitValue();
  96.                 string nextLeadId = ((AxaptaObject)this.axapta.CallStaticClassMethod("NumberSeq", "NewGetNum", this.axapta.CallStaticRecordMethod("SalesParameters", "numRefSalesId"))).Call("num").ToString();
  97.                 axaptaRecord.Field("LeadId", str);
  98.                 axaptaRecord.Field("Subject", topic.Title);
  99.                 axaptaRecord.Field("LeadDescription", topic.Description ?? (" [Contact information:" + topic.User.Email + "]"));
  100.                 axaptaRecord.Field("PartyId", this.GetUserId(topic.User));
  101.                 axaptaRecord.Field("TypeId", "Forum lead");
  102.                 axaptaRecord.Field("Priority", "Medium");
  103.                 axaptaRecord.Field("Rating", "Hot");
  104.                 axaptaRecord.Field("SourceTypeId", "Forum lead");
  105.                 axaptaRecord.Insert();
  106.                 return nextLeadId;
  107.             }
  108.         }
  109.  
  110.         private string GetLeadId(string title)
  111.         {
  112.             using (var axaptaRecord = this.axapta.CreateAxaptaRecord("smmLeadTable"))
  113.             {
  114.                 axaptaRecord.ExecuteStmt(string.Format("select * from %1 where %1.Subject like '{0}'", title));
  115.                 while (axaptaRecord.Found())
  116.                 {
  117.                     return (string)axaptaRecord.Field("LeadId");
  118.                 }
  119.             }
  120.             return null;
  121.         }
  122.  
  123.         #endregion
  124.  
  125.         #region Export post
  126.  
  127.         public void Export(Post post)
  128.         {
  129.             if ((((post == null) || (post.User == null)) || (post.Topic == null)) ||
  130.                 (post.Topic.Category.Name != this.categoryName))
  131.                 return;
  132.  
  133.             this.Logon();
  134.             try
  135.             {
  136.                 this.axapta.TTSBegin();
  137.                 var leadId = this.GetLeadId(post.Topic.Title) ?? this.AddNewLead(post.Topic);
  138.                 this.AddNewDiscussion(post, leadId);
  139.                 this.axapta.TTSCommit();
  140.             }
  141.             catch (Exception)
  142.             {
  143.                 this.axapta.TTSAbort();
  144.                 throw;
  145.             }
  146.             finally
  147.             {
  148.                 this.Logoff();
  149.             }
  150.         }
  151.  
  152.         private void AddNewDiscussion(Post post, string leadId)
  153.         {
  154.             using (var axaptaRecord = this.axapta.CreateAxaptaRecord("smmLeadDiscussion"))
  155.             {
  156.                 axaptaRecord.Clear();
  157.                 axaptaRecord.InitValue();
  158.                 axaptaRecord.Field("LeadId", leadId);
  159.                 axaptaRecord.Field("PostSubject", post.Subject ?? "No subject");
  160.                 axaptaRecord.Field("PostBody", post.Body);
  161.                 axaptaRecord.Field("UserName", post.User.Login);
  162.                 axaptaRecord.Insert();
  163.             }
  164.         }
  165.  
  166.         #endregion
  167.  
  168.         #region User func
  169.  
  170.         private string AddUser(User user)
  171.         {
  172.             using (var axaptaRecord = this.axapta.CreateAxaptaRecord("DirPartyTable"))
  173.             {
  174.                 axaptaRecord.Clear();
  175.                 axaptaRecord.InitValue();
  176.                 string str = ((AxaptaObject)this.axapta.CallStaticClassMethod("NumberSeq", "NewGetNum", this.axapta.CallStaticRecordMethod("EcpParameters", "numRefECPCustSignUpId"))).Call("num").ToString();
  177.                 axaptaRecord.Field("PartyId", str);
  178.                 axaptaRecord.Field("Name", user.Login);
  179.                 axaptaRecord.Field("FirstName", user.Name ?? "");
  180.                 axaptaRecord.Field("LastName", user.Surname ?? "");
  181.                 axaptaRecord.Field("Type", 1);
  182.                 axaptaRecord.Insert();
  183.                 return str;
  184.             }
  185.         }
  186.  
  187.         private string GetUserId(User user)
  188.         {
  189.             using (var axaptaRecord = this.axapta.CreateAxaptaRecord("DirPartyTable"))
  190.             {
  191.                 axaptaRecord.ExecuteStmt(string.Format("select * from %1 where %1.Name like '{0}'", user.Login);
  192.                 while (axaptaRecord.Found())
  193.                 {
  194.                     return (string)axaptaRecord.Field("PartyId");
  195.                 }
  196.             }
  197.             return this.AddUser(user);
  198.         }
  199.  
  200.         #endregion
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement