Bocian.php.graphShowHide.js 26 KB

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