1616using Migration . Tool . Common . Abstractions ;
1717using Migration . Tool . Common . Builders ;
1818using Migration . Tool . Common . Helpers ;
19+ using Migration . Tool . Common . Model ;
1920using Migration . Tool . KXP . Api ;
2021using Migration . Tool . Source . Mappers ;
2122using Migration . Tool . Source . Model ;
2223using Migration . Tool . Source . Providers ;
2324using Migration . Tool . Source . Services ;
2425
2526using Newtonsoft . Json ;
27+ using Newtonsoft . Json . Linq ;
2628
2729namespace Migration . Tool . Source . Handlers ;
2830
@@ -34,7 +36,8 @@ public class MigrateCategoriesCommandHandler(
3436 IUmtMapper < TagModelSource > tagModelMapper ,
3537 SpoiledGuidContext spoiledGuidContext ,
3638 KxpClassFacade kxpClassFacade ,
37- ClassMappingProvider classMappingProvider
39+ ClassMappingProvider classMappingProvider ,
40+ DeferredTreeNodesService deferredTreeNodesService
3841) : IRequestHandler < MigrateCategoriesCommand , CommandResult >
3942{
4043 public async Task < CommandResult > Handle ( MigrateCategoriesCommand request , CancellationToken cancellationToken )
@@ -69,6 +72,8 @@ FROM View_CMS_Tree_Joined [TJ]
6972 var skippedClasses = new List < int > ( ) ;
7073 var schemaGuid = Guid . Empty ;
7174 string categoryFieldName = "Category_Legacy" ;
75+ string widgetCategoryFieldName = "Category" ;
76+
7277 foreach ( var classWithCategoryUsage in classesWithCategories )
7378 {
7479 var targetDataClass = DataClassInfoProvider . ProviderObject . Get ( classWithCategoryUsage . ClassGuid ) ;
@@ -171,6 +176,42 @@ FROM View_CMS_Tree_Joined [TJ]
171176 }
172177 }
173178
179+ if ( deferredTreeNodesService . WidgetizedDocuments . TryGetValue ( dwc . DocumentGUID , out var widget ) )
180+ {
181+ var widgetHostPageCommonData = ContentItemCommonDataInfo . Provider . Get ( )
182+ . WhereEquals ( nameof ( ContentItemCommonDataInfo . ContentItemCommonDataGUID ) , widget . ContentItemCommonDataGuid )
183+ . FirstOrDefault ( ) ;
184+
185+ if ( widgetHostPageCommonData . ContentItemCommonDataVisualBuilderWidgets is not null )
186+ {
187+ var visualBuilderWidgets = JsonConvert . DeserializeObject < EditableAreasConfiguration > ( widgetHostPageCommonData . ContentItemCommonDataVisualBuilderWidgets ) ;
188+ foreach ( var area in visualBuilderWidgets . EditableAreas )
189+ {
190+ foreach ( var section in area . Sections )
191+ {
192+ foreach ( var zone in section . Zones )
193+ {
194+ foreach ( var w in zone . Widgets )
195+ {
196+ foreach ( var variant in w . Variants )
197+ {
198+ if ( variant . Identifier . Equals ( widget . WidgetVariantGuid ) )
199+ {
200+ var array = variant . Properties . ContainsKey ( widgetCategoryFieldName )
201+ ? variant . Properties . Value < JArray > ( widgetCategoryFieldName )
202+ : [ ] ;
203+ array . Add ( new JObject { { "identifier" , tag . TagGUID . ToString ( ) } } ) ;
204+ variant . Properties [ widgetCategoryFieldName ] = array ;
205+ }
206+ }
207+ }
208+ }
209+ }
210+ }
211+ widgetHostPageCommonData . ContentItemCommonDataVisualBuilderWidgets = JsonConvert . SerializeObject ( visualBuilderWidgets ) ;
212+ ContentItemCommonDataInfo . Provider . Set ( widgetHostPageCommonData ) ;
213+ }
214+ }
174215 var commonData = ContentItemCommonDataInfo . Provider . Get ( )
175216 . WhereEquals ( nameof ( ContentItemCommonDataInfo . ContentItemCommonDataGUID ) , dwc . DocumentGUID )
176217 . FirstOrDefault ( ) ;
@@ -187,15 +228,13 @@ FROM View_CMS_Tree_Joined [TJ]
187228
188229 foreach ( var infoWithTag in infosWithTag )
189230 {
190- List < TagReference > tagReferences = [ ] ;
231+ HashSet < Guid > tagReferences = [ ] ;
191232 if ( infoWithTag [ categoryFieldName ] is string jsonTags )
192233 {
193- tagReferences = JsonConvert . DeserializeObject < List < TagReference > > ( jsonTags ) ?? [ ] ;
234+ tagReferences = new HashSet < Guid > ( JsonConvert . DeserializeObject < List < TagReference > > ( jsonTags ) ? . Select ( x => x . Identifier ) ?? [ ] ) ;
194235 }
195-
196- tagReferences . Add ( new TagReference { Identifier = tag . TagGUID } ) ;
197-
198- infoWithTag [ categoryFieldName ] = JsonConvert . SerializeObject ( tagReferences ) ;
236+ tagReferences . Add ( tag . TagGUID ) ;
237+ infoWithTag [ categoryFieldName ] = JsonConvert . SerializeObject ( tagReferences . Select ( x => new TagReference { Identifier = x } ) . ToList ( ) ) ;
199238
200239 ContentItemCommonDataInfo . Provider . Set ( infoWithTag ) ;
201240 }
0 commit comments