Bocian.php.graphShowHide.js 23 KB

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