public override string GetAuditNoteForComponentProperty(AbstractEntityPersister persister, ComponentType componentType, int propertyIndexToAudit)
        {
            string componentPropertyName = persister.PropertyNames[propertyIndexToAudit];

            object oldComponentValue = _oldStates[propertyIndexToAudit];
            object newComponentValue = _newStates[propertyIndexToAudit];

            string note = GetAuditNoteForComponent(persister, componentType, componentPropertyName, new UpdateComponentAuditStrategy(oldComponentValue, newComponentValue));

            return note;
        }
		public ComponentJoin(FromClause fromClause, FromElement origin, string alias, string componentPath, ComponentType componentType)
			: base(fromClause, origin, alias)
		{
			this.componentPath = componentPath;
			this.componentType = componentType;
			componentProperty = StringHelper.Unqualify(componentPath);
			fromClause.AddJoinByPathMap(componentPath, this);
			InitializeComponentJoin(new ComponentFromElementType(this));

			string[] cols = origin.GetPropertyMapping("").ToColumns(TableAlias, componentProperty);
			columns = string.Join(", ", cols);
		}
        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 ( note );
                }
            }

            return noteBulder.ToString().Trim();
        }
        public override string GetAuditNoteForComponentProperty(AbstractEntityPersister persister, ComponentType componentType, int propertyIndexToAudit)
        {
            string componentPropertyName = persister.PropertyNames[propertyIndexToAudit];

            object componentValue = _states[propertyIndexToAudit];

            string note = string.Empty;

            if (componentValue != null)
            {
                note = GetAuditNoteForComponent(persister, componentType, componentPropertyName, new DefaultComponentAuditStrategy(componentValue));
            }

            return note;
        }
 protected virtual void AddComponent(ClassMap map, Property property, ComponentType componentType)
 {
     // Assuming componentType.PropertyNames match up to property.ColumnIterator...
     for (var propertyIndex = 0; propertyIndex < componentType.PropertyNames.Length; propertyIndex++)
     {
         var columnCount = componentType.Subtypes[propertyIndex].GetColumnSpan(_mapping);
         if (columnCount != 1)
             throw new NotSupportedException(
                 string.Format(
                     "The multi-column property {0}.{1}.{2} is not supported. Each property must map to a single column",
                     property.PersistentClass.ClassName,
                     property.Name,
                     componentType.PropertyNames[propertyIndex]));
         var propertyPath = property.Name + "." + componentType.PropertyNames[propertyIndex];
         var columnName = property.ColumnIterator.ElementAt(propertyIndex).Text;
         map.Add(propertyPath, columnName);
     }
 }
