sigma.neo4j.cypher.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. ;(function (undefined) {
  2. 'use strict';
  3. if (typeof sigma === 'undefined')
  4. throw 'sigma is not declared';
  5. // Declare neo4j package
  6. sigma.utils.pkg("sigma.neo4j");
  7. // Initialize package:
  8. sigma.utils.pkg('sigma.utils');
  9. /**
  10. * This function is an helper for the neo4j communication.
  11. *
  12. * @param {string|object} neo4j The URL of neo4j server or a neo4j server object.
  13. * @param {string} endpoint Endpoint of the neo4j server
  14. * @param {string} method The calling method for the endpoint : 'GET' or 'POST'
  15. * @param {object|string} data Data that will be send to the server
  16. * @param {function} callback The callback function
  17. */
  18. sigma.neo4j.send = function(neo4j, endpoint, method, data, callback) {
  19. var xhr = sigma.utils.xhr(),
  20. url, user, password;
  21. // if neo4j arg is not an object
  22. url = neo4j;
  23. if(typeof neo4j === 'object') {
  24. url = neo4j.url;
  25. user = neo4j.user;
  26. password = neo4j.password;
  27. }
  28. if (!xhr)
  29. throw 'XMLHttpRequest not supported, cannot load the file.';
  30. // Construct the endpoint url
  31. url += endpoint;
  32. xhr.open(method, url, true);
  33. if( user && password) {
  34. xhr.setRequestHeader('Authorization', 'Basic ' + btoa(user + ':' + password));
  35. }
  36. xhr.setRequestHeader('Accept', 'application/json');
  37. xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
  38. xhr.onreadystatechange = function () {
  39. if (xhr.readyState === 4) {
  40. // Call the callback if specified:
  41. callback(JSON.parse(xhr.responseText));
  42. }
  43. };
  44. xhr.send(data);
  45. };
  46. /**
  47. * This function parse a neo4j cypher query result, and transform it into
  48. * a sigma graph object.
  49. *
  50. * @param {object} result The server response of a cypher query.
  51. *
  52. * @return A graph object
  53. */
  54. sigma.neo4j.cypher_parse = function(result) {
  55. var graph = { nodes: [], edges: [] },
  56. nodesMap = {},
  57. edgesMap = {},
  58. key;
  59. // Iteration on all result data
  60. result.results[0].data.forEach(function (data) {
  61. // iteration on graph for all node
  62. data.graph.nodes.forEach(function (node) {
  63. var sigmaNode = {
  64. id : node.id,
  65. label : node.id,
  66. x : Math.random(),
  67. y : Math.random(),
  68. size : 1,
  69. color : '#000000',
  70. neo4j_labels : node.labels,
  71. neo4j_data : node.properties
  72. };
  73. if (sigmaNode.id in nodesMap) {
  74. // do nothing
  75. } else {
  76. nodesMap[sigmaNode.id] = sigmaNode;
  77. }
  78. });
  79. // iteration on graph for all node
  80. data.graph.relationships.forEach(function (edge) {
  81. var sigmaEdge = {
  82. id : edge.id,
  83. label : edge.type,
  84. source : edge.startNode,
  85. target : edge.endNode,
  86. color : '#7D7C8E',
  87. neo4j_type : edge.type,
  88. neo4j_data : edge.properties
  89. };
  90. if (sigmaEdge.id in edgesMap) {
  91. // do nothing
  92. } else {
  93. edgesMap[sigmaEdge.id] = sigmaEdge;
  94. }
  95. });
  96. });
  97. // construct sigma nodes
  98. for (key in nodesMap) {
  99. graph.nodes.push(nodesMap[key]);
  100. }
  101. // construct sigma nodes
  102. for (key in edgesMap) {
  103. graph.edges.push(edgesMap[key]);
  104. }
  105. return graph;
  106. };
  107. /**
  108. * This function execute a cypher and create a new sigma instance or
  109. * updates the graph of a given instance. It is possible to give a callback
  110. * that will be executed at the end of the process.
  111. *
  112. * @param {object|string} neo4j The URL of neo4j server or a neo4j server object.
  113. * @param {string} cypher The cypher query
  114. * @param {?object|?sigma} sig A sigma configuration object or a sigma instance.
  115. * @param {?function} callback Eventually a callback to execute after
  116. * having parsed the file. It will be called
  117. * with the related sigma instance as
  118. * parameter.
  119. */
  120. sigma.neo4j.cypher = function (neo4j, cypher, sig, callback) {
  121. var endpoint = '/db/data/transaction/commit',
  122. data, cypherCallback;
  123. // Data that will be send to the server
  124. data = JSON.stringify({
  125. "statements": [
  126. {
  127. "statement": cypher,
  128. "resultDataContents": ["graph"],
  129. "includeStats": false
  130. }
  131. ]
  132. });
  133. // Callback method after server response
  134. cypherCallback = function (callback) {
  135. return function (response) {
  136. var graph = { nodes: [], edges: [] };
  137. graph = sigma.neo4j.cypher_parse(response);
  138. // Update the instance's graph:
  139. if (sig instanceof sigma) {
  140. sig.graph.clear();
  141. sig.graph.read(graph);
  142. // ...or instantiate sigma if needed:
  143. } else if (typeof sig === 'object') {
  144. sig = new sigma(sig);
  145. sig.graph.read(graph);
  146. sig.refresh();
  147. // ...or it's finally the callback:
  148. } else if (typeof sig === 'function') {
  149. callback = sig;
  150. sig = null;
  151. }
  152. // Call the callback if specified:
  153. if (callback)
  154. callback(sig || graph);
  155. };
  156. };
  157. // Let's call neo4j
  158. sigma.neo4j.send(neo4j, endpoint, 'POST', data, cypherCallback(callback));
  159. };
  160. /**
  161. * This function call neo4j to get all labels of the graph.
  162. *
  163. * @param {string} neo4j The URL of neo4j server or an object with the url, user & password.
  164. * @param {function} callback The callback function
  165. *
  166. * @return An array of label
  167. */
  168. sigma.neo4j.getLabels = function(neo4j, callback) {
  169. sigma.neo4j.send(neo4j, '/db/data/labels', 'GET', null, callback);
  170. };
  171. /**
  172. * This function parse a neo4j cypher query result.
  173. *
  174. * @param {string} neo4j The URL of neo4j server or an object with the url, user & password.
  175. * @param {function} callback The callback function
  176. *
  177. * @return An array of relationship type
  178. */
  179. sigma.neo4j.getTypes = function(neo4j, callback) {
  180. sigma.neo4j.send(neo4j, '/db/data/relationship/types', 'GET', null, callback);
  181. };
  182. }).call(this);