public static JObject CreateEntityObject(string model, int? id = null) { JObject result = new JObject(); result.SetModel(model); if (id != null) result.SetId(id ?? 0); result.Add("fields", new JObject()); return result; }
private JObject RetreiveObjectFromRow(DataRow row, bool diffOnly = false) { int? pk = row.Field<int?>(this.Table.Columns[KeyColumn]); string modelName = row.Field<string>(this.Table.Columns[ModelColumn]); JObject obj = new JObject(); obj.Add(FieldsField, new JObject()); if (pk != null) obj.SetId(pk ?? 0); if (modelName == null) throw new Exception("Error, model must be set for data field"); obj.SetModel(modelName); if (this.ModelMappings.ContainsKey(modelName)) { Dictionary<string, string> tableToModel = this.ModelMappings[modelName].TableToModel; EntityModel model = DonationModels.GetModel(modelName); foreach (string col in this.Columns) { JObject found = null; if (obj.GetId() != null) this.CachedObjectTable.TryGetValue(new Tuple<string, int?>(obj.GetModel(), obj.GetId()), out found); string fieldName = tableToModel[col]; string value = row[this.TrueColumnName(col)] == null ? null : row[this.TrueColumnName(col)].ToString(); if (!model.GetField(fieldName).ReadOnly && (!diffOnly || found == null || !Util.EqualsEx(found.GetField(fieldName), value))) obj.SetField(fieldName, value); } return obj; } else throw new Exception("Model mapping for " + modelName + " unregistered"); }
/** * Calling this with diffOnly=true will, if there is already a cached object, return * only those fields whose values have changed. */ public JObject SaveObject(bool diffOnly = false) { JObject obj = new JObject(); obj.Add(FieldsField, new JObject()); if (this.CachedData != null) { if (this.CachedData.GetId() != null) obj.SetId(this.CachedData.GetId() ?? 0); obj.SetModel(this.CachedData.GetModel()); } foreach (KeyValuePair<string, FieldBinding> entry in this.Bindings) { FieldModel field = this.SearchForm ? this.Model.GetSearchField(entry.Key).FieldType : this.Model.GetField(entry.Key); string fieldValue = entry.Value.RetreiveField(); if (!field.ReadOnly && (!diffOnly || this.CachedData == null || !Util.EqualsEx(fieldValue, this.CachedData.GetField(entry.Key)))) obj.SetField(entry.Key, fieldValue); } Console.WriteLine(obj.ToString()); return obj; }