using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web.UI.WebControls; using CMS.CMSHelper; using CMS.DataCom; using CMS.EventLog; using CMS.FormEngine; using CMS.GlobalHelper; using CMS.OnlineMarketing; /// /// A dialog page where user can edit the mapping of CMS contact (or account) fields and Data.com contact (or company) attributes. /// public partial class CMSModules_ContactManagement_Pages_Tools_DataCom_EditMapping : CMSDataComDialogPage { #region "Inner classes" /// /// Represents a set of objects that depend on the mapping context. /// private class RequestContext { /// /// CMS contact (or account) form info. /// public FormInfo FormInfo; /// /// Data.com contact (or company) entity info. /// public EntityInfo EntityInfo; /// /// Delegate to initialize the mapping control. /// public Action InitializeMappingControl; } #endregion #region "Variables" private string mEntityName; private string mSourceMappingHiddenFieldClientId; private string mSourceMappingPanelClientId; private EntityMapping mSourceMapping; /// /// Current request context. /// private RequestContext mContext = null; /// /// A dictionary of form field name/entity attribute dropdown control pairs. /// private Dictionary mAttributeNamesDropDownLists = new Dictionary(); #endregion #region "Properties" /// /// Gets the client identifier of the hidden input field with mapping on the page that opened this dialog. /// protected string SourceMappingHiddenFieldClientId { get { return mSourceMappingHiddenFieldClientId; } } /// /// Gets the client identifier of the mapping control with mapping on the page that opened this dialog. /// protected string SourceMappingPanelClientId { get { return mSourceMappingPanelClientId; } } /// /// Gets the last available entity mapping associated with this dialog. /// protected EntityMapping SourceMapping { get { return mSourceMapping; } } #endregion #region "Life cycle methods" protected override void OnInit(EventArgs e) { base.OnInit(e); ScriptHelper.RegisterWOpenerScript(Page); ScriptHelper.RegisterJQuery(Page); CurrentMaster.Title.TitleText = GetString("datacom.editmapping.title"); CurrentMaster.Title.TitleImage = GetImageUrl("Objects/OM_Contact/object.png"); ConfirmButton.Click += new EventHandler(ConfirmButton_Click); MappingPanel.Style.Add("display", "none"); try { RestoreParameters(); mContext = CreateContext(); InitializeControls(); } catch (Exception exception) { HandleException(exception); } } protected void ConfirmButton_Click(object sender, EventArgs e) { if (mContext != null) { try { EntityMapping mapping = CreateEntityMappingFromForm(); EntityMappingSerializer serializer = new EntityMappingSerializer(); MappingHiddenField.Value = serializer.SerializeEntityMapping(mapping); mContext.InitializeMappingControl(mapping); string parametersIdentifier = QueryHelper.GetString("pid", null); Hashtable parameters = WindowHelper.GetItem(parametersIdentifier) as Hashtable; parameters["Mapping"] = MappingHiddenField.Value; WindowHelper.Add(parametersIdentifier, parameters); } catch (Exception exception) { HandleException(exception); } } } protected void MappingItemRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) { Literal literal = e.Item.FindControl("FieldNameLiteral") as Literal; DropDownList dropDownList = e.Item.FindControl("AttributeNamesDropDownList") as DropDownList; FormFieldInfo fieldInfo = e.Item.DataItem as FormFieldInfo; literal.Text = ResHelper.LocalizeString(fieldInfo.Caption); dropDownList.Items.Add(new ListItem(String.Empty, String.Empty)); dropDownList.Items.AddRange(mContext.EntityInfo.Items.Select(x => new ListItem(ResHelper.LocalizeString(x.DisplayName), x.Name)).ToArray()); mAttributeNamesDropDownLists.Add(fieldInfo.Name, dropDownList); } #endregion #region "Private methods" private void InitializeControls() { MappingItemRepeater.DataSource = mContext.FormInfo.GetFields(true, false); MappingItemRepeater.DataBind(); if (!RequestHelper.IsPostBack()) { foreach (FormFieldInfo fieldInfo in mContext.FormInfo.GetFields(true, false)) { DropDownList dropDownList = mAttributeNamesDropDownLists[fieldInfo.Name]; EntityMappingItem item = SourceMapping.GetItem(fieldInfo.Name); if (item == null) { dropDownList.SelectedIndex = 0; } else { dropDownList.SelectedValue = item.EntityAttributeName; } } } } /// /// Creates a mapping of CMS contact (or account) fields and Data.com contact (or company) attributes from the current form state, and returns it. /// /// A mapping of CMS contact (or account) fields and Data.com contact (or company) attributes. private EntityMapping CreateEntityMappingFromForm() { EntityMapping mapping = new EntityMapping(); foreach (FormFieldInfo fieldInfo in mContext.FormInfo.GetFields(true, false)) { DropDownList dropDownList = mAttributeNamesDropDownLists[fieldInfo.Name]; if (dropDownList.SelectedIndex != 0) { EntityAttributeInfo attributeInfo = mContext.EntityInfo.GetAttributeInfo(dropDownList.SelectedValue); if (attributeInfo != null) { mapping.Add(attributeInfo, fieldInfo); } } } return mapping; } /// /// Creates current request context, and returns it. /// /// Current request context. private RequestContext CreateContext() { if (mEntityName == "Contact") { return new RequestContext { FormInfo = DataComHelper.GetContactFormInfo(), EntityInfo = DataComHelper.GetContactEntityInfo(), InitializeMappingControl = (x => ContactMappingControl.Mapping = x) }; } if (mEntityName == "Company") { IDataComConfiguration configuration = DataComHelper.GetConfiguration(CMSContext.CurrentSiteID); return new RequestContext { FormInfo = DataComHelper.GetAccountFormInfo(), EntityInfo = DataComHelper.GetCompanyEntityInfo(configuration), InitializeMappingControl = (x => CompanyMappingControl.Mapping = x) }; } throw new Exception(String.Format("[DataComEditMappingPage.CreateContext]: Invalid mapping type ({0}).", mEntityName)); } private void RestoreParameters() { // Validate parameters if (!QueryHelper.ValidateHash("hash")) { throw new Exception("[DataComEditMappingPage.RestoreParameters]: Invalid query hash."); } Hashtable parameters = WindowHelper.GetItem(QueryHelper.GetString("pid", null)) as Hashtable; if (parameters == null) { throw new Exception("[DataComEditMappingPage.RestoreParameters]: The dialog page parameters are missing, the session might have been lost."); } // Restore parameters mEntityName = ValidationHelper.GetString(parameters["EntityName"], null); mSourceMappingHiddenFieldClientId = ValidationHelper.GetString(parameters["MappingHiddenFieldClientId"], null); mSourceMappingPanelClientId = ValidationHelper.GetString(parameters["MappingPanelClientId"], null); // Restore mapping string content = ValidationHelper.GetString(parameters["Mapping"], null); if (String.IsNullOrEmpty(content)) { mSourceMapping = new EntityMapping(); } else { EntityMappingSerializer serializer = new EntityMappingSerializer(); mSourceMapping = serializer.UnserializeEntityMapping(content); } } /// /// Displays a default error message and logs the specified exception to the event log. /// /// The exception to handle. private void HandleException(Exception exception) { ErrorSummary.Report(exception); EventLogProvider.LogException("Data.com Connector", "EditMappingPage", exception); } #endregion }