From 696ecad4370849a306f1bad25c51162261267efc Mon Sep 17 00:00:00 2001 From: "Sean G. Wright" Date: Wed, 31 Jan 2024 18:33:28 -0500 Subject: [PATCH] fix(CRM.Common): update existing data class and form objects --- .../Admin/CRMModuleInstaller.cs | 93 ++++++++++--------- 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/src/Kentico.Xperience.CRM.Common/Admin/CRMModuleInstaller.cs b/src/Kentico.Xperience.CRM.Common/Admin/CRMModuleInstaller.cs index 70d0cb5..e0c1e39 100644 --- a/src/Kentico.Xperience.CRM.Common/Admin/CRMModuleInstaller.cs +++ b/src/Kentico.Xperience.CRM.Common/Admin/CRMModuleInstaller.cs @@ -52,19 +52,13 @@ private void InstallModuleClasses(ResourceInfo resourceInfo) private void InstallSyncedItemClass(ResourceInfo resourceInfo) { - var failedSyncItemClass = DataClassInfoProvider.GetDataClassInfo(CRMSyncItemInfo.OBJECT_TYPE); - if (failedSyncItemClass is not null) - { - return; - } - - failedSyncItemClass = DataClassInfo.New(CRMSyncItemInfo.OBJECT_TYPE); + var info = DataClassInfoProvider.GetDataClassInfo(CRMSyncItemInfo.OBJECT_TYPE) ?? DataClassInfo.New(CRMSyncItemInfo.OBJECT_TYPE); - failedSyncItemClass.ClassName = CRMSyncItemInfo.TYPEINFO.ObjectClassName; - failedSyncItemClass.ClassTableName = CRMSyncItemInfo.TYPEINFO.ObjectClassName.Replace(".", "_"); - failedSyncItemClass.ClassDisplayName = "CRM Successful sync item"; - failedSyncItemClass.ClassResourceID = resourceInfo.ResourceID; - failedSyncItemClass.ClassType = ClassType.OTHER; + info.ClassName = CRMSyncItemInfo.TYPEINFO.ObjectClassName; + info.ClassTableName = CRMSyncItemInfo.TYPEINFO.ObjectClassName.Replace(".", "_"); + info.ClassDisplayName = "CRM Successful sync item"; + info.ClassResourceID = resourceInfo.ResourceID; + info.ClassType = ClassType.OTHER; var formInfo = FormHelper.GetBasicFormDefinition(nameof(CRMSyncItemInfo.CRMSyncItemID)); @@ -131,26 +125,23 @@ private void InstallSyncedItemClass(ResourceInfo resourceInfo) }; formInfo.AddFormItem(formItem); - failedSyncItemClass.ClassFormDefinition = formInfo.GetXmlDefinition(); + SetFormDefinition(info, formInfo); - DataClassInfoProvider.SetDataClassInfo(failedSyncItemClass); + if (info.HasChanged) + { + DataClassInfoProvider.SetDataClassInfo(info); + } } private void InstallFailedSyncItemClass(ResourceInfo resourceInfo) { - var failedSyncItemClass = DataClassInfoProvider.GetDataClassInfo(FailedSyncItemInfo.OBJECT_TYPE); - if (failedSyncItemClass is not null) - { - return; - } - - failedSyncItemClass = DataClassInfo.New(FailedSyncItemInfo.OBJECT_TYPE); + var info = DataClassInfoProvider.GetDataClassInfo(FailedSyncItemInfo.OBJECT_TYPE) ?? DataClassInfo.New(FailedSyncItemInfo.OBJECT_TYPE); - failedSyncItemClass.ClassName = FailedSyncItemInfo.TYPEINFO.ObjectClassName; - failedSyncItemClass.ClassTableName = FailedSyncItemInfo.TYPEINFO.ObjectClassName.Replace(".", "_"); - failedSyncItemClass.ClassDisplayName = "CRM Failed sync item"; - failedSyncItemClass.ClassResourceID = resourceInfo.ResourceID; - failedSyncItemClass.ClassType = ClassType.OTHER; + info.ClassName = FailedSyncItemInfo.TYPEINFO.ObjectClassName; + info.ClassTableName = FailedSyncItemInfo.TYPEINFO.ObjectClassName.Replace(".", "_"); + info.ClassDisplayName = "CRM Failed sync item"; + info.ClassResourceID = resourceInfo.ResourceID; + info.ClassType = ClassType.OTHER; var formInfo = FormHelper.GetBasicFormDefinition(nameof(FailedSyncItemInfo.FailedSyncItemID)); @@ -214,26 +205,23 @@ private void InstallFailedSyncItemClass(ResourceInfo resourceInfo) }; formInfo.AddFormItem(formItem); - failedSyncItemClass.ClassFormDefinition = formInfo.GetXmlDefinition(); + SetFormDefinition(info, formInfo); - DataClassInfoProvider.SetDataClassInfo(failedSyncItemClass); + if (info.HasChanged) + { + DataClassInfoProvider.SetDataClassInfo(info); + } } private void InstallCRMIntegrationSettingsClass(ResourceInfo resourceInfo) { - var settingsCRM = DataClassInfoProvider.GetDataClassInfo(CRMIntegrationSettingsInfo.OBJECT_TYPE); - if (settingsCRM is not null) - { - return; - } - - settingsCRM = DataClassInfo.New(CRMIntegrationSettingsInfo.OBJECT_TYPE); + var info = DataClassInfoProvider.GetDataClassInfo(CRMIntegrationSettingsInfo.OBJECT_TYPE) ?? DataClassInfo.New(CRMIntegrationSettingsInfo.OBJECT_TYPE); - settingsCRM.ClassName = CRMIntegrationSettingsInfo.TYPEINFO.ObjectClassName; - settingsCRM.ClassTableName = CRMIntegrationSettingsInfo.TYPEINFO.ObjectClassName.Replace(".", "_"); - settingsCRM.ClassDisplayName = "CRM integration settings"; - settingsCRM.ClassResourceID = resourceInfo.ResourceID; - settingsCRM.ClassType = ClassType.OTHER; + info.ClassName = CRMIntegrationSettingsInfo.TYPEINFO.ObjectClassName; + info.ClassTableName = CRMIntegrationSettingsInfo.TYPEINFO.ObjectClassName.Replace(".", "_"); + info.ClassDisplayName = "CRM integration settings"; + info.ClassResourceID = resourceInfo.ResourceID; + info.ClassType = ClassType.OTHER; var formInfo = FormHelper.GetBasicFormDefinition(nameof(CRMIntegrationSettingsInfo.CRMIntegrationSettingsItemID)); @@ -315,9 +303,30 @@ private void InstallCRMIntegrationSettingsClass(ResourceInfo resourceInfo) }; formInfo.AddFormItem(formItem); + SetFormDefinition(info, formInfo); - settingsCRM.ClassFormDefinition = formInfo.GetXmlDefinition(); + if (info.HasChanged) + { + DataClassInfoProvider.SetDataClassInfo(info); + } + } - DataClassInfoProvider.SetDataClassInfo(settingsCRM); + /// + /// Ensure that the form is not upserted with any existing form + /// + /// + /// + private static void SetFormDefinition(DataClassInfo info, FormInfo form) + { + if (info.ClassID > 0) + { + var existingForm = new FormInfo(info.ClassFormDefinition); + existingForm.CombineWithForm(form, new()); + info.ClassFormDefinition = existingForm.GetXmlDefinition(); + } + else + { + info.ClassFormDefinition = form.GetXmlDefinition(); + } } } \ No newline at end of file