private static void AddOrUpdateAttribute( [NotNull] AttributedAssociation attributedAssociation, [NotNull] IField field, int fieldIndex) { Assert.ArgumentNotNull(field, nameof(field)); const bool includeDeleted = true; AssociationAttribute attribute = attributedAssociation.GetAttribute(field.Name, includeDeleted); if (attribute == null) { _msg.InfoFormat("Adding attribute {0}", field.Name); attribute = attributedAssociation.AddAttribute(field.Name, (FieldType)field.Type); } else { // attribute already registered if (attribute.Deleted) { _msg.WarnFormat( "Attribute {0} was registered as deleted, but exists now. " + "Resurrecting it...", attribute.Name); attribute.RegisterExisting(); } attribute.FieldType = (FieldType)field.Type; } // configure the attribute attribute.SortOrder = fieldIndex; }
// TODO: Is this a static Utils class? Or does each IAttributes implementation have // its specific AttributeHarvester? Or will there be a DatasetHarvester/ModelHarvester // that can create instances of this class (for the right IAttributes implementation) // in order to more loosely couple them with the domain classes? //private readonly IAttributes _attributeContainer; //public AttributeHarvester(IAttributes attributeContainer) //{ // _attributeContainer = attributeContainer; //} #region AttributedAssociation Attribute harvesting public static void HarvestAttributes([NotNull] AttributedAssociation attributedAssociation) { // TODO: support for configurator? attributedAssociation.ClearAttributeMaps(); using (_msg.IncrementIndentation( "Harvesting attributes for attributed association {0}", attributedAssociation.Name)) { const bool allowAlways = true; IRelationshipClass relationshipClass = ModelElementUtils.TryOpenFromMasterDatabase(attributedAssociation, allowAlways); Assert.NotNull(relationshipClass, "Relationship class not found in model master database: {0}", attributedAssociation.Name); var table = (ITable)relationshipClass; IList <IField> fields = DatasetUtils.GetFields(table); for (var fieldIndex = 0; fieldIndex < fields.Count; fieldIndex++) { IField field = fields[fieldIndex]; AddOrUpdateAttribute(attributedAssociation, field, fieldIndex); } DeleteAttributesNotInList(attributedAssociation, fields); attributedAssociation.ClearAttributeMaps(); } }