/// <summary> /// Tries to fill the document with data returning true if field count matched /// </summary> public static bool TryFillFromJSON(Doc doc, IJsonDataObject jsonData, SetFieldFunc setFieldFunc = null) { if (doc == null || jsonData == null) { return(false); } var allMatch = true; var map = jsonData as JsonDataMap; if (map != null) { foreach (var kvp in map) { var fdef = doc.Schema[kvp.Key]; if (fdef == null) { var ad = doc as IAmorphousData; if (ad != null && ad.AmorphousDataEnabled) { ad.AmorphousData[kvp.Key] = kvp.Value; } allMatch = false; continue; } if (setFieldFunc == null) { doc.SetFieldValue(fdef, kvp.Value); } else { var ok = setFieldFunc(doc, fdef, kvp.Value); if (!ok) { allMatch = false; } } } if (map.Count != doc.Schema.FieldCount) { allMatch = false; } } else { var arr = jsonData as JsonDataArray; if (arr == null) { return(false); } for (var i = 0; i < doc.Schema.FieldCount; i++) { if (i == arr.Count) { break; } var fdef = doc.Schema[i]; if (setFieldFunc == null) { doc.SetFieldValue(fdef, arr[i]); } else { var ok = setFieldFunc(doc, fdef, arr[i]); if (!ok) { allMatch = false; } } } if (arr.Count != doc.Schema.FieldCount) { allMatch = false; } } return(allMatch); }
/// <summary> /// Copies fields from this doc into another doc/form. /// Note: this is shallow copy, as field values for complex types are just copied over /// </summary> public void CopyFields(Doc other, string target = null, bool includeAmorphousData = true, bool invokeAmorphousAfterLoad = true, Func <string, Schema.FieldDef, bool> fieldFilter = null, Func <string, string, bool> amorphousFieldFilter = null) { if (other == null || object.ReferenceEquals(this, other)) { return; } if (target.IsNullOrWhiteSpace()) { if (other is Form frm) { target = frm.DataStoreTargetName; } if (target == null) { target = string.Empty; } } var oad = includeAmorphousData ? other as IAmorphousData : null; if (oad != null && this is IAmorphousData) { var athis = (IAmorphousData)this; if (oad.AmorphousDataEnabled && athis.AmorphousDataEnabled)//only copy IF amorphous behavior is enabled here and in the destination { foreach (var kvp in athis.AmorphousData) { if (amorphousFieldFilter != null && !amorphousFieldFilter(target, kvp.Key)) { continue; } var ofd = other.Schema[kvp.Key]; if (ofd != null) { try { if (!isFieldDefLoaded(target, ofd)) { continue; } other.SetFieldValue(ofd, kvp.Value); continue; }catch {} //this may be impossible, then we assign to amorphous in other } oad.AmorphousData[kvp.Key] = kvp.Value; } } } foreach (var fdef in this.Schema) { if (fieldFilter != null && !fieldFilter(target, fdef)) { continue; } if (!isFieldDefLoaded(target, fdef)) { continue; } var ofd = other.Schema[fdef.Name]; if (ofd == null) { if (oad != null && oad.AmorphousDataEnabled)// IF amorphous behavior is enabled in destination { oad.AmorphousData[fdef.Name] = GetFieldValue(fdef); } continue; } other.SetFieldValue(ofd, GetFieldValue(fdef)); }//foreach if (oad != null && oad.AmorphousDataEnabled && invokeAmorphousAfterLoad) { oad.AfterLoad(target); } }