Beispiel #6
0
        public Model(ISessionFactory sessionfactory, ComponentType type)
        {
            if (type == null) throw new ArgumentNullException("type");

            IsComponent = true;
            Type = type.ReturnedClass;
            ComponentType = type;
            Metadata = sessionfactory.GetClassMetadata(Type);
            Properties = new Dictionary<string, IType>();
            Components = new Dictionary<string, Model>();
            BelongsTos = new Dictionary<string, ManyToOneType>();
            OneToOnes = new Dictionary<string, OneToOneType>();
            Anys = new Dictionary<string, AnyType>();
            HasManys = new Dictionary<string, Collection>();
            HasAndBelongsToManys = new Dictionary<string, Collection>();
            foreach (var pc in type.PropertyNames) {
                var index = type.GetPropertyIndex(pc);
                var x = type.Subtypes[index];
                CategorizeProperty(sessionfactory, x, pc);
            }
        }
        /// <summary>
        /// Connect the related entities based on the foreign key values found in a component type.
        /// This updates the values of the component's properties.
        /// </summary>
        /// <param name="propName">Name of the (component) property of the entity.  May be null if the property is the entity's identifier.</param>
        /// <param name="compType">Type of the component</param>
        /// <param name="entityInfo">Breeze EntityInfo</param>
        /// <param name="meta">Metadata for the entity class</param>
        private void FixupComponentRelationships(string propName, ComponentType compType, EntityInfo entityInfo, IClassMetadata meta)
        {
            var compPropNames = compType.PropertyNames;
            var compPropTypes = compType.Subtypes;
            object component = null;
            object[] compValues = null;
            bool isChanged = false;
            for (int j = 0; j < compPropNames.Length; j++)
            {
                var compPropType = compPropTypes[j];
                if (compPropType.IsAssociationType && compPropType.IsEntityType)
                {
                    if (compValues == null)
                    {
                        // get the value of the component's subproperties
                        component = GetPropertyValue(meta, entityInfo.Entity, propName);
                        compValues = compType.GetPropertyValues(component, EntityMode.Poco);
                    }
                    if (compValues[j] == null)
                    {
                        // the related entity is null
                        var relatedEntity = GetRelatedEntity(compPropNames[j], (EntityType)compPropType, entityInfo, meta);
                        if (relatedEntity != null)
                        {
                            compValues[j] = relatedEntity;
                            isChanged = true;
                        }
                    }
                    else if (removeMode)
                    {
                        // remove the relationship
                        compValues[j] = null;
                        isChanged = true;
                    }
                }
            }
            if (isChanged)
            {
                compType.SetPropertyValues(component, compValues, EntityMode.Poco);
            }

        }
		public static void BindComponent( XmlNode node, Component model, System.Type reflectedClass, string className, string path, bool isNullable, Mappings mappings )
		{
			XmlAttribute classNode = node.Attributes[ "class" ];

			if( "dynamic-component".Equals( node.Name ) )
			{
				model.IsEmbedded = false;
				model.IsDynamic = true;
			}
			else if( classNode != null )
			{
				model.ComponentClass = ClassForNameChecked(
					classNode.Value, mappings,
					"component class not found: {0}" );
				model.IsEmbedded = false;
			}
			else if( reflectedClass != null )
			{
				model.ComponentClass = reflectedClass;
				model.IsEmbedded = false;
			}
			else
			{
				// an "embedded" component (ids only)
				model.ComponentClass = model.Owner.MappedClass;
				model.IsEmbedded = true;
			}

			foreach( XmlNode subnode in node.ChildNodes )
			{
				//I am only concerned with elements that are from the nhibernate namespace
				if( subnode.NamespaceURI != Configuration.MappingSchemaXMLNS )
				{
					continue;
				}

				string name = subnode.LocalName; //.Name;
				string propertyName = GetPropertyName( subnode );
				string subpath = propertyName == null ? null : StringHelper.Qualify( path, propertyName );

				CollectionType collectType = CollectionType.CollectionTypeFromString( name );
				IValue value = null;
				if( collectType != null )
				{
					Mapping.Collection collection = collectType.Create( subnode, className, subpath, model.Owner, mappings );
					mappings.AddCollection( collection );
					value = collection;
				}
				else if( "many-to-one".Equals( name ) || "key-many-to-one".Equals( name ) )
				{
					value = new ManyToOne( model.Table );
					BindManyToOne( subnode, ( ManyToOne ) value, subpath, isNullable, mappings );
				}
				else if( "one-to-one".Equals( name ) )
				{
					value = new OneToOne( model.Table, model.Owner.Identifier );
					BindOneToOne( subnode, ( OneToOne ) value, isNullable, mappings );
				}
				else if( "any".Equals( name ) )
				{
					value = new Any( model.Table );
					BindAny( subnode, ( Any ) value, isNullable, mappings );
				}
				else if( "property".Equals( name ) || "key-property".Equals( name ) )
				{
					value = new SimpleValue( model.Table );
					BindSimpleValue( subnode, ( SimpleValue ) value, isNullable, subpath, mappings );
				}
				else if( "component".Equals( name ) || "dynamic-component".Equals( name ) || "nested-composite-element".Equals( name ) )
				{
					System.Type subreflectedClass = model.ComponentClass == null ?
						null :
						GetPropertyType( subnode, mappings, model.ComponentClass, propertyName );
					value = ( model.Owner != null ) ?
						new Component( model.Owner ) : // a class component
						new Component( model.Table ); // a composite element
					BindComponent( subnode, ( Component ) value, subreflectedClass, className, subpath, isNullable, mappings );
				}
				else if( "parent".Equals( name ) )
				{
					model.ParentProperty = propertyName;
				}

				if( value != null )
				{
					model.AddProperty( CreateProperty( value, propertyName, model.ComponentClass, subnode, mappings ) );
				}
			}

			int span = model.PropertySpan;
			string[ ] names = new string[span];
			IType[ ] types = new IType[span];
			Cascades.CascadeStyle[ ] cascade = new Cascades.CascadeStyle[span];
			OuterJoinFetchStrategy[ ] joinedFetch = new OuterJoinFetchStrategy[span];

			int i = 0;
			foreach( Mapping.Property prop in model.PropertyCollection )
			{
				if( prop.IsFormula )
				{
					throw new MappingException( "properties of components may not be formulas: " + prop.Name );
				}
				if( !prop.IsInsertable || !prop.IsUpdateable )
				{
					throw new MappingException( "insert=\"false\", update=\"false\" not supported for properties of components: " + prop.Name );
				}
				names[ i ] = prop.Name;
				types[ i ] = prop.Type;
				cascade[ i ] = prop.CascadeStyle;
				joinedFetch[ i ] = prop.Value.OuterJoinFetchSetting;
				i++;
			}

			IType componentType;
			if( model.IsDynamic )
			{
				componentType = new DynamicComponentType( names, types, joinedFetch, cascade );
			}
			else
			{
				IGetter[ ] getters = new IGetter[span];
				ISetter[ ] setters = new ISetter[span];
				bool foundCustomAccessor = false;
				i = 0;
				foreach( Mapping.Property prop in model.PropertyCollection )
				{
					setters[ i ] = prop.GetSetter( model.ComponentClass );
					getters[ i ] = prop.GetGetter( model.ComponentClass );
					if( !prop.IsBasicPropertyAccessor )
					{
						foundCustomAccessor = true;
					}
					i++;
				}

				componentType = new ComponentType(
					model.ComponentClass,
					names,
					getters,
					setters,
					foundCustomAccessor,
					types,
					joinedFetch,
					cascade,
					model.ParentProperty );
			}
			model.Type = componentType;
		}
