Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Xml.Linq;
- using System.Net;
- namespace NiceSoftInc.ForumToDynamicPlugin
- {
- public class DynamicsLeadPostExportPlugin : IExportPost, IExportTopic
- {
- #region Private members
- private readonly Axapta axapta;
- private string userName;
- private string password;
- private string categoryName;
- #endregion
- public DynamicsLeadPostExportPlugin()
- {
- this.axapta = new Axapta();
- this.ReadSettings();
- }
- #region Xml reader
- private void ReadSettings()
- {
- var xml = XDocument.Load("NiceSoftInc.ForumToDynamicPlugin.Config.xml");
- userName = (from settings in xml.Descendants("settings")
- select settings.Element("username")).First().ToString();
- password = (from settings in xml.Descendants("settings")
- select settings.Element("password")).First().ToString();
- categoryName = (from settings in xml.Descendants("settings")
- select settings.Element("categoryname")).First().ToString();
- }
- #endregion
- #region Axapta Authentication
- private void Logon()
- {
- this.axapta.LogonAs(userName, "",
- new NetworkCredential(this.userName, this.password),
- null, null, null, null);
- }
- private void Logoff()
- {
- this.axapta.Logoff();
- }
- #endregion
- #region Export topic
- public void Export(Topic topic)
- {
- if (topic == null || topic.Title == null ||
- topic.Category.Name != this.categoryName)
- return;
- this.Logon();
- try
- {
- this.axapta.TTSBegin();
- if (this.GetLeadId(topic.Title) == null)
- {
- this.AddNewLead(topic);
- }
- this.axapta.TTSCommit();
- }
- catch (Exception)
- {
- this.axapta.TTSAbort();
- throw;
- }
- finally
- {
- this.Logoff();
- }
- }
- private string AddNewLead(Topic topic)
- {
- using (var axaptaRecord = this.axapta.CreateAxaptaRecord("smmLeadTable"))
- {
- axaptaRecord.Clear();
- axaptaRecord.InitValue();
- string nextLeadId = ((AxaptaObject)this.axapta.CallStaticClassMethod("NumberSeq", "NewGetNum", this.axapta.CallStaticRecordMethod("SalesParameters", "numRefSalesId"))).Call("num").ToString();
- axaptaRecord.Field("LeadId", str);
- axaptaRecord.Field("Subject", topic.Title);
- axaptaRecord.Field("LeadDescription", topic.Description ?? (" [Contact information:" + topic.User.Email + "]"));
- axaptaRecord.Field("PartyId", this.GetUserId(topic.User));
- axaptaRecord.Field("TypeId", "Forum lead");
- axaptaRecord.Field("Priority", "Medium");
- axaptaRecord.Field("Rating", "Hot");
- axaptaRecord.Field("SourceTypeId", "Forum lead");
- axaptaRecord.Insert();
- return nextLeadId;
- }
- }
- private string GetLeadId(string title)
- {
- using (var axaptaRecord = this.axapta.CreateAxaptaRecord("smmLeadTable"))
- {
- axaptaRecord.ExecuteStmt(string.Format("select * from %1 where %1.Subject like '{0}'", title));
- while (axaptaRecord.Found())
- {
- return (string)axaptaRecord.Field("LeadId");
- }
- }
- return null;
- }
- #endregion
- #region Export post
- public void Export(Post post)
- {
- if ((((post == null) || (post.User == null)) || (post.Topic == null)) ||
- (post.Topic.Category.Name != this.categoryName))
- return;
- this.Logon();
- try
- {
- this.axapta.TTSBegin();
- var leadId = this.GetLeadId(post.Topic.Title) ?? this.AddNewLead(post.Topic);
- this.AddNewDiscussion(post, leadId);
- this.axapta.TTSCommit();
- }
- catch (Exception)
- {
- this.axapta.TTSAbort();
- throw;
- }
- finally
- {
- this.Logoff();
- }
- }
- private void AddNewDiscussion(Post post, string leadId)
- {
- using (var axaptaRecord = this.axapta.CreateAxaptaRecord("smmLeadDiscussion"))
- {
- axaptaRecord.Clear();
- axaptaRecord.InitValue();
- axaptaRecord.Field("LeadId", leadId);
- axaptaRecord.Field("PostSubject", post.Subject ?? "No subject");
- axaptaRecord.Field("PostBody", post.Body);
- axaptaRecord.Field("UserName", post.User.Login);
- axaptaRecord.Insert();
- }
- }
- #endregion
- #region User func
- private string AddUser(User user)
- {
- using (var axaptaRecord = this.axapta.CreateAxaptaRecord("DirPartyTable"))
- {
- axaptaRecord.Clear();
- axaptaRecord.InitValue();
- string str = ((AxaptaObject)this.axapta.CallStaticClassMethod("NumberSeq", "NewGetNum", this.axapta.CallStaticRecordMethod("EcpParameters", "numRefECPCustSignUpId"))).Call("num").ToString();
- axaptaRecord.Field("PartyId", str);
- axaptaRecord.Field("Name", user.Login);
- axaptaRecord.Field("FirstName", user.Name ?? "");
- axaptaRecord.Field("LastName", user.Surname ?? "");
- axaptaRecord.Field("Type", 1);
- axaptaRecord.Insert();
- return str;
- }
- }
- private string GetUserId(User user)
- {
- using (var axaptaRecord = this.axapta.CreateAxaptaRecord("DirPartyTable"))
- {
- axaptaRecord.ExecuteStmt(string.Format("select * from %1 where %1.Name like '{0}'", user.Login);
- while (axaptaRecord.Found())
- {
- return (string)axaptaRecord.Field("PartyId");
- }
- }
- return this.AddUser(user);
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement