1 /*
  2  * SIMPLICITE - runtime & framework
  3  * http://www.simplicite.fr
  4  * Copyright (c)2006-2013 Simplicite Software. All rights reserved.
  5  */
  6 
  7 /*
  8  * Simplicité basic business services (using synchronous calls to Simplicite.Ajax lib)
  9  */
 10 
 11 /** @ignore */
 12 Simplicite._ajax = new Simplicite.Ajax(); // ZZZ it needs to be a new instance (not the Simplicite.Application singleton)
 13 Simplicite._ajax.setAsync(false);
 14 /**
 15  * Set public context usage (only for business objects).
 16  * @param pub True for public context usage
 17  * @function
 18  */
 19 Simplicite.setPublic = function() { this._ajax.setGateway(this._ajax.GATEWAY_UI_PUBLIC); };
 20 
 21 Simplicite.setDebugHandler = function(debugHandler) { this._ajax.setDebugHandler(debugHandler); };
 22 Simplicite.setDebugHandlerActive = function(active) { this._ajax.setDebugHandlerActive(active); };
 23 Simplicite._ajax.setDebugHandler(function(msg) { Simplicite.Tools.debug(msg); });
 24 
 25 Simplicite.setInfoHandler = function(infoHandler) { this._ajax.setInfoHandler(infoHandler); };
 26 Simplicite.setInfoHandlerActive = function(active) { this._ajax.setInfoHandlerActive(active); };
 27 Simplicite._ajax.setInfoHandler(function(msg) { Simplicite.Tools.info(msg); });
 28 
 29 Simplicite.setWarningHandler = function(warningHandler) { this._ajax.setWarningHandler(warningHandler); };
 30 Simplicite.setWarningHandlerActive = function(active) { this._ajax.setWarningHandlerActive(active); };
 31 Simplicite._ajax.setWarningHandler(function(msg) { Simplicite.Tools.warning(msg); });
 32 
 33 Simplicite.setErrorHandler = function(errorHandler) { this._ajax.setErrorHandler(errorHandler); };
 34 Simplicite.setErrorHandlerActive = function(active) { this._ajax.setErrorHandlerActive(active); };
 35 Simplicite._ajax.setErrorHandler(function(err) { Simplicite.Tools.error(Simplicite._ajax.getErrorMessage(err)); });
 36 
 37 Simplicite.getErrorMessage = function(err) { Simplicite._ajax.getErrorMessage(err); };
 38 
 39 /**
 40  * Returns document URL.
 41  * @param object Object name
 42  * @param field Field name
 43  * @param rowId Object record row ID
 44  * @param docId Document ID (can be omitted then a lookup is done on record matching rowId)
 45  * @function
 46  */
 47 Simplicite.documentURL = function(object, field, rowId, docId) { return this._ajax.documentURL(object, field, rowId, docId); };
 48 
 49 /**
 50  * Returns content URL.
 51  * @param file Content file name
 52  * @function
 53  */
 54 Simplicite.contentURL = function(file) { return this._ajax.contentURL(file); };
 55 
 56 /**
 57  * Returns disposition resource URL.
 58  * @param code Resource code
 59  * @param type Resource type (IMG=image (default), ICO=Icon, CSS=stylesheet, JS=Javascript, HTML=HTML)
 60  * @function
 61  */
 62 Simplicite.dispositionResourceURL = function(code, type) { return this._ajax.dispositionResourceURL(code, type); };
 63 
 64 /**
 65  * Returns resource URL.
 66  * @param id Resource ID (e.g. taken from business object or external object resources list in metadata)
 67  * @function
 68  */
 69 Simplicite.resourceURL = function(id) { return this._ajax.resourceURL(id); };
 70 
 71 /**
 72  * Grant data.
 73  * @field
 74  */
 75 Simplicite.grant = undefined;
 76 /**
 77  * Get grant data.
 78  * @param reload Force reload (non required, defaults to false)
 79  * @function
 80  */
 81 Simplicite.getGrant = function(reload) {
 82 	if (this.grant === undefined || reload == true)
 83 		this.grant = this._ajax.getGrant();
 84 	return this.grant;
 85 };
 86 
 87 /**
 88  * Menu data.
 89  * @field
 90  */
 91 Simplicite.menu = undefined;
 92 /**
 93  * Get menu data.
 94  * @param reload Force reload (non required, defaults to false)
 95  * @function
 96  */
 97 Simplicite.getMenu = function(reload) {
 98 	if (this.menu === undefined || reload == true)
 99 		this.menu = this._ajax.getMenu();
100 	return this.menu;
101 };
102 
103 /**
104  * System parameters associative array (with names as index).
105  * @field
106  */
107 Simplicite.sysparams = undefined;
108 /**
109  * Get system parameters array.
110  * @param reload Force reload (non required, defaults to false)
111  * @function
112  */
113 Simplicite.getSysParams = function(reload) {
114 	if (this.sysparams === undefined || reload == true) {
115 		var ps = this._ajax.getSysParams();
116 		this.sysparams = new Array();
117 		for (var i = 0; i < ps.length; i++)
118 			this.sysparams[ps[i].name] = ps[i].value;
119 	}
120 	return this.sysparams;
121 };
122 /**
123  * Get system parameter value.
124  * @param name System parameter name
125  * @function
126  */
127 Simplicite.getSysParam = function(name) {
128 	return this._ajax.getSysParam(undefined, name);
129 };
130 /**
131  * Set system parameter value (for current user).
132  * @param name System parameter name
133  * @param value System parameter value (removes the system parameter if undefined)
134  * @param save Save system parameter as user parameter ? (defaults to false)
135  * @function
136  */
137 Simplicite.setSysParam = function(name, value, save) {
138 	return this._ajax.setSysParam(undefined, name, value, save);
139 };
140 
141 /**
142  * Texts associative array (with codes as index).
143  * @field
144  */
145 Simplicite.texts = undefined;
146 /**
147  * Get texts array.
148  * @param reload Force reload (non required, defaults to false)
149  * @function
150  */
151 Simplicite.getTexts = function(reload) {
152 	if (this.texts === undefined || reload == true) {
153 		var ts = this._ajax.getTexts();
154 		this.texts = new Array();
155 		for (var i = 0; i < ts.length; i++)
156 			this.texts[ts[i].code] = ts[i].value;
157 	}
158 	return this.texts;
159 };
160 /**
161  * Get text value.
162  * @param code Text code
163  * @function
164  */
165 Simplicite.getText = function(code) {
166 	if (this.texts === undefined)
167 		this.getTexts();
168 	return this.texts[code];
169 };
170 
171 /**
172  * News array.
173  * @field
174  */
175 Simplicite.news = undefined;
176 /**
177  * Get news array.
178  * @param reload Force reload (non required, defaults to false)
179  * @function
180  */
181 Simplicite.getNews = function(reload) {
182 	if (this.news === undefined || reload == true)
183 		this.news = this._ajax.getNews(undefined, { inlineImages: true });
184 	return this.news;
185 };
186 
187 /**
188  * Simplicité business object service
189  * @param objName Object name
190  * @param objInstName Object instance name, optional (defauts to service_<object name>)
191  * @class
192  */
193 Simplicite.BusinessObject = function(objName, objInstName) {
194 	this._obj = Simplicite._ajax.getBusinessObject(objName, objInstName);
195 
196 	this._errorHandler = undefined;
197 	this.setErrorHandler = function(errorHandler) { this._errorHandler = errorHandler; };
198 
199 	this.metadata = { name: objName, instance: (objInstName ? objInstName : "service_" + objName), rowidfield: "row_id" };
200 
201 	this.getMetaData = function(context) {
202 		this.metadata = this._obj.getMetaData(undefined, context, { error: this._errorHandler });
203 		return this.metadata;
204 	};
205 
206 	this.getName = function() { return this.metadata.name; };
207 	this.getInstance = function() { return this.metadata.instance; };
208 	this.getLabel = function() { return this.metadata.label; };
209 
210 	this.getFields = function() { return this.metadata.fields; };
211 	this.getField = function(name) {
212 		var n = 0;
213 		var fs = this.getFields();
214 		while (n < fs.length && fs[n].name != name) n++;
215 		return (n == fs.length ? undefined : fs[n]);
216 	};
217 	this.getListValue = function(list, code) {
218 		for (var i = 0; i < list.length; i++) {
219 			var l = list[i];
220 			if (l.code == code) return l.value;
221 		}
222 		return code;
223 	};
224 
225 	this.getRowIdFieldName = function() { return this.metadata.rowidfield; };
226 	this.getRowField = function() { return this.getField(this.getRowIdFieldName()); };
227 	this.getRowId = function() { if (this.item !== undefined) return this.item[this.getRowIdFieldName()]; };
228 
229 	this.item = new Object();
230 
231 	this.get = function(rowId, fields, inlineDocs, inlineThumbs, inlineObjs) {
232 		this.item = this._obj.get(undefined, rowId, { fields: fields, inlineDocs: inlineDocs, inlineThumbs: inlineThumbs, inlineObjs: inlineObjs, error: this._errorHandler });
233 		return this.item;
234 	};
235 
236 	this.populate = function(item, inlineDocs, inlineThumbs, inlineObjs) {
237 		if (item !== undefined) this.item = item;
238 		this.item = this._obj.populate(undefined, this.item, { inlineDocs: inlineDocs, inlineThumbs: inlineThumbs, inlineObjs: inlineObjs, error: this._errorHandler });
239 		return this.item;
240 	};
241 
242 	this.filters = new Object();
243 
244 	this.getFilters = function(reset) {
245 		if (reset === undefined) reset = false;
246 		this.filters = this._obj.getFilters(undefined, { reset: reset });
247 		return this.filters;
248 	};
249 
250 	this.list = new Array();
251 	this.count = 0;
252 	this.maxpage = undefined;
253 	this.page = undefined;
254 
255 	this.search = function(filters, page, inlineDocs, inlineThumbs, inlineObjs) {
256 		if (filters !== undefined) this.filters = filters;
257 		var self = this;
258 		self._obj.search(undefined, this.filters, { page: page, inlineDocs: inlineDocs, inlineThumbs: inlineThumbs, inlineObjs: inlineObjs, error: this._errorHandler });
259 		this.count = this._obj.count;
260 		this.maxpage = this._obj.maxpage;
261 		this.page = this._obj.page;
262 		this.list = this._obj.list;
263 		return this.list;
264 	};
265 
266 	this.save = function(callback, item) {
267 		if (item !== undefined) this.item = item;
268 		this.item = this._obj.save(undefined, this.item, { error: this._errorHandler });
269 		if (Simplicite.UndoRedo !== undefined) Simplicite.UndoRedo.refreshUI();
270 		return this.item;
271 	};
272 
273 	this.create = function(item) {
274 		if (item !== undefined) this.item = item;
275 		this.item = this._obj.create(undefined, this.item, { error: this._errorHandler });
276 		if (Simplicite.UndoRedo !== undefined) Simplicite.UndoRedo.refreshUI();
277 		return this.item;
278 	};
279 
280 	this.update = function(item) {
281 		if (item !== undefined) this.item = item;
282 		this.item = this._obj.update(undefined, this.item, { error: this._errorHandler });
283 		if (Simplicite.UndoRedo !== undefined) Simplicite.UndoRedo.refreshUI();
284 		return this.item;
285 	};
286 
287 	this.del = function(item) {
288 		if (item !== undefined) this.item = item;
289 		this.item = this._obj.del(undefined, this.item, { error: this._errorHandler });
290 		if (Simplicite.UndoRedo !== undefined) Simplicite.UndoRedo.refreshUI();
291 		return this.item;
292 	};
293 
294 	this.crosstabdata = new Object();
295 
296 	this.crosstab = function(ctb, filters) {
297 		if (filters !== undefined) this.filters = filters;
298 		this.crosstabdata = this._obj.crosstab(undefined, ctb, this.filters, { error: this._errorHandler });
299 		return this.crosstabdata;
300 	};
301 
302 	// TODO : add other crosstab and chart related funCtions
303 
304 	this.action = function(act) {
305 		var res = this._obj.action(undefined, act, { error: this._errorHandler });
306 		if (Simplicite.UndoRedo !== undefined) Simplicite.UndoRedo.refreshUI();
307 		return res;
308 	};
309 
310 	this.print = function(pt, all, mailing) {
311 		var res = this._obj.print(undefined, pt, { all: all, mailing: mailing, error: this._errorHandler });
312 		return res;
313 	};
314 
315 	this.setParameter = function(name, value) {
316 		var res = this._obj.setParameter(undefined, name, value, { error: this._errorHandler });
317 		return res;
318 	};
319 
320 	this.removeParameter = function(name) {
321 		var res = this._obj.removeParameter(undefined, name, { error: this._errorHandler });
322 		return res;
323 	};
324 
325 	this.getParameter = function(name) {
326 		var res = this._obj.getParameter(undefined, name, { error: this._errorHandler });
327 		return res;
328 	};
329 
330 	this.completion = function(field, req) {
331 		var res = this._obj.completion(undefined, field, req, { error: this._errorHandler });
332 		return res;
333 	};
334 };
335 
336 /**
337  * Simplicité business process service
338  * @param pcsName Process name
339  * @class
340  */
341 Simplicite.BusinessProcess = function(pcsName) {
342 	this._pcs = Simplicite._ajax.getBusinessProcess(pcsName);
343 
344 	this._errorHandler = undefined;
345 	this.setErrorHandler = function(errorHandler) { this._errorHandler = errorHandler; };
346 
347 	this.metadata = { name: pcsName };
348 
349 	this.getMetaData = function() {
350 		this.metadata = this._pcs.getMetaData(undefined, { error: this._errorHandler });
351 		return this.metadata;
352 	};
353 
354 	this.getName = function() { return this.metadata.name; };
355 	this.getLabel = function() { return this.metadata.label; };
356 
357 	// TODO : to be completed
358 };