Beispiel #9
0
        /// <summary>
        /// Adds a complex type definition
        /// </summary>
        /// <param name="compType">The complex type</param>
        /// <param name="propColumns">The columns which the complex type spans.  These are used to get the length and defaultValues</param>
        /// <returns>The class name and namespace in the form "Location:#Breeze.Nhibernate.NorthwindIBModel"</returns>
        string AddComponent(ComponentType compType, List<ISelectable> propColumns)
        {
            var type = compType.ReturnedClass;

            // "Location:#Breeze.Nhibernate.NorthwindIBModel"
            var classKey = type.Name + ":#" + type.Namespace;
            if (_typeNames.Contains(classKey))
            {
                // Only add a complex type definition once.
                return classKey;
            }

            var cmap = new Dictionary<string, object>();
            _typeList.Insert(0, cmap);  // insert, because complex type definitions must come before they are referenced
            _typeNames.Add(classKey);

            cmap.Add("shortName", type.Name);
            cmap.Add("namespace", type.Namespace);
            cmap.Add("isComplexType", true);

            var dataList = new List<Dictionary<string, object>>();
            cmap.Add("dataProperties", dataList);

            var propNames = compType.PropertyNames;
            var propTypes = compType.Subtypes;
            var propNull = compType.PropertyNullability;

            var colIndex = 0;
            for (int i = 0; i < propNames.Length; i++)
            {
                var propType = propTypes[i];
                var propName = propNames[i];
                if (propType.IsComponentType)
                {
                    // complex type
                    var compType2 = (ComponentType)propType;
                    var span = compType2.GetColumnSpan((IMapping) _sessionFactory);
                    var subColumns = propColumns.Skip(colIndex).Take(span).ToList();
                    var complexTypeName = AddComponent(compType2, subColumns);
                    var compMap = new Dictionary<string, object>();
                    compMap.Add("nameOnServer", propName);
                    compMap.Add("complexTypeName", complexTypeName);
                    compMap.Add("isNullable", propNull[i]);
                    dataList.Add(compMap);
                    colIndex += span;
                }
                else
                {
                    // data property
                    var col = propColumns.Count() == 1 ? propColumns[colIndex] as Column : null;
                    var dmap = MakeDataProperty(propName, propType.Name, propNull[i], col, false, false);
                    dataList.Add(dmap);
                    colIndex++;
                }
            }
            return classKey;
        }
		/// <summary> Extract a component property value. </summary>
		/// <param name="type">The component property types. </param>
		/// <param name="component">The component instance itself. </param>
		/// <param name="propertyPath">The property path for the property to be extracted. </param>
		/// <returns> The property value extracted. </returns>
		protected virtual object GetComponentValue(ComponentType type, object component, string propertyPath)
		{
			int loc = propertyPath.IndexOf('.');
			string basePropertyName = loc > 0 ? propertyPath.Substring(0, (loc) - (0)) : propertyPath;

			string[] propertyNames = type.PropertyNames;
			int index = 0;
			for (; index < propertyNames.Length; index++)
			{
				if (basePropertyName.Equals(propertyNames[index]))
					break;
			}
			if (index == propertyNames.Length)
			{
				throw new MappingException("component property not found: " + basePropertyName);
			}

			object baseValue = type.GetPropertyValue(component, index, EntityMode);

			if (loc > 0)
			{
				ComponentType subtype = (ComponentType)type.Subtypes[index];
				return GetComponentValue(subtype, baseValue, propertyPath.Substring(loc + 1));
			}
			else
			{
				return baseValue;
			}
		}
		public FromElement CreateComponentJoin(ComponentType type)
		{
			// need to create a "place holder" from-element that can store the component/alias for this	component join
			return new ComponentJoin(_fromClause, _origin, _classAlias, _path, type);
		}
 public abstract string GetAuditNoteForComponentProperty( AbstractEntityPersister persister, ComponentType componentType, int propertyIndexToAudit );