Skip to content

Commit afd01ec

Browse files
committed
added filenodes and xml node mapping logic
1 parent c08a684 commit afd01ec

File tree

6 files changed

+118
-44
lines changed

6 files changed

+118
-44
lines changed

ibm-mq-practice/mq-message-processor/src/main/java/com/smtb/mqmessageprocessor/dao/MetadataDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ public interface MetadataDao extends JpaRepository<MQMetadata, Integer > {
1818
@Query(value="select file_nodes from mq_metadta where record_typ = :recordType", nativeQuery = true)
1919
public List<List<String>> getFileNodes(@Param("recordType") String recordType);
2020

21+
@Query(value="select trgt_table from mq_metadta where record_typ = :recordType", nativeQuery = true)
22+
public List<String> getTargetTableName(@Param("recordType") String recordType);
2123
}

ibm-mq-practice/mq-message-processor/src/main/java/com/smtb/mqmessageprocessor/entities/Department.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
public class Department {
88
@Id
99
@Column(name = "dept_id")
10-
@GeneratedValue(strategy = GenerationType.IDENTITY)
1110
private int deptId;
1211

1312
@Column(name="dept_name")

ibm-mq-practice/mq-message-processor/src/main/java/com/smtb/mqmessageprocessor/entities/Employee.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
public class Employee {
77
@Id
88
@Column(name = "emp_id")
9-
@GeneratedValue(strategy = GenerationType.IDENTITY)
109
private int empId;
1110

1211
@Column(name="emp_first_name")

ibm-mq-practice/mq-message-processor/src/main/java/com/smtb/mqmessageprocessor/services/ProcessMessage.java

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public String convertFromSQLClobToString(Clob clob) throws SQLException, IOExcep
4949
return stringBuilder.toString();
5050
}
5151

52+
// jsonRsp from staging table to an MqStage object
5253
public MqStage loadXMLMessageFromDB(int mq_stg_id) {
5354
Optional<MqStage> stageRsp = mqStageDao.findById(mq_stg_id);
5455
List<MqStage> stageRspList = stageRsp.stream().toList();
@@ -57,69 +58,70 @@ public MqStage loadXMLMessageFromDB(int mq_stg_id) {
5758
return stageRspList.get(0);
5859
}
5960

61+
// jsonArray to list of map of xml rsp recprds
62+
public List<Map<String, Object>> jsonArrayTOListOfMap(JSONArray jsonArray, List<Map<String, Object>> list) {
63+
int totalRecords = jsonArray.toList().size(); // size of array of objects
64+
// traversing through the JSONArray and saving items as List<Map<String, Object>>
65+
for (int index = 0; index < totalRecords; index++) {
66+
Map<String, Object> recordsMap = new HashMap<>();
67+
JSONObject jsonObject = jsonArray.getJSONObject(index);
68+
for (String key : jsonObject.keySet()) {
69+
Object value = jsonObject.get(key);
70+
recordsMap.put(key, value.toString());
71+
}
72+
// storing records of type: Map<String, Object> in list
73+
list.add(recordsMap);
74+
}
75+
logger.info("Records list" + list);
76+
return list;
77+
}
78+
6079
public void parseXMLMessage(MqStage mqStgObj) throws SQLException, IOException {
6180
List<Map<String, Object>> listOfRecordMaps = new ArrayList<>();
6281
try {
6382
// convert the SQL CLOB to String
6483
String rspStr = convertFromSQLClobToString(mqStgObj.getFileResponse());
6584
// convert String to JSONObj
6685
JSONObject rspJSONObj = XML.toJSONObject(rspStr);
67-
logger.info("rspJSONObject: " + rspJSONObj);
6886
// fetching the response record type by parsing the JSONObj
6987
String rspRecordType = rspJSONObj.keySet().stream().toList().get(0); // "employees"
70-
logger.info("rspRecordType: " + rspRecordType);
7188
// metadata table contains below record types
7289
List<String> exisitingRecordTypes = metadataDao.findAllRecordTypes();
7390
// compare the resp record type with table record type and fetch details for specific record type
7491
if (exisitingRecordTypes.contains(rspRecordType)) {
7592
List<String> fileNodes = metadataDao.getFileNodes(rspRecordType).get(0); // ["emp-id","emp-first-name"]
76-
// replacing '-' by '_' to map with database table column fields
77-
fileNodes = fileNodes.stream().map(x-> x.replaceAll("-","_")).collect(Collectors.toList());
78-
logger.info("File nodes obtained: " + fileNodes);
93+
// replace square brackets and double quotes which we are there in metadata filenodes records
94+
fileNodes.replaceAll(e -> e.replaceAll("[\\[\\]\"\"]", ""));
7995
JSONObject tableJSONObj = rspJSONObj.getJSONObject(rspRecordType); // {"employee":[{},{},{}]}
80-
logger.info("Table json obj: " + tableJSONObj);
81-
JSONArray dataRecordsArrayObj = tableJSONObj.getJSONArray(tableJSONObj.keySet().stream().toList().get(0)); // "employee"
82-
logger.info("Data Records: " + dataRecordsArrayObj); // [{"name":"abc", "": ""},...]
83-
84-
int totalRecords = dataRecordsArrayObj.toList().size(); // size of array of objects
85-
// traversing through the JSONArray and saving items as List<Map<String, Object>>
86-
for (int index = 0; index < totalRecords; index++) {
87-
Map<String, Object> recordsMap = new HashMap<>();
88-
JSONObject jsonObject = dataRecordsArrayObj.getJSONObject(index);
89-
for (String key : jsonObject.keySet()) {
90-
Object value = jsonObject.get(key);
91-
recordsMap.put(key, value);
96+
String rspTrgtTableName = tableJSONObj.keySet().stream().toList().get(0); // "employee"
97+
JSONArray dataRecordsArrayObj = tableJSONObj.getJSONArray(rspTrgtTableName); // [{"emp-id":1001, "": ""},...]
98+
jsonArrayTOListOfMap(dataRecordsArrayObj, listOfRecordMaps); // [{emp-id=1001, = , ..},...]
99+
100+
String mainTargetTableName = metadataDao.getTargetTableName(rspRecordType).get(0);
101+
logger.info("mainTargetTableName: " + mainTargetTableName);
102+
103+
for (Map<String, Object> map : listOfRecordMaps) {
104+
String insertQuery = "";
105+
StringBuilder sb = new StringBuilder();
106+
sb.append("insert into ").append(mainTargetTableName).append("(");
107+
Set<String> fileNodesSet = new HashSet<>(fileNodes);
108+
if (map.keySet().equals(fileNodesSet)) {
109+
sb.append(map.keySet())
110+
.append(" ) values ( ")
111+
.append(map.values())
112+
.append(" )");
92113
}
93-
listOfRecordMaps.add(recordsMap);
114+
insertQuery = sb.toString().replaceAll("[\\[\\]]","");
115+
insertQuery = insertQuery.replaceAll("-","_");
116+
logger.info("Insert Query: " + insertQuery);
94117
}
95-
logger.info("Records list" + listOfRecordMaps);
96-
97-
// lets map the metadata file_nodes with actual resp file nodes
98-
for(Map<String, Object> map: listOfRecordMaps){
99-
int index = -1;
100-
Employee emp = null;
101-
List<String> finalFileNodes = fileNodes;
102-
logger.info("inside nested for");
103-
map.keySet().stream().map(o -> finalFileNodes.stream().map(k -> {
104-
105-
logger.info("inside nested stream map");
106-
if (!o.equalsIgnoreCase(k)) {
107-
throw new RuntimeException("Mapping of file nodes failed");
108-
}
109-
logger.info("map: " + map);
110-
return map;
111-
}));
112-
}
113-
114-
115118
}
116119
} catch (SQLException | IOException e) {
117120
throw new RuntimeException(e);
118121
}
119122
}
120123

121124

122-
123125
public String processXMLMessage(int mq_stg_id) throws SQLException, IOException {
124126
// load
125127
MqStage rsp = loadXMLMessageFromDB(mq_stg_id);

ibm-mq-practice/sql-scripts/all-queries.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ create table mq_metadta(
2121
);
2222

2323
create table department(
24-
dept_id int not null identity primary key,
24+
dept_id int not null primary key,
2525
dept_name varchar(30) not null
2626
);
2727

2828

2929
create table employee(
30-
emp_id int not null identity primary key,
30+
emp_id int not null primary key,
3131
emp_first_name varchar(30) not null,
3232
emp_last_name varchar(30) not null,
3333
dept_id int foreign key references department(dept_id)

ibm-mq-practice/sql-scripts/metadata-insert-queries.sql

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,76 @@ insert into mq_metadta(file_nm,file_nodes,trgt_table, insert_tms, record_typ)
22
values ('XML', '["emp-id","emp-first-name","emp-last-name","dept-id"]', 'employee', (select CURRENT_TIMESTAMP), 'employees');
33

44
insert into mq_metadta(file_nm,file_nodes,trgt_table, insert_tms, record_typ)
5-
values ('XML', '["dept-id","dept-name"]', 'department', (select CURRENT_TIMESTAMP), 'departments');
5+
values ('XML', '["dept-id","dept-name"]', 'department', (select CURRENT_TIMESTAMP), 'departments');
6+
7+
8+
9+
10+
11+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ JSON DATA PARSING FLOW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
13+
rspJSONObject: {"employees":
14+
{"employee":
15+
[
16+
{"emp-first-name":"John","emp-last-name":"Doe","dept-id":1,"emp-id":1001},
17+
{"emp-first-name":"Matt","emp-last-name":"Pattinson","dept-id":2,"emp-id":1002},
18+
{"emp-first-name":"Emily","emp-last-name":"Jones","dept-id":3,"emp-id":1003},
19+
{"emp-first-name":"Jack","emp-last-name":"Wilson","dept-id":4,"emp-id":1004},
20+
{"emp-first-name":"Kyle","emp-last-name":"Hardy","dept-id":2,"emp-id":1005},
21+
{"emp-first-name":"Matthew","emp-last-name":"Potts","dept-id":3,"emp-id":1006},
22+
{"emp-first-name":"Ryan","emp-last-name":"Adams","dept-id":4,"emp-id":1007}
23+
]
24+
}
25+
}
26+
27+
28+
metadata record_typ = ["employees","department"]
29+
30+
String rspRecordType = rspJSONObj.keySet().stream().toList().get(0); // "employees"
31+
32+
if(rspRecordType exists in record_typ){
33+
// fetch file nodes from metadata table
34+
List<String> fileNodes = metadataDao.getFileNodes(rspRecordType).get(0);
35+
JSONObject tableJSONObj = rspJSONObj.getJSONObject(rspRecordType); // JSONObject => {"employee":[{},{},{}]}
36+
String rspTrgtTableName = tableJSONObj.keySet().stream().toList().get(0); // String => "employee"
37+
JSONArray dataRecordsArrayObj = tableJSONObj.getJSONArray(rspTrgtTableName);// jsonArrray => [{"emp-id":"1", "": ""},...]
38+
39+
// converting jsonArrray to List<Map<String, Object>>
40+
int totalRecords = dataRecordsArrayObj.toList().size(); // size of array of objects
41+
// traversing through the JSONArray and saving items as List<Map<String, Object>>
42+
for (int index = 0; index < totalRecords; index++) {
43+
Map<String, Object> recordsMap = new HashMap<>();
44+
JSONObject jsonObject = dataRecordsArrayObj.getJSONObject(index);
45+
for (String key : jsonObject.keySet()) {
46+
Object value = jsonObject.get(key);
47+
recordsMap.put(key, value);
48+
}
49+
// storing records of type: Map<String, Object> in list
50+
listOfRecordMaps.add(recordsMap);
51+
}
52+
}
53+
54+
========================================================================================================================
55+
Data Records: [
56+
{"emp-first-name":"John","emp-last-name":"Doe","dept-id":1,"emp-id":1001},
57+
{"emp-first-name":"Matt","emp-last-name":"Pattinson","dept-id":2,"emp-id":1002},
58+
{"emp-first-name":"Emily","emp-last-name":"Jones","dept-id":3,"emp-id":1003},
59+
{"emp-first-name":"Jack","emp-last-name":"Wilson","dept-id":4,"emp-id":1004},
60+
{"emp-first-name":"Kyle","emp-last-name":"Hardy","dept-id":2,"emp-id":1005},
61+
{"emp-first-name":"Matthew","emp-last-name":"Potts","dept-id":3,"emp-id":1006},
62+
{"emp-first-name":"Ryan","emp-last-name":"Adams","dept-id":4,"emp-id":1007}
63+
]
64+
65+
Records list[
66+
{emp-first-name=John, emp-last-name=Doe, dept-id=1, emp-id=1001},
67+
{emp-first-name=Matt, emp-last-name=Pattinson, dept-id=2, emp-id=1002},
68+
{emp-first-name=Emily, emp-last-name=Jones, dept-id=3, emp-id=1003},
69+
{emp-first-name=Jack, emp-last-name=Wilson, dept-id=4, emp-id=1004},
70+
{emp-first-name=Kyle, emp-last-name=Hardy, dept-id=2, emp-id=1005},
71+
{emp-first-name=Matthew, emp-last-name=Potts, dept-id=3, emp-id=1006},
72+
{emp-first-name=Ryan, emp-last-name=Adams, dept-id=4, emp-id=1007}
73+
]
74+
75+
76+
File nodes obtained from Metadata table:
77+
[["emp-id", "emp-first-name", "emp-last-name", "dept-id"]]

0 commit comments

Comments
 (0)