internal override ExplorerEFElement GetParentNodeForElement(EFElement childElement)
 {
     Debug.Assert(childElement != null, "on ExplorerEFElement with name " + Name + " received null child");
     var childElementType = childElement.GetType();
     if (typeof(StorageEntityType) == childElementType)
     {
         return _typesGhostNode;
     }
     else if (typeof(Association) == childElementType)
     {
         return _assocsGhostNode;
     }
     else if (typeof(Function) == childElementType)
     {
         return _funcsGhostNode;
     }
     else if (typeof(StorageEntityContainer) == childElementType)
     {
         return this;
     }
     else
     {
         Debug.Fail(
             string.Format(
                 CultureInfo.CurrentCulture,
                 Resources.BadChildForParentException, GetType().FullName, childElementType.FullName));
         return null;
         // TODO: we need to provide a general exception-handling mechanism and replace the above Assert()
         // by e.g. the excepiton below
         // throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
         //     Resources.BadChildForParentException, this.GetType().FullName, childElementType.FullName));
     }
 }
        // TODO - make this private, and remove the need to pass in the type to GetNew()/GetNewOrExisting().
        protected override Type GetViewModelTypeForEFlement(EFElement efElement)
        {
            if (!IsDisplayedInExplorer(efElement))
            {
                return null;
            }

            var efElementType = efElement.GetType();

            Type explorerType = null;
            _modelType2ExplorerViewModelType.TryGetValue(efElementType, out explorerType);

            // Get correct view-model type for a c-side or s-side entity type.  
            if (explorerType == null)
            {
                var assoc = efElement as Association;
                if (assoc != null)
                {
                    if (assoc.EntityModel.IsCSDL)
                    {
                        explorerType = typeof(ExplorerConceptualAssociation);
                    }
                    else
                    {
                        explorerType = typeof(ExplorerStorageAssociation);
                    }
                }
            }

            Debug.Assert(explorerType != null, "Unable to find explorer type for efobject type " + efElementType);

            return explorerType;
        }
 internal override ExplorerEFElement GetParentNodeForElement(EFElement childElement)
 {
     Debug.Assert(childElement != null, "GetParentNodeForElement on ExplorerEFElement with name " + Name + " received null child");
     Debug.Assert(
         ModelItem == childElement.Parent, "GetParentNodeForElement - underlying model element with identity " +
                                           ModelItem.Identity + " is not the same as the child element's parent, which has identity "
                                           + childElement.Parent.Identity);
     var childElementType = childElement.GetType();
     if (typeof(ConceptualEntitySet) == childElementType)
     {
         return _entitySetsGhostNode;
     }
     else if (typeof(AssociationSet) == childElementType)
     {
         return _assocSetsGhostNode;
     }
     else if (typeof(FunctionImport) == childElementType)
     {
         // if asked what FunctionImport parent node is redirect to the parent (i.e. the ConceptulaEntityModel)
         // This is because, for FunctionImports, we are not using the same parent-child relationship in the 
         // View-Model that we are in the Model
         return Parent.GetParentNodeForElement(childElement);
     }
     else
     {
         throw new InvalidOperationException(
             string.Format(
                 CultureInfo.CurrentCulture,
                 Resources.BadChildForParentException, GetType().FullName, childElementType.FullName));
     }
 }
        // <summary>
        //     Helper method that determine whether an EFElement is represented in model browser.
        // </summary>
        internal static bool IsDisplayedInExplorer(EFElement efElement)
        {
            // If efElement type is StorageEntityContainer or EFDesignerInfoRoot, don't display it in Model Browser.
            // Note: GetParentOfType() will also return true if self is of passed-in type.
            if (null != efElement.GetParentOfType(typeof(StorageEntityContainer)))
            {
                return false;
            }
                // We do not display Enum type members
            else if (efElement is EnumTypeMember)
            {
                return false;
            }
                // For any Designer objects, check whether the map between the EFElement and ExplorerEFElement exists.
            else if (null != efElement.GetParentOfType(typeof(EFDesignerInfoRoot)))
            {
                return _modelType2ExplorerViewModelType.ContainsKey(efElement.GetType());
            }

            return true;
        }
        internal override void CreateModelItem(CommandProcessorContext cpc, EditingContext context, EFElement underlyingModelItem)
        {
            Debug.Assert(context != null, "The context argument cannot be null");
            Debug.Assert(StorageEntityType == null, "Don't call this method if we already have a ModelItem");
            Debug.Assert(MappingConceptualEntityType.ConceptualEntityType != null, "The parent item isn't set up correctly");
            Debug.Assert(underlyingModelItem != null, "The underlyingModelItem cannot be null");
            var storeEntityType = underlyingModelItem as EntityType;
            Debug.Assert(
                storeEntityType != null,
                "underlyingModelItem must be of type EntityType, actual type = " + underlyingModelItem.GetType().FullName);
            Debug.Assert(storeEntityType.EntityModel.IsCSDL == false, "The storageEntityType must not be a CSDL EntityType");

            Context = context;
            ColumnMappings.Context = context;

            // create a context if we weren't passed one
            if (cpc == null)
            {
                cpc = new CommandProcessorContext(
                    Context, EfiTransactionOriginator.MappingDetailsOriginatorId, Resources.Tx_CreateMappingFragment);
            }

            // create the MappingFragment - if we already have a default EntityTypeMapping then just add
            // the MappingFragment to that mapping, otherwise if we already have an IsTypeOf
            // EntityTypeMapping then add the MappingFragment to that, otherwise create an IsTypeOf
            // EntityTypeMapping and add the MappingFragment to that
            var cet = MappingConceptualEntityType.ConceptualEntityType;
            var defaultEtm = ModelHelper.FindEntityTypeMapping(cpc, cet, EntityTypeMappingKind.Default, false);
            var etmKind = (defaultEtm == null ? EntityTypeMappingKind.IsTypeOf : EntityTypeMappingKind.Default);
            var cmd = new CreateMappingFragmentCommand(cet, storeEntityType, etmKind);

            // add post-invoke event to fix up our view model
            cmd.PostInvokeEvent += (o, eventsArgs) =>
                {
                    // fix up our view model
                    ModelItem = storeEntityType;
                    Parent.AddChild(this);

                    // assign the table to our container node as well
                    ColumnMappings.ModelItem = storeEntityType;

                    // now try and do some match ups between the entity and the table
                    var mappingStrategy = ModelHelper.DetermineCurrentInheritanceStrategy(cet);
                    var topMostBaseType = cet.ResolvableTopMostBaseType;
                    foreach (var child in ColumnMappings.Children)
                    {
                        var msp = child as MappingScalarProperty;
                        if (msp != null)
                        {
                            List<Property> properties;
                            if (ModelHelper.FindScalarPropertyPathByLocalName(cet, msp.ColumnName, out properties))
                            {
                                msp.CreateModelItem(cpc, _context, properties);
                            }
                            else if (InheritanceMappingStrategy.TablePerType == mappingStrategy
                                     &&
                                     ModelHelper.FindScalarPropertyPathByLocalName(topMostBaseType, msp.ColumnName, out properties))
                            {
                                msp.CreateModelItem(cpc, _context, properties);
                            }
                        }
                    }
                };

            try
            {
                // now update the model
                var cp = new CommandProcessor(cpc);
                cp.EnqueueCommand(cmd);
                cp.Invoke();
            }
            catch
            {
                ModelItem = null;
                ColumnMappings.ModelItem = null;
                Parent.RemoveChild(this);
                throw;
            }
        }
        internal static MappingEFElement GetNewOrExisting(EditingContext context, EFElement modelElement, MappingEFElement parent)
        {
            MappingEFElement result;

            var xref = GetModelToMappingModelXRef(context);
            result = xref.GetExisting(modelElement);
            if (result != null)
            {
                result.Parent = parent;
            }
            else
            {
                Type viewModelType;
                ModelTypeToViewModelType.TryGetValue(modelElement.GetType(), out viewModelType);
                if (viewModelType == null)
                {
                    // try the base class type
                    ModelTypeToViewModelType.TryGetValue(modelElement.GetType().BaseType, out viewModelType);
                }

                if (viewModelType != null)
                {
                    result = Activator.CreateInstance(viewModelType, context, modelElement, parent) as MappingEFElement;
                    xref.Add(modelElement, result);
                }
                else
                {
                    // implement a special case for entity type
                    // create the correct C- or S-space entity type in our view model
                    var entityType = modelElement as EntityType;
                    if (entityType != null)
                    {
                        var mappingDetailsInfo = context.Items.GetValue<MappingDetailsInfo>();
                        if (mappingDetailsInfo.EntityMappingMode == EntityMappingModes.Tables)
                        {
                            var entityModel = entityType.Parent as BaseEntityModel;
                            Debug.Assert(
                                entityModel != null,
                                "entityType's parent should be an EntityModel but received type "
                                + (entityType.Parent == null ? "NULL" : entityType.Parent.GetType().FullName));

                            if (entityModel.IsCSDL)
                            {
                                result =
                                    Activator.CreateInstance(typeof(MappingConceptualEntityType), context, modelElement, parent) as
                                    MappingEFElement;
                            }
                            else
                            {
                                result =
                                    Activator.CreateInstance(typeof(MappingStorageEntityType), context, modelElement, parent) as
                                    MappingEFElement;
                            }
                        }
                        else
                        {
                            result =
                                Activator.CreateInstance(typeof(MappingFunctionEntityType), context, modelElement, parent) as
                                MappingEFElement;
                        }
                        xref.Add(modelElement, result);
                    }

                    // special case for scalar properties
                    var scalarProperty = modelElement as ScalarProperty;
                    if (scalarProperty != null)
                    {
                        if (scalarProperty.Parent is MappingFragment
                            || scalarProperty.Parent is ComplexProperty)
                        {
                            result =
                                Activator.CreateInstance(typeof(MappingScalarProperty), context, modelElement, parent) as MappingEFElement;
                        }
                        else
                        {
                            result =
                                Activator.CreateInstance(typeof(MappingEndScalarProperty), context, modelElement, parent) as
                                MappingEFElement;
                        }
                        xref.Add(modelElement, result);
                    }
                }
            }

            return result;
        }
