public void LoadInstance(JObject instance) { if (instance.GetModel().IEquals("donation")) this.LoadText(instance.GetDonationDonorDisplayName(this.PublicFacing)); else if (instance.GetModel().IEquals("donor")) this.LoadText(instance.GetDonorDisplayName(this.PublicFacing)); else throw new Exception("Error, this only works with donation or donor entities"); }
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"); }
public void AddRow(JObject dataObj) { int? primaryKey = dataObj.GetId(); string modelName = dataObj.GetModel(); if (primaryKey != null) { foreach (DataRow row in this.Table.Rows) { if (row[ModelColumn].ToString().IEquals(modelName) && row[KeyColumn].ToString().Equals(primaryKey.ToString())) { this.FillObjectToRow(row, dataObj); return; } } } DataRow newRow = this.Table.Rows.Add(primaryKey, modelName); this.FillObjectToRow(newRow, dataObj); }
private void FillObjectToRow(DataRow row, JObject dataObj) { int? primaryKey = dataObj.GetId(); string modelName = dataObj.GetModel(); EntityModel model = DonationModels.GetModel(modelName); if (this.ModelMappings.ContainsKey(modelName)) { Dictionary<string,string> tableToModel = this.ModelMappings[modelName].TableToModel; foreach (string col in this.Columns) { string trueColumn = this.TrueColumnName(col); FieldModel field = model.GetField(tableToModel[col]); string trueValue = dataObj.GetField(tableToModel[col]); row[trueColumn] = trueValue; if (field is EntityFieldModel) { EntityFieldModel entityField = field as EntityFieldModel; EntitySelectionCache cache = this.Context.GetEntitySelectionCache(entityField.ModelName); cache.RequestRefresh(EntitySelectionCacheRefreshType.Strong); var mapping = cache.GetLatestCache(); if (trueValue != null) { int fk = int.Parse(trueValue); string name; if (mapping.TryGetLeftToRight(fk, out name)) row[col] = name; else row[col] = row[trueColumn]; } else { row[col] = ""; } } else { row[col] = trueValue; } } } else throw new Exception("Model mapping for " + modelName + " unregistered"); }