Browse Source

fixed parse graph wfs response

Piotr Labudda 6 years ago
parent
commit
0f57f01cf7
1 changed files with 210 additions and 1 deletions
  1. 210 1
      tools/Bocian.php.graphShowHide.js

+ 210 - 1
tools/Bocian.php.graphShowHide.js

@@ -476,7 +476,216 @@ function graphRaportRender(props, wrapNode) {
 	})
 }
 
-function parseResponseRec(_todoGraphData, json, typeName, parentNodeId, level) {
+/**
+ * Response format:
+ * NOTE: every object could be xlink if found before
+ * lvl1      > lvl2         > lvl3  > lvl4         > lvl5
+ * Pracownik > *_row_object > *_row > *_row_object > Podmiot
+ * Pracownik > *_row_object > *_row > *_row_object > ( KRS | CEIDG | ... )
+ * Pracownik > *_row_object > *_row > *_row_object > Pracownik
+ *
+ * Podmiot > *_row_object > *_row > *_row_object > Pracownik
+ * Podmiot > *_row_object > *_row > *_row_object > ( KRS | CEIDG | ... )
+ * Podmiot > *_row_object > *_row > *_row_object > Podmiot
+ *
+ * TODO: make connections only: Pracownik -> ( KRS | CEIDG | ... )* -> Podmiot
+ * NOTE: lvl4 last `_row_object` is (wfs xlink cache system) xlink to lvl2 `_row_object` - ref to lvl1 object
+ * NOTE: lvl3 is always 1 because it is from __backRef
+ */
+var TN_PRACOWNICY = 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY';
+var TN_KONTRAHENCI = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI';
+var TN_ROW_OBJ = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object';
+var TN_ROW = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row';
+var TN_KRS = 'default_db__x3A__BI_audit_KRS:BI_audit_KRS';
+var TN_CEIDG = 'default_db__x3A__BI_audit_CEIDG:BI_audit_CEIDG';
+var TN_MSIG = 'default_db__x3A__BI_audit_MSIG:BI_audit_MSIG';
+function parseResponseRec__Helper__addTodoNode(_todoGraphData, level, json, typeName) { // @return string - node id
+	if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
+	var nodeObject = dataMakeNode({
+		typeName: typeName,
+		primaryKey: (json['ID']) ? json['ID'] : null, // TODO: get primaryKey from object
+		row: json,
+	})
+	_todoGraphData[level].nodes.push(nodeObject)
+	// if (parentNodeId) {
+	// 	_todoGraphData[level].edges.push(dataMakeEdge(parentNodeId, nodeObject))
+	// }
+	return nodeObject.id;
+}
+function parseResponseRec__Helper__addTodoEdge(_todoGraphData, level, parentNodeId, newNodeId) {
+	if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
+	_todoGraphData[level].edges.push(
+		// dataMakeEdge(parentNodeId, nodeObject)
+		{
+			id: parentNodeId + newNodeId,
+			source: parentNodeId,
+			target: newNodeId,
+		}
+	)
+}
+function parseResponseRec(_todoGraphData, json, typeName) {
+	DBG = 1;
+	var parentNodeId = null
+	DBG && console.log('DBG::parseResponseRec', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
+	switch (typeName) {
+		case TN_PRACOWNICY: parseResponseRec__lvl1__Pracownik(_todoGraphData, json, typeName); break;
+		case TN_KONTRAHENCI: parseResponseRec__lvl1__Kontrahent(_todoGraphData, json, typeName); break;
+		default: throw "Not implemented root element typeName '" + typeName + "'";
+	}
+}
+function parseResponseRec__lvl1__Pracownik(_todoGraphData, jsonList, typeName) {
+	if (!p5Utils__isArray(jsonList)) throw "Not implemented root element - expected list of elements";
+	var isXlinkList = (jsonList.length > 0 && p5Utils__isString(jsonList[0]))
+	DBG && console.log('DBG::parseResponseRec__lvl1__Pracownik', {jsonList:jsonList, typeName:typeName, isString: p5Utils__isString(jsonList), isArray: p5Utils__isArray(jsonList), isObject: p5Utils__isObject(jsonList), isXlinkList});
+	jsonList.forEach(function (jsonPracownik) {
+		if (isXlinkList) {
+			DBG && console.warn('DBG::parseResponseRec__lvl1__Pracownik', {jsonPracownik:jsonPracownik, typeName:typeName, isString: p5Utils__isString(jsonPracownik), isArray: p5Utils__isArray(jsonPracownik), isObject: p5Utils__isObject(jsonPracownik), isXlinkList});
+			// parseResponseXlinkListRec(_todoGraphData, jsonPracownik, typeName)
+			throw "Not implemented xlink in Pracownik child";
+		} else {
+			var idNode = parseResponseRec__Helper__addTodoNode(_todoGraphData, level = 1, jsonPracownik, typeName);
+			if (jsonPracownik[TN_ROW_OBJ] && jsonPracownik[TN_ROW_OBJ].length) {
+				parseResponseRec__lvl2__Pracownik_RowObj(_todoGraphData, idNode, jsonPracownik[TN_ROW_OBJ]);
+			}
+		}
+	})
+}
+function parseResponseRec__lvl2__Pracownik_RowObj(_todoGraphData, parentIdNode, rowObjList) {
+	DBG && console.log('DBG::parseResponseRec__lvl2__Pracownik_RowObj rec...', { parentIdNode, rowObjList });
+	rowObjList.forEach(function (jsonRowObj) {
+		if (!p5Utils__isObject(jsonRowObj)) {
+			DBG && console.warn('DBG::parseResponseRec__lvl2__Pracownik_RowObj SKIP non object element', { jsonRowObj });
+			// skip non object elements
+			return;
+		}
+		if (isP5LinkObject(jsonRowObj)) {
+			DBG && console.log('DBG::parseResponseRec__lvl2__Pracownik_RowObj SKIP xlink', { jsonRowObj });
+			// skip p5 link object (type: "next")
+			return;
+		}
+		DBG && console.log('DBG::parseResponseRec__lvl2__Pracownik_RowObj row obj', { jsonRowObj });
+		if (jsonRowObj[TN_ROW] && jsonRowObj[TN_ROW].length) {
+			// NOTE: jsonRowObj[TN_ROW] is array(1) - always 1 because it is from __backRef
+			parseResponseRec__lvl3__Pracownik_RowObj_Row(_todoGraphData, parentIdNode, jsonRowObj[TN_ROW][0]);
+		}
+	})
+}
+function parseResponseRec__lvl3__Pracownik_RowObj_Row(_todoGraphData, parentIdNode, jsonRow) {
+	DBG && console.log('DBG::parseResponseRec__lvl3__Pracownik_RowObj_Row', { jsonRow, parentIdNode });
+	if (jsonRow[TN_ROW_OBJ] && jsonRow[TN_ROW_OBJ].length) {
+		var jsonListRowObj = jsonRow[TN_ROW_OBJ];
+		DBG && console.log('DBG::parseResponseRec__lvl3__Pracownik_RowObj_Row row obj', { jsonListRowObj, parentIdNode });
+		// NOTE: RowObj on list is a connection path in reverse order
+		// NOTE: last element is Pracownik (parentIdNode)
+		// NOTE: first element is Kontrahent - TODO: or another element defines in Raport.od.* or Raport.do.*
+		// var TN_PRACOWNICY = 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY';
+		// var TN_KONTRAHENCI = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI';
+		// var TN_ROW_OBJ = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object';
+		// var TN_ROW = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row';
+		// var TN_KRS = 'default_db__x3A__BI_audit_KRS:BI_audit_KRS';
+		// var TN_CEIDG = 'default_db__x3A__BI_audit_CEIDG:BI_audit_CEIDG';
+		// var TN_MSIG = 'default_db__x3A__BI_audit_MSIG:BI_audit_MSIG';
+		var pathPoints = jsonListRowObj.reverse().slice(1).map(function (jsonRowObj) {
+			if (jsonRowObj[TN_KONTRAHENCI]) return Object.assign(jsonRowObj[TN_KONTRAHENCI][0], { typeName: TN_KONTRAHENCI });
+			if (jsonRowObj[TN_PRACOWNICY]) return Object.assign(jsonRowObj[TN_PRACOWNICY][0], { typeName: TN_PRACOWNICY });
+			if (jsonRowObj[TN_KRS]) return Object.assign(jsonRowObj[TN_KRS][0], { typeName: TN_KRS });
+			if (jsonRowObj[TN_CEIDG]) return Object.assign(jsonRowObj[TN_CEIDG][0], { typeName: TN_CEIDG });
+			if (jsonRowObj[TN_MSIG]) return Object.assign(jsonRowObj[TN_MSIG][0], { typeName: TN_MSIG });
+			return null;
+		}).filter(function (jsonRowObj) {
+			return !!jsonRowObj;
+		}).map(function (jsonRowObj) {
+			DBG && console.warn('DBG::parseResponseRec__lvl3__Pracownik_RowObj_Row path', { parentIdNode, fid: (jsonRowObj ? jsonRowObj.typeName + '.' + jsonRowObj.ID : null), jsonRowObj });
+			return jsonRowObj;
+		})
+		for (var i = 0, prevNodeId = parentIdNode, newNodeId; i < pathPoints.length; i++) {
+			jsonObject = pathPoints[i];
+			newNodeId = parseResponseRec__Helper__addTodoNode(_todoGraphData, level = 4, jsonObject, jsonObject.typeName);
+			parseResponseRec__Helper__addTodoEdge(_todoGraphData, level = 4, prevNodeId, newNodeId);
+			prevNodeId = newNodeId;
+		}
+	}
+}
+
+function parseResponseRec__lvl1__Kontrahent(_todoGraphData, jsonList, typeName) {
+	if (!p5Utils__isArray(jsonList)) throw "Not implemented root element - expected list of elements";
+	var isXlinkList = (jsonList.length > 0 && p5Utils__isString(jsonList[0]))
+	DBG && console.log('DBG::parseResponseRec__lvl1__Kontrahent', {jsonList:jsonList, typeName:typeName, isString: p5Utils__isString(jsonList), isArray: p5Utils__isArray(jsonList), isObject: p5Utils__isObject(jsonList), isXlinkList});
+	jsonList.forEach(function (jsonPracownik) {
+		if (isXlinkList) {
+			DBG && console.warn('DBG::parseResponseRec__lvl1__Kontrahent', {jsonPracownik:jsonPracownik, typeName:typeName, isString: p5Utils__isString(jsonPracownik), isArray: p5Utils__isArray(jsonPracownik), isObject: p5Utils__isObject(jsonPracownik), isXlinkList});
+			// parseResponseXlinkListRec(_todoGraphData, jsonPracownik, typeName)
+			throw "Not implemented xlink in Pracownik child";
+		} else {
+			var idNode = parseResponseRec__Helper__addTodoNode(_todoGraphData, level = 1, jsonPracownik, typeName);
+			if (jsonPracownik[TN_ROW_OBJ] && jsonPracownik[TN_ROW_OBJ].length) {
+				parseResponseRec__lvl2__Kontrahent_RowObj(_todoGraphData, idNode, jsonPracownik[TN_ROW_OBJ]);
+			}
+		}
+	})
+}
+function parseResponseRec__lvl2__Kontrahent_RowObj(_todoGraphData, parentIdNode, rowObjList) {
+	DBG && console.log('DBG::parseResponseRec__lvl2__Kontrahent_RowObj rec...', { parentIdNode, rowObjList });
+	rowObjList.forEach(function (jsonRowObj) {
+		if (!p5Utils__isObject(jsonRowObj)) {
+			DBG && console.warn('DBG::parseResponseRec__lvl2__Kontrahent_RowObj SKIP non object element', { jsonRowObj });
+			// skip non object elements
+			return;
+		}
+		if (isP5LinkObject(jsonRowObj)) {
+			DBG && console.log('DBG::parseResponseRec__lvl2__Kontrahent_RowObj SKIP xlink', { jsonRowObj });
+			// skip p5 link object (type: "next")
+			return;
+		}
+		DBG && console.log('DBG::parseResponseRec__lvl2__Kontrahent_RowObj row obj', { jsonRowObj });
+		if (jsonRowObj[TN_ROW] && jsonRowObj[TN_ROW].length) {
+			// NOTE: jsonRowObj[TN_ROW] is array(1) - always 1 because it is from __backRef
+			parseResponseRec__lvl3__Kontrahent_RowObj_Row(_todoGraphData, parentIdNode, jsonRowObj[TN_ROW][0]);
+		}
+	})
+}
+function parseResponseRec__lvl3__Kontrahent_RowObj_Row(_todoGraphData, parentIdNode, jsonRow) {
+	DBG && console.log('DBG::parseResponseRec__lvl3__Pracownik_RowObj_Row', { jsonRow, parentIdNode });
+	if (jsonRow[TN_ROW_OBJ] && jsonRow[TN_ROW_OBJ].length) {
+		var jsonListRowObj = jsonRow[TN_ROW_OBJ];
+		DBG && console.log('DBG::parseResponseRec__lvl3__Kontrahent_RowObj_Row row obj', { jsonListRowObj, parentIdNode });
+		// NOTE: RowObj on list is a connection path in reverse order
+		// NOTE: last element is Pracownik (parentIdNode)
+		// NOTE: first element is Kontrahent - TODO: or another element defines in Raport.od.* or Raport.do.*
+		// var TN_PRACOWNICY = 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY';
+		// var TN_KONTRAHENCI = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI';
+		// var TN_ROW_OBJ = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object';
+		// var TN_ROW = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row';
+		// var TN_KRS = 'default_db__x3A__BI_audit_KRS:BI_audit_KRS';
+		// var TN_CEIDG = 'default_db__x3A__BI_audit_CEIDG:BI_audit_CEIDG';
+		// var TN_MSIG = 'default_db__x3A__BI_audit_MSIG:BI_audit_MSIG';
+		var pathPoints = jsonListRowObj.slice(1).reverse().map(function (jsonRowObj) {
+			if (jsonRowObj[TN_KONTRAHENCI]) return Object.assign(jsonRowObj[TN_KONTRAHENCI][0], { typeName: TN_KONTRAHENCI });
+			if (jsonRowObj[TN_PRACOWNICY]) return Object.assign(jsonRowObj[TN_PRACOWNICY][0], { typeName: TN_PRACOWNICY });
+			if (jsonRowObj[TN_KRS]) return Object.assign(jsonRowObj[TN_KRS][0], { typeName: TN_KRS });
+			if (jsonRowObj[TN_CEIDG]) return Object.assign(jsonRowObj[TN_CEIDG][0], { typeName: TN_CEIDG });
+			if (jsonRowObj[TN_MSIG]) return Object.assign(jsonRowObj[TN_MSIG][0], { typeName: TN_MSIG });
+			return null;
+		}).filter(function (jsonRowObj) {
+			return !!jsonRowObj;
+		}).map(function (jsonRowObj) {
+			DBG && console.warn('DBG::parseResponseRec__lvl3__Kontrahent_RowObj_Row path', { parentIdNode, fid: (jsonRowObj ? jsonRowObj.typeName + '.' + jsonRowObj.ID : null), jsonRowObj });
+			return jsonRowObj;
+		})
+		for (var i = 0, prevNodeId, newNodeId; i < pathPoints.length; i++) {
+			jsonObject = pathPoints[i];
+			newNodeId = parseResponseRec__Helper__addTodoNode(_todoGraphData, level = 4, jsonObject, jsonObject.typeName);
+			if (prevNodeId) parseResponseRec__Helper__addTodoEdge(_todoGraphData, level = 4, prevNodeId, newNodeId);
+			prevNodeId = newNodeId;
+		}
+		{
+			parseResponseRec__Helper__addTodoEdge(_todoGraphData, level = 4, prevNodeId, parentIdNode);
+		}
+	}
+}
+
+
+function parseResponseRec__OLD(_todoGraphData, json, typeName, parentNodeId, level) {
 	var level = level || 0
 	var parentNodeId = parentNodeId || null
 	DBG && console.log('DBG::parseResponseRec', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});