Bocian.php.graphShowHide.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. var getItemLocalStorage = global.getItemLocalStorage;
  2. var setItemLocalStorage = global.setItemLocalStorage;
  3. var p5WFS_GetFeature = global.p5WFS_GetFeature;
  4. var renderGraph = global.renderGraph; // @from sankey-init
  5. var DBG = DBG || 0;
  6. function graphShowHide(nameSection) {
  7. var node = $('#smad-'+nameSection+'-graph-view')
  8. if (!node || !node.length) return;
  9. if ('block' !== node.css('display')) {
  10. node.show()
  11. graphFetchData(node.get(0), nameSection)
  12. } else {
  13. node.hide()
  14. }
  15. }
  16. function graphFetchData(node, nameSection) {
  17. var page = page || getItemLocalStorage('Bocian.biAuditForm.'+nameSection+'.pagination.page');
  18. if ( page === 1) {
  19. setItemLocalStorage('Bocian.biAuditForm.'+nameSection+'.pagination.page', 1);
  20. }
  21. var filterIdGroup = getItemLocalStorage('Bocian.biAuditForm.'+nameSection+'.filterIdGroup');
  22. var frm = document.getElementById('filtersFieldRemoveBtn-' + nameSection.toUpperCase()).form
  23. var fieldNameList = ('pracownicy' === nameSection) ? FIELD_LIST_PRACOWNICY : FIELD_LIST_KONTRAHENCI
  24. var filterFields = fieldNameList.filter(function (fieldName) {
  25. if (!frm[fieldName] && DBG) console.log('Err missing field: "'+fieldName+'"')
  26. return (frm[fieldName]) ? true : false
  27. }).map(function (fieldName) {
  28. return [ fieldName, frm[fieldName].value ]
  29. }).filter(function (filter) {
  30. return ( filter[1].length > 0 )
  31. })
  32. // filterFields = (filterFields.length > 0) ? '&' + filterFields : ''
  33. // <ogc:Filter>
  34. // <ogc:Or>
  35. // <ogc:PropertyIsLike wildCard="*" singleChar="%23" escapeChar="!">
  36. // <ogc:PropertyName>A_STATUS</ogc:PropertyName>
  37. // <ogc:Literal>*O%23MA*</ogc:Literal>
  38. // </ogc:PropertyIsLike>
  39. // <ogc:PropertyIsLike wildCard="*" singleChar="%23" escapeChar="!">
  40. // <ogc:PropertyName>A_STATUS</ogc:PropertyName>
  41. // <ogc:Literal>*ARNING</ogc:Literal>
  42. // </ogc:PropertyIsLike>
  43. // </ogc:Or>
  44. // </ogc:Filter>
  45. var ogcFilterFields = (filterFields.length > 0)
  46. ? '<ogc:Filter><ogc:And>' + filterFields.map(function(filter) {
  47. if ('ID' === filter[0].substr(2)) return '<ogc:PropertyIsEqualTo>' +
  48. '<ogc:PropertyName>' + filter[0].substr(2) + '</ogc:PropertyName>' +
  49. '<ogc:Literal>' + filter[1] + '</ogc:Literal>' +
  50. '</ogc:PropertyIsEqualTo>';
  51. return '<ogc:PropertyIsLike wildCard="*" singleChar="%23" escapeChar="!">' +
  52. '<ogc:PropertyName>' + filter[0].substr(2) + '</ogc:PropertyName>' +
  53. '<ogc:Literal>*' + filter[1] + '*</ogc:Literal>' +
  54. '</ogc:PropertyIsLike>';
  55. }) + '</ogc:And></ogc:Filter>'
  56. : ''
  57. var paginationLimit = 20;
  58. if(DBG)console.log('graphFetchData...', {
  59. paginationLimit: paginationLimit,
  60. page: page,
  61. filterIdGroup: filterIdGroup,
  62. filterFields: filterFields,
  63. ogcFilterFields: ogcFilterFields,
  64. });
  65. function refFieldsOnPathToList(typeName, fields) {
  66. var fields = fields || []
  67. var flatList = []
  68. var refPathBase = [
  69. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
  70. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
  71. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object'
  72. ]
  73. flatList.push(refPathBase.concat(typeName).join('/'))
  74. fields.forEach(function (fieldName) {
  75. flatList.push(refPathBase.concat(typeName, fieldName).join('/'))
  76. })
  77. return flatList;
  78. }
  79. p5WFS_GetFeature('default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY',
  80. Object.assign({
  81. sortBy: 'ID+D',
  82. maxFeatures: paginationLimit,
  83. startIndex: (page - 1) * paginationLimit,
  84. // TODO: backRefNS, backRefPK, backRefField - TODO: from groups
  85. // resolve: 'all',
  86. // resolveDepth: 2
  87. 'ogc:Filter': '<wfs:Query>' + '\n' + [
  88. 'ID',
  89. 'imiona',
  90. 'nazwisko',
  91. 'miejscowosc'
  92. ].concat(refFieldsOnPathToList('default_db__x3A__BI_audit_KRS:BI_audit_KRS', [ 'ID', 'nazwa', 'krs', 'S_miejscowosc' ]))
  93. .concat(refFieldsOnPathToList('default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI', [ 'ID', 'Pelna_nazwa_kontrahenta' ]))
  94. .concat(refFieldsOnPathToList('default_db__x3A__BI_audit_MSIG:BI_audit_MSIG', [ 'ID', 'nazwa' ]))
  95. .concat(refFieldsOnPathToList('default_db__x3A__BI_audit_CEIDG:BI_audit_CEIDG', [ 'ID', 'nazwisko', 'firma' ]))
  96. .concat([
  97. // '*',
  98. // 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY_adresy:BI_audit_ENERGA_PRACOWNICY_adresy/*',
  99. [
  100. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
  101. 'ID'
  102. ].join('/'),
  103. [
  104. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
  105. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
  106. 'ID'
  107. ].join('/'),
  108. [
  109. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
  110. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
  111. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
  112. 'ID'
  113. ].join('/')
  114. ]).map(function (fieldName) {
  115. return '<wfs:PropertyName>' + fieldName + '</wfs:PropertyName>';
  116. }).join('\n') + '\n' +
  117. ( ogcFilterFields ? ogcFilterFields : '' ) +
  118. '</wfs:Query>'
  119. }, (filterIdGroup > 0)
  120. ? {
  121. backRefNS: 'default_db/BI_audit_ENERGA_PRACOWNICY_group/BI_audit_ENERGA_PRACOWNICY_group',
  122. backRefPK: filterIdGroup,
  123. backRefField: 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY',
  124. }
  125. : {}
  126. )
  127. ).then(function (items) {
  128. if(DBG)console.log('p5WFS_GetFeature: items: ', items);
  129. graphRaportRender({
  130. msg: "Pobrano dane",
  131. typeName: ('pracownicy' === nameSection) ? 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY' : 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI',
  132. items: items
  133. }, node)
  134. }).catch(function (err) {
  135. if(DBG)console.log('p5WFS_GetFeature: err: ', err);
  136. // graphRaportRender({ msg: "Wystąpiły błędy podczas pobierania danych", nameSection: nameSection, err: err }, node)
  137. })
  138. }
  139. function graphRender(props, wrapNode) {
  140. throw "TODO: graphRender..."
  141. }
  142. function graphRaportRender(props, wrapNode) {
  143. var svgNode = jQuery(wrapNode).children('svg')
  144. svgNode = (svgNode.length) ? d3.select(svgNode.get(0)) : d3.select(wrapNode).append("svg")
  145. // svg.append("rect").attr("x",0).attr("y",0).attr("width","100%").attr("height","100%").attr("fill","white").attr('class','background').on('mouseup', () => redraw())
  146. // svgNode.attr("width", '100%')
  147. // .attr("height", '300px')
  148. // .append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  149. // var data = parseGraphRec(props.items, props.nameSection)
  150. var _todoGraphData = [];
  151. parseResponseRec(_todoGraphData, props.items, props.typeName)
  152. if(DBG)console.log('_todoGraphData', _todoGraphData)
  153. var _nodes = [];
  154. var _links = [];
  155. var mapNodeIdToIdx = {};
  156. _todoGraphData.forEach(function (levelData, levelIdx) {
  157. if (levelData.nodes && levelData.nodes.length) {
  158. levelData.nodes.forEach(function (node) {
  159. try {
  160. if (node.id in mapNodeIdToIdx) return;
  161. _nodes.push(node) // _nodes.add(node)
  162. mapNodeIdToIdx[node.id] = _nodes.length - 1
  163. } catch (e) {
  164. if(DBG)console.log('_graphData.nodes.add [level='+levelIdx+'] error:', e);
  165. }
  166. })
  167. }
  168. })
  169. var linkIdsAdded = []
  170. _todoGraphData.forEach(function (levelData, levelIdx) {
  171. if (levelData.edges && levelData.edges.length) {
  172. levelData.edges.forEach(function (edge) {
  173. if(DBG)console.log('_graphData.edges.add [level='+levelIdx+']:', {edge, source:mapNodeIdToIdx[edge.source], target:mapNodeIdToIdx[edge.target], mapNodeIdToIdx});
  174. // try {
  175. // var source = mapNodeIdToIdx[edge.source]
  176. // var target = mapNodeIdToIdx[edge.target]
  177. // if (!source || !target) { // && !== 0
  178. // console.warn('(!source || !target)', {source, target, edge, mapNodeIdToIdx})
  179. // }
  180. if (-1 !== linkIdsAdded.indexOf(edge.id)) return
  181. // _links.push({
  182. // id: edge.id,
  183. // source: mapNodeIdToIdx[edge.source],
  184. // target: mapNodeIdToIdx[edge.target],
  185. // value: 1
  186. // })
  187. _links.push({
  188. id: edge.id,
  189. source: edge.source,
  190. target: edge.target,
  191. value: 1
  192. }) // _edges.add(edge)
  193. linkIdsAdded.push(edge.id)
  194. // } catch (e) {
  195. // if(DBG)console.log('_graphData.edges.add [level='+levelIdx+'] error:', e);
  196. // }
  197. })
  198. }
  199. })
  200. var totalLinks = _links.length
  201. // console.log('DBG:0: _links', _links)
  202. // console.log('DBG:1: _nodes', _nodes)
  203. _nodes = _nodes.filter(function (node, idx) {
  204. if (node.typeName !== props.typeName) return true
  205. for (var i=0; i<totalLinks; i++) {
  206. var link = _links[i]
  207. if (_links[i].source === node.id) return true
  208. if (_links[i].target === node.id) return true
  209. }
  210. return false
  211. })
  212. // console.log('DBG:2: _nodes', _nodes)
  213. jQuery(wrapNode).find('p').remove()
  214. if (!_nodes || !_nodes.length) {
  215. jQuery(wrapNode).find('svg').remove()
  216. jQuery(wrapNode).append('<p>Brak powiązań</p>')
  217. return;
  218. }
  219. var rightSideNodes = _nodes.filter(function (node, idx) {
  220. for (var i=0; i<totalLinks; i++) {
  221. var link = _links[i]
  222. // if (_links[i].source === node.id) return true
  223. if (_links[i].target === node.id) return true
  224. }
  225. return false
  226. })
  227. DBG && console.log('DBG: rightSideNodes (total:'+rightSideNodes.length+') ', rightSideNodes)
  228. if (rightSideNodes.length > 20) {
  229. // TODO: increase height
  230. }
  231. var graphData = { nodes: _nodes, links: _links };
  232. var graf = renderGraph(svgNode, graphData, {
  233. width: jQuery(wrapNode).width(),
  234. height: (rightSideNodes.length > 20) ? rightSideNodes.length * 22 : 500
  235. })
  236. graf.on('click', (event) => {
  237. if(DBG)console.log('event', event)
  238. // event = {
  239. // nativeEvent: d3.event,
  240. // node: a,
  241. // element: d3.event.srcElement
  242. // }
  243. // TODO: !a.expanded && redraw({type: a.name, criteria: 'ID'})
  244. })
  245. }
  246. function parseResponseRec(_todoGraphData, json, typeName, parentNodeId, level) {
  247. var level = level || 0
  248. var parentNodeId = parentNodeId || null
  249. if(DBG)console.log('DBG::parseResponseRec', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
  250. if (p5Utils__isArray(json)) {
  251. // TODO: create named group
  252. var isXlinkList = (json.length > 0 && p5Utils__isString(json[0]))
  253. json.forEach(function (subJson) {
  254. if (isXlinkList) {
  255. parseResponseXlinkListRec(_todoGraphData, subJson, typeName, parentNodeId, level)
  256. } else {
  257. parseResponseRec(_todoGraphData, subJson, typeName, parentNodeId, level)
  258. }
  259. })
  260. } else if (p5Utils__isObject(json) && isP5LinkObject(json)) {
  261. if(DBG)console.log('DBG::parseResponseRec isP5LinkObject');
  262. parseResponseP5Link(_todoGraphData, json, typeName, parentNodeId, level)
  263. } else if (p5Utils__isObject(json)) {
  264. // _todoGraphData.nodes.add({ id: nodeId, label: nodeId })
  265. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  266. var nodeObject = dataMakeNode({
  267. typeName: typeName,
  268. primaryKey: (json['ID']) ? json['ID'] : null, // TODO: get primaryKey from object
  269. row: json,
  270. })
  271. var nodeId = nodeObject.id
  272. if ("default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row" === typeName) {
  273. parseResponseRec__row(_todoGraphData, json, typeName, parentNodeId, level)
  274. }
  275. else if ("default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object" === typeName) {
  276. // parseResponseRec__row_object(_todoGraphData, json, typeName, parentNodeId, level)
  277. Object.keys(json).filter(function (fieldName) {
  278. return (fieldName.indexOf(':') > -1)
  279. })
  280. .forEach(function (fieldName) {
  281. var value = json[fieldName]
  282. parseResponseRec(_todoGraphData, value, fieldName, parentNodeId, level + 1)
  283. })
  284. }
  285. else {
  286. _todoGraphData[level].nodes.push(nodeObject)
  287. if (parentNodeId) {
  288. _todoGraphData[level].edges.push(dataMakeEdge(parentNodeId, nodeObject))
  289. }
  290. Object.keys(json).filter(function (fieldName) {
  291. return (fieldName.indexOf(':') > -1)
  292. })
  293. .forEach(function (fieldName) {
  294. var value = json[fieldName]
  295. parseResponseRec(_todoGraphData, value, fieldName, nodeId, level + 1)
  296. })
  297. }
  298. } else if (p5Utils__isString(json)) {
  299. if(DBG)console.log('TODO: Not implemented - parseResponseRec isString');
  300. } else {
  301. if(DBG)console.log('TODO: Not implemented - parseResponseRec is not string, not array and not object');
  302. }
  303. }
  304. function parseResponseXlinkListRec(_todoGraphData, json, typeName, parentNodeId, level) {
  305. if(DBG)console.log('DBG::parseResponseRec:XlinkList', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
  306. if (p5Utils__isString(json)) { // xlink "https://biuro.biall-net.pl/wfs/default_db/BI_audit_ENERGA_RUM_KONTRAHENCI#BI_audit_ENERGA_RUM_KONTRAHENCI.9233",
  307. var nodeId = json.substr(json.indexOf('#') + 1)
  308. {
  309. // _graphData.nodes.add({ id: nodeId, label: nodeId })
  310. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  311. var nodeObject = dataMakeXlinkNode({
  312. xlink: json,
  313. typeName: typeName,
  314. })
  315. _todoGraphData[level].nodes.push(nodeObject)
  316. if (parentNodeId) {
  317. _todoGraphData[level].edges.push(dataMakeEdge(parentNodeId, nodeObject))
  318. }
  319. }
  320. } else if (p5Utils__isObject(json) && isP5LinkObject(json)) {
  321. parseResponseP5Link(_todoGraphData, json, typeName, parentNodeId, level)
  322. } else {
  323. if(DBG)console.log('TODO: Not implemented - parseResponseRec:XlinkList is not string and not object');
  324. }
  325. }
  326. function parseResponseP5Link(_todoGraphData, json, typeName, parentNodeId, level) {
  327. if(DBG)console.log('parseResponseRec isObject and P5Link - fetch more xlink object');
  328. // example json: { type: "next",
  329. // @backRefNS: "default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA",
  330. // @backRefPK: "42",
  331. // @typeName: "default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_P…ow:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row",
  332. // @startIndex: "10" }
  333. if (!parentNodeId) throw "Missing parentNodeId for ref link object";
  334. var objectName = typeName.substr(typeName.indexOf(':') + 1)
  335. {
  336. // _graphData.nodes.add({ id: nodeId, label: nodeId })
  337. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  338. switch (json.type) {
  339. case 'next': {
  340. // TODO: add fetch next node
  341. // var nodeObject = dataMakeFetchMoreNode({
  342. // parentNodeId: parentNodeId,
  343. // type: json.type,
  344. // objectName: objectName,
  345. // ref: json,
  346. // })
  347. // _todoGraphData[level].nodes.push(nodeObject)
  348. // _todoGraphData[level].edges.push(dataMakeFetchMoreEdge(parentNodeId, nodeObject))
  349. } break;
  350. default: {
  351. if(DBG)console.log('TODO: Not implemented - parseResponseRec isObject with type "'+json.type+'" - fetch more xlink object');
  352. }
  353. }
  354. }
  355. }
  356. function parseResponseRec__row(_todoGraphData, json, typeName, parentNodeId, level) {
  357. var nodeObject = dataMakeNode({
  358. typeName: typeName,
  359. primaryKey: (json['ID']) ? json['ID'] : null, // TODO: get primaryKey from object
  360. })
  361. var nodeId = nodeObject.id
  362. // 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
  363. // 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
  364. // 'default_db__x3A__BI_audit_KRS:BI_audit_KRS' --> Kontrahenci
  365. var rowObjectChildrens = []
  366. var rowTN = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row'
  367. var rowObjTN = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object'
  368. // { row: row_obj: [ { ID, kontr: [ "http...kontr.ID" ] }, { ID, kontr: [ "http...krs.ID" ] } ], ... }
  369. var rowJson = json
  370. if (rowJson[rowObjTN] && rowJson[rowObjTN].length > 1) {
  371. var pathItems = []
  372. rowJson[rowObjTN].forEach(function (rowObjJson) {
  373. Object.keys(rowObjJson)
  374. .filter(function (rowObjFld) { return (-1 !== rowObjFld.indexOf(':')); })
  375. .filter(function (rowObjRefFld) { return (rowObjJson[rowObjRefFld] && rowObjJson[rowObjRefFld].length); })
  376. .forEach(function (rowObjRefFld) {
  377. pathItems.push([ rowObjRefFld, rowObjJson[rowObjRefFld][0] ])
  378. })
  379. })
  380. }
  381. // console.log('node('+nodeId+') pathItems:', pathItems);
  382. var lastParentFid = null
  383. pathItems.reverse().forEach(function (pathItem) {
  384. var nodeObject = ('string' === typeof pathItem[1])
  385. ? dataMakeXlinkNode({
  386. xlink: pathItem[1],
  387. typeName: pathItem[0]
  388. })
  389. : dataMakeNode({
  390. typeName: pathItem[0],
  391. primaryKey: (pathItem[1]['ID']) ? pathItem[1]['ID'] : null,
  392. row: pathItem[1]
  393. })
  394. ;
  395. // _todoGraphData[todoLevel].nodes.push(nodeObject) // already added below
  396. if (lastParentFid) _todoGraphData[level].edges.push(dataMakeEdge(lastParentFid, nodeObject))
  397. lastParentFid = nodeObject.id
  398. })
  399. Object.keys(json).filter(function (fieldName) {
  400. return (fieldName.indexOf(':') > -1)
  401. })
  402. .forEach(function (fieldName) {
  403. var value = json[fieldName]
  404. parseResponseRec(_todoGraphData, value, fieldName, parentNodeId, level + 1)
  405. })
  406. }
  407. function parseGraphRec(items, featureType, parentFeatureId) { // TODO: not used
  408. var parentFeatureId = parentFeatureId || null
  409. var nodesArray = []
  410. var linksArray = []
  411. if (!items) return;
  412. items.forEach(function (item) {
  413. if ('string' === typeof item) {
  414. if(DBG)console.log('TODO: xlink "'+item+'": ', {item:item, parentFeatureId:parentFeatureId})
  415. return;
  416. }
  417. if (!item['ID']) {
  418. if(DBG)console.log('TODO: SKIP item('+featureType+') - missing ID: ', {item:item, parentFeatureId:parentFeatureId})
  419. return;
  420. }
  421. var id = item['ID']
  422. console.log('item('+featureType+'): ', {item:item, parentFeatureId:parentFeatureId})
  423. var featureId = featureType + '.' + id
  424. Object.keys(item).filter(function (fieldName) { return ('ID' !== fieldName); })
  425. .forEach(function (fieldName) {
  426. parseGraphRec(item[fieldName], fieldName, featureId)
  427. })
  428. })
  429. // items.forEach(element => {
  430. // const rowObject = hasTargetArray(element);
  431. // if (rowObject)
  432. // rowObject.array.forEach(objectElement => {
  433. // const trackNode = hasTargetArray(objectElement);
  434. // if (trackNode) {
  435. // let group = trackNode.name;
  436. // let expanded = false;
  437. // if (expansion && expansion.type && group === expansion.type) {
  438. // group = trackNode.array[0][expansion.criteria];
  439. // expanded = true;
  440. // }
  441. // nodes[group] = {expanded: expanded, type: trackNode.name};
  442. // instanceTypes[trackNode.array[0].ID] = group;
  443. // }
  444. // });
  445. // });
  446. return {
  447. nodes: nodesArray,
  448. links: linksArray
  449. }
  450. }
  451. function isP5LinkObject(json) {
  452. if ( !('type' in json) || !json['type'] ) return false;
  453. if ( !('value' in json) || !json['value'] ) return false;
  454. if ( !('@typeName' in json) || !json['@typeName'] ) return false;
  455. if ( !('@startIndex' in json) || !json['@startIndex'] ) return false;
  456. if ( !('@backRefPK' in json) || !json['@backRefPK'] ) return false;
  457. if ( !('@backRefNS' in json) || !json['@backRefNS'] ) return false;
  458. return true;
  459. }
  460. function dataMakeNode(params) {
  461. var objectName = params.typeName.substr(params.typeName.indexOf(':') + 1)
  462. var nodeId = objectName + '.' + params.primaryKey // TODO: primaryKey?
  463. var graphNode = {
  464. id: nodeId,
  465. name: nodeId,
  466. group: objectName,
  467. _loaded: true,
  468. typeName: params.typeName,
  469. primaryKey: params.primaryKey // TODO: _primaryKey
  470. }
  471. if (params.row) graphNode.row = params.row
  472. return graphAddNodeLabel(graphNode)
  473. }
  474. function dataMakeEdge(parentNodeId, nodeObject) {
  475. return {
  476. id: parentNodeId + nodeObject.id,
  477. source: parentNodeId,
  478. target: nodeObject.id,
  479. }
  480. }
  481. function dataMakeXlinkNode(params) {
  482. var objectName = params.typeName.substr(params.typeName.indexOf(':') + 1)
  483. var nodeId = params.xlink.substr(params.xlink.indexOf('#') + 1)
  484. var primaryKey = nodeId.substr(nodeId.lastIndexOf('.') + 1)
  485. var graphNode = {
  486. id: nodeId,
  487. name: nodeId,
  488. group: objectName,
  489. _loaded: false,
  490. typeName: params.typeName,
  491. primaryKey: primaryKey
  492. }
  493. return graphAddNodeLabel(graphNode)
  494. }
  495. function dataMakeFetchMoreNode(params) {
  496. // params = {
  497. // parentNodeId: parentNodeId,
  498. // type: json.type,
  499. // objectName: objectName,
  500. // ref: json,
  501. // }
  502. if(DBG)console.log('DBG dataMakeFetchMoreNode(params)', params)
  503. var nodeId = params.parentNodeId+'fetch-more-features-'+params.type+'-on-' + params.objectName;
  504. return {
  505. id: nodeId,
  506. label: 'Pobierz więcej (+)',
  507. group: 'fetch-more-data', // params.objectName,
  508. _loaded: false,
  509. _type: 'ref',
  510. parentNodeId: params.parentNodeId,
  511. ref: params.ref,
  512. // typeName: typeName,
  513. // primaryKey: nodeId.substr(nodeId.lastIndexOf('.') + 1)
  514. };
  515. }
  516. function dataMakeFetchMoreEdge(parentNodeId, fetchMoreNode) {
  517. // @param parentNodeId - from node id
  518. // @param fetchMoreNode - from dataMakeFetchMoreNode
  519. if(DBG)console.log('DBG dataMakeFetchMoreEdge(parentNodeId, fetchMoreNode)', {parentNodeId:parentNodeId, fetchMoreNode:fetchMoreNode})
  520. return {
  521. id: fetchMoreNode.id,
  522. from: parentNodeId,
  523. to: fetchMoreNode.id
  524. }
  525. }
  526. function makeShortLabel(label) { // TODO: shorter name
  527. // BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object.1234567
  528. if (label.length < 30) return label;
  529. var exLabel = label.split('.')
  530. if (exLabel.length !== 2) return label;
  531. return exLabel[0].substr(0, 24) + '...' + exLabel[0].substr(-10) + '.' + exLabel[1];
  532. }
  533. function graphAddNodeLabel(graphNode) {
  534. // var graphNode = {
  535. // id: nodeId,
  536. // name: nodeId,
  537. // group: objectName,
  538. // _loaded: true,
  539. // typeName: params.typeName,
  540. // primaryKey: params.primaryKey // TODO: _primaryKey
  541. // }
  542. var label = graphNode.id // TODO: get from schema assert @label attribute
  543. label = makeShortLabel(graphNode.id)
  544. switch (graphNode.typeName) {
  545. case 'default_db__x3A__BI_audit_KRS:BI_audit_KRS': {
  546. // <xs:assert test="@default_db__x3A__BI_audit_KRS:label = concat(nazwa, ' ', krs, ' ', S_miejscowosc)" id="I_audit_KRS___d6e76977-1">
  547. if (graphNode.row) label = [graphNode.row.nazwa, graphNode.row.krs, graphNode.row.S_miejscowosc].filter(function (val) { return val; }).join(' ');
  548. label = (label) ? label : makeShortLabel(graphNode.id)
  549. label = label + ' (krs)'
  550. } break;
  551. case 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY': {
  552. // <xs:assert id="_PRACOWNICY___d6e76324-1" test="@label = concat(imiona, ' ', nazwisko, ' ', miejscowosc)"/>
  553. if (graphNode.row) label = [graphNode.row.imiona, graphNode.row.nazwisko, graphNode.row.miejscowosc].filter(function (val) { return val; }).join(' ');
  554. label = (label) ? label : makeShortLabel(graphNode.id)
  555. label = label + ' (pracownicy)'
  556. } break;
  557. case 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI': {
  558. // <xs:assert id="KONTRAHENCI___d6e76526-1" test="@label = concat(substr(Pelna_nazwa_kontrahenta, 0, 20, '\n umów na kwotę ', @sum))"/>
  559. if (graphNode.row) label = [graphNode.row.Pelna_nazwa_kontrahenta].filter(function (val) { return val; }).join(' ');
  560. label = (label) ? label : makeShortLabel(graphNode.id)
  561. label = label + ' (kontrahent)'
  562. } break;
  563. case 'default_db__x3A__BI_audit_CEIDG:BI_audit_CEIDG': {
  564. // <xs:assert test="@label = concat(nazwisko, substring(firma, 1, 20))"/>
  565. if (graphNode.row) label = [graphNode.row.nazwisko, graphNode.row.firma].filter(function (val) { return val; }).join(' ');
  566. label = (label) ? label : makeShortLabel(graphNode.id)
  567. label = label + ' (ceidg)'
  568. } break;
  569. case 'default_db__x3A__BI_audit_MSIG:BI_audit_MSIG': {
  570. // <xs:assert test="@label = substring(nazwa, 1, 20)"/>
  571. if (graphNode.row) label = [graphNode.row.nazwa].filter(function (val) { return val; }).join(' ');
  572. label = (label) ? label : makeShortLabel(graphNode.id)
  573. label = label + ' (msig)'
  574. } break;
  575. }
  576. return Object.assign(graphNode, {
  577. label: label,
  578. })
  579. }
  580. module.exports = {
  581. graphShowHide: graphShowHide,
  582. graphRender: graphRender,
  583. graphRaportRender: graphRaportRender,
  584. }