lazhen
2025-01-09 8afe90b633046db39042aada36b88193062f8cff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
Quintiq file version 2.0
#parent: #root
Method SyncMetadata (const LibDEF_DataRepository dataRepository_i, Boolean isDatasetConstructed_i)
{
  Description: "Synchronize from repository's channels, set types and sets."
  TextBody:
  [*
    // Jacky CHAN May-6-2016 (created)
    // 1. Initialization: mark SoftDeleted first
    traverse( this, SetTypeMeta, st )
    {
      traverse( st, SetMeta, s )
      {
        s.IsSoftDeleted( true );
      }
      st.IsSoftDeleted( true );
    }
    traverse( this, ChannelMeta, channel )
    {
      channel.IsSoftDeleted( true );
    }
    
    // 2. Synchronization
    traverse( dataRepository_i, Channel, channel )
    {
      channelMeta := LibDEF_ChannelMeta::CreateFromRepository( this, channel );
      channelMeta.IsSoftDeleted( false );
      channelMeta.SyncFromRepository( channel );
    
      traverse( channel, SetType, setType )
      {
        setTypeMeta := LibDEF_SetTypeMeta::CreateFromRepository( this, channelMeta, setType );
        setTypeMeta.IsSoftDeleted( false );
    
        traverse( setType, Set, set )
        {
          isNew   := false;
          setMeta := LibDEF_SetMeta::CreateFromRepository( setTypeMeta, set, isNew );
          setMeta.IsSoftDeleted( false );
    
          hasUpdate := setMeta.SyncFromRepository( set );
    
          // broadcast to other systems if new
          if( isNew )
          {
            this.SendMetadataRequestSetCreated( setMeta );
          }
          // Send SetUpdated if change in LastUpdated
          else if( hasUpdate )
          {
            setMeta.SendSetUpdated();
          }
        }
      }
    }
    
    // 3. Finalization: clean-up leftovers
    traverse( this, SetTypeMeta, st )
    {
      if( st.IsSoftDeleted() )
      {
        // Update other Systems about removal of SetType
        this.SendMetadataRequestSetTypeDeleted( st );
        st.Delete();
      }
      else
      {
        traverse( st, SetMeta, s, s.IsSoftDeleted() )
        {
          this.SendMetadataRequestSetDeleted( s );
          s.Delete();
        }
      }
    }
    traverse( this, ChannelMeta, channel, channel.IsSoftDeleted() )
    {
      channel.Delete();
    }
    
    // if the DataRepository just constructed (during wake-up), sync with the Domain too
    dataBroker := this.DataBroker();
    if( isDatasetConstructed_i and not dataBroker.IsInSyncWithDataRepository() )
    {
      // only connect when allowed, upon dataset constructed
      if( LibDEF_Util::GetSettingValueConnectOnStartup() )
      {
        dataBroker.RequestJoinDomainOrBroadcast();
        // also sync interests
        LibDEF_SetInterestOfDataset::SyncDatasetStatusExternally( dataBroker );
      }
      dataBroker.IsInSyncWithDataRepository( true );
    }
    
    LibDEF_Util::Log( "Metadata in DataBroker has been synchronized with Data in DataRepository." );
  *]
}