Ejemplo n.º 1
0
        // e.g. componentPropertyNameChain = "Name.First" for patient first name
        internal string GetAuditNoteForComponent(AbstractEntityPersister persister, ComponentType componentType, string componentPropertyNameChain, IComponentAuditStrategy propertyAuditStrategy)
        {
            var noteBulder = new StringBuilder();

            Type componentDotNetType = componentType.ReturnedClass;

            foreach (PropertyInfo propertyInfo in componentDotNetType.GetProperties())
            {
                if (propertyAuditStrategy.IsExcludedFromAudit(propertyInfo))
                {
                    continue;
                }

                var ignoreMappingTypeAttributes = propertyInfo.GetCustomAttributes(typeof(IgnoreMappingAttribute), false);
                if (ignoreMappingTypeAttributes.Length != 0)
                {
                    continue;
                }

                string propertyName = propertyInfo.Name;
                var    wholeComponentPropertyNameChain = string.Format("{0}.{1}", componentPropertyNameChain, propertyName);

                var subComponentType =
                    componentType.Subtypes.FirstOrDefault(p => p.ReturnedClass.FullName == propertyInfo.PropertyType.FullName && p.IsComponentType)
                    as
                    ComponentType;

                string note;

                if (subComponentType == null)
                {
                    string columnName = string.Join(",", persister.GetPropertyColumnNames(wholeComponentPropertyNameChain));

                    note = propertyAuditStrategy.GetAuditNoteForNonComponentProperty(propertyInfo, columnName);
                }
                else
                {
                    note = GetAuditNoteForComponent(
                        persister,
                        subComponentType,
                        wholeComponentPropertyNameChain,
                        propertyAuditStrategy.GetComponentPropertyAuditStrategy(propertyInfo));
                }

                if (!string.IsNullOrWhiteSpace(note))
                {
                    noteBulder.AppendLine();
                }
            }

            return(noteBulder.ToString().Trim());
        }
 /// <summary>
 /// Get the column names for a given property as a comma-delimited string of unbracketed names.
 /// For a collection property, the column name is the inverse foreign key (i.e. the column on
 /// the other table that points back to the persister's table)
 /// </summary>
 /// <param name="persister"></param>
 /// <param name="propertyName"></param>
 /// <returns></returns>
 string GetPropertyColumnNames(AbstractEntityPersister persister, string propertyName, IType propType)
 {
     string[] propColumnNames = null;
     if (propType.IsCollectionType)
     {
         propColumnNames = ((CollectionType)propType).GetReferencedColumns((ISessionFactoryImplementor)this._sessionFactory);
     }
     else
     {
         propColumnNames = persister.GetPropertyColumnNames(propertyName);
     }
     if (propColumnNames == null || propColumnNames.Length == 0)
     {
         // this happens when the property is part of the key
         propColumnNames = persister.KeyColumnNames;
     }
     return(CatColumnNames(propColumnNames));
 }
