Common Packable

Here is a Class for packing record information into a container. Once packed the record data may be saved to a table field of type Container for instance.

\Classes\Common_Packable\classDeclaration class Common_Packable implements SysPackable {    Common      common; container  packedCommon;

TableId    tableId; RecId      recId;

#define.CurrentVersion(1) #localMacro.CurrentList tableId, recId, packedCommon #endMacro }

\Classes\Common_Packable\fields private Map fields(SysDictTable _sysDictTable) {    SysDictField    sysDictField; SysDictField   sysDictFieldArray; Map            fieldsMap           = new Map(Types::Integer, Types::Container); Container      con; int            cnt; int            i;     int             j;

//    // Include fields //    cnt = _sysDictTable.fieldCnt; for (i = 1; i <= cnt; i++) {        sysDictField = new SysDictField(tableId, _sysDictTable.fieldCnt2Id(i));

//        // Include each array field separately //        for (j = 1; j <= sysDictField.arraySize; j++) {            sysDictFieldArray = new SysDictField(tableId, _sysDictTable.fieldCnt2Id(i), j); con = [ fieldid2name(tableId, sysDictFieldArray.id) ];            fieldsMap.insert(sysDictFieldArray.id, con); }    }     return fieldsMap; }

\Classes\Common_Packable\init public void init {    ;

tableId = common.TableId; recId  = common.RecId; }

\Classes\Common_Packable\pack public container pack {    ;     this.packCommon; return [#CurrentVersion, #CurrentList]; }

\Classes\Common_Packable\packCommon private void packCommon {    container fieldInfo; container recordInfo; ;

tableId        = common.TableId; recId          = common.RecId;

fieldInfo      = this.packFieldInfo; recordInfo     = this.packRecordInfo;

packedCommon   = [fieldInfo, recordInfo]; }

\Classes\Common_Packable\packFieldInfo private container packFieldInfo {    container       fieldInfo; ;

if (tableId) {        fieldInfo   = this.fields(SysDictTable::newTableId(tableId)).pack; }

return fieldInfo; }

\Classes\Common_Packable\packRecordInfo private container packRecordInfo {    SysDictField    sysDictField; Map            recordMap; MapEnumerator  me; container      recordInfo; ;

if (tableId) {        recordMap   = new Map(Types::Integer, Types::Container); me = this.fields(SysDictTable::newTableId(tableId)).getEnumerator; while (me.moveNext) {            sysDictField    = new SysDictField(tableId, me.currentKey); recordMap.insert(sysDictField.id, [common.(sysDictField.id)]); }        recordInfo  = recordMap.pack; }

return recordInfo; }

\Classes\Common_Packable\parmCommon public Common parmCommon(Common _common = common) {    ;     common = _common; return common; }

\Classes\Common_Packable\unpack public boolean unpack(container packedClass) {    Integer     _version     = conpeek(packedClass,1); ;

switch (_version) {        case #CurrentVersion: {            [_version,#CurrentList] = packedClass; this.unpackCommon; break; }        default : return false; }

return true; }

\Classes\Common_Packable\unpackCommon

private void unpackCommon {    DictTable       dictTable   = new DictTable(tableId); SysDictField   sysDictField; Map            recordMap; MapEnumerator  me; container      fieldInfo; container      recordInfo; int            i;     ;

if (!dictTable) return;

common = dictTable.makeRecord;

[fieldInfo, recordInfo] = packedCommon;

recordMap  = Map::create(recordInfo); me         = Map::create(fieldInfo).getEnumerator; while (me.moveNext) {        sysDictField                = new SysDictField(tableId, me.currentKey); common.(sysDictField.id) = conPeek(recordMap.lookup(sysDictField.id), 1); } }

\Classes\Common_Packable\create public static Common_Packable create(container _packedContainer) {    Common_Packable  common_Packable = new Common_Packable; ;    common_Packable.unpack(_packedContainer); common_Packable.init;

return common_Packable; }

\Classes\Common_Packable\createCommon public static Common_Packable createCommon(Common _common) {    Common_Packable  common_Packable = new Common_Packable; ;    common_Packable.parmCommon(_common); common_Packable.init;

return common_Packable; }