Example #1
0
        private void ProcessComponent(component hComponent, Entity newEntity, ITable mappedTable, Dictionary<Class, ComponentSpecification> specifications, string hmNamespace, ParseResults parseResults)
        {
            if (hComponent.@class == null)
            {
                log.ErrorFormat("Could not load component named {0} on class {1} because it does not have a class attribute.", hComponent.name, newEntity.Name);
                return;
            }

            var possibleClasses = GetPossibleClasses(hComponent.@class, hmNamespace, mappedTable.Schema, parseResults);

            if (possibleClasses.Count == 0)
            {
                log.ErrorFormat("Could not load component named {0} on class {1} because we could not find the class named {2}.", hComponent.name, newEntity.Name, hComponent.@class);
                return;
            }
            ComponentSpecification spec = null;

            foreach (var possibleClass in possibleClasses)
            {
                spec = specifications.GetValueOrDefault(possibleClass);

                if (spec != null)
                    break;
            }

            bool createProperties = false;

            if (spec == null)
            {
                // Create a new spec from these.
                spec = new ComponentSpecificationImpl(GetShortClassName(hComponent.@class));
                newEntity.EntitySet.AddComponentSpecification(spec);
                createProperties = true;
            }
            Component component = spec.CreateImplementedComponentFor(newEntity, hComponent.name);
            newEntity.Key.Component = component;

            var mapping = new ComponentMappingImpl();

            foreach (var prop in hComponent.Properties())
            {
                if (createProperties)
                {
                    ComponentProperty idProperty = new ComponentPropertyImpl(prop.name);
                    idProperty.Type = prop.type1;
                    idProperty.ValidationOptions.MaximumLength = prop.length.As<int>();
                    SetPropertyInfoFromParsedCode(possibleClasses, idProperty);

                    spec.AddProperty(idProperty);
                }

                var compProperty = component.GetProperty(prop.name);
                var column = mappedTable.GetColumn(prop.column.UnBackTick());
                if (column == null)
                {
                    // Create the column
                    column = entityProcessor.CreateColumn(compProperty.RepresentedProperty);
                    mapping.FromTable.AddColumn(column);
                }

                mapping.AddPropertyAndColumn(compProperty, column);
            }
            newEntity.EntitySet.MappingSet.AddMapping(mapping);
        }