Ejemplo n.º 3
0
        private static object CopyOnlyForeignKeyProperties(object entity, System.Type entityType,
                                                           AbstractEntityPersister entityMetadata, DeepCloneOptions opts, DeepCloneParentEntity parentEntity)
        {
            var propertyInfos = entityType.GetProperties();

            //Copy only Fks
            foreach (var propertyInfo in propertyInfos
                     .Where(p => opts.CanCloneIdentifier(entityType) || entityMetadata.IdentifierPropertyName != p.Name)
                     .Where(p => !opts.GetIgnoreMembers(entityType).Contains(p.Name))
                     .Where(p => p.GetSetMethod(true) != null))
            {
                IType propertyType;
                try
                {
                    propertyType = entityMetadata.GetPropertyType(propertyInfo.Name);
                }
                catch (Exception)
                {
                    continue;
                }
                if (!NHibernateUtil.IsPropertyInitialized(entity, propertyInfo.Name))
                {
                    continue;
                }
                var propertyValue = propertyInfo.GetValue(entity, null);
                if (!NHibernateUtil.IsInitialized(propertyValue))
                {
                    continue;
                }

                var colNames = entityMetadata.GetPropertyColumnNames(propertyInfo.Name);
                if (!propertyType.IsEntityType)
                {
                    continue;
                }
                //Check if we have a parent entity and that is bidirectional related to the current property (one-to-many)
                if (parentEntity.ReferencedColumns.SequenceEqual(colNames))
                {
                    propertyInfo.SetValue(entity, parentEntity.Entity, null);
                }
            }
            return(entity);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get the column names for a given property as a comma-delimited string of unbracketed names.
        /// </summary>
        /// <param name="persister"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        string GetPropertyColumnNames(AbstractEntityPersister persister, string propertyName)
        {
            var propColumnNames = persister.GetPropertyColumnNames(propertyName);

            if (propColumnNames.Length == 0)
            {
                // this happens when the property is part of the key
                propColumnNames = persister.KeyColumnNames;
            }
            var sb = new StringBuilder();

            foreach (var s in propColumnNames)
            {
                if (sb.Length > 0)
                {
                    sb.Append(',');
                }
                sb.Append(UnBracket(s));
            }
            return(sb.ToString());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets the simple (non-Entity) property that has the given columns
        /// </summary>
        /// <param name="persister"></param>
        /// <param name="columnNames">Comma-delimited column name string</param>
        /// <returns></returns>
        string GetPropertyNameForColumn(AbstractEntityPersister persister, string columnNames)
        {
            var propNames = persister.PropertyNames;
            var propTypes = persister.PropertyTypes;

            for (int i = 0; i < propNames.Length; i++)
            {
                var propName = propNames[i];
                var propType = propTypes[i];
                if (propType.IsAssociationType)
                {
                    continue;
                }
                var columnArray = persister.GetPropertyColumnNames(i);
                var columns     = CatColumnNames(columnArray);
                if (columns == columnNames)
                {
                    return(propName);
                }
            }
            return(persister.IdentifierPropertyName);
        }
 /// <summary>
 /// Get the column names for a given property as a comma-delimited string of unbracketed names.
 /// For a collection property, the column name is the inverse foreign key (i.e. the column on
 /// the other table that points back to the persister's table)
 /// </summary>
 /// <param name="persister"></param>
 /// <param name="propertyName"></param>
 /// <param name="propType"></param>
 /// <returns></returns>
 string[] GetPropertyColumnNames(AbstractEntityPersister persister, string propertyName, IType propType)
 {
     string[] propColumnNames = null;
     if (propType.IsCollectionType)
     {
         propColumnNames = ((CollectionType)propType).GetReferencedColumns((ISessionFactoryImplementor)this._sessionFactory);
     }
     else
     {
         propColumnNames = persister.GetPropertyColumnNames(propertyName);
     }
     if (propColumnNames == null || propColumnNames.Length == 0)
     {
         // this happens when the property is part of the key
         propColumnNames = persister.KeyColumnNames;
     }
     // HACK for NHibernate formula: when using formula propColumnNames[0] equals null
     if (propColumnNames[0] == null)
     {
         propColumnNames = new string[] { propertyName };
     }
     return(UnBracket(propColumnNames));
 }
        /// <summary>
        /// Gets the properties matching the given columns.  May be a component, but will not be an association.
        /// </summary>
        /// <param name="persister"></param>
        /// <param name="columnNames">Array of column names</param>
        /// <returns></returns>
        static string[] GetPropertyNamesForColumns(AbstractEntityPersister persister, string[] columnNames)
        {
            var propNames = persister.PropertyNames;
            var propTypes = persister.PropertyTypes;

            for (int i = 0; i < propNames.Length; i++)
            {
                var propName = propNames[i];
                var propType = propTypes[i];
                if (propType.IsAssociationType)
                {
                    continue;
                }
                var columnArray = persister.GetPropertyColumnNames(i);
                // HACK for NHibernate formula: when using formula GetPropertyColumnNames(i) returns an array with the first element null
                if (columnArray[0] == null)
                {
                    continue;
                }
                if (NamesEqual(columnArray, columnNames))
                {
                    return new string[] { propName }
                }
                ;
            }

            // If we got here, maybe the property is the identifier
            var keyColumnArray = persister.KeyColumnNames;

            if (NamesEqual(keyColumnArray, columnNames))
            {
                if (persister.IdentifierPropertyName != null)
                {
                    return(new string[] { persister.IdentifierPropertyName });
                }
                if (persister.IdentifierType.IsComponentType)
                {
                    var compType = (ComponentType)persister.IdentifierType;
                    return(compType.PropertyNames);
                }
            }

            if (columnNames.Length > 1)
            {
                // go one-by-one through columnNames, trying to find a matching property.
                // TODO: maybe this should split columnNames into all possible combinations of ordered subsets, and try those
                var propList = new List <string>();
                var prop     = new string[1];
                for (int i = 0; i < columnNames.Length; i++)
                {
                    prop[0] = columnNames[i];
                    var names = GetPropertyNamesForColumns(persister, prop);  // recursive call
                    if (names != null)
                    {
                        propList.AddRange(names);
                    }
                }
                if (propList.Count > 0)
                {
                    return(propList.ToArray());
                }
            }
            return(null);
        }