public static List<PropertyItem> GetCustomFieldPropertyItems(IEnumerable<ICustomFieldDefinition> definitions, Row row, string fieldPrefix) { var list = new List<PropertyItem>(); foreach (var def in definitions) { string name = fieldPrefix + def.Name; var field = row.FindFieldByPropertyName(name) ?? row.FindField(name); list.Add(GetCustomFieldPropertyItem(def, field)); } return list; }
public bool ActivateFor(Row row) { if (ReferenceEquals(null, Target)) return false; attr = Target.GetAttribute<ImageUploadEditorAttribute>(); if (attr == null || attr.DisableDefaultBehavior || attr.EditorType != "ImageUpload") return false; if (!(Target is StringField)) throw new ArgumentException(String.Format( "Field '{0}' on row type '{1}' has a UploadEditor attribute but it is not a String field!", Target.PropertyName ?? Target.Name, row.GetType().FullName)); if (!(row is IIdRow)) throw new ArgumentException(String.Format( "Field '{0}' on row type '{1}' has a UploadEditor attribute but Row type doesn't implement IIdRow!", Target.PropertyName ?? Target.Name, row.GetType().FullName)); if (!attr.OriginalNameProperty.IsEmptyOrNull()) { var originalNameField = row.FindFieldByPropertyName(attr.OriginalNameProperty) ?? row.FindField(attr.OriginalNameProperty); if (ReferenceEquals(null, originalNameField)) throw new ArgumentException(String.Format( "Field '{0}' on row type '{1}' has a UploadEditor attribute but " + "a field with OriginalNameProperty '{2}' is not found!", Target.PropertyName ?? Target.Name, row.GetType().FullName, attr.OriginalNameProperty)); this.originalNameField = (StringField)originalNameField; } var format = attr.FilenameFormat; if (format == null) { format = row.GetType().Name; if (format.EndsWith("Row")) format = format.Substring(0, format.Length - 3); format += "/~"; } this.fileNameFormat = format.Replace("~", SplittedFormat); this.replaceFields = ParseReplaceFields(fileNameFormat, row, Target); this.uploadHelper = new UploadHelper((attr.SubFolder.IsEmptyOrNull() ? "" : (attr.SubFolder + "/")) + (this.fileNameFormat)); return true; }
public bool ActivateFor(Row row) { var attrs = row.GetType().GetCustomAttributes<UpdatableExtensionAttribute>(); if (attrs == null || !attrs.Any()) return false; var sourceByExpression = row.GetFields().ToLookup(x => BracketLocator.ReplaceBrackets(x.Expression.TrimToEmpty(), BracketRemoverDialect.Instance)); this.infoList = attrs.Select(attr => { var info = new RelationInfo(); info.Attr = attr; var rowType = attr.RowType; if (rowType.GetIsAbstract() || !typeof(Row).IsAssignableFrom(rowType)) { throw new ArgumentException(String.Format( "Row type '{1}' has an ExtensionRelation attribute " + "but its specified extension row type '{0}' is not a valid row class!", rowType.FullName, row.GetType().FullName)); } info.RowFactory = FastReflection.DelegateForConstructor<Row>(rowType); info.ListHandlerFactory = FastReflection.DelegateForConstructor<IListRequestProcessor>( typeof(ListRequestHandler<>).MakeGenericType(rowType)); info.SaveHandlerFactory = FastReflection.DelegateForConstructor<ISaveRequestProcessor>( typeof(SaveRequestHandler<>).MakeGenericType(rowType)); info.SaveRequestFactory = FastReflection.DelegateForConstructor<ISaveRequest>( typeof(SaveRequest<>).MakeGenericType(rowType)); info.DeleteHandlerFactory = FastReflection.DelegateForConstructor<IDeleteRequestProcessor>( typeof(DeleteRequestHandler<>).MakeGenericType(rowType)); var thisKey = attr.ThisKey; if (string.IsNullOrEmpty(thisKey)) { if (!(row is IIdRow)) { throw new ArgumentException(String.Format( "Row type '{0}' has an ExtensionRelation attribute " + "but its ThisKey is not specified!", row.GetType().FullName)); } info.ThisKeyField = (Field)(((IIdRow)row).IdField); } else { info.ThisKeyField = row.FindFieldByPropertyName(attr.ThisKey) ?? row.FindField(attr.ThisKey); if (ReferenceEquals(info.ThisKeyField, null)) throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." + "This field is specified for an ExtensionRelation attribute", attr.ThisKey, row.GetType().FullName)); } var ext = info.RowFactory(); var otherKey = attr.OtherKey; if (string.IsNullOrEmpty(otherKey)) { info.OtherKeyField = ext.FindField(info.ThisKeyField.Name); if (ReferenceEquals(info.OtherKeyField, null) && ext is IIdRow) info.OtherKeyField = (Field)(((IIdRow)row).IdField); if (ReferenceEquals(info.OtherKeyField, null)) throw new ArgumentException(String.Format( "Row type '{1}' has an ExtensionRelation attribute " + "but its OtherKey is not specified!", row.GetType().FullName)); } else { info.OtherKeyField = ext.FindFieldByPropertyName(attr.OtherKey) ?? ext.FindField(attr.OtherKey); if (ReferenceEquals(info.OtherKeyField, null)) throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." + "This field is specified for an ExtensionRelation attribute on '{2}'", attr.OtherKey, ext.GetType().FullName, row.GetType().FullName)); } if (!string.IsNullOrEmpty(attr.FilterField)) { info.FilterField = ext.FindFieldByPropertyName(attr.FilterField) ?? ext.FindField(attr.FilterField); if (ReferenceEquals(info.FilterField, null)) throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." + "This field is specified as FilterField for an ExtensionRelation attribute on '{2}'", attr.OtherKey, ext.GetType().FullName, row.GetType().FullName)); info.FilterValue = info.FilterField.ConvertValue(attr.FilterValue, CultureInfo.InvariantCulture); } if (!string.IsNullOrEmpty(attr.PresenceField)) { info.PresenceField = ext.FindFieldByPropertyName(attr.PresenceField) ?? ext.FindField(attr.PresenceField); if (ReferenceEquals(info.PresenceField, null)) throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." + "This field is specified as PresenceField as an ExtensionRelation attribute.", attr.PresenceField, row.GetType().FullName)); info.PresenceValue = attr.PresenceValue; } var extFields = ext.GetFields(); var alias = attr.Alias; var aliasPrefix = attr.Alias + "_"; var joinByKey = new HashSet<string>(extFields.Joins.Keys, StringComparer.OrdinalIgnoreCase); Func<string, string> mapAlias = x => { if (x == "t0" || x == "T0") return alias; if (!joinByKey.Contains(x)) return x; return aliasPrefix + x; }; Func<string, string> mapExpression = x => { if (x == null) return null; return JoinAliasLocator.ReplaceAliases(x, mapAlias); }; info.Mappings = new List<Tuple<Field, Field>>(); foreach (var field in extFields) { if (ReferenceEquals(info.OtherKeyField, field)) continue; if (ReferenceEquals(info.FilterField, field)) continue; var expression = field.Expression.TrimToEmpty(); if (string.IsNullOrEmpty(expression)) continue; expression = mapExpression(expression); expression = BracketLocator.ReplaceBrackets(expression, BracketRemoverDialect.Instance); var match = sourceByExpression[expression].FirstOrDefault(); if (ReferenceEquals(null, match)) continue; if (match.IsTableField()) continue; if (ReferenceEquals(info.ThisKeyField, match)) continue; if (field.GetType() != match.GetType()) throw new ArgumentException(String.Format( "Row type '{0}' has an ExtensionRelation attribute to '{1}'." + "Their '{2}' and '{3}' fields are matched but they have different types ({4} and {5})!", row.GetType().FullName, ext.GetType().FullName, field.PropertyName ?? field.Name, match.PropertyName ?? match.Name, field.GetType().Name, match.GetType().Name)); info.Mappings.Add(new Tuple<Field, Field>(match, field)); } if (info.Mappings.Count == 0) throw new ArgumentException(String.Format( "Row type '{0}' has an ExtensionRelation attribute " + "but no view fields could be matched to extension row '{1}'!", row.GetType().FullName, ext.GetType().FullName)); return info; }).ToList(); return true; }
internal static Dictionary<string, Field> ParseReplaceFields(string fileNameFormat, Row row, Field target) { if (fileNameFormat.IndexOf('|') < 0) return null; var replaceFields = new Dictionary<string, Field>(); int start = 0; while ((start = fileNameFormat.IndexOf('|', start)) >= 0) { var end = fileNameFormat.IndexOf('|', start + 1); if (end <= start + 1) throw new ArgumentException(String.Format( "Field '{0}' on row type '{1}' has a UploadEditor attribute " + "with invalid format string '{2}'!", target.PropertyName ?? target.Name, row.GetType().FullName, fileNameFormat)); var fieldName = fileNameFormat.Substring(start + 1, end - start - 1); var actualName = fieldName; var colon = fieldName.IndexOf(":"); if (colon >= 0) actualName = fieldName.Substring(0, colon); var replaceField = row.FindFieldByPropertyName(actualName) ?? row.FindField(actualName); if (ReferenceEquals(null, replaceField)) { throw new ArgumentException(String.Format( "Field '{0}' on row type '{1}' has a UploadEditor attribute that " + "references field '{2}', but no such field is found!'", target.PropertyName ?? target.Name, row.GetType().FullName, actualName)); } replaceFields['|' + fieldName + '|'] = replaceField; start = end + 1; } return replaceFields; }