Ejemplo n.º 7
0
        internal override void CreateModelItem(CommandProcessorContext cpc, EditingContext context, EFElement underlyingModelItem)
        {
            Debug.Assert(context != null, "context must not be null");
            Debug.Assert(Condition == null, "Don't call this method if we already have a ModelItem");
            Debug.Assert(MappingStorageEntityType.StorageEntityType != null, "The parent item isn't set up correctly");
            Debug.Assert(underlyingModelItem != null, "underlyingModelItem must not be null");

            var tableColumn = underlyingModelItem as Property;
            Debug.Assert(
                tableColumn != null, "underlyingModelItem must be of type Property, actual type = " + underlyingModelItem.GetType().FullName);

            // store this off in case we have recover the condition later (if it moves to another ETM on us)
            _modelItemColumnName = tableColumn.LocalName.Value;

            Context = context;

            // local shortcuts
            EntityType entityType = MappingConceptualEntityType.ConceptualEntityType;

            // create a context if we weren't passed one
            if (cpc == null)
            {
                cpc = new CommandProcessorContext(
                    Context, EfiTransactionOriginator.MappingDetailsOriginatorId, Resources.Tx_CreateCondition);
            }

            // use empty string as a default condition value
            var cmd = new CreateFragmentConditionCommand(entityType, tableColumn, null, String.Empty);

            // set up our post event to fix up the view model
            cmd.PostInvokeEvent += (o, eventsArgs) =>
                {
                    var cond = cmd.CreatedCondition;
                    Debug.Assert(cond != null, "cmd failed to create Condition");

                    // fix up our view model
                    ModelItem = cond;
                    Parent.AddChild(this);
                };

            try
            {
                // now make the change
                var cp = new CommandProcessor(cpc, cmd);
                cp.Invoke();
            }
            catch
            {
                ModelItem = null;
                Parent.RemoveChild(this);

                throw;
            }
        }
 internal override void CreateModelItem(CommandProcessorContext cpc, EditingContext context, EFElement underlyingModelItem)
 {
     Debug.Assert(underlyingModelItem != null, "underlyingModelItem argument cannot be null");
     if (underlyingModelItem != null)
     {
         var entityProperty = underlyingModelItem as Property;
         Debug.Assert(
             entityProperty != null,
             "underlyingModelItem argument was of type " + underlyingModelItem.GetType().FullName + ", should be Property");
         if (entityProperty != null)
         {
             var properties = new List<Property>(1);
             properties.Add(entityProperty);
             CreateModelItem(cpc, context, properties);
         }
     }
 }
        internal override void CreateModelItem(CommandProcessorContext cpc, EditingContext context, EFElement underlyingModelItem)
        {
            Debug.Assert(context != null, "null context");

            Debug.Assert(Function == null, "Don't call this method if we already have a ModelItem");
            Debug.Assert(MappingFunctionEntityType.EntityType != null, "The parent item isn't set up correctly");

            Debug.Assert(underlyingModelItem != null, "null underlyingModelItem");

            var function = underlyingModelItem as Function;
            Debug.Assert(
                function != null, "underlyingModelItem must be of type Function, actual type = " + underlyingModelItem.GetType().FullName);
            Debug.Assert(!function.EntityModel.IsCSDL, "The function must be in the SSDL");

            Context = context;

            // create a context if we weren't passed one
            if (cpc == null)
            {
                cpc = new CommandProcessorContext(
                    Context, EfiTransactionOriginator.MappingDetailsOriginatorId, Resources.Tx_CreateFunctionMapping);
            }

            // create the commands
            var cmd = new CreateFunctionMappingCommand(MappingFunctionEntityType.EntityType, function, null, _functionType);
            // set up our post event to fix up the view model
            cmd.PostInvokeEvent += (o, eventsArgs) =>
                {
                    var mf = cmd.ModificationFunction;
                    Debug.Assert(mf != null, "null ModificationFunction");

                    // fix up our view model
                    ModelItem = mf;
                    // The parent item for the function mapping view model always has 3 children; insert, update and delete items.  If there isn’t 
                    // a function mapped for any of these, then there is still a view model item since we want to display the ‘creator node’ text.
                    // Calling this.Parent.AddChild(this) here would make the parent think it had a new child instead of updating the existing one - 
                    // so it is correct to _not_ call it here.
                };

            var cmd2 = new DelegateCommand(
                () =>
                    {
                        var mf = ModificationFunction;
                        Debug.Assert(
                            mf != null,
                            "Null ModificationFunction when trying to create view-model dummy nodes in MappingModificationFunctionMapping.CreateModelItem()");

                        if (mf != null)
                        {
                            //set up _properties and _resultBindings here as they are dummy ViewModel nodes
                            // (i.e. don't correspond to any underlying ModelItem)
                            _properties = new MappingFunctionScalarProperties(context, mf, this);
                            _resultBindings = new MappingResultBindings(context, mf, this);
                            _properties.Parent.AddChild(_properties);
                            _resultBindings.Parent.AddChild(_resultBindings);

                            // now ensure _properties scalar properties children have been calculated
                            // (this creates scalar properties with just the column info 
                            // since this ModificationFunction has not yet been mapped)
                            _properties.LoadScalarProperties();

                            // now try and do some match ups between the function and the entity
                            var mappedEntityType = MappingFunctionEntityType.EntityType as ConceptualEntityType;

                            Debug.Assert(
                                MappingFunctionEntityType.EntityType == null || mappedEntityType != null,
                                "EntityType is not ConceptualEntityType");

                            if (mappedEntityType != null)
                            {
                                foreach (var mfsp in _properties.ScalarProperties)
                                {
                                    // Try to do some auto-matching of the sproc's parameters to the EntityType's properties.
                                    // Search for a property in the mapped EntityType's inheritance hierarchy that matches the
                                    // parameter's name. First search this EntityType (both its scalar and complex properties),
                                    // then search its parents scalar and complex properties and so on up the hierarchy
                                    var propNameToSearchFor = mfsp.StoreParameter.LocalName.Value;
                                    var propList = new List<Property>();
                                    var entityTypeToSearch = mappedEntityType;
                                    // reset this back to the mapped EntityType each time through the loop
                                    while (entityTypeToSearch != null
                                           && false
                                           == ModelHelper.FindScalarPropertyPathByLocalName(
                                               entityTypeToSearch, propNameToSearchFor, out propList))
                                    {
                                        if (entityTypeToSearch.BaseType == null)
                                        {
                                            // safety code - this should not happen but will prevent an infinite loop if it does
                                            entityTypeToSearch = null;
                                        }
                                        else
                                        {
                                            entityTypeToSearch = entityTypeToSearch.BaseType.Target;
                                        }
                                    }

                                    // if propList is still empty that means we did not find a match - so leave the parameter unmapped
                                    if (propList.Count > 0)
                                    {
                                        mfsp.CreateModelItem(cpc, _context, propList);
                                    }
                                }
                            }
                        }
                    });

            try
            {
                // now make the change
                var cp = new CommandProcessor(cpc);
                cp.EnqueueCommand(cmd);
                cp.EnqueueCommand(cmd2);
                cp.Invoke();
            }
            catch
            {
                ModelItem = null;
                ClearChildren();

                throw;
            }
        }
        // <summary>
        //     NOTE: The association set mapping view model doesn't keep a reference to the mapping model item. Instead, it
        //     keeps it to the AssociationSet and then it can find the AssociationSetMapping as an anti-dep.  We don't need to clear
        //     or set the ModelItem property.
        // </summary>
        internal override void CreateModelItem(CommandProcessorContext cpc, EditingContext context, EFElement underlyingModelItem)
        {
            Debug.Assert(context != null, "The context argument cannot be null");
            Debug.Assert(AssociationSet.AssociationSetMapping == null, "Don't call this method if we already have a mapping");
            Debug.Assert(underlyingModelItem != null, "The underlyingModelItem cannot be null");
            var storeEntityType = underlyingModelItem as EntityType;
            Debug.Assert(
                storeEntityType != null,
                "underlyingModelItem must be of type EntityType, actual type = " + underlyingModelItem.GetType().FullName);
            Debug.Assert(!storeEntityType.EntityModel.IsCSDL, "The storageEntityType must not be a CSDL EntityType");

            Context = context;

            // create a context if we weren't passed one
            if (cpc == null)
            {
                cpc = new CommandProcessorContext(
                    Context, EfiTransactionOriginator.MappingDetailsOriginatorId, Resources.Tx_CreateAssociationSetMapping);
            }

            // create the item
            var cmd1 = new CreateAssociationSetMappingCommand(MappingAssociation.Association, storeEntityType);

            // now try and do some match ups by name
            var cmd2 = new DelegateCommand(
                () =>
                    {
                        Parent.AddChild(this);

                        foreach (var child in Children)
                        {
                            var end = child as MappingAssociationSetEnd;
                            if (end != null)
                            {
                                foreach (var child2 in end.Children)
                                {
                                    var mesp = child2 as MappingEndScalarProperty;
                                    if (mesp != null)
                                    {
                                        var tableColumn =
                                            MappingAssociationSet.StorageEntityType.GetFirstNamedChildByLocalName(mesp.Property, true) as
                                            Property;
                                        if (tableColumn != null)
                                        {
                                            mesp.CreateModelItem(cpc, _context, tableColumn);
                                        }
                                    }
                                }
                            }
                        }
                    });

            // now make the change
            var cp = new CommandProcessor(cpc);
            cp.EnqueueCommand(cmd1);
            cp.EnqueueCommand(cmd2);
            cp.Invoke();
        }
        private static ExplorerEFElement GetNewOrExisting(
            EditingContext context, EFElement efElement, ExplorerEFElement parent, Type viewModelType, bool mustNotExist)
        {
            var xref = GetModelToBrowserModelXRef(context);
            var result = xref.GetExisting(efElement);
            if (result != null)
            {
                if (mustNotExist)
                {
                    Debug.Fail(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.BadInsertChildAlreadyExists, efElement.GetType().FullName, parent.GetType().FullName));
                    return null;
                    // TODO: we need to provide a general exception-handling mechanism and replace the above Assert()
                    // by e.g. the excepiton below
                    // throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.BadInsertChildAlreadyExists, efElement.GetType().FullName, parent.GetType().FullName));
                }
                else
                {
                    result.Parent = parent;
                }
            }
            else
            {
                if (viewModelType != null)
                {
                    if (!xref.IsDisplayedInExplorerProtected(efElement))
                    {
                        Debug.Fail(
                            "Attempting to create an ExplorerEFElement of type " + viewModelType.FullName +
                            " based on an EFElement which is not displayed in the Explorer " + efElement.ToPrettyString());
                        return null;
                    }

                    result = Activator.CreateInstance(viewModelType, context, efElement, parent) as ExplorerEFElement;
                    xref.Add(efElement, result);
                }
            }

            return result;
        }
        internal override ExplorerEFElement GetParentNodeForElement(EFElement childElement)
        {
            Debug.Assert(
                childElement != null,
                "GetParentNodeForElement on ExplorerEFElement with name " + Name + " received null child");

            var childElementType = childElement.GetType();
            if (typeof(ConceptualEntityType) == childElementType)
            {
                return _typesGhostNode;
            }
            if (typeof(ComplexType) == childElementType)
            {
                return _complexTypesGhostNode;
            }
            else if (typeof(EnumType) == childElementType)
            {
                return _enumTypesGhostNode;
            }
            else if (typeof(Association) == childElementType)
            {
                return _assocsGhostNode;
            }
            else if (typeof(FunctionImport) == childElementType)
            {
                return _funcImportsGhostNode;
            }
            else if (typeof(ConceptualEntityContainer) == childElementType)
            {
                return this;
            }
            else
            {
                throw new InvalidOperationException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.BadChildForParentException, GetType().FullName, childElementType.FullName));
            }
        }