Bocian.php.graphShowHide.js 25 KB

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