internal override bool ParseSingleElement(ICollection <XName> unprocessedElements, XElement elem)
        {
            if (elem.Name.LocalName == Property.ElementName)
            {
                Property prop;
                var      conceptualModel = (ConceptualEntityModel)GetParentOfType(typeof(ConceptualEntityModel));
                Debug.Assert(
                    conceptualModel != null,
                    typeof(ComplexType).Name + "ParseSingleElement: Unable to find parent of type ConceptualEntityModel");

                if (conceptualModel != null &&
                    ModelHelper.IsElementComplexProperty(elem, conceptualModel))
                {
                    prop = new ComplexConceptualProperty(this, elem);
                }
                else
                {
                    prop = new ConceptualProperty(this, elem);
                }
                prop.Parse(unprocessedElements);
                AddProperty(prop);
            }
            else
            {
                return(base.ParseSingleElement(unprocessedElements, elem));
            }
            return(true);
        }
        internal ChangeComplexPropertyTypeCommand(ComplexConceptualProperty property, ComplexType newType)
        {
            CommandValidation.ValidateProperty(property);
            CommandValidation.ValidateComplexType(newType);

            _property = property;
            _newType = newType;
        }
        /// <summary>
        ///     Creates a FunctionComplexProperty using a FunctionComplexProperty from prereq command
        /// </summary>
        /// <param name="prereq">Pre-requisite Command which will produce the FunctionComplexProperty inside which the newly produced FunctionComplexProperty will be placed</param>
        /// <param name="property">This must be a valid ComplexConceptualProperty.</param>
        internal CreateFunctionComplexPropertyCommand(CreateFunctionComplexPropertyCommand prereq, ComplexConceptualProperty property)
            : base(PrereqId)
        {
            ValidatePrereqCommand(prereq);
            CommandValidation.ValidateConceptualProperty(property);

            _property = property;
            AddPreReqCommand(prereq);
        }
        /// <summary>
        ///     Creates a FunctionComplexProperty within a ModificationFunction mapped to the passed in
        ///     C-side property.
        /// </summary>
        /// <param name="parentModificationFunction">The parent inside which this FunctionComplexProperty will be placed</param>
        /// <param name="property">This must be a valid Property from the C-Model.</param>
        internal CreateFunctionComplexPropertyCommand(ModificationFunction parentModificationFunction, ComplexConceptualProperty property)
            : base(PrereqId)
        {
            CommandValidation.ValidateModificationFunction(parentModificationFunction);
            CommandValidation.ValidateConceptualProperty(property);

            _parentModificationFunction = parentModificationFunction;
            _property = property;
        }
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            // first create new ComplexType
            _createdComplexType = CreateComplexTypeCommand.CreateComplexTypeWithDefaultName(cpc);
            // add a copy of Entity properties to the ComplexType
            var copyCmd = new CopyPropertiesCommand(new PropertiesClipboardFormat(_properties), _createdComplexType);
            var propertyName = ModelHelper.GetUniqueConceptualPropertyName(
                ComplexConceptualProperty.DefaultComplexPropertyName, _entityType);
            // add a new Property of created ComplexType to the Entity
            var createCPCmd = new CreateComplexPropertyCommand(propertyName, _entityType, _createdComplexType);
            var cp = new CommandProcessor(cpc, copyCmd, createCPCmd);
            cp.Invoke();
            _createdComplexProperty = createCPCmd.Property;

            // preserve mappings
            foreach (var property in _properties)
            {
                if (property is ComplexConceptualProperty)
                {
                    var createdComplexTypeProperty =
                        _createdComplexType.FindPropertyByLocalName(property.LocalName.Value) as ComplexConceptualProperty;
                    Debug.Assert(createdComplexTypeProperty != null, "Copied complex property not found");
                    if (createdComplexTypeProperty != null)
                    {
                        foreach (var complexPropertyMapping in property.GetAntiDependenciesOfType<ComplexProperty>())
                        {
                            PreserveComplexPropertyMapping(cpc, complexPropertyMapping, createdComplexTypeProperty);
                        }

                        foreach (var fcp in property.GetAntiDependenciesOfType<FunctionComplexProperty>())
                        {
                            PreserveFunctionComplexPropertyMapping(cpc, fcp, createdComplexTypeProperty);
                        }
                    }
                }
                else
                {
                    var createdComplexTypeProperty = _createdComplexType.FindPropertyByLocalName(property.LocalName.Value);
                    Debug.Assert(createdComplexTypeProperty != null, "Copied property not found");
                    if (createdComplexTypeProperty != null)
                    {
                        // update EntityTypeMappings
                        foreach (var scalarPropertyMapping in property.GetAntiDependenciesOfType<ScalarProperty>())
                        {
                            PreserveScalarPropertyMapping(cpc, scalarPropertyMapping, createdComplexTypeProperty);
                        }

                        // update ModificationFunctionMappings
                        foreach (var fsp in property.GetAntiDependenciesOfType<FunctionScalarProperty>())
                        {
                            PreserveFunctionScalarPropertyMapping(cpc, fsp, createdComplexTypeProperty);
                        }
                    }
                }
            }
        }
        /// <summary>
        ///     Creates a ComplexProperty in a MappingFragment based on conceptualEntityType passed in.
        /// </summary>
        /// <param name="conceptualEntityType">A C side entity</param>
        /// <param name="property">This must be a valid Property from the C-Model.</param>
        /// <param name="tableColumn">This must be a valid Property from the S-Model.</param>
        internal CreateFragmentComplexPropertyCommand(
            EntityType conceptualEntityType, ComplexConceptualProperty property, Property tableColumn)
            : base(PrereqId)
        {
            CommandValidation.ValidateConceptualEntityType(conceptualEntityType);
            CommandValidation.ValidateConceptualProperty(property);
            CommandValidation.ValidateTableColumn(tableColumn);

            _conceptualEntityType = conceptualEntityType;
            _property = property;
            _tableColumn = tableColumn;
            _mode = Mode.EntityType;
        }
        /// <summary>
        ///     Creates a ComplexProperty in the given MappingFragment.
        /// </summary>
        /// <param name="mappingFragment">The MappingFragment to place this ComplexProperty; cannot be null.</param>
        /// <param name="property">This must be a valid ComplexTypeProperty.</param>
        /// <param name="isPartial"></param>
        internal CreateFragmentComplexPropertyCommand(MappingFragment mappingFragment, ComplexConceptualProperty property)
            : base(PrereqId)
        {
            CommandValidation.ValidateMappingFragment(mappingFragment);
            CommandValidation.ValidateConceptualProperty(property);

            _mappingFragment = mappingFragment;
            if (mappingFragment != null
                && mappingFragment.EntityTypeMapping != null)
            {
                _conceptualEntityType = mappingFragment.EntityTypeMapping.FirstBoundConceptualEntityType;
            }
            _property = property;
            _mode = Mode.MappingFragment;
        }
        internal override bool ParseSingleElement(ICollection<XName> unprocessedElements, XElement elem)
        {
            if (elem.Name.LocalName == Property.ElementName)
            {
                Property prop;
                var conceptualModel = (ConceptualEntityModel)GetParentOfType(typeof(ConceptualEntityModel));
                Debug.Assert(
                    conceptualModel != null,
                    typeof(ComplexType).Name + "ParseSingleElement: Unable to find parent of type ConceptualEntityModel");

                if (conceptualModel != null
                    && ModelHelper.IsElementComplexProperty(elem, conceptualModel))
                {
                    prop = new ComplexConceptualProperty(this, elem);
                }
                else
                {
                    prop = new ConceptualProperty(this, elem);
                }
                prop.Parse(unprocessedElements);
                AddProperty(prop);
            }
            else
            {
                return base.ParseSingleElement(unprocessedElements, elem);
            }
            return true;
        }
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            // get unique name for the property
            var propertyName = ModelHelper.GetUniqueName(typeof(ConceptualProperty), _parentComplexType, _clipboardProperty.PropertyName);

            if (!_clipboardProperty.IsComplexProperty)
            {
                // scalar property case
                var cmd = new CreateComplexTypePropertyCommand(
                    propertyName, _parentComplexType, _clipboardProperty.PropertyType, _clipboardProperty.IsNullable);
                CommandProcessor.InvokeSingleCommand(cpc, cmd);
                _createdProperty = cmd.Property;
            }
            else
            {
                // complex property case
                // first try to find ComplexType by it's name
                var complexTypeNormalizedName = EFNormalizableItemDefaults.DefaultNameNormalizerForEDM(
                    _parentComplexType, _clipboardProperty.PropertyType);
                var items = _parentComplexType.Artifact.ArtifactSet.GetSymbolList(complexTypeNormalizedName.Symbol);
                ComplexType complexType = null;
                foreach (var efElement in items)
                {
                    // the GetSymbolList() method might return more than one element so choose the first ComplexType
                    complexType = efElement as ComplexType;
                    if (complexType != null)
                    {
                        break;
                    }
                }
                if (complexType != null)
                {
                    // if the ComplexType is found, simply use the create command
                    var cmd = new CreateComplexTypePropertyCommand(propertyName, _parentComplexType, complexType, false);
                    CommandProcessor.InvokeSingleCommand(cpc, cmd);
                    _createdProperty = cmd.Property;
                }
                else
                {
                    // in this case we're going to create ComplexProperty with unresolved type
                    var complexProperty = new ComplexConceptualProperty(_parentComplexType, null);
                    complexProperty.ComplexType.SetXAttributeValue(_clipboardProperty.PropertyType);
                    // set the name and add to the parent entity
                    complexProperty.LocalName.Value = propertyName;
                    _parentComplexType.AddProperty(complexProperty);

                    // set other attributes of the property
                    complexProperty.Nullable.Value = BoolOrNone.FalseValue;

                    XmlModelHelper.NormalizeAndResolve(complexProperty);
                    Debug.Assert(
                        complexProperty.ComplexType.Status != BindingStatus.Known,
                        "Why didn't we find the ComplexType in the ArtifactSet previously?");
                    _createdProperty = complexProperty;
                }
            }

            // safety check
            Debug.Assert(_createdProperty != null, "We didn't get good Property out of the command");
            if (_createdProperty != null)
            {
                // set Property attributes
                var cmd2 = new SetConceptualPropertyFacetsCommand(
                    _createdProperty, _clipboardProperty.Default,
                    _clipboardProperty.ConcurrencyMode, _clipboardProperty.GetterAccessModifier, _clipboardProperty.SetterAccessModifier,
                    _clipboardProperty.MaxLength,
                    DefaultableValueBoolOrNone.GetFromNullableBool(_clipboardProperty.FixedLength), _clipboardProperty.Precision,
                    _clipboardProperty.Scale,
                    DefaultableValueBoolOrNone.GetFromNullableBool(_clipboardProperty.Unicode), _clipboardProperty.Collation);
                CommandProcessor.InvokeSingleCommand(cpc, cmd2);
            }
        }
        private static ComplexProperty CreateNewComplexProperty(EFElement parent, ComplexConceptualProperty property)
        {
            // actually create it in the XLinq tree
            var cp = new ComplexProperty(parent, null);
            cp.Name.SetRefName(property);

            XmlModelHelper.NormalizeAndResolve(cp);

            if (cp == null)
            {
                throw new ItemCreationFailureException();
            }

            Debug.Assert(cp.Name.Target != null && cp.Name.Target.LocalName.Value == cp.Name.RefName, "Broken property resolution");

            return cp;
        }
 private static ComplexProperty CreateComplexPropertyUsingFragment(
     MappingFragment mappingFragment, ComplexConceptualProperty property)
 {
     // make sure that we don't already have one
     var cp = mappingFragment.FindComplexProperty(property);
     if (cp == null)
     {
         cp = CreateNewComplexProperty(mappingFragment, property);
         mappingFragment.AddComplexProperty(cp);
     }
     return cp;
 }
 private static ComplexProperty CreateComplexPropertyUsingComplexProperty(
     ComplexProperty parentComplexProperty, ComplexConceptualProperty property)
 {
     // make sure that we don't already have one
     var cp = parentComplexProperty.FindComplexProperty(property);
     if (cp == null)
     {
         cp = CreateNewComplexProperty(parentComplexProperty, property);
         parentComplexProperty.AddComplexProperty(cp);
     }
     return cp;
 }
 private void PreserveComplexPropertyMapping(
     CommandProcessorContext cpc, ComplexProperty complexPropertyMapping, ComplexConceptualProperty createdComplexTypeProperty)
 {
     // walk the Properties tree
     foreach (var sp in complexPropertyMapping.ScalarProperties())
     {
         PreserveScalarPropertyMapping(cpc, sp, createdComplexTypeProperty);
     }
     foreach (var cp in complexPropertyMapping.ComplexProperties())
     {
         PreserveComplexPropertyMapping(cpc, cp, createdComplexTypeProperty);
     }
 }
        private static ComplexProperty CreateComplexPropertyUsingEntity(
            CommandProcessorContext cpc, EntityType conceptualEntityType, ComplexConceptualProperty property, Property tableColumn)
        {
            // the S-Side entity
            var storageEntityType = tableColumn.Parent as EntityType;
            Debug.Assert(storageEntityType != null, "tableColumn.Parent should be an EntityType");

            // get the fragment to use
            var mappingFragment = ModelHelper.FindMappingFragment(cpc, conceptualEntityType, tableColumn.EntityType, true);
            Debug.Assert(mappingFragment != null, "Failed to create the MappingFragment to house this ComplexProperty");
            if (mappingFragment == null)
            {
                throw new ParentItemCreationFailureException();
            }

            // now go do the real work
            var cp = CreateComplexPropertyUsingFragment(mappingFragment, property);

            return cp;
        }
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            // safety check, this should never be hit
            Debug.Assert(EntityType != null, "InvokeInternal is called when EntityType is null.");
            if (EntityType == null)
            {
                throw new InvalidOperationException("InvokeInternal is called when EntityType is null");
            }

            // check for uniqueness
            string msg;
            if (!ModelHelper.ValidateEntityPropertyName(EntityType, Name, true, out msg))
            {
                throw new CommandValidationFailedException(msg);
            }

            // create the property
            _createdProperty = new ComplexConceptualProperty(EntityType, null, _insertPosition);

            // set the name and add to the parent entity
            _createdProperty.LocalName.Value = Name;
            if (ComplexType != null)
            {
                _createdProperty.ComplexType.SetRefName(ComplexType);
            }
            else if (!String.IsNullOrEmpty(_typeName))
            {
                // separate this name into the namespace and name parts
                string typeNamespace;
                string typeLocalName;
                EFNormalizableItemDefaults.SeparateRefNameIntoParts(_typeName, out typeNamespace, out typeLocalName);

                // look to see if the referenced complex type exists (it may not if they are pasting across models)
                // just search on local name since two models will have different namespaces probably
                ComplexType type = null;
                var cem = EntityType.EntityModel as ConceptualEntityModel;
                foreach (var c in cem.ComplexTypes())
                {
                    if (string.Compare(typeLocalName, c.LocalName.Value, StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        type = c;
                        break;
                    }
                }

                // set the complex type reference
                if (type == null)
                {
                    // if we didn't find the complex type locally, write out the type name - but for the local namespace
                    // this will let the user subsequently copy the complex type and this property will start working again
                    var typeSymbol = new Symbol(cem.Namespace.Value, typeLocalName);
                    _createdProperty.ComplexType.SetXAttributeValue(typeSymbol.ToDisplayString());
                }
                else
                {
                    _createdProperty.ComplexType.SetRefName(type);
                }
            }
            else
            {
                _createdProperty.ComplexType.SetXAttributeValue(Resources.ComplexPropertyUndefinedType);
            }

            // runtime does not support nullable complex properties, need to set it to false since the default is true
            _createdProperty.Nullable.Value = BoolOrNone.FalseValue;
            EntityType.AddProperty(_createdProperty);

            XmlModelHelper.NormalizeAndResolve(_createdProperty);
        }
 private void PreserveFunctionComplexPropertyMapping(
     CommandProcessorContext cpc, FunctionComplexProperty fcp, ComplexConceptualProperty createdComplexTypeProperty)
 {
     // walk the Properties tree
     foreach (var fsp in fcp.ScalarProperties())
     {
         PreserveFunctionScalarPropertyMapping(cpc, fsp, createdComplexTypeProperty);
     }
     foreach (var childFcp in fcp.ComplexProperties())
     {
         PreserveFunctionComplexPropertyMapping(cpc, childFcp, createdComplexTypeProperty);
     }
 }
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            // safety check, this should never be hit
            Debug.Assert(ParentComplexType != null, "InvokeInternal is called when ParentComplexType is null.");
            if (ParentComplexType == null)
            {
                throw new InvalidOperationException("InvokeInternal is called when ParentComplexType is null");
            }

            Debug.Assert(!(Type == null && ComplexType == null), "InvokeInternal is called when Type or ComplexType is null.");
            if (Type == null
                && ComplexType == null)
            {
                throw new InvalidOperationException("InvokeInternal is called when Type and ComplexType is null");
            }

            // check for uniqueness
            string msg;
            if (!ModelHelper.ValidateComplexTypePropertyName(ParentComplexType, Name, true, out msg))
            {
                throw new CommandValidationFailedException(msg);
            }

            // check for ComplexType circular definition
            if (ComplexType != null)
            {
                if (ModelHelper.ContainsCircularComplexTypeDefinition(ParentComplexType, ComplexType))
                {
                    throw new CommandValidationFailedException(
                        String.Format(
                            CultureInfo.CurrentCulture, Resources.Error_CircularComplexTypeDefinitionOnAdd, ComplexType.LocalName.Value));
                }
            }
            // create the property
            Property property = null;
            if (Type != null)
            {
                var conceptualProperty = new ConceptualProperty(ParentComplexType, null);
                conceptualProperty.ChangePropertyType(Type);
                property = conceptualProperty;
            }
            else
            {
                var complexProperty = new ComplexConceptualProperty(ParentComplexType, null);
                complexProperty.ComplexType.SetRefName(ComplexType);
                property = complexProperty;
            }
            Debug.Assert(property != null, "property should not be null");
            if (property == null)
            {
                throw new ItemCreationFailureException();
            }

            // set the name and add to the parent entity
            property.LocalName.Value = Name;
            ParentComplexType.AddProperty(property);

            // set other attributes of the property
            if (Nullable != null)
            {
                property.Nullable.Value = (Nullable.Value ? BoolOrNone.TrueValue : BoolOrNone.FalseValue);
            }

            XmlModelHelper.NormalizeAndResolve(property);
            _createdProperty = property;
        }
 private static FunctionComplexProperty CreateComplexPropertyUsingComplexProperty(
     FunctionComplexProperty parentComplexProperty, ComplexConceptualProperty property)
 {
     // make sure that we don't already have one
     var fcp = parentComplexProperty.FindFunctionComplexProperty(property);
     if (fcp == null)
     {
         fcp = CreateNewFunctionComplexProperty(parentComplexProperty, property);
         parentComplexProperty.AddComplexProperty(fcp);
     }
     return fcp;
 }
        private static FunctionComplexProperty CreateNewFunctionComplexProperty(EFElement parent, ComplexConceptualProperty property)
        {
            Debug.Assert(property != null, 
                "CreateFunctionComplexPropertyCommand.CreateNewFunctionComplexProperty() received null property");
            Debug.Assert
                (property.ComplexType.Target != null,
                 typeof(CreateFunctionComplexPropertyCommand).Name
                    + ".CreateNewFunctionComplexProperty() received property with null ComplexType.Target");

            // actually create it in the XLinq tree
            var fcp = new FunctionComplexProperty(parent, null);
            fcp.Name.SetRefName(property);
            fcp.TypeName.SetRefName(property.ComplexType.Target);

            XmlModelHelper.NormalizeAndResolve(fcp);

            if (fcp == null)
            {
                throw new ItemCreationFailureException();
            }

            Debug.Assert(
                fcp.Name.Target != null && fcp.Name.Target.LocalName.Value == fcp.Name.RefName,
                (fcp.Name.Target == null
                     ? "Broken property resolution for FunctionComplexProperty " + fcp.ToPrettyString() + ": null Target"
                     : "Broken property resolution for FunctionComplexProperty " + fcp.ToPrettyString() + ": Target.LocalName = "
                       + fcp.Name.Target.LocalName.Value + ", RefName = " + fcp.Name.RefName));

            return fcp;
        }