Bocian.php.graphShowHide.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 (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 graphData = { nodes: _nodes, links: _links };
  220. var graf = renderGraph(svgNode, graphData, {
  221. width: jQuery(wrapNode).width()
  222. })
  223. graf.on('click', (event) => {
  224. if(DBG)console.log('event', event)
  225. // event = {
  226. // nativeEvent: d3.event,
  227. // node: a,
  228. // element: d3.event.srcElement
  229. // }
  230. // TODO: !a.expanded && redraw({type: a.name, criteria: 'ID'})
  231. })
  232. }
  233. function parseResponseRec(_todoGraphData, json, typeName, parentNodeId, level) {
  234. var level = level || 0
  235. var parentNodeId = parentNodeId || null
  236. if(DBG)console.log('DBG::parseResponseRec', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
  237. if (p5Utils__isArray(json)) {
  238. // TODO: create named group
  239. var isXlinkList = (json.length > 0 && p5Utils__isString(json[0]))
  240. json.forEach(function (subJson) {
  241. if (isXlinkList) {
  242. parseResponseXlinkListRec(_todoGraphData, subJson, typeName, parentNodeId, level)
  243. } else {
  244. parseResponseRec(_todoGraphData, subJson, typeName, parentNodeId, level)
  245. }
  246. })
  247. } else if (p5Utils__isObject(json) && isP5LinkObject(json)) {
  248. if(DBG)console.log('DBG::parseResponseRec isP5LinkObject');
  249. parseResponseP5Link(_todoGraphData, json, typeName, parentNodeId, level)
  250. } else if (p5Utils__isObject(json)) {
  251. // _todoGraphData.nodes.add({ id: nodeId, label: nodeId })
  252. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  253. var nodeObject = dataMakeNode({
  254. typeName: typeName,
  255. primaryKey: (json['ID']) ? json['ID'] : null, // TODO: get primaryKey from object
  256. row: json,
  257. })
  258. var nodeId = nodeObject.id
  259. if ("default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row" === typeName) {
  260. parseResponseRec__row(_todoGraphData, json, typeName, parentNodeId, level)
  261. }
  262. else if ("default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object" === typeName) {
  263. // parseResponseRec__row_object(_todoGraphData, json, typeName, parentNodeId, level)
  264. Object.keys(json).filter(function (fieldName) {
  265. return (fieldName.indexOf(':') > -1)
  266. })
  267. .forEach(function (fieldName) {
  268. var value = json[fieldName]
  269. parseResponseRec(_todoGraphData, value, fieldName, parentNodeId, level + 1)
  270. })
  271. }
  272. else {
  273. _todoGraphData[level].nodes.push(nodeObject)
  274. if (parentNodeId) {
  275. _todoGraphData[level].edges.push(dataMakeEdge(parentNodeId, nodeObject))
  276. }
  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, nodeId, level + 1)
  283. })
  284. }
  285. } else if (p5Utils__isString(json)) {
  286. if(DBG)console.log('TODO: Not implemented - parseResponseRec isString');
  287. } else {
  288. if(DBG)console.log('TODO: Not implemented - parseResponseRec is not string, not array and not object');
  289. }
  290. }
  291. function parseResponseXlinkListRec(_todoGraphData, json, typeName, parentNodeId, level) {
  292. 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)});
  293. if (p5Utils__isString(json)) { // xlink "https://biuro.biall-net.pl/wfs/default_db/BI_audit_ENERGA_RUM_KONTRAHENCI#BI_audit_ENERGA_RUM_KONTRAHENCI.9233",
  294. var nodeId = json.substr(json.indexOf('#') + 1)
  295. {
  296. // _graphData.nodes.add({ id: nodeId, label: nodeId })
  297. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  298. var nodeObject = dataMakeXlinkNode({
  299. xlink: json,
  300. typeName: typeName,
  301. })
  302. _todoGraphData[level].nodes.push(nodeObject)
  303. if (parentNodeId) {
  304. _todoGraphData[level].edges.push(dataMakeEdge(parentNodeId, nodeObject))
  305. }
  306. }
  307. } else if (p5Utils__isObject(json) && isP5LinkObject(json)) {
  308. parseResponseP5Link(_todoGraphData, json, typeName, parentNodeId, level)
  309. } else {
  310. if(DBG)console.log('TODO: Not implemented - parseResponseRec:XlinkList is not string and not object');
  311. }
  312. }
  313. function parseResponseP5Link(_todoGraphData, json, typeName, parentNodeId, level) {
  314. if(DBG)console.log('parseResponseRec isObject and P5Link - fetch more xlink object');
  315. // example json: { type: "next",
  316. // @backRefNS: "default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA",
  317. // @backRefPK: "42",
  318. // @typeName: "default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_P…ow:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row",
  319. // @startIndex: "10" }
  320. if (!parentNodeId) throw "Missing parentNodeId for ref link object";
  321. var objectName = typeName.substr(typeName.indexOf(':') + 1)
  322. {
  323. // _graphData.nodes.add({ id: nodeId, label: nodeId })
  324. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  325. switch (json.type) {
  326. case 'next': {
  327. // TODO: add fetch next node
  328. // var nodeObject = dataMakeFetchMoreNode({
  329. // parentNodeId: parentNodeId,
  330. // type: json.type,
  331. // objectName: objectName,
  332. // ref: json,
  333. // })
  334. // _todoGraphData[level].nodes.push(nodeObject)
  335. // _todoGraphData[level].edges.push(dataMakeFetchMoreEdge(parentNodeId, nodeObject))
  336. } break;
  337. default: {
  338. if(DBG)console.log('TODO: Not implemented - parseResponseRec isObject with type "'+json.type+'" - fetch more xlink object');
  339. }
  340. }
  341. }
  342. }
  343. function parseResponseRec__row(_todoGraphData, json, typeName, parentNodeId, level) {
  344. var nodeObject = dataMakeNode({
  345. typeName: typeName,
  346. primaryKey: (json['ID']) ? json['ID'] : null, // TODO: get primaryKey from object
  347. })
  348. var nodeId = nodeObject.id
  349. // 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
  350. // 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
  351. // 'default_db__x3A__BI_audit_KRS:BI_audit_KRS' --> Kontrahenci
  352. var rowObjectChildrens = []
  353. var rowTN = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row'
  354. var rowObjTN = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object'
  355. // { row: row_obj: [ { ID, kontr: [ "http...kontr.ID" ] }, { ID, kontr: [ "http...krs.ID" ] } ], ... }
  356. var rowJson = json
  357. if (rowJson[rowObjTN] && rowJson[rowObjTN].length > 1) {
  358. var pathItems = []
  359. rowJson[rowObjTN].forEach(function (rowObjJson) {
  360. Object.keys(rowObjJson)
  361. .filter(function (rowObjFld) { return (-1 !== rowObjFld.indexOf(':')); })
  362. .filter(function (rowObjRefFld) { return (rowObjJson[rowObjRefFld] && rowObjJson[rowObjRefFld].length); })
  363. .forEach(function (rowObjRefFld) {
  364. pathItems.push([ rowObjRefFld, rowObjJson[rowObjRefFld][0] ])
  365. })
  366. })
  367. }
  368. // console.log('node('+nodeId+') pathItems:', pathItems);
  369. var lastParentFid = null
  370. pathItems.reverse().forEach(function (pathItem) {
  371. var nodeObject = ('string' === typeof pathItem[1])
  372. ? dataMakeXlinkNode({
  373. xlink: pathItem[1],
  374. typeName: pathItem[0]
  375. })
  376. : dataMakeNode({
  377. typeName: pathItem[0],
  378. primaryKey: (pathItem[1]['ID']) ? pathItem[1]['ID'] : null,
  379. row: pathItem[1]
  380. })
  381. ;
  382. // _todoGraphData[todoLevel].nodes.push(nodeObject) // already added below
  383. if (lastParentFid) _todoGraphData[level].edges.push(dataMakeEdge(lastParentFid, nodeObject))
  384. lastParentFid = nodeObject.id
  385. })
  386. Object.keys(json).filter(function (fieldName) {
  387. return (fieldName.indexOf(':') > -1)
  388. })
  389. .forEach(function (fieldName) {
  390. var value = json[fieldName]
  391. parseResponseRec(_todoGraphData, value, fieldName, parentNodeId, level + 1)
  392. })
  393. }
  394. function parseGraphRec(items, featureType, parentFeatureId) { // TODO: not used
  395. var parentFeatureId = parentFeatureId || null
  396. var nodesArray = []
  397. var linksArray = []
  398. if (!items) return;
  399. items.forEach(function (item) {
  400. if ('string' === typeof item) {
  401. if(DBG)console.log('TODO: xlink "'+item+'": ', {item:item, parentFeatureId:parentFeatureId})
  402. return;
  403. }
  404. if (!item['ID']) {
  405. if(DBG)console.log('TODO: SKIP item('+featureType+') - missing ID: ', {item:item, parentFeatureId:parentFeatureId})
  406. return;
  407. }
  408. var id = item['ID']
  409. console.log('item('+featureType+'): ', {item:item, parentFeatureId:parentFeatureId})
  410. var featureId = featureType + '.' + id
  411. Object.keys(item).filter(function (fieldName) { return ('ID' !== fieldName); })
  412. .forEach(function (fieldName) {
  413. parseGraphRec(item[fieldName], fieldName, featureId)
  414. })
  415. })
  416. // items.forEach(element => {
  417. // const rowObject = hasTargetArray(element);
  418. // if (rowObject)
  419. // rowObject.array.forEach(objectElement => {
  420. // const trackNode = hasTargetArray(objectElement);
  421. // if (trackNode) {
  422. // let group = trackNode.name;
  423. // let expanded = false;
  424. // if (expansion && expansion.type && group === expansion.type) {
  425. // group = trackNode.array[0][expansion.criteria];
  426. // expanded = true;
  427. // }
  428. // nodes[group] = {expanded: expanded, type: trackNode.name};
  429. // instanceTypes[trackNode.array[0].ID] = group;
  430. // }
  431. // });
  432. // });
  433. return {
  434. nodes: nodesArray,
  435. links: linksArray
  436. }
  437. }
  438. function isP5LinkObject(json) {
  439. if ( !('type' in json) || !json['type'] ) return false;
  440. if ( !('value' in json) || !json['value'] ) return false;
  441. if ( !('@typeName' in json) || !json['@typeName'] ) return false;
  442. if ( !('@startIndex' in json) || !json['@startIndex'] ) return false;
  443. if ( !('@backRefPK' in json) || !json['@backRefPK'] ) return false;
  444. if ( !('@backRefNS' in json) || !json['@backRefNS'] ) return false;
  445. return true;
  446. }
  447. function dataMakeNode(params) {
  448. var objectName = params.typeName.substr(params.typeName.indexOf(':') + 1)
  449. var nodeId = objectName + '.' + params.primaryKey // TODO: primaryKey?
  450. var graphNode = {
  451. id: nodeId,
  452. name: nodeId,
  453. group: objectName,
  454. _loaded: true,
  455. typeName: params.typeName,
  456. primaryKey: params.primaryKey // TODO: _primaryKey
  457. }
  458. if (params.row) graphNode.row = params.row
  459. return graphAddNodeLabel(graphNode)
  460. }
  461. function dataMakeEdge(parentNodeId, nodeObject) {
  462. return {
  463. id: parentNodeId + nodeObject.id,
  464. source: parentNodeId,
  465. target: nodeObject.id,
  466. }
  467. }
  468. function dataMakeXlinkNode(params) {
  469. var objectName = params.typeName.substr(params.typeName.indexOf(':') + 1)
  470. var nodeId = params.xlink.substr(params.xlink.indexOf('#') + 1)
  471. var primaryKey = nodeId.substr(nodeId.lastIndexOf('.') + 1)
  472. var graphNode = {
  473. id: nodeId,
  474. name: nodeId,
  475. group: objectName,
  476. _loaded: false,
  477. typeName: params.typeName,
  478. primaryKey: primaryKey
  479. }
  480. return graphAddNodeLabel(graphNode)
  481. }
  482. function dataMakeFetchMoreNode(params) {
  483. // params = {
  484. // parentNodeId: parentNodeId,
  485. // type: json.type,
  486. // objectName: objectName,
  487. // ref: json,
  488. // }
  489. if(DBG)console.log('DBG dataMakeFetchMoreNode(params)', params)
  490. var nodeId = params.parentNodeId+'fetch-more-features-'+params.type+'-on-' + params.objectName;
  491. return {
  492. id: nodeId,
  493. label: 'Pobierz więcej (+)',
  494. group: 'fetch-more-data', // params.objectName,
  495. _loaded: false,
  496. _type: 'ref',
  497. parentNodeId: params.parentNodeId,
  498. ref: params.ref,
  499. // typeName: typeName,
  500. // primaryKey: nodeId.substr(nodeId.lastIndexOf('.') + 1)
  501. };
  502. }
  503. function dataMakeFetchMoreEdge(parentNodeId, fetchMoreNode) {
  504. // @param parentNodeId - from node id
  505. // @param fetchMoreNode - from dataMakeFetchMoreNode
  506. if(DBG)console.log('DBG dataMakeFetchMoreEdge(parentNodeId, fetchMoreNode)', {parentNodeId:parentNodeId, fetchMoreNode:fetchMoreNode})
  507. return {
  508. id: fetchMoreNode.id,
  509. from: parentNodeId,
  510. to: fetchMoreNode.id
  511. }
  512. }
  513. function makeShortLabel(label) { // TODO: shorter name
  514. // BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object.1234567
  515. if (label.length < 30) return label;
  516. var exLabel = label.split('.')
  517. if (exLabel.length !== 2) return label;
  518. return exLabel[0].substr(0, 24) + '...' + exLabel[0].substr(-10) + '.' + exLabel[1];
  519. }
  520. function graphAddNodeLabel(graphNode) {
  521. // var graphNode = {
  522. // id: nodeId,
  523. // name: nodeId,
  524. // group: objectName,
  525. // _loaded: true,
  526. // typeName: params.typeName,
  527. // primaryKey: params.primaryKey // TODO: _primaryKey
  528. // }
  529. var label = graphNode.id // TODO: get from schema assert @label attribute
  530. label = makeShortLabel(graphNode.id)
  531. switch (graphNode.typeName) {
  532. case 'default_db__x3A__BI_audit_KRS:BI_audit_KRS': {
  533. // <xs:assert test="@default_db__x3A__BI_audit_KRS:label = concat(nazwa, ' ', krs, ' ', S_miejscowosc)" id="I_audit_KRS___d6e76977-1">
  534. if (graphNode.row) label = [graphNode.row.nazwa, graphNode.row.krs, graphNode.row.S_miejscowosc].filter(function (val) { return val; }).join(' ');
  535. label = (label) ? label : makeShortLabel(graphNode.id)
  536. label = label + ' (krs)'
  537. } break;
  538. case 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY': {
  539. // <xs:assert id="_PRACOWNICY___d6e76324-1" test="@label = concat(imiona, ' ', nazwisko, ' ', miejscowosc)"/>
  540. if (graphNode.row) label = [graphNode.row.imiona, graphNode.row.nazwisko, graphNode.row.miejscowosc].filter(function (val) { return val; }).join(' ');
  541. label = (label) ? label : makeShortLabel(graphNode.id)
  542. label = label + ' (pracownicy)'
  543. } break;
  544. case 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI': {
  545. // <xs:assert id="KONTRAHENCI___d6e76526-1" test="@label = concat(substr(Pelna_nazwa_kontrahenta, 0, 20, '\n umów na kwotę ', @sum))"/>
  546. if (graphNode.row) label = [graphNode.row.Pelna_nazwa_kontrahenta].filter(function (val) { return val; }).join(' ');
  547. label = (label) ? label : makeShortLabel(graphNode.id)
  548. label = label + ' (kontrahent)'
  549. } break;
  550. case 'default_db__x3A__BI_audit_CEIDG:BI_audit_CEIDG': {
  551. // <xs:assert test="@label = concat(nazwisko, substring(firma, 1, 20))"/>
  552. if (graphNode.row) label = [graphNode.row.nazwisko, graphNode.row.firma].filter(function (val) { return val; }).join(' ');
  553. label = (label) ? label : makeShortLabel(graphNode.id)
  554. label = label + ' (ceidg)'
  555. } break;
  556. case 'default_db__x3A__BI_audit_MSIG:BI_audit_MSIG': {
  557. // <xs:assert test="@label = substring(nazwa, 1, 20)"/>
  558. if (graphNode.row) label = [graphNode.row.nazwa].filter(function (val) { return val; }).join(' ');
  559. label = (label) ? label : makeShortLabel(graphNode.id)
  560. label = label + ' (msig)'
  561. } break;
  562. }
  563. return Object.assign(graphNode, {
  564. label: label,
  565. })
  566. }
  567. module.exports = {
  568. graphShowHide: graphShowHide,
  569. graphRender: graphRender,
  570. graphRaportRender: graphRaportRender,
  571. }