1 /**
  2  * @author <a href="tarek.salah@badrit.com">Tarek Salah</a>
  3  */
  4 
  5 
  6  /** 
  7   * contains reference to local database and handle its functionalities
  8   * @namespace 
  9   */
 10 var LocalDB = {
 11    /** @memberOf LocalDB */
 12 	connection : null,
 13 	/** 
 14 	  * max database size
 15 	  * @memberOf LocalDB 
 16 	  */
 17 	max_db_size : 1000000,
 18 	/**
 19 	 * @memberOf LocalDB
 20 	 * @description connect to local database named <b>main</b> with max size = 100 GB. <br>
 21 	 * <u><b> Description </b></u><br>
 22 	 * if it is found just open it,
 23 	 * else create it. <br>
 24 	 * then begin initiation of database by calling {@link LocalDB.init}.
 25 	 * if the initiation has been done successfuly call {@link LocalDB.onReady},
 26 	 * else call {@link LocalDB.onError}
 27 	 */
 28 	connect : function() {
 29 		console.log("connecting to local database...");
 30 		LocalDB.connection = window.openDatabase("main", "1", "Main DB", LocalDB.max_db_size);
 31 		LocalDB.connection.transaction(LocalDB.init, LocalDB.onError, LocalDB.onReady);
 32 	},
 33 	/**	
 34 	 * @memberOf LocalDB 
 35 	 * @description called in case of error in connecting with local database.<br>
 36 	 * <u><b> Description </b></u><br>
 37 	 * printing the error code and the type of it 
 38 	 * @param {Object} e error code and type
 39 	 */
 40 	onError : function(e) {
 41 		console.log("SQL ERROR");
 42 		console.dir(e);
 43 	},
 44 	/**
 45 	 * @description initiate the tables of the database. <br>
 46 	 * <u><b> Description </b></u><br>
 47 	 * create table myProducts if not exits using the following query <br>
 48 	 * <b> create table if not exists myProduct(id INTEGER PRIMARY KEY,name,price) </b>
 49 	 * @memberOf LocalDB 
 50 	 * @param {Object} tx transaction manager that used to execute the SQL statement 
 51 	 */
 52 	init : function(tx) {
 53 		tx.executeSql("create table if not exists myProduct(id INTEGER PRIMARY KEY,name,price)");
 54 	},
 55 	/**
 56 	 * @description if database is ready to connect to server begin synchronization using  {@link RemoteDB.sync}. <br>
 57 	 * <u><b> Description </b></u><br>
 58 	 * make many checks to begin synchronization such as: <br>
 59 	 * 1. checking network state <br>
 60 	 * 2. checking connection state <br>
 61 	 * if the connection failed only display the offers	calling  {@link Device.display}
 62 	 * @memberOf LocalDB 
 63 	 */
 64 	onReady : function() {
 65 		if (navigator.network && navigator.network.connection.type != Connection.NONE) {
 66 			RemoteDB.sync();
 67 		} else
 68 			Device.display();
 69 	},
 70 };
 71 
 72 
 73 
 74 
 75  /** 
 76   * contains reference to remote database and handle its functionalities
 77   * @namespace 
 78   */
 79   
 80 var RemoteDB = {
 81 	/** 
 82 	 * conatins the previous md5 that determine the state of database
 83 	 * if it is changed or not
 84 	 * @memberOf RemoteDB 
 85 	 */
 86 	md5 : '',
 87 	/** 
 88 	 * the interval to check for md5
 89 	 * @memberOf RemoteDB 
 90 	 */
 91 	md5_tick_interval : 10000,
 92     /** 
 93 	 * server URL 196.218.156.154:8880
 94 	 * @memberOf RemoteDB 
 95 	 */
 96 	url : 'http://192.168.1.18:8082',
 97 	//	url : 'http://196.218.156.154:8880',
 98 	/**
 99 	 * check md5 
100 	 * if md5 changed connect with local database.<br>
101 	 * <u><b> Description </b></u><br>
102 	 * use get request to get md5 and compare it with
103 	 * the last md5 <br> if it is changed so the database changed
104 	 * and needs to make updates so call {@link LocalDB.connect}.
105 	 * @memberOf RemoteDB 
106 	 */
107 	checkMD5 : function() {
108 		var url = RemoteDB.url + "/content/campaignHash";
109 		$.get(url, function(resp) {
110 			console.log(resp);
111 			if (resp != RemoteDB.md5) {
112 				RemoteDB.md5 = resp;
113 				LocalDB.connect();
114 			}
115 		})
116 	},
117 	/**
118 	 * begin synchronization.<br>
119 	 * <u><b> Description </b></u><br>
120 	 * use get request to get the records of the remote database <b>(myProduct table)</b>
121 	 * using method getProducts on backend then begin synchronization<br>
122 	 * 1.call {@link RemoteDB.checkForChanges} to make insert and update operations.<br>
123 	 * 2.call {@link RemoteDB.checkForDeletionChanges} to make delete operations.<br>
124 	 * then call {@link Device.display} to show the new offers
125 	 * @memberOf RemoteDB 
126 	 */
127 	sync : function() {
128 		$("#docs").html("Refreshing documentation...");
129 		$.get(RemoteDB.url + "/index.php/content/getProducts", function(resp, code) {
130 			console.log("back from getting updates with " + resp.length + " items to process.");
131 			RemoteDB.checkForChanges(resp);
132 			RemoteDB.checkForDeletionChanges(resp);
133 			Device.display();
134 		}, "json");
135 	},
136 	/**
137 	 * connect to remote server and get the changes of updates and inserts.<br>
138 	 * <u><b> Description </b></u><br>
139 	 * connect to myProduct table on local database and get its records then compare
140 	 * it with records from remote database<br>
141 	 * 1.update every common record<br>
142 	 * 2.if record was founded on remote database and not found on local one
143 	 * ,so insert it in local
144 	 * @memberOf RemoteDB
145 	 * @param {Object} records the records for remote database	 
146 	 */
147 	checkForChanges : function(records) {
148 		//check fo new db updates or insertions
149 		records.forEach(function(ob) {
150 			LocalDB.connection.transaction(function(ctx) {
151 				ctx.executeSql("select name from myProduct where id = ?", [ob.id], function(tx, checkres) {
152 					if (checkres.rows.length) {
153 						console.log("updating " + ob.id + " " + ob.name);
154 						tx.executeSql("update myProduct set id=?,name=?,price=? where id=?", [ob.id, ob.name, ob.price, ob.id]);
155 					} else {
156 						console.log("insert " + ob.id + " " + ob.name);
157 						tx.executeSql("insert into myProduct (id,name,price) values(?,?,?)", [ob.id, ob.name, ob.price]);
158 						Device.showAlert(ob.name + " " + ob.price);
159 						Device.playBeep;
160 						Device.vibrate;
161 					}
162 				});
163 			});
164 		});
165 	},
166 	/**
167 	 * connect to remote server and get the changes of deletes.<br>
168 	 * <u><b> Description </b></u><br>
169 	 * connect to myProduct table on local database and get its records then compare
170 	 * it with records from remote database<br>
171 	 * if record was founded on local database and not found on remote one
172 	 * ,so delete it from local
173 	 * @memberOf RemoteDB 
174 	 * @param {Object} records the records for remote database	
175 	 */
176 	checkForDeletionChanges : function(records) {
177 		LocalDB.connection.transaction(function(tx) {
178 			tx.executeSql("select id,name,price from myProduct", [], function(tx, results) {
179 				for (var i = 0; i < results.rows.length; i++) {
180 					var x = true;
181 					records.forEach(function(ob) {
182 						if (results.rows.item(i).id == ob.id) {
183 							x = false;
184 						}
185 					});
186 					if (x) {
187 						console.log("possible delete " + results.rows.item(i).id);
188 						tx.executeSql("delete from myProduct where id = ?", [results.rows.item(i).id]);
189 					}
190 				}
191 			});
192 		});
193 	}
194 }