@@ -100,6 +100,25 @@ public static TopicData Export(this Topic topic, [NotNull]ExportOptions? options
100
100
) ;
101
101
}
102
102
103
+ /*------------------------------------------------------------------------------------------------------------------------
104
+ | Set topic references
105
+ \-----------------------------------------------------------------------------------------------------------------------*/
106
+ foreach ( var reference in topic . References ) {
107
+ if (
108
+ ! options . IncludeExternalReferences &&
109
+ ! ( reference . Value ? . GetUniqueKey ( ) . StartsWith ( options . ExportScope , StringComparison . InvariantCultureIgnoreCase ) ?? true )
110
+ ) {
111
+ continue ;
112
+ }
113
+ topicData . References . Add (
114
+ new ( ) {
115
+ Key = reference . Key ,
116
+ Value = reference . Value ? . GetUniqueKey ( ) ,
117
+ LastModified = reference . LastModified
118
+ }
119
+ ) ;
120
+ }
121
+
103
122
/*------------------------------------------------------------------------------------------------------------------------
104
123
| Set relationships
105
124
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -165,24 +184,25 @@ public static void Import(this Topic topic, TopicData topicData, [NotNull]Import
165
184
/*------------------------------------------------------------------------------------------------------------------------
166
185
| Establish cache
167
186
\-----------------------------------------------------------------------------------------------------------------------*/
168
- var unresolvedRelationships = new List < Tuple < Topic , string , string > > ( ) ;
187
+ var unresolvedAssociations = new List < Tuple < Topic , bool , string , string > > ( ) ;
169
188
170
189
/*------------------------------------------------------------------------------------------------------------------------
171
190
| Handle first pass
172
191
\-----------------------------------------------------------------------------------------------------------------------*/
173
- topic . Import ( topicData , options , unresolvedRelationships ) ;
192
+ topic . Import ( topicData , options , unresolvedAssociations ) ;
174
193
175
194
/*------------------------------------------------------------------------------------------------------------------------
176
- | Attempt to resolve outstanding relationships
195
+ | Attempt to resolve outstanding assocations
177
196
\-----------------------------------------------------------------------------------------------------------------------*/
178
- foreach ( var relationship in unresolvedRelationships ) {
197
+ foreach ( var relationship in unresolvedAssociations ) {
179
198
180
- //Attempt to find the target relationship
199
+ //Attempt to find the target association
181
200
var source = relationship . Item1 ;
182
- var target = topic . GetByUniqueKey ( relationship . Item3 ) ;
183
- var key = relationship . Item2 ;
201
+ var isRelationship = relationship . Item2 ;
202
+ var key = relationship . Item3 ;
203
+ var target = topic . GetByUniqueKey ( relationship . Item4 ) ;
184
204
185
- //If the relationship STILL can't be resolved, skip it
205
+ //If the association STILL can't be resolved, skip it
186
206
if ( target is null ) {
187
207
continue ;
188
208
}
@@ -193,10 +213,15 @@ public static void Import(this Topic topic, TopicData topicData, [NotNull]Import
193
213
}
194
214
195
215
//Wire up relationships
196
- else {
216
+ else if ( isRelationship ) {
197
217
source . Relationships . SetValue ( key , target ) ;
198
218
}
199
219
220
+ //Wire up topic references
221
+ else {
222
+ source . References . SetValue ( key , target ) ;
223
+ }
224
+
200
225
}
201
226
202
227
}
@@ -219,13 +244,15 @@ public static void Import(this Topic topic, TopicData topicData, [NotNull]Import
219
244
/// this via <see cref="Import"/> directly.
220
245
/// </para>
221
246
/// </remarks>
222
- /// <param name="topic">The source <see cref="Topic"/> to operate off of.</param>
247
+ /// <param name="topic">The target <see cref="Topic"/> to write data to.</param>
248
+ /// <param name="topicData">The source <see cref="TopicData"/> to import data from.</param>
223
249
/// <param name="options">An optional <see cref="ImportOptions"/> object to specify import settings.</param>
250
+ /// <param name="unresolvedAssociations">A list of associations that could not be resolved on the first </param>
224
251
private static void Import (
225
252
this Topic topic ,
226
253
TopicData topicData ,
227
254
[ NotNull ] ImportOptions ? options ,
228
- List < Tuple < Topic , string , string > > unresolvedRelationships
255
+ List < Tuple < Topic , bool , string , string > > unresolvedAssociations
229
256
) {
230
257
231
258
/*------------------------------------------------------------------------------------------------------------------------
@@ -266,7 +293,7 @@ List<Tuple<Topic, string, string>> unresolvedRelationships
266
293
topic . BaseTopic = target ;
267
294
}
268
295
else {
269
- unresolvedRelationships . Add ( new ( topic , "DerivedTopic" , topicData . BaseTopicKey ) ) ;
296
+ unresolvedAssociations . Add ( new ( topic , false , "DerivedTopic" , topicData . BaseTopicKey ) ) ;
270
297
}
271
298
}
272
299
@@ -349,11 +376,44 @@ List<Tuple<Topic, string, string>> unresolvedRelationships
349
376
topic . Relationships . SetValue ( relationship . Key , relatedTopic ) ;
350
377
}
351
378
else {
352
- unresolvedRelationships . Add ( new ( topic , relationship . Key ! , relatedTopicKey ) ) ;
379
+ unresolvedAssociations . Add ( new ( topic , true , relationship . Key ! , relatedTopicKey ) ) ;
353
380
}
354
381
}
355
382
}
356
383
384
+
385
+ /*------------------------------------------------------------------------------------------------------------------------
386
+ | Set topic references
387
+ \-----------------------------------------------------------------------------------------------------------------------*/
388
+
389
+ //First delete any unmatched records, if appropriate
390
+ if ( options . DeleteUnmatchedReferences ) {
391
+ var unmatchedReferences = topic . References . Where ( a1 =>
392
+ ! topicData . Attributes . Any ( a2 => a1 . Key == a2 . Key )
393
+ ) ;
394
+ foreach ( var reference in unmatchedReferences . ToArray ( ) ) {
395
+ topic . References . Remove ( reference ) ;
396
+ } ;
397
+ }
398
+
399
+ //Update records based on the source collection
400
+ foreach ( var reference in topicData . References ) {
401
+ if ( useCustomMergeRules ( reference ) ) continue ;
402
+ var matchedReference = topic . References . FirstOrDefault ( a => a . Key == reference . Key ) ;
403
+ if ( matchedReference is not null && isStrategy ( ImportStrategy . Add ) ) continue ;
404
+ if ( matchedReference ? . LastModified >= reference . LastModified && isStrategy ( ImportStrategy . Merge ) ) continue ;
405
+ var referencedTopic = topic . GetByUniqueKey ( reference . Key ) ;
406
+ if ( reference . Value is null || referencedTopic != null ) {
407
+ topic . References . SetValue (
408
+ reference . Key ,
409
+ referencedTopic
410
+ ) ;
411
+ }
412
+ else {
413
+ unresolvedAssociations . Add ( new ( topic , false , reference . Key , reference . Value ) ) ;
414
+ }
415
+ }
416
+
357
417
/*------------------------------------------------------------------------------------------------------------------------
358
418
| Recurse over children
359
419
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -376,7 +436,7 @@ topic.ContentType is not "List" && options.DeleteUnmatchedChildren
376
436
if ( childTopic is null ) {
377
437
childTopic = TopicFactory . Create ( childTopicData . Key , childTopicData . ContentType , topic ) ;
378
438
}
379
- childTopic . Import ( childTopicData , options , unresolvedRelationships ) ;
439
+ childTopic . Import ( childTopicData , options , unresolvedAssociations ) ;
380
440
}
381
441
382
442
/*------------------------------------------------------------------------------------------------------------------------
0 commit comments