Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.20 KB | None | 0 0
  1. // array containing ID selected to anonimization
  2. var names = [];
  3. // array containing primary keys names for table (column captions)
  4. var pknames = [];
  5. // column headers (captions) of table
  6. var tHeadNames = [];
  7. // cell values for specific record
  8. var cellValues = [];
  9. // Index of pk in thead array
  10. var pkPositions = [];
  11. // row objects
  12. var rowDOMs = [];
  13. //tableID
  14. var tableId;
  15. // json result
  16. var json;
  17. // set counter (json inner array counter)
  18. var setCounter = 0;
  19.  
  20. /// Button click - sprawdz checki
  21. $("#checkboxes").click(function () {
  22. console.log("array.Names: " + names);
  23. });
  24.  
  25. // Function call on every row checkbox change
  26. function addRowToAnonimizeList(cb, boxId) {
  27. if (cb.checked) {
  28. names.push(boxId)
  29. } else {
  30. var index = names.indexOf(boxId);
  31. names.splice(index, 1);
  32. }
  33. }
  34.  
  35.  
  36. // function call on button click id="get" - Wyświetl
  37. // get table data from database and send result to div #dispresult in shared view _tableDisplay.cshtml
  38. function getTableFromDB(tableId) {
  39. $.ajax({
  40. url: "/tablenames/kar",
  41. data: {
  42. "id": tableId
  43. },
  44. type: 'GET',
  45. success: function (result) {
  46. $('#dispresult').html(result);
  47. },
  48. error: function (result) {
  49. console.log("Error with sending to tablenames controler");
  50. $('#dispresult').html("Error.");
  51. }
  52. });
  53. }
  54.  
  55. //function call when button clicked to anonimize DB
  56. function anonimizeDBbyID(dbID) {
  57. var operationType = 1;
  58. var dataObject = JSON.stringify({
  59. 'dbId': dbID,
  60. 'opType': operationType
  61. });
  62. $.ajax({
  63. url: "/ManageActions/AnonimizeDatabase",
  64. method: 'POST',
  65. data: dataObject,
  66. contentType: 'application/json; charset=utf-8',
  67. success: function (result) {
  68. $('#dispresult').html("Sucessfuly Anonimized Database id" + dataObject);
  69. },
  70. error: function (result) {
  71. $('#dispresult').html("Error with anonimizing database");
  72. }
  73. });
  74. }
  75. // fucntion call when clicked anonimize witch selected rows (checkboxes from table)
  76. function AnonimizeRecordsByID(tableID) {
  77. tableId = tableID;
  78. if (names.length > 0) {
  79. getPrimaryKeysCaptions(tableID)
  80. } else {
  81. $('#dispresult').html("Nie zaznaczono żadnego rekordu.");
  82. }
  83.  
  84. }
  85.  
  86.  
  87. ///////////////////////////////////function call on anonimize selectod only.
  88. // Step 1 - get Priamry keys names (captions only) for table.
  89. function getPrimaryKeysCaptions(tableID) {
  90. $.ajax({
  91. url: "/tablenames/getPKnames",
  92. data: {
  93. "id": tableID
  94. },
  95. type: 'GET',
  96. success: function (result) {
  97. if (result === "none") {
  98. $('#dispresult').html("Tabela nie posiada kluczy głównych. <br> Nie można wykonać operacji. ");
  99. } else {
  100. pknames = result.split(',');
  101. getRowsFromHTMLTable()
  102. }
  103. },
  104. error: function (result) {
  105. console.log("Error while getting PK from server" + error);
  106. }
  107. });
  108. }
  109.  
  110. //Step 2. Selected single row values (id of rows selected by user - checkboxes)
  111. function getRowsFromHTMLTable() {
  112.  
  113. var DOMtable = document.getElementById("databaseTable");
  114. // get rowDOM array from table
  115. for (var rowIterator = 0; rowIterator < names.length; rowIterator++) {
  116. var singleRow = DOMtable.rows[names[rowIterator]]
  117. rowDOMs.push(singleRow)
  118. }
  119.  
  120. // Start building Json with data for server.
  121. // Table ID is same for every row.
  122. // function ddRowPKValuesToJson call (every row call) adds new object to JSON
  123.  
  124.  
  125.  
  126. json = '{"opType": 1, "tableId":' + tableId + ', "UpdateKeyValuesParameters": [ '
  127. //json = '{['
  128.  
  129. // get table columns names
  130. getHeadersFromHTMLTable(DOMtable)
  131.  
  132. //for every row call function.
  133. rowDOMs.forEach(addRowPKValuesToJson)
  134.  
  135. // Cut last coma before closing
  136. json = json.slice(0, json.length - 1);
  137.  
  138. // Close JSON structure
  139. json = json + ']}'
  140. obj = JSON.parse(json)
  141. sendJSONtoDB()
  142. // display JSON to console.
  143. console.log(obj)
  144.  
  145. // clear arrays and vars before next operation.
  146. tHeadNames.length = 0;
  147. pknames.length = 0;
  148. pkPositions.length = 0;
  149. rowDOMs.length = 0;
  150. cellValues.length = 0;
  151. json = "";
  152. setCounter = 0;
  153. }
  154.  
  155. function getHeadersFromHTMLTable(DOMtable) {
  156. // get table column names
  157. var headersDOM = DOMtable.getElementsByTagName("th")
  158. // parse DOM object in array to strings
  159. for (var arrayIterator = 0; arrayIterator < headersDOM.length; arrayIterator++) {
  160. tHeadNames[arrayIterator] = headersDOM[arrayIterator].textContent
  161. }
  162. // delete first element from array - checkbox value. It doesnt exists in native database
  163. tHeadNames.splice(0, 1)
  164. }
  165.  
  166.  
  167. // function fired for every row (every element in rowsDOMs array.)
  168. function addRowPKValuesToJson(value, index) {
  169. // increment set (identifier for keys as they belong to one item-row in database)
  170. setCounter += 1;
  171. // parse row cells to array.
  172. var cellValuesDOM = value.getElementsByTagName("td")
  173. // parse DOM object in array to strings
  174. for (var arrayIterator = 0; arrayIterator < cellValuesDOM.length; arrayIterator++) {
  175. cellValues[arrayIterator] = cellValuesDOM[arrayIterator].textContent
  176. }
  177. // delete first element from array - checkbox value. It doesnt exists in native database
  178. cellValues.splice(0, 1)
  179.  
  180. // Compare tables: theadNames with pknames. When compare is positive get value from cellValues by index from tHeadNames
  181. for (var arrayIterator = 0; arrayIterator < pknames.length; arrayIterator++) {
  182. pknames[arrayIterator] = pknames[arrayIterator].toUpperCase()
  183. for (var headersIterator = 0; headersIterator < tHeadNames.length; headersIterator++) {
  184. tHeadNames[headersIterator] = tHeadNames[headersIterator].toUpperCase()
  185. if (tHeadNames[headersIterator] == pknames[arrayIterator]) {
  186. // add to JSON key and its value.
  187. //json = json + '{"Name": "' + tHeadNames[headersIterator] + '", "Value": ' + cellValues[headersIterator]+'},'
  188. json = json + '{"Name": "' + tHeadNames[headersIterator] + '", "Value": '
  189. if (isNaN(cellValues[headersIterator])) {
  190. json = json + '"'+cellValues[headersIterator] + '"'
  191. }else {
  192. json = json + cellValues[headersIterator]
  193. }
  194. json = json + ', "set": ' + setCounter + '},'
  195. }
  196. }
  197. }
  198. }
  199.  
  200.  
  201.  
  202. function sendJSONtoDB() {
  203. var dataJson = JSON.stringify(json)
  204.  
  205. /// #region DEBUG - do usuniecia
  206.  
  207. var SqlStatementParameter = [
  208. { Name: 'ID_ADRES', Value: 5, set: 1 }
  209. ];
  210.  
  211. var ParamModel =
  212. { opType: 1, tableId: 225, UpdateKeyValuesParameters: SqlStatementParameter}
  213.  
  214.  
  215. var ParamStringified = JSON.stringify(ParamModel)
  216.  
  217. var cont = '<h1><font color ="red">TODO.</font></h1><br>' +
  218. 'JSON jest budowany : <br><font color ="blue" style="font-family: Consolas; font-size:80%;"> '+ json +'</font><br>' +
  219. 'Jest problem w przesyłaniu do kontrolera <mark>/ManageActions/AnonimizeRows. </mark><br>' +
  220. 'JSON wyslany AJAXem nie jest odbierany w kontrolerze (przyjmowana wartosc jest null)' +
  221. '<br><br><font style="font-family: Courier; font-size:90%;">Do testow: Tabela UZYTKOWNICY2UPRAWNIENIA zawiera 3 klcuze PK <br> ' +
  222. 'tabela WZOR_OSOBY nie zawiera żadnych kluczy </font>' +
  223. '<br><br>stringified:: ' + dataJson
  224. $('#dispresult').html(cont);
  225. /// #endregion
  226.  
  227.  
  228.  
  229.  
  230.  
  231. console.log("json ready to send: "+json)
  232. $.ajax({
  233. url: "/ManageActions/AnonimizeRows",
  234. method: 'POST',
  235. data: dataJson,
  236. contentType: 'application/json; charset=utf-8',
  237. dataType: 'json',
  238. success: function (result) {
  239. console.log("Sucessfuly Anonimized rows " + result);
  240. },
  241. error: function (result) {
  242. console.log("Error with anonimizing selected rows. "+result.statusText);
  243. }
  244. });
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement