Skip to content

Commit 74fbe19

Browse files
committed
document getResultColumns. Add getReferenceIds, serialize and unserialize helper methods for working with getReferenceValue REST method
1 parent 050ccd8 commit 74fbe19

1 file changed

Lines changed: 310 additions & 0 deletions

File tree

coreBOSAPI.js

Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ angular.module('coreBOSAPIservice', [])
231231

232232
/**
233233
* Get Result Column Names.
234+
* @param result: array Set of rows obtained from a Query
235+
* @returns columns: array of names (strings) of columns present in result set
234236
*/
235237
corebosAPI.getResultColumns = function(result) {
236238
var columns = [];
@@ -242,6 +244,25 @@ angular.module('coreBOSAPIservice', [])
242244
return columns;
243245
};
244246

247+
/**
248+
* Get Reference IDs from Result Set.
249+
* @param result: array Set of rows obtained from a Query
250+
* @param columns: array of column names to get IDs from (should be reference fields)
251+
* @returns ids: array of unique REST IDs (strings) present in all indicated columns of result set
252+
*/
253+
corebosAPI.getReferenceIDsFromResultSet = function(result,columns) {
254+
var rdoids = [];
255+
if(result != null && result.length != 0 && columns != null && columns.length != 0) {
256+
angular.forEach(result, function(value, key) {
257+
angular.forEach(columns, function(col, ck) {
258+
if (value[col]!='' && value[col]!= null && rdoids.indexOf(value[col])==-1)
259+
rdoids.push(value[col]);
260+
});
261+
});
262+
}
263+
return rdoids;
264+
};
265+
245266
/**
246267
* List types (modules) available.
247268
*/
@@ -466,6 +487,295 @@ angular.module('coreBOSAPIservice', [])
466487
});
467488
};
468489

