Data writing procedure

 


Let us store the procedures that write and read exchange data in the object module of the exchange plan.

  1. In the editor of the Branches exchange plan configuration object, click the Other tab and then click Object module.

    Let us create the WriteMessageWithChanges procedure. This will take several steps.

    First, let us implement the name generation for the file that will store the exchange data, and user notifications for the beginning and end of data export.
  2. Add the script shown in listing 24.11 to the module.

    Listing 24.11. Generating a file name in the data writing procedure

    Procedure WriteMessageWithChanges() Export
        
        Message = New UserMessage;
        Message.Text = "-------- Starting export to node: " + String(ThisObject) + " --------";
        Message.Message();
        Directory = TempFilesDir();
     
        // Generating temporary file name
        FileName = Directory + ?(Right(Directory,1)= "\", "", "\") + "Message"
            + TrimAll(ExchangePlans.Branches.ThisNode().Code) + "_" + TrimAll(Ref.Code)
            + ".xml";
        Message = New UserMessage;
        Message.Text = "-------- Export completed --------";
        Message.Message();
     
    EndProcedure
    To simplify the example, let us exchange messages through a temporary files directory. The message names are standardized and follow the pattern: MessageSourceNodeCode_TargetNodeCode.xml.

    Now let us use the XML document read/write feature. The next procedure part creates an XMLWriter object. Then it uses the created object for opening a new XML file for writing and writes an XML declaration to this file. Finally, it closes the file.
  3. Update the procedure as shown in listing 24.12.

    Listing 24.12. Creating an XMLWriter object in the data writing procedure

    Procedure WriteMessageWithChanges() Export
        
        Message = New UserMessage;
        Message.Text = "-------- Starting export to node: " + String(ThisObject) + " --------";
        Message.Message();
        Directory = TempFilesDir();
    
        // Generating temporary file name
        FileName = Directory + ?(Right(Directory,1)= "\", "", "\") + "Message"
            + TrimAll(ExchangePlans.Branches.ThisNode().Code) + "_" + TrimAll(Ref.Code)
            + ".xml";
     
        // Creating XMLWriter object
        // *** Writing XML documents
        XMLWriter = New XMLWriter;
        XMLWriter.OpenFile(FileName);
        XMLWriter.WriteXMLDeclaration();
        XMLWriter.Close(); 
     
        Message = New UserMessage;
        Message.Text = "-------- Export completed --------";
        Message.Message();
     
    EndProcedure
    Now let us implement the message infrastructure. The next procedure part creates an ExchangeMessageWriter object. Its BeginWrite() method creates the next message number and writes the message header to the XML file. Finally, the procedure ends the message writing.
  4. Update the procedure as shown in listing 24.13.

    Listing 24.13. Creating the message sequential number and writing the message header to the XML file

    Procedure WriteMessageWithChanges() Export
        
        Message = New UserMessage;
        Message.Text = "-------- Starting export to node: " + String(ThisObject) + " --------";
        Message.Message();
        Directory = TempFilesDir();
     
        // Generating temporary file name
        FileName = Directory + ?(Right(Directory,1)= "\", "", "\") + "Message"
            + TrimAll(ExchangePlans.Branches.ThisNode().Code) + "_" + TrimAll(Ref.Code)
            + xml";
     
        // Creating XMLWriter object
        // *** Writing XML documents
        XMLWriter = New XMLWriter;
        XMLWriter.OpenFile(FileName);
        XMLWriter.WriteXMLDeclaration();
        
        // *** Message infrastructure
        ExchangeMessageWriter = ExchangePlans.CreateMessageWriter();
        ExchangeMessageWriter.BeginWrite(XMLWriter, Ref);
        Message = New UserMessage;
        Message.Text = "Message number: " + ExchangeMessageWriter.MessageNo;
        Message.Message();
        ExchangeMessageWriter.EndWrite(); 
     
        XMLWriter.Close();
     
        Message = New UserMessage;
        Message.Text = "-------- Export completed --------";
        Message.Message();
     
    EndProcedure
    Since the procedure is located in the object module, you can use the standard Ref attribute as a reference to the Branches exchange plan object.

    In order to get the data for storing to the file, let us use the change registration feature. The next procedure part gets a selection of change registration records intended for this node. The method that generates the selection accepts the message number as its second parameter.
  5. Update the procedure as shown in listing 24.14.

    Listing 24.14. Getting a selection of change registration records intended for this node

    Procedure WriteMessageWithChanges() Export
        
        Message = New UserMessage;
        Message.Text = "-------- Starting export to node: " + String(ThisObject) + " --------";
        Message.Message();
        Directory = TempFilesDir();
     
        // Generating temporary file name
        FileName = Directory + ?(Right(Directory,1)= "\", "", "\") + "Message"
            + TrimAll(ExchangePlans.Branches.ThisNode().Code) + "_" + TrimAll(Ref.Code)
            + ".xml";
     
        // Creating XMLWriter object
        // *** Writing XML documents
        XMLWriter = New XMLWriter;
        XMLWriter.OpenFile(FileName);
        XMLWriter.WriteXMLDeclaration();
        
        // *** Message infrastructure
        ExchangeMessageWriter = ExchangePlans.CreateMessageWriter();
        ExchangeMessageWriter.BeginWrite(XMLWriter, Ref);
        Message = New UserMessage;
        Message.Text = "Message number: " + ExchangeMessageWriter.MessageNo;
        Message.Message();
     
        // Getting the selection of changed data
        // *** Change registration
        SelectionOfChanges = ExchangePlans.SelectChanges(ExchangeMessageWriter.Recipient,
            ExchangeMessageWriter.MessageNo);
     
        ExchangeMessageWriter.EndWrite(); 
        XMLWriter.Close();
     
        Message = New UserMessage;
        Message.Text = "-------- Export completed --------";
        Message.Message();
     
    EndProcedure
    All that is left to do is adding a loop for selecting records and serializing them to the XML file that is open.
  6. Update the procedure as shown in listing 24.15.

    Listing 24.15. Iterating through the record selection and serializing records to the XML file that is open

    Procedure WriteMessageWithChanges() Export
        
        Message = New UserMessage;
        Message.Text = "-------- Starting export to node: " + String(ThisObject) + " --------";
        Message.Message();
        Directory = TempFilesDir();
     
        // Generating temporary file name
        FileName = Directory + ?(Right(Directory,1)= "\", "", "\") + "Message"
            + TrimAll(ExchangePlans.Branches.ThisNode().Code) + "_" + TrimAll(Ref.Code)
            + ".xml";
     
        // Creating XMLWriter object
        // *** Writing XML documents
        XMLWriter = New XMLWriter;
        XMLWriter.OpenFile(FileName);
        XMLWriter.WriteXMLDeclaration();
        
        // *** Message infrastructure
        ExchangeMessageWriter = ExchangePlans.CreateMessageWriter();
        ExchangeMessageWriter.BeginWrite(XMLWriter, Ref);
        Message = New UserMessage;
        Message.Text = "Message number: " + ExchangeMessageWriter.MessageNo;
        Message.Message();
     
        // Getting the selection of changed data
        // *** Change registration
        SelectionOfChanges = ExchangePlans.SelectChanges(ExchangeMessageWriter.Recipient,
            ExchangeMessageWriter.MessageNo);
     
        While SelectionOfChanges.Next() Do
            // Writing data to the message
            // ***XML serialization
            WriteXML(XMLWriter, SelectionOfChanges.Get());
        EndDo;
     
        ExchangeMessageWriter.EndWrite(); 
        XMLWriter.Close();
     
        Message = New UserMessage;
        Message.Text = "-------- Export completed --------";
        Message.Message();
     
    EndProcedure
    This completes the procedure for writing exchange data.

Comments
0
Add comment