private void populateDataWithFieldValues(FieldMap[] fieldMaps, ClarifyDataRow record, ModelData model) { var tableName = record.Table.TableName; if (tableName.Contains(":")) { tableName = tableName.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries).Last(); } var tableMetadata = _metadata.MetadataFor(tableName); foreach (var fieldMap in fieldMaps) { if (fieldMap.Key.IsEmpty()) { continue; } try { var fieldMetadata = fieldMap.FieldNames.Length == 1 ? tableMetadata.MetadataFor(fieldMap.FieldNames[0]) : new FieldSchemaMetadata(); var propertyValue = GetFieldValueForRecord(fieldMap, record); if (propertyValue is string && fieldMap.ShouldEncode) { propertyValue = _encoder.Encode((string)propertyValue); } if (fieldMap.PropertyType == typeof(int)) { propertyValue = Convert.ToInt32(propertyValue); } if (fieldMap.PropertyType == typeof(DateTime)) { var dateTime = Convert.ToDateTime(propertyValue); if (fieldMetadata.IsDateOnlyField()) { propertyValue = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, DateTimeKind.Utc); } else { var utcDateTime = new DateTime(dateTime.Ticks, DateTimeKind.Utc); propertyValue = utcDateTime; } } model[fieldMap.Key] = propertyValue; } catch (Exception ex) { throw new ApplicationException("Could not set property on type {0}. Field: {1}".ToFormat(model.GetType().Name, fieldMap.ToString()), ex); } } }
public static FieldSchemaMetadata MetadataFor(this ISchemaMetadataCache cache, ISchemaField field) { var clarifyField = field as SchemaFieldBase; if (clarifyField == null) { return new FieldSchemaMetadata { Name = field.Name } } ; var tableName = clarifyField.Parent.Name; var tableMetadata = cache.MetadataFor(tableName); return(tableMetadata.MetadataFor(field.Name)); } }
public void Visit(BeginProperty instruction) { var target = _properties.SingleOrDefault(_ => _.Name.EqualsIgnoreCase(instruction.Key.Resolve(_services).ToString())); if (target == null || instruction.Field == null) { return; } var fieldName = instruction.Field.Resolve(_services).ToString(); var field = Table.Fields[fieldName]; if (field == null) { return; } var fieldMetadata = _metadata.MetadataFor(field); Properties.Add(new MappedProperty(target, field, fieldMetadata)); }