490+
/**
491+
* Convert javascript object into PHP serialize format
492+
* Helper method to interface with PHP REST methods
493+
* Copied from php.js project: Thank you !!
494+
*/
495+
corebosAPI.serialize = function(mixed_value) {
496+
// discuss at: http://phpjs.org/functions/serialize/
497+
// original by: Arpad Ray (mailto:arpad@php.net)
498+
// improved by: Dino
499+
// improved by: Le Torbi (http://www.letorbi.de/)
500+
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net/)
501+
// bugfixed by: Andrej Pavlovic
502+
// bugfixed by: Garagoth
503+
// bugfixed by: Russell Walker (http://www.nbill.co.uk/)
504+
// bugfixed by: Jamie Beck (http://www.terabit.ca/)
505+
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net/)
506+
// bugfixed by: Ben (http://benblume.co.uk/)
507+
// input by: DtTvB (http://dt.in.th/2008-09-16.string-length-in-bytes.html)
508+
// input by: Martin (http://www.erlenwiese.de/)
509+
// note: We feel the main purpose of this function should be to ease the transport of data between php & js
510+
// note: Aiming for PHP-compatibility, we have to translate objects to arrays
511+
// example 1: serialize(['Kevin', 'van', 'Zonneveld']);
512+
// returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
513+
// example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});
514+
// returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'
515+
516+
var val, key, okey, ktype = '', vals = '', count = 0, _utf8Size = function(str) {
517+
var size = 0, i = 0, l = str.length, code = '';
518+
for ( i = 0; i < l; i++) {
519+
code = str.charCodeAt(i);
520+
if (code < 0x0080) {
521+
size += 1;
522+
} else if (code < 0x0800) {
523+
size += 2;
524+
} else {
525+
size += 3;
526+
}
527+
}
528+
return size;
529+
}, _getType = function(inp) {
530+
var match, key, cons, types, type = typeof inp;
531+
532+
if (type === 'object' && !inp) {
533+
return 'null';
534+
}
535+
536+
if (type === 'object') {
537+
if (!inp.constructor) {
538+
return 'object';
539+
}
540+
cons = inp.constructor.toString();
541+
match = cons.match(/(\w+)\(/);
542+
if (match) {
543+
cons = match[1].toLowerCase();
544+
}
545+
types = ['boolean', 'number', 'string', 'array'];
546+
for (key in types) {
547+
if (cons == types[key]) {
548+
type = types[key];
549+
break;
550+
}
551+
}
552+
}
553+
return type;
554+
}, type = _getType(mixed_value);
555+
556+
switch (type) {
557+
case 'function':
558+
val = '';
559+
break;
560+
case 'boolean':
561+
val = 'b:' + ( mixed_value ? '1' : '0');
562+
break;
563+
case 'number':
564+
val = (Math.round(mixed_value) == mixed_value ? 'i' : 'd') + ':' + mixed_value;
565+
break;
566+
case 'string':
567+
val = 's:' + _utf8Size(mixed_value) + ':"' + mixed_value + '"';
568+
break;
569+
case 'array':
570+
case 'object':
571+
val = 'a';
572+
/*
573+
if (type === 'object') {
574+
var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/);
575+
if (objname == undefined) {
576+
return;
577+
}
578+
objname[1] = this.serialize(objname[1]);
579+
val = 'O' + objname[1].substring(1, objname[1].length - 1);
580+
}
581+
*/
582+
583+
for (key in mixed_value) {
584+
if (mixed_value.hasOwnProperty(key)) {
585+
ktype = _getType(mixed_value[key]);
586+
if (ktype === 'function') {
587+
continue;
588+
}
589+
590+
okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
591+
vals += this.serialize(okey) + this.serialize(mixed_value[key]);
592+
count++;
593+
}
594+
}
595+
val += ':' + count + ':{' + vals + '}';
596+
break;
597+
case 'undefined':
598+
// Fall-through
599+
default:
600+
// if the JS object has a property which contains a null value, the string cannot be unserialized by PHP
601+
val = 'N';
602+
break;
603+
}
604+
if (type !== 'object' && type !== 'array') {
605+
val += ';';
606+
}
607+
return val;
608+
};
609+
610+
/**
611+
* Convert PHP serialized string into javascript object
612+
* Helper method to interface with PHP REST methods
613+
* Copied from php.js project: Thank you !!
614+
*/
615+
corebosAPI.unserialize = function(data) {
616+
// discuss at: http://phpjs.org/functions/unserialize/
617+
// original by: Arpad Ray (mailto:arpad@php.net)
618+
// improved by: Pedro Tainha (http://www.pedrotainha.com)
619+
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
620+
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
621+
// improved by: Chris
622+
// improved by: James
623+
// improved by: Le Torbi
624+
// improved by: Eli Skeggs
625+
// bugfixed by: dptr1988
626+
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
627+
// bugfixed by: Brett Zamir (http://brett-zamir.me)
628+
// revised by: d3x
629+
// input by: Brett Zamir (http://brett-zamir.me)
630+
// input by: Martin (http://www.erlenwiese.de/)
631+
// input by: kilops
632+
// input by: Jaroslaw Czarniak
633+
// note: We feel the main purpose of this function should be to ease the transport of data between php & js
634+
// note: Aiming for PHP-compatibility, we have to translate objects to arrays
635+
// example 1: unserialize('a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}');
636+
// returns 1: ['Kevin', 'van', 'Zonneveld']
637+
// example 2: unserialize('a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}');
638+
// returns 2: {firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'}
639+
640+
var utf8Overhead = function(chr) {
641+
// http://phpjs.org/functions/unserialize:571#comment_95906
642+
var code = chr.charCodeAt(0);
643+
if (code < 0x0080) {
644+
return 0;
645+
}
646+
if (code < 0x0800) {
647+
return 1;
648+
}
649+
return 2;
650+
};
651+
var read_until = function(data, offset, stopchr) {
652+
var i = 2, buf = [], chr = data.slice(offset, offset + 1);
653+
654+
while (chr != stopchr) {
655+
if ((i + offset) > data.length) {
656+
return [0,'']; //('Error', 'Invalid');
657+
}
658+
buf.push(chr);
659+
chr = data.slice(offset + (i - 1), offset + i);
660+
i += 1;
661+
}
662+
return [buf.length, buf.join('')];
663+
};
664+
var read_chrs = function(data, offset, length) {
665+
var i, chr, buf;
666+
667+
buf = [];
668+
for ( i = 0; i < length; i++) {
669+
chr = data.slice(offset + (i - 1), offset + i);
670+
buf.push(chr);
671+
length -= utf8Overhead(chr);
672+
}
673+
return [buf.length, buf.join('')];
674+
};
675+
var _unserialize = function(data, offset) {
676+
var dtype, dataoffset, keyandchrs, keys, contig, length, array, readdata, readData, ccount, stringlength, i, key, kprops, kchrs, vprops, vchrs, value, chrs = 0, typeconvert = function(x) {
677+
return x;
678+
};
679+
680+
if (!offset) {
681+
offset = 0;
682+
}
683+
dtype = (data.slice(offset, offset + 1)).toLowerCase();
684+
685+
dataoffset = offset + 2;
686+
687+
switch (dtype) {
688+
case 'i':
689+
typeconvert = function(x) {
690+
return parseInt(x, 10);
691+
};
692+
readData = read_until(data, dataoffset, ';');
693+
chrs = readData[0];
694+
readdata = readData[1];
695+
dataoffset += chrs + 1;
696+
break;
697+
case 'b':
698+
typeconvert = function(x) {
699+
return parseInt(x, 10) !== 0;
700+
};
701+
readData = read_until(data, dataoffset, ';');
702+
chrs = readData[0];
703+
readdata = readData[1];
704+
dataoffset += chrs + 1;
705+
break;
706+
case 'd':
707+
typeconvert = function(x) {
708+
return parseFloat(x);
709+
};
710+
readData = read_until(data, dataoffset, ';');
711+
chrs = readData[0];
712+
readdata = readData[1];
713+
dataoffset += chrs + 1;
714+
break;
715+
case 'n':
716+
readdata = null;
717+
break;
718+
case 's':
719+
ccount = read_until(data, dataoffset, ':');
720+
chrs = ccount[0];
721+
stringlength = ccount[1];
722+
dataoffset += chrs + 2;
723+
724+
readData = read_chrs(data, dataoffset + 1, parseInt(stringlength, 10));
725+
chrs = readData[0];
726+
readdata = readData[1];
727+
dataoffset += chrs + 2;
728+
// if (chrs != parseInt(stringlength, 10) && chrs != readdata.length) {
729+
// error('SyntaxError', 'String length mismatch');
730+
// }
731+
break;
732+
case 'a':
733+
readdata = {};
734+
735+
keyandchrs = read_until(data, dataoffset, ':');
736+
chrs = keyandchrs[0];
737+
keys = keyandchrs[1];
738+
dataoffset += chrs + 2;
739+
740+
length = parseInt(keys, 10);
741+
contig = true;
742+
743+
for ( i = 0; i < length; i++) {
744+
kprops = _unserialize(data, dataoffset);
745+
kchrs = kprops[1];
746+
key = kprops[2];
747+
dataoffset += kchrs;
748+
749+
vprops = _unserialize(data, dataoffset);
750+
vchrs = vprops[1];
751+
value = vprops[2];
752+
dataoffset += vchrs;
753+
754+
if (key !== i)
755+
contig = false;
756+
757+
readdata[key] = value;
758+
}
759+
760+
if (contig) {
761+
array = new Array(length);
762+
for ( i = 0; i < length; i++)
763+
array[i] = readdata[i];
764+
readdata = array;
765+
}
766+
767+
dataoffset += 1;
768+
break;
769+
default:
770+
//error('SyntaxError', 'Unknown / Unhandled data type(s): ' + dtype);
771+
break;
772+
}
773+
return [dtype, dataoffset - offset, typeconvert(readdata)];
774+
};
775+
776+
return _unserialize((data + ''), 0)[2];
777+
};
778+
469779
return corebosAPI;
470780
}
471781
)

0 commit comments

Comments
 (0)