Save and Load Diff to Record
Updated: Feb 22, 2024
This example shows how to save and load Diff
s to and from Record
. This feature facilitates the saving and sharing of changes between EntityDefs, EntityRelationshipDefs, FieldDefs, IndexDefs, IndexFieldDefs, SchemaDefs and Records.
Java Code Listing
package com.northconcepts.datapipeline.foundations.examples.difference; import java.math.BigDecimal; import java.time.LocalDate; import com.northconcepts.datapipeline.core.Record; import com.northconcepts.datapipeline.foundations.difference.RecordDiff; public class SaveDiffToAndFromRecord { public static void main(String[] args) { Record oldRecord = new Record() .setField("name", "John Doe") .setField("dob", LocalDate.parse("2000-01-01")) .setField("languages", new String[] {"English", "French"}) .setField("height", 1.70) .setField("netIncome", new BigDecimal(100_286.99)); Record newRecord = new Record() .setField("name", "John Doe") .setField("age", 24) .setField("languages", new String[] {"English", "French", "Spanish"}) .setField("height", 1.73) .setField("netIncome", new BigDecimal(120_286.99)); RecordDiff oldDiff = RecordDiff.diff("userRecord", oldRecord, newRecord, "height", "netIncome"); System.out.println("old diff: " + oldDiff); System.out.println("==============================================================="); Record diffRecord = oldDiff.toRecord(); System.out.println(diffRecord); System.out.println("==============================================================="); RecordDiff newDiff = new RecordDiff().fromRecord(diffRecord); System.out.println("new diff: " + newDiff); } }
Code Walkthrough
- An
oldRecord
is created and initialized with the following fields:name
,dob
,languages
,height
, andnetIncome
. - A
newRecord
is created and initialized with the following fields:name
,age
,languages
,height
, andnetIncome
. - RecordDiff instance is created by calling
diff
() method with the following arguments: name of the diff, the old record, the new record, and a list of field names to exclude from the comparison. - We save the Diff as a Record on line 37 and then use this Record after instantiating a new Diff using the
fromRecord()
method on line 42.
Console Output
old diff: {
"children" : [ {
"name" : "name",
"type" : "NONE"
}, {
"name" : "dob",
"type" : "REMOVED"
}, {
"name" : "English",
"type" : "NONE"
}, {
"name" : "French",
"type" : "NONE"
}, {
"name" : "Spanish",
"type" : "ADDED"
}, {
"name" : "age",
"type" : "ADDED"
} ],
"name" : "userRecord",
"type" : "CHANGED"
}
===============================================================
Record (MODIFIED) (has child records) {
0:[__class__]:STRING=[com.northconcepts.datapipeline.foundations.difference.RecordDiff]:String
1:[name]:STRING=[userRecord]:String
2:[type]:UNDEFINED=[CHANGED]:DiffType
3:[oldValue]:RECORD=[
Record (MODIFIED) (is child record) {
0:[name]:STRING=[John Doe]:String
1:[dob]:UNDEFINED=[2000-01-01]:com.amazonaws.thirdparty.joda.time.LocalDate
2:[languages]:ARRAY of STRING=[[English, French]]:ArrayValue
3:[height]:DOUBLE=[1.7]:Double
4:[netIncome]:BIG_DECIMAL=[100286.990000000005238689482212066650390625]:BigDecimal
}]:Record
4:[newValue]:RECORD=[
Record (MODIFIED) (is child record) {
0:[name]:STRING=[John Doe]:String
1:[age]:INT=[24]:Integer
2:[languages]:ARRAY of STRING=[[English, French, Spanish]]:ArrayValue
3:[height]:DOUBLE=[1.73]:Double
4:[netIncome]:BIG_DECIMAL=[120286.990000000005238689482212066650390625]:BigDecimal
}]:Record
5:[children]:ARRAY of RECORD=[[
Record (MODIFIED) (is child record) {
0:[__class__]:STRING=[com.northconcepts.datapipeline.foundations.difference.PropertyDiff]:String
1:[name]:STRING=[name]:String
2:[type]:UNDEFINED=[NONE]:DiffType
3:[oldValue]:STRING=[John Doe]:String
4:[newValue]:STRING=[John Doe]:String
5:[children]:ARRAY of UNDEFINED=[[]]:ArrayValue
},
Record (MODIFIED) (is child record) {
0:[__class__]:STRING=[com.northconcepts.datapipeline.foundations.difference.PropertyDiff]:String
1:[name]:STRING=[dob]:String
2:[type]:UNDEFINED=[REMOVED]:DiffType
3:[oldValue]:UNDEFINED=[null]
4:[newValue]:UNDEFINED=[null]
5:[children]:ARRAY of UNDEFINED=[[]]:ArrayValue
},
Record (MODIFIED) (is child record) {
0:[__class__]:STRING=[com.northconcepts.datapipeline.foundations.difference.PropertyDiff]:String
1:[name]:STRING=[English]:String
2:[type]:UNDEFINED=[NONE]:DiffType
3:[oldValue]:STRING=[English]:String
4:[newValue]:STRING=[English]:String
5:[children]:ARRAY of UNDEFINED=[[]]:ArrayValue
},
Record (MODIFIED) (is child record) {
0:[__class__]:STRING=[com.northconcepts.datapipeline.foundations.difference.PropertyDiff]:String
1:[name]:STRING=[French]:String
2:[type]:UNDEFINED=[NONE]:DiffType
3:[oldValue]:STRING=[French]:String
4:[newValue]:STRING=[French]:String
5:[children]:ARRAY of UNDEFINED=[[]]:ArrayValue
},
Record (MODIFIED) (is child record) {
0:[__class__]:STRING=[com.northconcepts.datapipeline.foundations.difference.PropertyDiff]:String
1:[name]:STRING=[Spanish]:String
2:[type]:UNDEFINED=[ADDED]:DiffType
3:[oldValue]:UNDEFINED=[null]
4:[newValue]:STRING=[Spanish]:String
5:[children]:ARRAY of UNDEFINED=[[]]:ArrayValue
},
Record (MODIFIED) (is child record) {
0:[__class__]:STRING=[com.northconcepts.datapipeline.foundations.difference.PropertyDiff]:String
1:[name]:STRING=[age]:String
2:[type]:UNDEFINED=[ADDED]:DiffType
3:[oldValue]:UNDEFINED=[null]
4:[newValue]:INT=[24]:Integer
5:[children]:ARRAY of UNDEFINED=[[]]:ArrayValue
}]]:ArrayValue
}
===============================================================
new diff: {
"children" : [ {
"name" : "name",
"type" : "NONE"
}, {
"name" : "dob",
"type" : "REMOVED"
}, {
"name" : "English",
"type" : "NONE"
}, {
"name" : "French",
"type" : "NONE"
}, {
"name" : "Spanish",
"type" : "ADDED"
}, {
"name" : "age",
"type" : "ADDED"
} ],
"name" : "userRecord",
"type" : "CHANGED"
}