1 define(['require',"NexcelDataObject"], 
  2     // module definition function
  3     // dependencies (foo and bar) are mapped to function parameters
  4     function ( require,NexcelDataObject ) {
  5 		/**
  6 		 * @description A Class to handle All Kind of Data Services Associated with Nexcel Sheet 
  7 		 * @name WorkBookDataService
  8 		 * @class WorkBookDataService
  9 		 * @example require('WorkBookDataService')
 10 		 * @exports WorkBookDataService
 11 		 * @version 1.0
 12 		 * @module WorkBookDataService
 13 		 * @augments NexcelDataObject
 14 		 **/
 15 
 16 		var WorkBookDataService = function(){
 17 			/**
 18 			 * 
 19 			 */
 20 			var url = window.location.pathname;
 21 			url = url.split('/');
 22 			url.splice(0,1);
 23 			url.splice(url.length-1,1);
 24 			var serviceUrl = window.location.protocol+'//'+window.location.host+"/"+url.join('/')+"/php/WorkBookService.php?method=";
 25 			var WorkBookObject = require('WorkBookObject');
 26 			/**
 27 			 * @name loadWorkBooks
 28 			 * @memberOf WorkBookDataService
 29 			 * @method 
 30 			 * @return {Void}
 31 			 * @description An Asynchronous process. It calls the server with parent ID, so that it will return the list of files/Workbooks and folders under it. Then it parses the data and send back to the callback function.
 32 			 * @param parentId the Parent folder Id 
 33 			 * @param callback The call back function will be called once the Asynchronous process is completed.
 34 			 * @function
 35 			 */
 36 			this.loadWorkBooks = function(callback, parentId){
 37 				console.log('Load Work Books.....');
 38 				jQuery.ajax({
 39 					 type: "GET",
 40 					 url:  serviceUrl + WorkBookObject.WORKBOOK,
 41 					 data:{"parentId":parentId},
 42 					 success: function(result) {
 43 									var parseData = JSON.parse(result);
 44 									callback(parseData.parentId, parseData.data);
 45 							  },
 46 					 async:   true
 47 				});   	  
 48 			};
 49 			/**
 50 			 * @name addWorkBook
 51 			 * @memberOf WorkBookDataService
 52 			 * @method 
 53 			 * @return {Void}
 54 			 * @description An Asynchronous process. It calls the server with parent ID, so that it will return the list of files/Workbooks and folders under it. Then it parses the data and send back to the callback function.
 55 			 * @param name The name of the WorkBook need to be added.
 56 			 * @param folderId the Parent folder where the workbook is created. 
 57 			 * @param callback The call back function will be called once the Asynchronous process is completed.
 58 			 * @function
 59 			 */
 60 			this.addWorkBook = function(name, folderId, callback){
 61 				console.log('Adding Work Book .....');
 62 				jQuery.ajax({
 63 					 type: "POST",
 64 					 url:  serviceUrl + WorkBookObject.ADD_WORK_BOOK,
 65 					 data: { "WorkBook": WorkBookObject.WorkBook(-1, name, folderId).json()},
 66 					 success: function(result) {
 67 									console.log('Created Work Book Succsessfully.....');
 68 									callback(WorkBookObject.WorkBook().parse(result).id);
 69 							  },
 70 					 async:   false
 71 				});   	  
 72 			};
 73 			/**
 74 			 * @name duplicateWorkBook
 75 			 * @memberOf WorkBookDataService
 76 			 * @method 
 77 			 * @return {Void}
 78 			 * @description An Asynchronous process. It calls the server with workbook id, It duplicates all the data associated with the workbook including Sheets, ranges cell and formulas.
 79 			 * @param wsId the Workbook id, which need to be duplicated. 
 80 			 * @param callback The call back function will be called once the Asynchronous process is completed.
 81 			 * @function
 82 			 */
 83 			this.duplicateWorkBook = function(wsId, callback){
 84 				console.log('Duplicating Work Book .....');
 85 				jQuery.ajax({
 86 					 type: "POST",
 87 					 url:  serviceUrl + WorkBookObject.DUPLICATE_WORK_BOOK,
 88 					 data: { "WorkBook": WorkBookObject.WorkBook(wsId).json()},
 89 					 success: function(result) {
 90 									console.log('Duplicated Work Book Succsessfully.....');
 91 									var parseData = JSON.parse(result);
 92 									callback(parseData.id);
 93 							  },
 94 					 async:   false
 95 				});   	  
 96 			};
 97 			/**
 98 			 * @name renameWorkBook
 99 			 * @memberOf WorkBookDataService
100 			 * @method 
101 			 * @return {Void}
102 			 * @description An Asynchronous process. It calls the server with workbook id, It renames the work book with the new name.
103 			 * @param id the Workbook id, which need to be renamed. 
104 			 * @param name The new name for the Workbook
105 			 * @function
106 			 */	
107 			this.renameWorkBook = function(id, name){
108 				console.log('Rename Work Book .....');
109 				jQuery.ajax({
110 					 type: "POST",
111 					 url:  serviceUrl + WorkBookObject.RENAME_WORK_BOOK,
112 					 data: { "WorkBook": WorkBookObject.WorkBook(id, name).json()},
113 					 success: function(result) {
114 									console.log('Renamed Work Book Succsessfully.....');
115 							  },
116 					 async:   true
117 				});   	  
118 			};
119 			/**
120 			 * @name setActiveSheet
121 			 * @memberOf WorkBookDataService
122 			 * @method 
123 			 * @return {Void}
124 			 * @description An Asynchronous process. It calls the server with workbook id, It renames the work book with the new name.
125 			 * @param wsid the Workbook id, where the active sheet number need to be updated.
126 			 * @param sheetId The new active sheet id 
127 			 * @function
128 			 */	
129 			this.setActiveSheet = function(wbId, sheetId){
130 				console.log('Renaming Sheet.....');
131 				jQuery.ajax({
132 					 type: "POST",
133 					 url:  serviceUrl + WorkBookObject.SET_ACTIVE_SHEET,
134 					 data: {'WorkBook' : WorkBookObject.WorkBook(wbId).setActiveSheeetId(sheetId).json()},
135 					 success: function(result) {
136 									console.log('Updated Active Sheet Succsessfully.....');
137 							  },
138 					 async:   true
139 				});   	  
140 			};
141 			
142 			/**
143 			 * @name loadWorkBookSheets
144 			 * @memberOf WorkBookDataService
145 			 * @method 
146 			 * @return {Void}
147 			 * @description An Asynchronous process. It calls the server with workbook id, Once it gets the JSON response of the list of sheets in the workbook, It calls the callback function
148 			 * @param wsid the Workbook id,
149 			 * @param callback The function need to be called once the response received. 
150 			 * @function
151 			 */	
152 			this.loadWorkBookSheets = function(wsId, callBack){
153 					var dataServiceUrl = serviceUrl+WorkBookObject.SHEET;
154 					jQuery.ajax({
155 					 type: "GET",
156 					 url:  dataServiceUrl,
157 					 data: {'wsId' : wsId},
158 					 success: function(result){callBack(result);},
159 					 async:   false
160 				});  
161 			};
162 			
163 			/**
164 			 * @name loadZeroSheetInitialData
165 			 * @memberOf WorkBookDataService
166 			 * @method 
167 			 * @return {Void}
168 			 * @description An Asynchronous process. It calls the server with workbook id,sheetId, Hence it can load only the initial view data is loaded with respect to teh Size of teh Sheet  to ensure it happens very fastly.
169 			 * @param wsid the Workbook id,
170 			 * @param sheetObj The sheet Object contains id, rows, columns 
171 			 * @param maxRow The maximum number of rows can be viewed initially in the screen.
172 			 * @param callback The function need to be called once the response received. 
173 			 * @function
174 			 */	
175 			this.loadZeroSheetInitialData = function(wsId, sheetObj, maxRow, callBack){
176 					var dataServiceUrl = serviceUrl+WorkBookObject.CELLS;
177 					jQuery.ajax({
178 						 type: "GET",
179 						 url:  dataServiceUrl,
180 						 data: {'wsId' : wsId, sheetId:sheetObj.sheetId, maxRow : maxRow},
181 						 success: function(result) {
182 							callBack(sheetObj, result);
183 						 },
184 						 async:   true
185 					});  
186 			};
187 			/**
188 			 * @name loadCellsOnSheet
189 			 * @memberOf WorkBookDataService
190 			 * @method 
191 			 * @return {Void}
192 			 * @description An Asynchronous process. It calls the server with workbook id,sheetId, Hence it can load the entire data cells in the sheet Obj. It will ensure entire server data is present in the cient side as well. for the sheet view.
193 			 * @param wsid the Workbook id,
194 			 * @param sheetObj The sheet Object contains id, rows, columns 
195 			 * @param callback The function need to be called once the response received. 
196 			 * @function
197 			 */	
198 			this.loadCellsOnSheet = function (wsId, sheetObj, callBack){
199 					var dataServiceUrl = serviceUrl+WorkBookObject.CELLS;
200 					jQuery.ajax({
201 					 type: "GET",
202 					 url:  dataServiceUrl,
203 					 data: {'wsId' : wsId, sheetId:sheetObj.sheetId},
204 					 success: function(result) {
205 							callBack(result, sheetObj);
206 							  },
207 					 async:   true
208 				});  
209 			};
210 			/**
211 			 * @name updateListOfCells
212 			 * @memberOf WorkBookDataService
213 			 * @method 
214 			 * @return {Void}
215 			 * @description An Asynchronous process. It calls the server with updated list of cells, It ensures that multiple cell update can be applied together in the server with making delays. It ensures that it calls batch update in server side.
216 			 * @param wsid the Workbook id,
217 			 * @param sheetObj The sheet Object contains id, rows, columns 
218 			 * @param callback The function need to be called once the response received. 
219 			 * @function
220 			 */	
221 			this.updateListOfCells = function(jsonData){
222 				jQuery.ajax({
223 					 type: "POST",
224 					 url:  serviceUrl + NexcelDataObject.UPDATE_LIST_OF_CELLS,
225 					 data: {'cellList' : jsonData},
226 					 success: function(result) {
227 									console.log('SUCCESSFULLY UPDATED...');
228 							  },
229 					 async:   true
230 				}); 
231 			 };  
232 			 /**
233 			 * @name addSheet
234 			 * @memberOf WorkBookDataService
235 			 * @method 
236 			 * @return {Void}
237 			 * @description An Asynchronous process. It calls the server with sheet details
238 			 * @param wsid the Workbook id,
239 			 * @param sheetObj The sheet Object contains id, rows, columns 
240 			 * @param callback The function need to be called once the response received. 
241 			 * @function
242 			 */	
243 			 this.addSheet = function(sheetObj, wsId,callback){
244 				var sheetId=-1;
245 				jQuery.ajax({
246 					 type: "POST",
247 					 url:  serviceUrl + WorkBookObject.ADD_SHEET,
248 					 data: {'sheet' : WorkBookObject.Sheet(sheetId, sheetObj.name, wsId, sheetObj.rows, sheetObj.cols).json()},
249 					 success: function(result) {
250 								sheetObj.id = JSON.parse(result).id;
251 								callback(sheetObj);
252 							  },
253 					 async:   true
254 				});   	  
255 			};
256 			 /**
257 			 * @name renameSheet
258 			 * @memberOf WorkBookDataService
259 			 * @method 
260 			 * @return {Void}
261 			 * @description An Asynchronous process. It calls the server with updated sheet details
262 			 * @param sheetId The sheet Id which need to be renamed 
263 			 * @param name The new name for the Sheet 
264 			 * @function
265 			 */
266 			this.renameSheet = function(sheetId, name){
267 				console.log('Renaming Sheet.....');
268 				jQuery.ajax({
269 					 type: "POST",
270 					 url:  serviceUrl + WorkBookObject.RENAME_SHEET,
271 					 data: {'sheet' : WorkBookObject.Sheet(sheetId, name).json()},
272 					 success: function(result) {
273 									console.log('Updated Sheet Name Succsessfully.....');
274 							  },
275 					 async:   true
276 				});   	  
277 			};
278 			
279 			/**
280 			 * @name deleteSheet
281 			 * @memberOf WorkBookDataService
282 			 * @method 
283 			 * @return {Void}
284 			 * @description An Asynchronous process. It calls the server with updated sheet details
285 			 * @param sheetId The sheet Id which need to be renamed 
286 			 * @param name The new name for the Sheet 
287 			 * @function
288 			 */
289 			this.deleteSheet = function(sheetId){
290 				jQuery.ajax({
291 					 type: "POST",
292 					 url:  serviceUrl + WorkBookObject.DELETE_SHEET,
293 					 data: {'sheet' : WorkBookObject.Sheet(sheetId).json()},
294 					 success: function(result) {
295 									console.log('Updated Sheet Name Succsessfully.....');
296 							  },
297 					 async:   true
298 				});   	  
299 			};
300 			/**
301 			 * @name updateSheetSize
302 			 * @memberOf WorkBookDataService
303 			 * @method 
304 			 * @return {Void}
305 			 * @description An Asynchronous process. It calls the server with updated sheet details
306 			 * @param sheetId The sheet Id which need to be renamed 
307 			 * @param rows The new rows in the sheet
308 			 * @param columns The new rows in the sheet 
309 			 * @function
310 			 */
311 			this.updateSheetSize = function(sheetId, rows, cols){
312 				console.log('ReSizing Sheet.....');
313 				jQuery.ajax({
314 					 type: "POST",
315 					 url:  serviceUrl + WorkBookObject.UPDATE_SHEET_SIZE,
316 					 data: {'sheet' : WorkBookObject.Sheet(sheetId).setSize(rows,cols).json()},
317 					 success: function(result) {
318 									console.log('Updated Sheet Size Succsessfully.....');
319 							  },
320 					 async:   true
321 				});   	  
322 			};
323 			/**
324 			 * @name updateColumnSize
325 			 * @memberOf WorkBookDataService
326 			 * @method 
327 			 * @return {Void}
328 			 * @description An Asynchronous process. It calls the server with updated column details
329 			 * @param sheetId The sheet Id which need to be renamed 
330 			 * @param colIndex The column index A is 1
331 			 * @param width The column width in pixel. 
332 			 * @function
333 			 */
334 			this.updateColumnSize = function(sheetId, colIndex, width){
335 				jQuery.ajax({
336 					 type: "POST",
337 					 url:  serviceUrl + WorkBookObject.UPDATE_COLUMN_SIZE,
338 					 data: {'Column' : WorkBookObject.Column(sheetId, colIndex, width).json()},
339 					 success: function(result) {
340 									console.log('Updated Sheet Column Size Succsessfully.....');
341 							  },
342 					 async:   true
343 				});   	  
344 			};
345 			/**
346 			 * @name updateRowSize
347 			 * @memberOf WorkBookDataService
348 			 * @method 
349 			 * @return {Void}
350 			 * @description An Asynchronous process. It calls the server with updated row size details
351 			 * @param sheetId The sheet Id which need to be renamed 
352 			 * @param rowIndex The column index A is 1
353 			 * @param height The height of the rows in pixel. 
354 			 * @function
355 			 */
356 			this.updateRowSize = function(sheetId, rowIndex, height){
357 				//console.log('Updating '+ WorkBookObject.Row(sheetId, rowIndex, height).json());
358 				jQuery.ajax({
359 					 type: "POST",
360 					 url:  serviceUrl + WorkBookObject.UPDATE_ROW_SIZE,
361 					 data: {'Row' : WorkBookObject.Row(sheetId, rowIndex, height).json()},
362 					 success: function(result) {
363 									console.log('Updated Sheet Row Size Succsessfully.....');
364 							  },
365 					 async:   true
366 				});   	  
367 			};
368 			/**
369 			 * @name updateSheetOrder
370 			 * @memberOf WorkBookDataService
371 			 * @method 
372 			 * @return {Void}
373 			 * @description An Asynchronous process. It updates the server with which order the sheets are arranges in the UI.
374 			 * @param wsId The worksheet Id 
375 			 * @param sheetOrder {Array} An Array of the order of the sheets.
376 			 * @function
377 			 */
378 			this.updateSheetOrder = function(wsId, sheetOrder){
379 				//console.log('Saving Sheet Order .....');
380 				jQuery.ajax({
381 					 type: "POST",
382 					 url:  serviceUrl + WorkBookObject.UPDATE_SHEET_ORDER,
383 					 data: {'sheetOrder' : WorkBookObject.WorkBook(wsId).setSheetOrder(sheetOrder).json()},
384 					 success: function(result) {
385 									console.log('Updated Sheet Size Succsessfully.....');
386 							  },
387 					 async:   true
388 				});   	  
389 			};
390 			
391 			/**
392 			 * @name loadColumnSize
393 			 * @memberOf WorkBookDataService
394 			 * @method 
395 			 * @return {Void}
396 			 * @description An Asynchronous process. It loads the column size for the workbook for each sheets in the workbook.
397 			 * @param wsId The worksheet Id 
398 			 * @param callback {Function} The given callback function will be called once the data loading is completed.
399 			 * @function
400 			 */
401 			this.loadColumnSize = function (wsId, callBack){
402 					var dataServiceUrl = serviceUrl+WorkBookObject.LOAD_COLUMN_SIZE;
403 					jQuery.ajax({
404 					 type: "GET",
405 					 url:  dataServiceUrl,
406 					 data: {'wsId' : wsId},
407 					 success: function(result) {
408 							callBack(result);
409 							  },
410 					 async:   true
411 				});  
412 			};
413 			
414 			/**
415 			 * @name loadRowSize
416 			 * @memberOf WorkBookDataService
417 			 * @method 
418 			 * @return {Void}
419 			 * @description An Asynchronous process. It loads the row  size for the workbook for each sheets in the workbook.
420 			 * @param wsId The worksheet Id 
421 			 * @param callback {Function} The given callback function will be called once the data loading is completed.
422 			 * @function
423 			 */
424 			this.loadRowSize = function (wsId, callBack){
425 					var dataServiceUrl = serviceUrl+WorkBookObject.LOAD_ROW_SIZE;
426 					jQuery.ajax({
427 					 type: "GET",
428 					 url:  dataServiceUrl,
429 					 data: {'wsId' : wsId},
430 					 success: function(result) {
431 							callBack(result);
432 							  },
433 					 async:   true
434 				});  
435 			};
436 			
437 			/**
438 			 *	Add rows to sheet for a specific postion  
439 			 */
440 			/**
441 			 * @name addRow
442 			 * @memberOf WorkBookDataService
443 			 * @method 
444 			 * @return {Void}
445 			 * @description An Asynchronous process. It adds a Row in the workbook
446 			 * @param sheetId The sheet Id
447 			 * @param pos the position where the rows are added. 
448 			 * @param rows {Integer} The number of rows are added.
449 			 * @function
450 			 */
451 			this.addRow = function(sheetId, pos, rows){
452 				var dataServiceUrl = serviceUrl+WorkBookObject.ADD_ROW;
453 				var data=new Object();
454 				data.sheetId = sheetId;
455 				data.pos= pos;
456 				data.rows = rows;
457 				jQuery.ajax({
458 					 type: "POST",
459 					 url:  dataServiceUrl,
460 					 data: {'data' : JSON.stringify(data)},
461 					 success: function(result) {
462 							//callBack(result);
463 							  },
464 					 async:   true
465 				}); 
466 			};
467 			/**
468 			 * @name addColumn
469 			 * @memberOf WorkBookDataService
470 			 * @method 
471 			 * @return {Void}
472 			 * @description An Asynchronous process. It adds a Column in the workbook
473 			 * @param sheetId The sheet Id
474 			 * @param pos the position where the rows are added. 
475 			 * @param columns {Integer} The number of columns are added.
476 			 * @function
477 			 */
478 			this.addColumn = function(sheetId, pos, columns){
479 				var dataServiceUrl = serviceUrl+WorkBookObject.ADD_COL;
480 				var data=new Object();
481 				data.sheetId = sheetId;
482 				data.pos= pos;
483 				data.columns = columns;
484 				jQuery.ajax({
485 					 type: "POST",
486 					 url:  dataServiceUrl,
487 					 data: {'data' : JSON.stringify(data)},
488 					 success: function(result) {
489 							//callBack(result);
490 							  },
491 					 async:   true
492 				}); 
493 			};
494 		};
495 		return new WorkBookDataService();
496 	}
497 );