yanyuan
2023-11-10 143ef74e2eeee697ac8fda3d9032a790fbb4e146
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
Quintiq file version 2.0
#parent: #root
StaticMethod HandleIncomingMessage (
  MPSync mpsync,
  NamedValueTree requestnvt,
  NamedValue requestNV,
  MessageIntegrationLog messagelog
)
{
  Description: 'Process incoming message to create, update or delete IO object'
  TextBody:
  [*
    syncidgenerator := mpsync.GetSyncIDGenerator();
    sourcemessage := MPSyncUtility::ID_Source_Message();
    
    // Pre-process data to handle MPSyncID type index duplicates, assign unique ID
    mpsyncidhandle := requestnvt.GetHandle( IOBase::ATTRIBUTENAME_MPSyncID() );
    body := LibInt_NvtAccessor::FirstChildNamed( requestnvt, requestNV, MPSyncUtility::NVT_NAME_BODY() );
    
    traverse( body.Children(), Elements, nvtobjects )
    {
      // to process particular object type only
      traverse( nvtobjects.Children(), Elements, nvtobject, nvtobject.Name() = typeof( IORouting ).ShortName() )
      {
        // Get latest MPSyncID and add to NVT
        nvtobject.AddChild( mpsyncidhandle, syncidgenerator.GetRoutingSyncID() );
        importedobject := NamedValueTreeIO::ImportObject( requestnvt, nvtobject, typeof( IORouting ) ).astype( IORouting );
        
        if( not isnull( importedobject ) )
        {
          // Find existing data
          mode   := importedobject.Mode();
          object := select( mpsync, IORouting, io,
                            io.DataSource() = sourcemessage 
                            and io.ID() = importedobject.ID()
                          );
          
          if( mode = MPSyncUtility::Message_Mode_InsertUpdate() )
          {
            if( isnull( object ) )
            {
              // Assign imported object as real instance
              importedobject.SetDataSource( MPSyncUtility::ID_Source_Message(), false, '' );
              mpsync.IORouting( relinsert, &importedobject );
            }
            else
            {
              // Not to update record if its old message
              if( importedobject.Timestamp() >= object.Timestamp() )
              {
                // Update data
                object.Update( importedobject, sourcemessage );
              }
              else
              {
                messagelog.AddWarning( Translations::MPSync_MessageBasedIntegration_IgnoreOldMessage( importedobject.DefinitionName(), 
                                                                                                      importedobject.GetFunctionalKeyValues(),
                                                                                                      importedobject.Timestamp(),
                                                                                                      object.Timestamp()
                                                                                                    ) );
              }
            }
          }
          // Delete data
          else if( mode = MPSyncUtility::Message_Mode_Delete() )
          {
            if( isnull( object ) )
            {
              messagelog.AddWarning( Translations::MPSync_MessageBasedIntegration_Delete_NotExists( importedobject.DefinitionName(), 
                                                                                                    importedobject.GetFunctionalKeyValues() ) 
                                                                                                  );
            }
            else
            {
              object.Delete();
            }
          }    
          else
          {
            messagelog.AddError( Translations::MPSync_MessageBasedIntegration_UnexpectedMode( importedobject.Mode(), 
                                                                                              importedobject.DefinitionName(), 
                                                                                              importedobject.GetFunctionalKeyValues() ) 
                                                                                            );
          }
        }
      }
    }
  *]
}