Guest User

Untitled

a guest
Aug 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.webDb = (function (w) {
  2.  
  3.     var api = {}, db;
  4.  
  5.     // set up event handlers and file system
  6.     api.init = function () {
  7.         // Create database
  8.         var dbSize = 20 * 1024 * 1024; // 20MB
  9.         api.db = openDatabase("Database_Banque", "1.0", "Gestion des documents clients", dbSize);
  10.  
  11.         // Create tables
  12.         api.createTables();
  13.     };
  14.  
  15.     //event handler for filesystem action
  16.     api.createTables = function () {
  17.         api.db.transaction(function (tx) {
  18.             //(Destroy/Re)Create table customers
  19.             tx.executeSql("DROP TABLE IF EXISTS CUST_Customers", []);
  20.             tx.executeSql("CREATE TABLE IF NOT EXISTS CUST_Customers(tpar_id INTEGER PRIMARY KEY ASC, display_Name TEXT, external_id TEXT)", []);
  21.  
  22.             //(Destroy/Re)Create table document types
  23.             tx.executeSql("DROP TABLE IF EXISTS DOTY_DocumentType", []);
  24.             tx.executeSql("CREATE TABLE IF NOT EXISTS DOTY_DocumentType(doty_code INTEGER PRIMARY KEY ASC, doty_label TEXT)", []);
  25.  
  26.             //(Destroy/Re)Create table documents
  27.             tx.executeSql("DROP TABLE IF EXISTS DOCU_Documents", []);
  28.             tx.executeSql("CREATE TABLE IF NOT EXISTS DOCU_Documents(docu_id INTEGER PRIMARY KEY ASC, doty_label TEXT, external_id TEXT, selected BOOLEAN, deleted BOOL, viewed BOOL, filesystem_url TEXT, filename TEXT)", []);
  29.         });
  30.     };
  31.  
  32.     // pull file down into local
  33.     api.fillData = function (document) {
  34.         try {
  35.             api.db.transaction(function (tx) {
  36.                
  37.                 //Verify if CUST is into db
  38.                 tx.executeSql('SELECT * FROM CUST_Customers WHERE external_id = ?', [document.client_external_id], function (t, data) {
  39.                     if (data.rows.length == 0) {
  40.                         tx.executeSql('INSERT INTO CUST_Customers (display_name, external_id) VALUES (?, ?)', [document.clientFullName, document.client_external_id]);
  41.                         console.log("client created");
  42.                     }
  43.                 });
  44.  
  45.                 //Verify if DOTY is into db
  46.                 tx.executeSql('SELECT * FROM DOTY_DocumentType WHERE doty_label = ?', [document.theme], function (t, data) {
  47.                     if (data.rows.length == 0) {
  48.                         tx.executeSql('INSERT INTO DOTY_DocumentType (doty_label) VALUES (?)', [document.theme]);
  49.                         console.log("documentType created");
  50.                     }
  51.                 });
  52.  
  53.                 //Add DOCU into db
  54.                 tx.executeSql('INSERT INTO DOCU_Documents (doty_label, external_id, selected, deleted, viewed, filesystem_url, filename) VALUES (?, ?, ?, ?, ?, ?, ?)', [document.theme, document.client_external_id, false, false, false, document.filesystem, document.fileName]);
  55.            
  56.             });
  57.         } catch (ev) {
  58.             alert("error");
  59.         }
  60.         //Insert doty into db
  61.         //tx.executeSql();
  62.  
  63.         //Insert docu into db
  64.         //tx.executeSql();
  65.  
  66.  
  67.  
  68.  
  69.     };
  70.  
  71.     // default error handler - that doesn't do anything useful
  72.     api.err = function (ev) {
  73.         console.log(ev);
  74.     };
  75.  
  76.     api.getDocuments = function () {
  77.         return api.documentsList;
  78.     };
  79.  
  80.     api.setDocumentsList = function (docList) {
  81.         api.documentsList = docList;
  82.     };
  83.  
  84.     return api;
  85.  
  86. })(window);
Add Comment
Please, Sign In to add comment