Bocian.php.graphShowHide.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. var globalGraphStore = null;
  8. function graphStore(state, action) {
  9. var prevState = state || {
  10. isLoading: false,
  11. sentRequestId: 0,
  12. receivedRequestId: 0,
  13. items: [],
  14. };
  15. DBG && console.log('DBG: graph store', { prevState, action });
  16. switch (action.type) {
  17. case 'SET_SENT_REQUEST_ID': return Object.assign(prevState, {
  18. sentRequestId: action.sentRequestId,
  19. isLoading: true
  20. });
  21. case 'SET_RESPONSE': return Object.assign(prevState, {
  22. receivedRequestId: action.requestId,
  23. items: action.items,
  24. isLoading: (prevState.sentRequestId > action.requestId) ? true : false,
  25. });
  26. default: return prevState;
  27. }
  28. }
  29. var createGraphActions = function () {
  30. var setSentRequestId = function (sentRequestId) {
  31. return { type: 'SET_SENT_REQUEST_ID', sentRequestId: sentRequestId };
  32. }
  33. var setResponse = function (obj) {
  34. return Object.assign(obj, { type: 'SET_RESPONSE' });
  35. }
  36. var fetchData = function (nameSection) {
  37. return function (dispatch, getState) {
  38. var state = getState();
  39. var this__requestId = state.sentRequestId + 1;
  40. dispatch(setSentRequestId(this__requestId));
  41. var reqPromise = graphFetchData(nameSection);
  42. return reqPromise.then(function (response) {
  43. var items = response;
  44. var state = getState();
  45. if (state.receivedRequestId > this__requestId) {
  46. DBG && console.log('DBG: skiped response', { 'state.receivedRequestId': state.receivedRequestId, this__requestId });
  47. return;
  48. }
  49. dispatch(
  50. setResponse({
  51. requestId: this__requestId,
  52. msg: "Pobrano dane",
  53. 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',
  54. items: items
  55. })
  56. );
  57. }).catch(function (e) {
  58. p5UI__notifyAjaxCallback({type: 'error', msg: 'Wystąpił błąd #GS1: ' + e});
  59. // dispatch( setErrorMsg(e) ); // TODO: show error with msg and refresh button
  60. });
  61. }
  62. }
  63. return {
  64. fetchData: fetchData,
  65. }
  66. }
  67. var globalGraphActions = ''; // createGraphActions();
  68. var createReactClass = global.p5VendorJs.createReactClass;
  69. var h = global.p5VendorJs.React.createElement;
  70. var ReactDOM = global.p5VendorJs.ReactDOM;
  71. var Redux = global.p5VendorJs.Redux;
  72. var ReduxThunk = global.p5VendorJs.ReduxThunk;
  73. var createStoreWithThunkMiddleware = Redux.applyMiddleware(ReduxThunk)(Redux.createStore); // TODO: to vendor.js
  74. global['P5UI__BocianGraphView'] = createReactClass({
  75. // props.nameSection: PropTypes.string.isRequired
  76. // props.store: Redux store with state: { isLoading: false, sentRequestId: 0, receivedRequestId: 0, items: [] }
  77. // props.actions: Redux store actions { }
  78. getStateFromStore: function () {
  79. var state = this.props.store.getState();
  80. return {
  81. isLoading: state.isLoading,
  82. items: state.items,
  83. sentRequestId: state.sentRequestId,
  84. receivedRequestId: state.receivedRequestId,
  85. };
  86. },
  87. getInitialState: function () {
  88. return this.getStateFromStore();
  89. },
  90. componentDidMount: function () {
  91. DBG && console.log('DBG::P5UI__BocianGraphView::componentDidMount');
  92. this.unsubscribe = this.props.store.subscribe(this.storeUpdated)
  93. },
  94. componentWillUnmount: function () {
  95. this.unsubscribe()
  96. },
  97. storeUpdated: function () {
  98. DBG && console.log('DBG::P5UI__BocianGraphView::storeUpdated');
  99. this.setState(this.getStateFromStore())
  100. },
  101. shouldComponentUpdate: function (nextProps, nextState) {
  102. DBG && console.log('DBG::P5UI__BocianGraphView::shouldComponentUpdate', { state: this.state, nextState});
  103. // return (
  104. // this.state.isLoading !== nextState.isLoading
  105. // || this.state.sentRequestId !== nextState.sentRequestId
  106. // || this.state.receivedRequestId !== nextState.receivedRequestId
  107. // );
  108. if (nextState.receivedRequestId > this.state.receivedRequestId) {
  109. var htmlId = 'p5_graphView_' + this.props.nameSection; // #smad-'+nameSection+'-graph-view
  110. graphRaportRender({
  111. msg: "Pobrano dane",
  112. typeName: ('pracownicy' === this.props.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. items: nextState.items
  114. }, document.getElementById(htmlId))
  115. }
  116. return (
  117. this.state.isLoading !== nextState.isLoading
  118. );
  119. },
  120. render: function () {
  121. DBG && console.log('DBG::P5UI__BocianGraphView::render', { props: this.props, state: this.state });
  122. var htmlId = 'p5_graphView_' + this.props.nameSection; // #smad-'+nameSection+'-graph-view
  123. var msg = (this.state.isLoading) ? "Pobieranie danych..." : null;
  124. return h('div', {}, [
  125. msg && h('div', {
  126. className: 'alert alert-info',
  127. style: {
  128. maxWidth: '600px',
  129. margin: '0 auto',
  130. padding: '3px 24px',
  131. }
  132. }, msg),
  133. h('div', { id: htmlId })
  134. ]);
  135. }
  136. });
  137. var btnRefresh = null;
  138. var btnHide = null;
  139. function graphShowHide(btnNode, nameSection) {
  140. var node = $('#smad-'+nameSection+'-graph-view')
  141. if (!node || !node.length) {
  142. console.log("Missing dom node '#smad-"+nameSection+"-graph-view'")
  143. return;
  144. }
  145. var btnJqNode = jQuery(btnNode);
  146. if ('block' == node.css('display')) {
  147. DBG && console.warn("DBG:graphShowHide: hide");
  148. node.hide()
  149. btnRefresh.remove();
  150. btnHide.remove();
  151. return;
  152. }
  153. node.show()
  154. btnRefresh = jQuery('<button class="btn btn-primary" style="padding:1px 5px">odśwież</button>').appendTo(btnJqNode.parent());
  155. btnRefresh.on('click', function () {
  156. globalGraphStore.dispatch(globalGraphActions.fetchData(nameSection));
  157. })
  158. btnHide = jQuery('<button class="btn btn-primary" style="padding:1px 5px"><i class="glyphicon glyphicon-remove"></i></button>').appendTo(btnJqNode.parent());
  159. btnHide.on('click', function () {
  160. node.hide()
  161. btnRefresh.remove();
  162. btnHide.remove();
  163. })
  164. var graphResultNode = node.get(0)
  165. if (!globalGraphStore) {
  166. globalGraphStore = createStoreWithThunkMiddleware(graphStore);
  167. globalGraphActions = createGraphActions();
  168. ReactDOM.render(
  169. h(P5UI__BocianGraphView, {
  170. nameSection: nameSection,
  171. store: globalGraphStore,
  172. actions: globalGraphActions,
  173. }),
  174. graphResultNode
  175. );
  176. }
  177. globalGraphStore.dispatch(globalGraphActions.fetchData(nameSection));
  178. }
  179. function graphFetchData(nameSection) { // @return Promise
  180. var page = page || getItemLocalStorage('Bocian.biAuditForm.'+nameSection+'.pagination.page');
  181. if ( page === 1) {
  182. setItemLocalStorage('Bocian.biAuditForm.'+nameSection+'.pagination.page', 1);
  183. }
  184. var filterIdGroup = getItemLocalStorage('Bocian.biAuditForm.'+nameSection+'.filterIdGroup');
  185. var filterIdRaport = getItemLocalStorage('Bocian.biAuditForm.'+nameSection+'.filterIdRaport');
  186. var frm = document.getElementById('filtersFieldRemoveBtn-' + nameSection.toUpperCase()).form
  187. var fieldNameList = ('pracownicy' === nameSection) ? FIELD_LIST_PRACOWNICY : FIELD_LIST_KONTRAHENCI
  188. var filterFields = fieldNameList.filter(function (fieldName) {
  189. if (!frm[fieldName] && DBG) console.log('Err missing field: "'+fieldName+'"')
  190. return (frm[fieldName]) ? true : false
  191. }).map(function (fieldName) {
  192. return [ fieldName, frm[fieldName].value ]
  193. }).filter(function (filter) {
  194. return ( filter[1].length > 0 )
  195. })
  196. // filterFields = (filterFields.length > 0) ? '&' + filterFields : ''
  197. // <ogc:Filter>
  198. // <ogc:Or>
  199. // <ogc:PropertyIsLike wildCard="*" singleChar="%23" escapeChar="!">
  200. // <ogc:PropertyName>A_STATUS</ogc:PropertyName>
  201. // <ogc:Literal>*O%23MA*</ogc:Literal>
  202. // </ogc:PropertyIsLike>
  203. // <ogc:PropertyIsLike wildCard="*" singleChar="%23" escapeChar="!">
  204. // <ogc:PropertyName>A_STATUS</ogc:PropertyName>
  205. // <ogc:Literal>*ARNING</ogc:Literal>
  206. // </ogc:PropertyIsLike>
  207. // </ogc:Or>
  208. // </ogc:Filter>
  209. var ogcFilterFields = (filterFields.length > 0)
  210. ? '<ogc:Filter><ogc:And>' + filterFields.map(function(filter) {
  211. if ('ID' === filter[0].substr(2)) return '<ogc:PropertyIsEqualTo>' +
  212. '<ogc:PropertyName>' + filter[0].substr(2) + '</ogc:PropertyName>' +
  213. '<ogc:Literal>' + filter[1] + '</ogc:Literal>' +
  214. '</ogc:PropertyIsEqualTo>';
  215. return '<ogc:PropertyIsLike wildCard="*" singleChar="%23" escapeChar="!">' +
  216. '<ogc:PropertyName>' + filter[0].substr(2) + '</ogc:PropertyName>' +
  217. '<ogc:Literal>*' + filter[1] + '*</ogc:Literal>' +
  218. '</ogc:PropertyIsLike>';
  219. }) + '</ogc:And></ogc:Filter>'
  220. : ''
  221. var paginationLimit = 20;
  222. DBG && console.log('graphFetchData...', {
  223. paginationLimit: paginationLimit,
  224. page: page,
  225. filterIdGroup: filterIdGroup,
  226. filterIdRaport: filterIdRaport,
  227. filterFields: filterFields,
  228. ogcFilterFields: ogcFilterFields,
  229. });
  230. function refFieldsOnPathToList(typeName, fields) {
  231. var fields = fields || []
  232. var flatList = []
  233. var refPathBase = [
  234. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
  235. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
  236. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object'
  237. ]
  238. flatList.push(refPathBase.concat(typeName).join('/'))
  239. fields.forEach(function (fieldName) {
  240. flatList.push(refPathBase.concat(typeName, fieldName).join('/'))
  241. })
  242. return flatList;
  243. }
  244. 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',
  245. Object.assign({
  246. sortBy: 'ID+D',
  247. maxFeatures: paginationLimit,
  248. startIndex: (page - 1) * paginationLimit,
  249. // TODO: backRefNS, backRefPK, backRefField - TODO: from groups
  250. // resolve: 'all',
  251. // resolveDepth: 2
  252. // 'ogc:Filter': '<wfs:Query>' + '\n' + [
  253. // 'ID',
  254. // 'imiona',
  255. // 'nazwisko',
  256. // 'miejscowosc'
  257. // ]
  258. 'ogc:Filter': '<wfs:Query>' + '\n' + [].concat(
  259. ('pracownicy' === nameSection)
  260. ? [
  261. 'ID',
  262. 'imiona',
  263. 'nazwisko',
  264. 'miejscowosc'
  265. ] : [
  266. 'ID',
  267. 'Pelna_nazwa_kontrahenta'
  268. ]
  269. )
  270. .concat(refFieldsOnPathToList('default_db__x3A__BI_audit_KRS:BI_audit_KRS', [ 'ID', 'nazwa', 'krs', 'S_miejscowosc' ]))
  271. .concat(refFieldsOnPathToList('default_db__x3A__BI_audit_MSIG:BI_audit_MSIG', [ 'ID', 'nazwa' ]))
  272. .concat(refFieldsOnPathToList('default_db__x3A__BI_audit_CEIDG:BI_audit_CEIDG', [ 'ID', 'nazwisko', 'firma' ]))
  273. .concat(refFieldsOnPathToList('default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI', [ 'ID', 'Pelna_nazwa_kontrahenta' ]))
  274. .concat(
  275. ('pracownicy' === nameSection)
  276. ? []
  277. : refFieldsOnPathToList('default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY', [ 'ID', 'imiona', 'nazwisko', 'miejscowosc' ])
  278. )
  279. .concat([
  280. // '*',
  281. // 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY_adresy:BI_audit_ENERGA_PRACOWNICY_adresy/*',
  282. [
  283. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
  284. 'ID'
  285. ].join('/'),
  286. [
  287. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
  288. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
  289. 'ID'
  290. ].join('/'),
  291. [
  292. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
  293. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
  294. 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
  295. 'ID'
  296. ].join('/')
  297. ]).map(function (fieldName) {
  298. return '<wfs:PropertyName>' + fieldName + '</wfs:PropertyName>';
  299. }).join('\n') + '\n' +
  300. ( ogcFilterFields ? ogcFilterFields : '' ) +
  301. '</wfs:Query>'
  302. }, ('pracownicy' === nameSection && filterIdRaport > 0)
  303. ? {
  304. backRefNS: 'default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA',
  305. backRefPK: filterIdRaport,
  306. backRefField: 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY',
  307. }
  308. : {}
  309. , ('kontrahenci' === nameSection && filterIdRaport > 0)
  310. ? {
  311. backRefNS: 'default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA',
  312. backRefPK: filterIdRaport,
  313. backRefField: 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI',
  314. }
  315. : {}
  316. , ('pracownicy' === nameSection && filterIdGroup > 0)
  317. ? {
  318. backRefNS: 'default_db/BI_audit_ENERGA_PRACOWNICY_group/BI_audit_ENERGA_PRACOWNICY_group',
  319. backRefPK: filterIdGroup,
  320. backRefField: 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY',
  321. }
  322. : {}
  323. , ('kontrahenci' === nameSection && filterIdGroup > 0)
  324. ? {
  325. backRefNS: 'default_db/BI_audit_ENERGA_PRACOWNICY_group/BI_audit_ENERGA_PRACOWNICY_group',
  326. backRefPK: filterIdGroup,
  327. backRefField: 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI',
  328. }
  329. : {}
  330. )
  331. );
  332. }
  333. function graphRender(props, wrapNode) {
  334. throw "TODO: graphRender..."
  335. }
  336. function graphRaportRender(props, wrapNode) {
  337. var svgNode = jQuery(wrapNode).children('svg')
  338. svgNode = (svgNode.length) ? d3.select(svgNode.get(0)) : d3.select(wrapNode).append("svg")
  339. // svg.append("rect").attr("x",0).attr("y",0).attr("width","100%").attr("height","100%").attr("fill","white").attr('class','background').on('mouseup', () => redraw())
  340. // svgNode.attr("width", '100%')
  341. // .attr("height", '300px')
  342. // .append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  343. // var data = parseGraphRec(props.items, props.nameSection)
  344. var _todoGraphData = [];
  345. parseResponseRec(_todoGraphData, props.items, props.typeName)
  346. DBG && console.log('_todoGraphData', _todoGraphData)
  347. var _nodes = [];
  348. var _links = [];
  349. var mapNodeIdToIdx = {};
  350. _todoGraphData.forEach(function (levelData, levelIdx) {
  351. if (levelData.nodes && levelData.nodes.length) {
  352. levelData.nodes.forEach(function (node) {
  353. try {
  354. if (node.id in mapNodeIdToIdx) return;
  355. _nodes.push(node) // _nodes.add(node)
  356. mapNodeIdToIdx[node.id] = _nodes.length - 1
  357. } catch (e) {
  358. DBG && console.log('_graphData.nodes.add [level='+levelIdx+'] error:', e);
  359. }
  360. })
  361. }
  362. })
  363. var linkIdsAdded = []
  364. _todoGraphData.forEach(function (levelData, levelIdx) {
  365. if (levelData.edges && levelData.edges.length) {
  366. levelData.edges.forEach(function (edge) {
  367. DBG && console.log('_graphData.edges.add [level='+levelIdx+']:', {edge, source:mapNodeIdToIdx[edge.source], target:mapNodeIdToIdx[edge.target], mapNodeIdToIdx});
  368. // try {
  369. // var source = mapNodeIdToIdx[edge.source]
  370. // var target = mapNodeIdToIdx[edge.target]
  371. // if (!source || !target) { // && !== 0
  372. // console.warn('(!source || !target)', {source, target, edge, mapNodeIdToIdx})
  373. // }
  374. if (-1 !== linkIdsAdded.indexOf(edge.id)) return
  375. // _links.push({
  376. // id: edge.id,
  377. // source: mapNodeIdToIdx[edge.source],
  378. // target: mapNodeIdToIdx[edge.target],
  379. // value: 1
  380. // })
  381. _links.push({
  382. id: edge.id,
  383. source: edge.source,
  384. target: edge.target,
  385. value: 1
  386. }) // _edges.add(edge)
  387. linkIdsAdded.push(edge.id)
  388. // } catch (e) {
  389. // DBG && console.log('_graphData.edges.add [level='+levelIdx+'] error:', e);
  390. // }
  391. })
  392. }
  393. })
  394. var totalLinks = _links.length
  395. // console.log('DBG:0: _links', _links)
  396. DBG && console.log('DBG:1: _nodes', _nodes)
  397. _nodes = _nodes.filter(function (node, idx) {
  398. if (node.typeName !== props.typeName) return true
  399. for (var i=0; i<totalLinks; i++) {
  400. var link = _links[i]
  401. if (_links[i].source === node.id) return true
  402. if (_links[i].target === node.id) return true
  403. }
  404. return false
  405. })
  406. // console.log('DBG:2: _nodes', _nodes)
  407. jQuery(wrapNode).find('p').remove()
  408. if (!_nodes || !_nodes.length) {
  409. jQuery(wrapNode).find('svg').remove()
  410. jQuery(wrapNode).append('<p>Brak powiązań</p>')
  411. return;
  412. }
  413. var rightSideNodes = _nodes.filter(function (node, idx) {
  414. for (var i=0; i<totalLinks; i++) {
  415. var link = _links[i]
  416. // if (_links[i].source === node.id) return true
  417. if (_links[i].target === node.id) return true
  418. }
  419. return false
  420. })
  421. DBG && console.log('DBG: rightSideNodes (total:'+rightSideNodes.length+') ', rightSideNodes)
  422. if (rightSideNodes.length > 20) {
  423. // TODO: increase height
  424. }
  425. var graphData = {
  426. nodes: _nodes,
  427. links: _links.filter(function (link) {
  428. DBG && console.log('DBG loop link', { link, source: link.source, target: link.target, filtered: (link.source && link.target) });
  429. return (link.source && link.target);
  430. })
  431. };
  432. DBG && console.warn('render graphData', graphData);
  433. var graf = renderGraph(svgNode, graphData, {
  434. width: jQuery(wrapNode).width(),
  435. height: (rightSideNodes.length > 20) ? rightSideNodes.length * 22 : 500
  436. })
  437. graf.on('click', (event) => {
  438. DBG && console.log('event', event)
  439. // event = {
  440. // nativeEvent: d3.event,
  441. // node: a,
  442. // element: d3.event.srcElement
  443. // }
  444. // TODO: !a.expanded && redraw({type: a.name, criteria: 'ID'})
  445. })
  446. }
  447. /**
  448. * Response format:
  449. * NOTE: every object could be xlink if found before
  450. * lvl1 > lvl2 > lvl3 > lvl4 > lvl5
  451. * Pracownik > *_row_object > *_row > *_row_object > Podmiot
  452. * Pracownik > *_row_object > *_row > *_row_object > ( KRS | CEIDG | ... )
  453. * Pracownik > *_row_object > *_row > *_row_object > Pracownik
  454. *
  455. * Podmiot > *_row_object > *_row > *_row_object > Pracownik
  456. * Podmiot > *_row_object > *_row > *_row_object > ( KRS | CEIDG | ... )
  457. * Podmiot > *_row_object > *_row > *_row_object > Podmiot
  458. *
  459. * TODO: make connections only: Pracownik -> ( KRS | CEIDG | ... )* -> Podmiot
  460. * NOTE: lvl4 last `_row_object` is (wfs xlink cache system) xlink to lvl2 `_row_object` - ref to lvl1 object
  461. * NOTE: lvl3 is always 1 because it is from __backRef
  462. */
  463. var TN_PRACOWNICY = 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY';
  464. var TN_KONTRAHENCI = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI';
  465. var TN_ROW_OBJ = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object';
  466. var TN_ROW = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row';
  467. var TN_KRS = 'default_db__x3A__BI_audit_KRS:BI_audit_KRS';
  468. var TN_CEIDG = 'default_db__x3A__BI_audit_CEIDG:BI_audit_CEIDG';
  469. var TN_MSIG = 'default_db__x3A__BI_audit_MSIG:BI_audit_MSIG';
  470. function parseResponseRec__Helper__addTodoNode(_todoGraphData, level, json, typeName) { // @return string - node id
  471. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  472. var nodeObject = dataMakeNode({
  473. typeName: typeName,
  474. primaryKey: (json['ID']) ? json['ID'] : null, // TODO: get primaryKey from object
  475. row: json,
  476. })
  477. _todoGraphData[level].nodes.push(nodeObject)
  478. // if (parentNodeId) {
  479. // _todoGraphData[level].edges.push(dataMakeEdge(parentNodeId, nodeObject))
  480. // }
  481. return nodeObject.id;
  482. }
  483. function parseResponseRec__Helper__addTodoEdge(_todoGraphData, level, parentNodeId, newNodeId) {
  484. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  485. _todoGraphData[level].edges.push(
  486. // dataMakeEdge(parentNodeId, nodeObject)
  487. {
  488. id: parentNodeId + newNodeId,
  489. source: parentNodeId,
  490. target: newNodeId,
  491. }
  492. )
  493. }
  494. function parseResponseRec(_todoGraphData, json, typeName) {
  495. DBG = 1;
  496. var parentNodeId = null
  497. DBG && console.log('DBG::parseResponseRec', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
  498. switch (typeName) {
  499. case TN_PRACOWNICY: parseResponseRec__lvl1__Pracownik(_todoGraphData, json, typeName); break;
  500. case TN_KONTRAHENCI: parseResponseRec__lvl1__Kontrahent(_todoGraphData, json, typeName); break;
  501. default: throw "Not implemented root element typeName '" + typeName + "'";
  502. }
  503. }
  504. function parseResponseRec__lvl1__Pracownik(_todoGraphData, jsonList, typeName) {
  505. if (!p5Utils__isArray(jsonList)) throw "Not implemented root element - expected list of elements";
  506. var isXlinkList = (jsonList.length > 0 && p5Utils__isString(jsonList[0]))
  507. DBG && console.log('DBG::parseResponseRec__lvl1__Pracownik', {jsonList:jsonList, typeName:typeName, isString: p5Utils__isString(jsonList), isArray: p5Utils__isArray(jsonList), isObject: p5Utils__isObject(jsonList), isXlinkList});
  508. jsonList.forEach(function (jsonPracownik) {
  509. if (isXlinkList) {
  510. DBG && console.warn('DBG::parseResponseRec__lvl1__Pracownik', {jsonPracownik:jsonPracownik, typeName:typeName, isString: p5Utils__isString(jsonPracownik), isArray: p5Utils__isArray(jsonPracownik), isObject: p5Utils__isObject(jsonPracownik), isXlinkList});
  511. // parseResponseXlinkListRec(_todoGraphData, jsonPracownik, typeName)
  512. throw "Not implemented xlink in Pracownik child";
  513. } else {
  514. var idNode = parseResponseRec__Helper__addTodoNode(_todoGraphData, level = 1, jsonPracownik, typeName);
  515. if (jsonPracownik[TN_ROW_OBJ] && jsonPracownik[TN_ROW_OBJ].length) {
  516. parseResponseRec__lvl2__Pracownik_RowObj(_todoGraphData, idNode, jsonPracownik[TN_ROW_OBJ]);
  517. }
  518. }
  519. })
  520. }
  521. function parseResponseRec__lvl2__Pracownik_RowObj(_todoGraphData, parentIdNode, rowObjList) {
  522. DBG && console.log('DBG::parseResponseRec__lvl2__Pracownik_RowObj rec...', { parentIdNode, rowObjList });
  523. rowObjList.forEach(function (jsonRowObj) {
  524. if (!p5Utils__isObject(jsonRowObj)) {
  525. DBG && console.warn('DBG::parseResponseRec__lvl2__Pracownik_RowObj SKIP non object element', { jsonRowObj });
  526. // skip non object elements
  527. return;
  528. }
  529. if (isP5LinkObject(jsonRowObj)) {
  530. DBG && console.log('DBG::parseResponseRec__lvl2__Pracownik_RowObj SKIP xlink', { jsonRowObj });
  531. // skip p5 link object (type: "next")
  532. return;
  533. }
  534. DBG && console.log('DBG::parseResponseRec__lvl2__Pracownik_RowObj row obj', { jsonRowObj });
  535. if (jsonRowObj[TN_ROW] && jsonRowObj[TN_ROW].length) {
  536. // NOTE: jsonRowObj[TN_ROW] is array(1) - always 1 because it is from __backRef
  537. parseResponseRec__lvl3__Pracownik_RowObj_Row(_todoGraphData, parentIdNode, jsonRowObj[TN_ROW][0]);
  538. }
  539. })
  540. }
  541. function parseResponseRec__lvl3__Pracownik_RowObj_Row(_todoGraphData, parentIdNode, jsonRow) {
  542. DBG && console.log('DBG::parseResponseRec__lvl3__Pracownik_RowObj_Row', { jsonRow, parentIdNode });
  543. if (jsonRow[TN_ROW_OBJ] && jsonRow[TN_ROW_OBJ].length) {
  544. var jsonListRowObj = jsonRow[TN_ROW_OBJ];
  545. DBG && console.log('DBG::parseResponseRec__lvl3__Pracownik_RowObj_Row row obj', { jsonListRowObj, parentIdNode });
  546. // NOTE: RowObj on list is a connection path in reverse order
  547. // NOTE: last element is Pracownik (parentIdNode)
  548. // NOTE: first element is Kontrahent - TODO: or another element defines in Raport.od.* or Raport.do.*
  549. // var TN_PRACOWNICY = 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY';
  550. // var TN_KONTRAHENCI = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI';
  551. // var TN_ROW_OBJ = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object';
  552. // var TN_ROW = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row';
  553. // var TN_KRS = 'default_db__x3A__BI_audit_KRS:BI_audit_KRS';
  554. // var TN_CEIDG = 'default_db__x3A__BI_audit_CEIDG:BI_audit_CEIDG';
  555. // var TN_MSIG = 'default_db__x3A__BI_audit_MSIG:BI_audit_MSIG';
  556. var pathPoints = jsonListRowObj.reverse().slice(1).map(function (jsonRowObj) {
  557. if (jsonRowObj[TN_KONTRAHENCI]) return Object.assign(jsonRowObj[TN_KONTRAHENCI][0], { typeName: TN_KONTRAHENCI });
  558. if (jsonRowObj[TN_PRACOWNICY]) return Object.assign(jsonRowObj[TN_PRACOWNICY][0], { typeName: TN_PRACOWNICY });
  559. if (jsonRowObj[TN_KRS]) return Object.assign(jsonRowObj[TN_KRS][0], { typeName: TN_KRS });
  560. if (jsonRowObj[TN_CEIDG]) return Object.assign(jsonRowObj[TN_CEIDG][0], { typeName: TN_CEIDG });
  561. if (jsonRowObj[TN_MSIG]) return Object.assign(jsonRowObj[TN_MSIG][0], { typeName: TN_MSIG });
  562. return null;
  563. }).filter(function (jsonRowObj) {
  564. return !!jsonRowObj;
  565. }).map(function (jsonRowObj) {
  566. DBG && console.warn('DBG::parseResponseRec__lvl3__Pracownik_RowObj_Row path', { parentIdNode, fid: (jsonRowObj ? jsonRowObj.typeName + '.' + jsonRowObj.ID : null), jsonRowObj });
  567. return jsonRowObj;
  568. })
  569. for (var i = 0, prevNodeId = parentIdNode, newNodeId; i < pathPoints.length; i++) {
  570. jsonObject = pathPoints[i];
  571. newNodeId = parseResponseRec__Helper__addTodoNode(_todoGraphData, level = 4, jsonObject, jsonObject.typeName);
  572. parseResponseRec__Helper__addTodoEdge(_todoGraphData, level = 4, prevNodeId, newNodeId);
  573. prevNodeId = newNodeId;
  574. }
  575. }
  576. }
  577. function parseResponseRec__lvl1__Kontrahent(_todoGraphData, jsonList, typeName) {
  578. if (!p5Utils__isArray(jsonList)) throw "Not implemented root element - expected list of elements";
  579. var isXlinkList = (jsonList.length > 0 && p5Utils__isString(jsonList[0]))
  580. DBG && console.log('DBG::parseResponseRec__lvl1__Kontrahent', {jsonList:jsonList, typeName:typeName, isString: p5Utils__isString(jsonList), isArray: p5Utils__isArray(jsonList), isObject: p5Utils__isObject(jsonList), isXlinkList});
  581. jsonList.forEach(function (jsonPracownik) {
  582. if (isXlinkList) {
  583. DBG && console.warn('DBG::parseResponseRec__lvl1__Kontrahent', {jsonPracownik:jsonPracownik, typeName:typeName, isString: p5Utils__isString(jsonPracownik), isArray: p5Utils__isArray(jsonPracownik), isObject: p5Utils__isObject(jsonPracownik), isXlinkList});
  584. // parseResponseXlinkListRec(_todoGraphData, jsonPracownik, typeName)
  585. throw "Not implemented xlink in Pracownik child";
  586. } else {
  587. var idNode = parseResponseRec__Helper__addTodoNode(_todoGraphData, level = 1, jsonPracownik, typeName);
  588. if (jsonPracownik[TN_ROW_OBJ] && jsonPracownik[TN_ROW_OBJ].length) {
  589. parseResponseRec__lvl2__Kontrahent_RowObj(_todoGraphData, idNode, jsonPracownik[TN_ROW_OBJ]);
  590. }
  591. }
  592. })
  593. }
  594. function parseResponseRec__lvl2__Kontrahent_RowObj(_todoGraphData, parentIdNode, rowObjList) {
  595. DBG && console.log('DBG::parseResponseRec__lvl2__Kontrahent_RowObj rec...', { parentIdNode, rowObjList });
  596. rowObjList.forEach(function (jsonRowObj) {
  597. if (!p5Utils__isObject(jsonRowObj)) {
  598. DBG && console.warn('DBG::parseResponseRec__lvl2__Kontrahent_RowObj SKIP non object element', { jsonRowObj });
  599. // skip non object elements
  600. return;
  601. }
  602. if (isP5LinkObject(jsonRowObj)) {
  603. DBG && console.log('DBG::parseResponseRec__lvl2__Kontrahent_RowObj SKIP xlink', { jsonRowObj });
  604. // skip p5 link object (type: "next")
  605. return;
  606. }
  607. DBG && console.log('DBG::parseResponseRec__lvl2__Kontrahent_RowObj row obj', { jsonRowObj });
  608. if (jsonRowObj[TN_ROW] && jsonRowObj[TN_ROW].length) {
  609. // NOTE: jsonRowObj[TN_ROW] is array(1) - always 1 because it is from __backRef
  610. parseResponseRec__lvl3__Kontrahent_RowObj_Row(_todoGraphData, parentIdNode, jsonRowObj[TN_ROW][0]);
  611. }
  612. })
  613. }
  614. function parseResponseRec__lvl3__Kontrahent_RowObj_Row(_todoGraphData, parentIdNode, jsonRow) {
  615. DBG && console.log('DBG::parseResponseRec__lvl3__Pracownik_RowObj_Row', { jsonRow, parentIdNode });
  616. if (jsonRow[TN_ROW_OBJ] && jsonRow[TN_ROW_OBJ].length) {
  617. var jsonListRowObj = jsonRow[TN_ROW_OBJ];
  618. DBG && console.log('DBG::parseResponseRec__lvl3__Kontrahent_RowObj_Row row obj', { jsonListRowObj, parentIdNode });
  619. // NOTE: RowObj on list is a connection path in reverse order
  620. // NOTE: last element is Pracownik (parentIdNode)
  621. // NOTE: first element is Kontrahent - TODO: or another element defines in Raport.od.* or Raport.do.*
  622. // var TN_PRACOWNICY = 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY';
  623. // var TN_KONTRAHENCI = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI';
  624. // var TN_ROW_OBJ = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object';
  625. // var TN_ROW = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row';
  626. // var TN_KRS = 'default_db__x3A__BI_audit_KRS:BI_audit_KRS';
  627. // var TN_CEIDG = 'default_db__x3A__BI_audit_CEIDG:BI_audit_CEIDG';
  628. // var TN_MSIG = 'default_db__x3A__BI_audit_MSIG:BI_audit_MSIG';
  629. var pathPoints = jsonListRowObj.slice(1).reverse().map(function (jsonRowObj) {
  630. if (jsonRowObj[TN_KONTRAHENCI]) return Object.assign(jsonRowObj[TN_KONTRAHENCI][0], { typeName: TN_KONTRAHENCI });
  631. if (jsonRowObj[TN_PRACOWNICY]) return Object.assign(jsonRowObj[TN_PRACOWNICY][0], { typeName: TN_PRACOWNICY });
  632. if (jsonRowObj[TN_KRS]) return Object.assign(jsonRowObj[TN_KRS][0], { typeName: TN_KRS });
  633. if (jsonRowObj[TN_CEIDG]) return Object.assign(jsonRowObj[TN_CEIDG][0], { typeName: TN_CEIDG });
  634. if (jsonRowObj[TN_MSIG]) return Object.assign(jsonRowObj[TN_MSIG][0], { typeName: TN_MSIG });
  635. return null;
  636. }).filter(function (jsonRowObj) {
  637. return !!jsonRowObj;
  638. }).map(function (jsonRowObj) {
  639. DBG && console.warn('DBG::parseResponseRec__lvl3__Kontrahent_RowObj_Row path', { parentIdNode, fid: (jsonRowObj ? jsonRowObj.typeName + '.' + jsonRowObj.ID : null), jsonRowObj });
  640. return jsonRowObj;
  641. })
  642. for (var i = 0, prevNodeId, newNodeId; i < pathPoints.length; i++) {
  643. jsonObject = pathPoints[i];
  644. newNodeId = parseResponseRec__Helper__addTodoNode(_todoGraphData, level = 4, jsonObject, jsonObject.typeName);
  645. if (prevNodeId) parseResponseRec__Helper__addTodoEdge(_todoGraphData, level = 4, prevNodeId, newNodeId);
  646. prevNodeId = newNodeId;
  647. }
  648. {
  649. parseResponseRec__Helper__addTodoEdge(_todoGraphData, level = 4, prevNodeId, parentIdNode);
  650. }
  651. }
  652. }
  653. function parseResponseRec__OLD(_todoGraphData, json, typeName, parentNodeId, level) {
  654. var level = level || 0
  655. var parentNodeId = parentNodeId || null
  656. DBG && console.log('DBG::parseResponseRec', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
  657. if (p5Utils__isArray(json)) {
  658. // TODO: create named group
  659. var isXlinkList = (json.length > 0 && p5Utils__isString(json[0]))
  660. json.forEach(function (subJson) {
  661. if (isXlinkList) {
  662. parseResponseXlinkListRec(_todoGraphData, subJson, typeName, parentNodeId, level)
  663. } else {
  664. parseResponseRec(_todoGraphData, subJson, typeName, parentNodeId, level)
  665. }
  666. })
  667. } else if (p5Utils__isObject(json) && isP5LinkObject(json)) {
  668. DBG && console.log('DBG::parseResponseRec isP5LinkObject');
  669. parseResponseP5Link(_todoGraphData, json, typeName, parentNodeId, level)
  670. } else if (p5Utils__isObject(json)) {
  671. // _todoGraphData.nodes.add({ id: nodeId, label: nodeId })
  672. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  673. var nodeObject = dataMakeNode({
  674. typeName: typeName,
  675. primaryKey: (json['ID']) ? json['ID'] : null, // TODO: get primaryKey from object
  676. row: json,
  677. })
  678. var nodeId = nodeObject.id
  679. if ("default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row" === typeName) {
  680. parseResponseRec__row(_todoGraphData, json, typeName, parentNodeId, level)
  681. }
  682. else if ("default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object" === typeName) {
  683. // parseResponseRec__row_object(_todoGraphData, json, typeName, parentNodeId, level)
  684. Object.keys(json).filter(function (fieldName) {
  685. return (fieldName.indexOf(':') > -1)
  686. })
  687. .forEach(function (fieldName) {
  688. var value = json[fieldName]
  689. parseResponseRec(_todoGraphData, value, fieldName, parentNodeId, level + 1)
  690. })
  691. }
  692. else {
  693. DBG && console.warn('DBG::parseResponseRec: obj node.push', { id: nodeObject.id, parentNodeId, typeName, nodeObject });
  694. _todoGraphData[level].nodes.push(nodeObject)
  695. if (parentNodeId) {
  696. _todoGraphData[level].edges.push(dataMakeEdge(parentNodeId, nodeObject))
  697. }
  698. Object.keys(json).filter(function (fieldName) {
  699. return (fieldName.indexOf(':') > -1)
  700. })
  701. .forEach(function (fieldName) {
  702. var value = json[fieldName]
  703. parseResponseRec(_todoGraphData, value, fieldName, nodeId, level + 1)
  704. })
  705. }
  706. } else if (p5Utils__isString(json)) {
  707. DBG && console.log('TODO: Not implemented - parseResponseRec isString');
  708. } else {
  709. DBG && console.log('TODO: Not implemented - parseResponseRec is not string, not array and not object');
  710. }
  711. }
  712. function parseResponseXlinkListRec(_todoGraphData, json, typeName, parentNodeId, level) {
  713. DBG && console.log('DBG::parseResponseRec:XlinkList', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
  714. if (p5Utils__isString(json)) { // xlink "https://biuro.biall-net.pl/wfs/default_db/BI_audit_ENERGA_RUM_KONTRAHENCI#BI_audit_ENERGA_RUM_KONTRAHENCI.9233",
  715. if ("default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object" === typeName) return;
  716. if ("default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row" === typeName) return;
  717. var nodeId = json.substr(json.indexOf('#') + 1)
  718. {
  719. // _graphData.nodes.add({ id: nodeId, label: nodeId })
  720. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  721. var nodeObject = dataMakeXlinkNode({
  722. xlink: json,
  723. typeName: typeName,
  724. })
  725. DBG && console.warn('DBG::parseResponseRec: xlink node.push', { id: nodeObject.id, parentNodeId, typeName, nodeObject });
  726. _todoGraphData[level].nodes.push(nodeObject)
  727. if (parentNodeId) {
  728. _todoGraphData[level].edges.push(dataMakeEdge(parentNodeId, nodeObject))
  729. }
  730. }
  731. } else if (p5Utils__isObject(json) && isP5LinkObject(json)) {
  732. parseResponseP5Link(_todoGraphData, json, typeName, parentNodeId, level)
  733. } else if (p5Utils__isObject(json)) {
  734. DBG && console.warn('TODO: Not implemented - parseResponseRec:XlinkList is object', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
  735. parseResponseRec(_todoGraphData, json, typeName, parentNodeId, level + 1)
  736. } else {
  737. DBG && console.warn('TODO: Not implemented - parseResponseRec:XlinkList is not string and not object');
  738. }
  739. }
  740. function parseResponseP5Link(_todoGraphData, json, typeName, parentNodeId, level) {
  741. DBG && console.log('parseResponseRec isObject and P5Link - fetch more xlink object');
  742. // example json: { type: "next",
  743. // @backRefNS: "default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA",
  744. // @backRefPK: "42",
  745. // @typeName: "default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_P…ow:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row",
  746. // @startIndex: "10" }
  747. if (!parentNodeId) throw "Missing parentNodeId for ref link object";
  748. var objectName = typeName.substr(typeName.indexOf(':') + 1)
  749. {
  750. // _graphData.nodes.add({ id: nodeId, label: nodeId })
  751. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  752. switch (json.type) {
  753. case 'next': {
  754. // TODO: add fetch next node
  755. // var nodeObject = dataMakeFetchMoreNode({
  756. // parentNodeId: parentNodeId,
  757. // type: json.type,
  758. // objectName: objectName,
  759. // ref: json,
  760. // })
  761. // _todoGraphData[level].nodes.push(nodeObject)
  762. // _todoGraphData[level].edges.push(dataMakeFetchMoreEdge(parentNodeId, nodeObject))
  763. } break;
  764. default: {
  765. DBG && console.log('TODO: Not implemented - parseResponseRec isObject with type "'+json.type+'" - fetch more xlink object');
  766. }
  767. }
  768. }
  769. }
  770. function parseResponseRec__row(_todoGraphData, json, typeName, parentNodeId, level) {
  771. var nodeObject = dataMakeNode({
  772. typeName: typeName,
  773. primaryKey: (json['ID']) ? json['ID'] : null, // TODO: get primaryKey from object
  774. })
  775. var nodeId = nodeObject.id
  776. // 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
  777. // 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
  778. // 'default_db__x3A__BI_audit_KRS:BI_audit_KRS' --> Kontrahenci
  779. var rowObjectChildrens = []
  780. var rowTN = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row'
  781. var rowObjTN = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object'
  782. // { row: row_obj: [ { ID, kontr: [ "http...kontr.ID" ] }, { ID, kontr: [ "http...krs.ID" ] } ], ... }
  783. var rowJson = json
  784. if (rowJson[rowObjTN] && rowJson[rowObjTN].length > 1) {
  785. var pathItems = []
  786. rowJson[rowObjTN].forEach(function (rowObjJson) {
  787. Object.keys(rowObjJson)
  788. .filter(function (rowObjFld) { return (-1 !== rowObjFld.indexOf(':')); })
  789. .filter(function (rowObjRefFld) { return (rowObjJson[rowObjRefFld] && rowObjJson[rowObjRefFld].length); })
  790. .forEach(function (rowObjRefFld) {
  791. pathItems.push([ rowObjRefFld, rowObjJson[rowObjRefFld][0] ])
  792. })
  793. })
  794. }
  795. // console.log('node('+nodeId+') pathItems:', pathItems);
  796. var lastParentFid = null
  797. pathItems.reverse().forEach(function (pathItem) {
  798. var nodeObject = ('string' === typeof pathItem[1])
  799. ? dataMakeXlinkNode({
  800. xlink: pathItem[1],
  801. typeName: pathItem[0]
  802. })
  803. : dataMakeNode({
  804. typeName: pathItem[0],
  805. primaryKey: (pathItem[1]['ID']) ? pathItem[1]['ID'] : null,
  806. row: pathItem[1]
  807. })
  808. ;
  809. // _todoGraphData[todoLevel].nodes.push(nodeObject) // already added below
  810. if (lastParentFid) _todoGraphData[level].edges.push(dataMakeEdge(lastParentFid, nodeObject))
  811. lastParentFid = nodeObject.id
  812. })
  813. Object.keys(json).filter(function (fieldName) {
  814. return (fieldName.indexOf(':') > -1)
  815. })
  816. .forEach(function (fieldName) {
  817. var value = json[fieldName]
  818. parseResponseRec(_todoGraphData, value, fieldName, parentNodeId, level + 1)
  819. })
  820. }
  821. function parseGraphRec(items, featureType, parentFeatureId) { // TODO: not used
  822. var parentFeatureId = parentFeatureId || null
  823. var nodesArray = []
  824. var linksArray = []
  825. if (!items) return;
  826. items.forEach(function (item) {
  827. if ('string' === typeof item) {
  828. DBG && console.log('TODO: xlink "'+item+'": ', {item:item, parentFeatureId:parentFeatureId})
  829. return;
  830. }
  831. if (!item['ID']) {
  832. DBG && console.log('TODO: SKIP item('+featureType+') - missing ID: ', {item:item, parentFeatureId:parentFeatureId})
  833. return;
  834. }
  835. var id = item['ID']
  836. console.log('item('+featureType+'): ', {item:item, parentFeatureId:parentFeatureId})
  837. var featureId = featureType + '.' + id
  838. Object.keys(item).filter(function (fieldName) { return ('ID' !== fieldName); })
  839. .forEach(function (fieldName) {
  840. parseGraphRec(item[fieldName], fieldName, featureId)
  841. })
  842. })
  843. // items.forEach(element => {
  844. // const rowObject = hasTargetArray(element);
  845. // if (rowObject)
  846. // rowObject.array.forEach(objectElement => {
  847. // const trackNode = hasTargetArray(objectElement);
  848. // if (trackNode) {
  849. // let group = trackNode.name;
  850. // let expanded = false;
  851. // if (expansion && expansion.type && group === expansion.type) {
  852. // group = trackNode.array[0][expansion.criteria];
  853. // expanded = true;
  854. // }
  855. // nodes[group] = {expanded: expanded, type: trackNode.name};
  856. // instanceTypes[trackNode.array[0].ID] = group;
  857. // }
  858. // });
  859. // });
  860. return {
  861. nodes: nodesArray,
  862. links: linksArray
  863. }
  864. }
  865. function isP5LinkObject(json) {
  866. if ( !('type' in json) || !json['type'] ) return false;
  867. if ( !('value' in json) || !json['value'] ) return false;
  868. if ( !('@typeName' in json) || !json['@typeName'] ) return false;
  869. if ( !('@startIndex' in json) || !json['@startIndex'] ) return false;
  870. if ( !('@backRefPK' in json) || !json['@backRefPK'] ) return false;
  871. if ( !('@backRefNS' in json) || !json['@backRefNS'] ) return false;
  872. return true;
  873. }
  874. function dataMakeNode(params) {
  875. var objectName = params.typeName.substr(params.typeName.indexOf(':') + 1)
  876. var nodeId = objectName + '.' + params.primaryKey // TODO: primaryKey?
  877. var graphNode = {
  878. id: nodeId,
  879. name: nodeId,
  880. group: objectName,
  881. _loaded: true,
  882. typeName: params.typeName,
  883. primaryKey: params.primaryKey // TODO: _primaryKey
  884. }
  885. if (params.row) graphNode.row = params.row
  886. return graphAddNodeLabel(graphNode)
  887. }
  888. function dataMakeEdge(parentNodeId, nodeObject) {
  889. return {
  890. id: parentNodeId + nodeObject.id,
  891. source: parentNodeId,
  892. target: nodeObject.id,
  893. }
  894. }
  895. function dataMakeXlinkNode(params) {
  896. var objectName = params.typeName.substr(params.typeName.indexOf(':') + 1)
  897. var nodeId = params.xlink.substr(params.xlink.indexOf('#') + 1)
  898. var primaryKey = nodeId.substr(nodeId.lastIndexOf('.') + 1)
  899. var graphNode = {
  900. id: nodeId,
  901. name: nodeId,
  902. group: objectName,
  903. _loaded: false,
  904. typeName: params.typeName,
  905. primaryKey: primaryKey
  906. }
  907. return graphAddNodeLabel(graphNode)
  908. }
  909. function dataMakeFetchMoreNode(params) {
  910. // params = {
  911. // parentNodeId: parentNodeId,
  912. // type: json.type,
  913. // objectName: objectName,
  914. // ref: json,
  915. // }
  916. DBG && console.log('DBG dataMakeFetchMoreNode(params)', params)
  917. var nodeId = params.parentNodeId+'fetch-more-features-'+params.type+'-on-' + params.objectName;
  918. return {
  919. id: nodeId,
  920. label: 'Pobierz więcej (+)',
  921. group: 'fetch-more-data', // params.objectName,
  922. _loaded: false,
  923. _type: 'ref',
  924. parentNodeId: params.parentNodeId,
  925. ref: params.ref,
  926. // typeName: typeName,
  927. // primaryKey: nodeId.substr(nodeId.lastIndexOf('.') + 1)
  928. };
  929. }
  930. function dataMakeFetchMoreEdge(parentNodeId, fetchMoreNode) {
  931. // @param parentNodeId - from node id
  932. // @param fetchMoreNode - from dataMakeFetchMoreNode
  933. DBG && console.log('DBG dataMakeFetchMoreEdge(parentNodeId, fetchMoreNode)', {parentNodeId:parentNodeId, fetchMoreNode:fetchMoreNode})
  934. return {
  935. id: fetchMoreNode.id,
  936. from: parentNodeId,
  937. to: fetchMoreNode.id
  938. }
  939. }
  940. function makeShortLabel(label) { // TODO: shorter name
  941. // BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object.1234567
  942. if (label.length < 30) return label;
  943. var exLabel = label.split('.')
  944. if (exLabel.length !== 2) return label;
  945. return exLabel[0].substr(0, 24) + '...' + exLabel[0].substr(-10) + '.' + exLabel[1];
  946. }
  947. function graphAddNodeLabel(graphNode) {
  948. // var graphNode = {
  949. // id: nodeId,
  950. // name: nodeId,
  951. // group: objectName,
  952. // _loaded: true,
  953. // typeName: params.typeName,
  954. // primaryKey: params.primaryKey // TODO: _primaryKey
  955. // }
  956. var label = graphNode.id // TODO: get from schema assert @label attribute
  957. label = makeShortLabel(graphNode.id)
  958. switch (graphNode.typeName) {
  959. case 'default_db__x3A__BI_audit_KRS:BI_audit_KRS': {
  960. // <xs:assert test="@default_db__x3A__BI_audit_KRS:label = concat(nazwa, ' ', krs, ' ', S_miejscowosc)" id="I_audit_KRS___d6e76977-1">
  961. if (graphNode.row) label = [graphNode.row.nazwa, graphNode.row.krs, graphNode.row.S_miejscowosc].filter(function (val) { return val; }).join(' ');
  962. label = (label) ? label : makeShortLabel(graphNode.id)
  963. label = label + ' (krs)'
  964. } break;
  965. case 'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY': {
  966. // <xs:assert id="_PRACOWNICY___d6e76324-1" test="@label = concat(imiona, ' ', nazwisko, ' ', miejscowosc)"/>
  967. if (graphNode.row) label = [graphNode.row.imiona, graphNode.row.nazwisko, graphNode.row.miejscowosc].filter(function (val) { return val; }).join(' ');
  968. label = (label) ? label : makeShortLabel(graphNode.id)
  969. label = label + ' (pracownicy)'
  970. } break;
  971. case 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI': {
  972. // <xs:assert id="KONTRAHENCI___d6e76526-1" test="@label = concat(substr(Pelna_nazwa_kontrahenta, 0, 20, '\n umów na kwotę ', @sum))"/>
  973. if (graphNode.row) label = [graphNode.row.Pelna_nazwa_kontrahenta].filter(function (val) { return val; }).join(' ');
  974. label = (label) ? label : makeShortLabel(graphNode.id)
  975. label = label + ' (kontrahent)'
  976. } break;
  977. case 'default_db__x3A__BI_audit_CEIDG:BI_audit_CEIDG': {
  978. // <xs:assert test="@label = concat(nazwisko, substring(firma, 1, 20))"/>
  979. if (graphNode.row) label = [graphNode.row.nazwisko, graphNode.row.firma].filter(function (val) { return val; }).join(' ');
  980. label = (label) ? label : makeShortLabel(graphNode.id)
  981. label = label + ' (ceidg)'
  982. } break;
  983. case 'default_db__x3A__BI_audit_MSIG:BI_audit_MSIG': {
  984. // <xs:assert test="@label = substring(nazwa, 1, 20)"/>
  985. if (graphNode.row) label = [graphNode.row.nazwa].filter(function (val) { return val; }).join(' ');
  986. label = (label) ? label : makeShortLabel(graphNode.id)
  987. label = label + ' (msig)'
  988. } break;
  989. }
  990. return Object.assign(graphNode, {
  991. label: label,
  992. })
  993. }
  994. module.exports = {
  995. graphShowHide: graphShowHide,
  996. graphRender: graphRender,
  997. graphRaportRender: graphRaportRender,
  998. /** @example: graphRaportRender({
  999. * msg: "Pobrano dane",
  1000. * 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',
  1001. * items: items
  1002. * }, graphResultNode)
  1003. */
  1004. }