1 define(['NexcelDataObject'],
  2 function(NexcelDataObject) {
  3 	/**
  4 	 * @description A Class to handle All Kind of Data Services Associated with Nexcel Sheet 
  5 	 * @name NexcelUIDataService
  6 	 * @class NexcelUIDataService
  7 	 * @example require('NexcelUIDataService')
  8 	 * @exports NexcelUIDataService
  9 	 * @version 1.0
 10 	 * @module NexcelUIDataService
 11 	 * @augments jquery
 12 	 * @augments DomObjects
 13 	 * @augments NexcelDataObject
 14 	 **/
 15 	var NexcelUIDataService = {
 16 		/**
 17 		 *
 18 		 */
 19 		dataServiceUrl : require('WorkBook').getServiceURL()+"/php/NexcelUIDataService.php?method=",
 20 		//var this.dataServiceUrl = window.location.protocol+'//'+window.location.host+"/"+window.location.pathname.split('/')[1]+"/php/DSSDataService.php?method=";
 21 		/**
 22 		 * @name createGroup
 23 		 * @memberOf NexcelUIDataService
 24 		 * @method 
 25 		 * @return {Integer} groupId The unique identification for the group.
 26 		 * @description A Synchronous process. With the given data the group will be created in the server side and Ajax responds with the group id, This id is used throughout globally.
 27 		 * @param name, 
 28 		 * @param sheetId, 
 29 		 * @param loc, 
 30 		 * @param formula, 
 31 		 * @param colSize, 
 32 		 * @param color, 
 33 		 * @param colNameAvailable, 
 34 		 * @param colNameList, 
 35 		 * @param groupTitle 
 36 		 * @function
 37 		 */
 38 		createGroup : function(name, sheetId, loc, formula, colSize, color, colNameAvailable, colNameList, groupTitle) {
 39 			var groupId = -1;
 40 			var group = NexcelDataObject.Group(name, sheetId, loc, formula, colSize, color, colNameAvailable, colNameList, groupTitle).json();
 41 			console.log('POSTing Data :' + group + '<br/> URL :' + this.dataServiceUrl + NexcelDataObject.CREATE_GROUP);
 42 			jQuery.ajax({
 43 				type : "POST",
 44 				url : this.dataServiceUrl + NexcelDataObject.CREATE_GROUP,
 45 				data : {
 46 					'group' : group
 47 				},
 48 				success : function(result) {
 49 					console.log("Result :" + result);
 50 					groupId = JSON.parse(result).id;
 51 				},
 52 				async : false
 53 			});
 54 			return groupId;
 55 		},
 56 
 57 		/**
 58 		 * @name updateGroupFormula
 59 		 * @memberOf NexcelUIDataService
 60 		 * @method 
 61 		 * @return {Integer} groupId The unique identification for the group.
 62 		 * @description An Asynchronous process. With the given formula for the group, It makes an aynchronous request and update the server with the new formula
 63 		 * @param groupId, 
 64 		 * @param formula, 
 65 		 * @function
 66 		 */
 67 		updateGroupFormula : function(groupId, formula) {
 68 			var group = NexcelDataObject.Group();
 69 			group.setId(groupId);
 70 			group.setFormula(formula);
 71 			console.log('POSTing Data :' + group + '<br/> URL :' + this.dataServiceUrl + NexcelDataObject.UPDATE_FORMULA);
 72 			jQuery.ajax({
 73 				type : "POST",
 74 				url : this.dataServiceUrl + NexcelDataObject.UPDATE_FORMULA,
 75 				data : {
 76 					'group' : group.json()
 77 				},
 78 				success : function(result) {
 79 					console.log("Result :" + result);
 80 				},
 81 				async : true
 82 			});
 83 			return groupId;
 84 		},
 85 
 86 		/**
 87 		 * @name updateGroupSize
 88 		 * @memberOf NexcelUIDataService
 89 		 * @method 
 90 		 * @return {Void} 
 91 		 * @description A synchronous process. It creates a JSON object of teh given group Object, It calls teh server with the json data, so that the server will be in sync with the client side.
 92 		 * @param group Thr group Object which have the id, start column, end column, start row, end column etc..., 
 93 		 * @function
 94 		 */
 95 		updateGroupSize : function(group) {
 96 			//formula wont be considered here and not be updated in server
 97 			var g = NexcelDataObject.Group();
 98 			g.setId(group.id);
 99 			g.setLoc(group.rowStart, group.colStart, group.rowEnd, group.colEnd);
100 			g.setColSize(group.colSize);
101 			console.log('POSTing Data :' + g + '<br/> URL :' + this.dataServiceUrl + NexcelDataObject.UPDATE_GROUP_SIZE);
102 			jQuery.ajax({
103 				type : "POST",
104 				url : this.dataServiceUrl + NexcelDataObject.UPDATE_GROUP_SIZE,
105 				data : {
106 					'group' : g.json()
107 				},
108 				success : function(result) {
109 					console.log("Result :" + result);
110 				},
111 				async : true
112 			});
113 		},
114 
115 		/**
116 		 * @name updateGroupSorting
117 		 * @memberOf NexcelUIDataService
118 		 * @method 
119 		 * @return {Void} 
120 		 * @description An Asynchronous process. It updates the server with the sorting of the group with which column.
121 		 * @param groupObj Thr group Object which have the id, start column, end column, start row, end column etc..., 
122 		 * @function
123 		 */
124 		updateGroupSorting : function(groupObj) {
125 			var group = NexcelDataObject.Group();
126 			group.setId(groupObj.id);
127 			group.setSorted(groupObj.sorted);
128 			group.setSortedCol(groupObj.sortedCol);
129 			jQuery.ajax({
130 				type : "POST",
131 				url : this.dataServiceUrl + NexcelDataObject.UPDATE_GROUP_SORTING,
132 				data : {
133 					'group' : group.json()
134 				},
135 				success : function(result) {
136 					console.log("Result :" + result);
137 				},
138 				async : true
139 			});
140 		},
141 		/**
142 		 * @name renameGroup
143 		 * @memberOf NexcelUIDataService
144 		 * @method 
145 		 * @return {Void} 
146 		 * @description An Asynchronous process. It updates the server with the new name for the Group.
147 		 * @param groupObj Thr group Object which have the id, start column, end column, start row, end column etc..., 
148 		 * @function
149 		 */
150 		renameGroup : function(groupObj) {
151 			
152 			console.log('POSTing Data :' + groupObj + '<br/> URL :' + this.dataServiceUrl + NexcelDataObject.RENAME_GROUP);
153 			jQuery.ajax({
154 				type : "POST",
155 				url : this.dataServiceUrl + NexcelDataObject.RENAME_GROUP,
156 				data : {
157 					'group' : groupObj.json()
158 				},
159 				success : function(result) {
160 					console.log("Result :" + result);
161 				},
162 				async : true
163 			});
164 			//return groupObj.id;
165 		},
166 		
167 		/**
168 		 * @name pinGroupHeader
169 		 * @memberOf NexcelUIDataService
170 		 * @method 
171 		 * @return {Void} 
172 		 * @description An Asynchronous process. Once the header of teh group in the UI is pinned it need to be updated in therver, the function does it.
173 		 * @param groupObj Thr group Object which have the id, start column, end column, start row, end column etc..., 
174 		 * @function
175 		 */
176 		pinGroupHeader : function(groupObj) {
177 			console.log('POSTing Data :' + groupObj + '<br/> URL :' + this.dataServiceUrl + NexcelDataObject.PIN_GROUP_HEADER);
178 			var group = NexcelDataObject.Group();
179 			group.setId(groupObj.id);
180 			group.setHeaderPinned(groupObj.headerPinned);
181 			jQuery.ajax({
182 				type : "POST",
183 				url : this.dataServiceUrl + NexcelDataObject.PIN_GROUP_HEADER,
184 				data : {
185 					'group' : group.json()
186 				},
187 				success : function(result) {
188 					console.log("Result :" + result);
189 				},
190 				async : true
191 			});
192 			//return groupObj.id;
193 		},
194 		/**
195 		 * @name deleteGroup
196 		 * @memberOf NexcelUIDataService
197 		 * @method 
198 		 * @return {Void} 
199 		 * @description An Asynchronous process. Once the header of teh group in the UI deleted, it need to be updated in the server, the function does it.
200 		 * @param groupId Thr group Id  
201 		 * @function
202 		 */
203 		deleteGroup : function(groupId) {
204 			var group = NexcelDataObject.Group();
205 			group.setId(groupId);
206 			console.log('POSTing Data :' + group + '<br/> URL :' + this.dataServiceUrl + NexcelDataObject.DELETE_GROUP);
207 			jQuery.ajax({
208 				type : "POST",
209 				url : this.dataServiceUrl + NexcelDataObject.DELETE_GROUP,
210 				data : {
211 					'group' : group.json()
212 				},
213 				success : function(result) {
214 					console.log("Result :" + result);
215 				},
216 				async : true
217 			});
218 			return groupId;
219 		},
220 		/**
221 		 * @name updateGroupData
222 		 * @memberOf NexcelUIDataService
223 		 * @method 
224 		 * @return {Integer} groupId the Id of teh group which is updated.
225 		 * @description An Asynchronous process. Once any group data is updated, then using this method, it can update the server also with the following results.
226 		 * @param groupId Thr group Id  
227 		 * @param resultDataArray [[][]] A two dimentional array of data.
228 		 * @function
229 		 */
230 		updateGroupData : function(groupId, resultDataArray) {
231 			var groupData = NexcelDataObject.GroupData(groupId, resultDataArray).json();
232 			console.log('POSTing Data :' + groupData + '<br/> URL :' + this.dataServiceUrl + NexcelDataObject.UPDATE_GROUP_DATA);
233 			jQuery.ajax({
234 				type : "POST",
235 				url : this.dataServiceUrl + NexcelDataObject.UPDATE_GROUP_DATA,
236 				data : {
237 					'groupData' : groupData
238 				},
239 				success : function(result) {
240 					console.log("Result :" + result);
241 				},
242 				async : true
243 			});
244 			return groupId;
245 		},
246 		/**
247 		 * @name storeGroupData
248 		 * @memberOf NexcelUIDataService
249 		 * @method 
250 		 * @return {Integer} groupId the Id of teh group which is updated.
251 		 * @description An Asynchronous process. Once any group data is updated, then using this method, it can update the server also with the following results.
252 		 * @param groupId Thr group Id  
253 		 * @param resultDataArray [[][]] A two dimentional array of data.
254 		 * @function
255 		 */
256 		storeGroupData : function(groupId, resultDataArray) {
257 			var groupData = NexcelDataObject.GroupData(groupId, resultDataArray).json();
258 			console.log('POSTing Data :' + groupData + '<br/> URL :' + this.dataServiceUrl + NexcelDataObject.GROUP_DATA);
259 			jQuery.ajax({
260 				type : "POST",
261 				url : this.dataServiceUrl + NexcelDataObject.GROUP_DATA,
262 				data : {
263 					'groupData' : groupData
264 				},
265 				success : function(result) {
266 					console.log("Result :" + result);
267 					//  groupId = JSON.parse(result).id;
268 				},
269 				async : true
270 			});
271 			return groupId;
272 		},
273 		/**
274 		 * @name storeGroupData
275 		 * @memberOf NexcelUIDataService
276 		 * @method 
277 		 * @return {Integer} groupId the Id of teh group which is updated.
278 		 * @description An Asynchronous process. On page loading, the groups in the workbook will be loaded and passed to to the call back function so that it can set up UI for the groups in the UI.
279 		 * @param wsId The worksheet id
280 		 * @param callBack {Function} A Callback wunction will be called once the process is completed.
281 		 * @function
282 		 */
283 		loadGroup : function(wsId, callBack) {
284 			jQuery.ajax({
285 				type : "GET",
286 				url : this.dataServiceUrl + NexcelDataObject.GROUP,
287 				data : {
288 					'wsId' : wsId
289 				},
290 				success : function(result) {
291 					callBack(NexcelDataObject.Group().parse(result));
292 				},
293 				async : false
294 			});
295 		}
296 	};
297 	return NexcelUIDataService;
298 });