public override void ExecCommand(MenuCommand menuCommand) { var currentSelection = GetCurrentSelectedPersistentType(); PersistentType currentPersistentType = currentSelection.CurrentPersistentType; PropertyBase newProperty = null; currentSelection.MakeActionWithinTransaction(string.Format("Add scalar property into '{0}'", currentPersistentType.Name), () => { newProperty = CreateNewProperty(currentPersistentType, GetPropertyKind()); }); if (newProperty != null) { Owner.SelectModelElement((ModelElement)currentPersistentType); var propertyBase = newProperty as IPropertyBase; var persistentType = propertyBase.GetRealOwner(); string compartmentName = propertyBase.PropertyKind == PropertyKind.Navigation ? "NavigationProperties" : "Properties"; DiagramUtil.SelectCompartmentItem(currentSelection.DiagramDocView, (ModelElement)persistentType, compartmentName, newProperty); } }
private PropertyBase CreateNewProperty(PersistentType ownerPersistentType, PropertyKind newPropertyKind) { int propertiesCount = 0; PropertyBase newProperty = null; switch (newPropertyKind) { case PropertyKind.Scalar: newProperty = new ScalarProperty(ownerPersistentType.Partition); ownerPersistentType.Properties.Add(newProperty); propertiesCount = ownerPersistentType.Properties.OfType <ScalarProperty>().Count(); break; case PropertyKind.Structure: newProperty = new StructureProperty(ownerPersistentType.Partition); ownerPersistentType.Properties.Add(newProperty); propertiesCount = ownerPersistentType.Properties.OfType <StructureProperty>().Count(); break; case PropertyKind.Navigation: newProperty = new NavigationProperty(ownerPersistentType.Partition); ownerPersistentType.NavigationProperties.Add((NavigationProperty)newProperty); propertiesCount = ownerPersistentType.NavigationProperties.OfType <NavigationProperty>().Count(); break; } newProperty.Name = string.Format("{0}Property{1}", newPropertyKind, propertiesCount + 1); return(newProperty); }
private bool IsSourceWithinAssociation() { var persistentTypeHasAssociations = this.PersistentTypeHasAssociations; PersistentType ownerPersistentType = this.PersistentTypeOfNavigationProperty; return(persistentTypeHasAssociations == null ? false : persistentTypeHasAssociations.SourcePersistentType == ownerPersistentType); }
private void UpdateFillColorAccordingToKind() { PersistentType persistentType = (PersistentType)this.ModelElement; if (persistentType != null) { this.FillColor = persistentTypesColors[persistentType.TypeKind]; } }
public static IPersistentTypeHasAssociations CreatePersistentTypesAssociation(ModelElement sourceElement, ModelElement targetElement, IAssociationInfo sourceInfo, IAssociationInfo targetInfo, string associationName, bool createPropertyEnd1, bool createPropertyEnd2) { PersistentType sourceEntity = (PersistentType)sourceElement; PersistentType targetEntity = (PersistentType)targetElement; PersistentTypeHasAssociations typeAssociations = new PersistentTypeHasAssociations(sourceEntity, targetEntity); typeAssociations.End1.Multiplicity = sourceInfo.Multiplicity; typeAssociations.End1.OnOwnerRemove = sourceInfo.OnOwnerRemove; typeAssociations.End1.OnTargetRemove = sourceInfo.OnTargetRemove; typeAssociations.End1.UseAssociationAttribute = sourceInfo.UseAssociationAttribute; // association End2 values typeAssociations.End2.Multiplicity = targetInfo.Multiplicity; typeAssociations.End2.OnOwnerRemove = targetInfo.OnOwnerRemove; typeAssociations.End2.OnTargetRemove = targetInfo.OnTargetRemove; typeAssociations.End2.UseAssociationAttribute = targetInfo.UseAssociationAttribute; typeAssociations.Name = associationName; NavigationProperty sourceNavigationProperty = createPropertyEnd1 ? new NavigationProperty(sourceElement.Partition) : null; //bool isToSelfLookup = sourceInfo == targetInfo; //NavigationProperty targetNavigationProperty = isToSelfLookup ? null : new NavigationProperty(targetElement.Partition); NavigationProperty targetNavigationProperty = null; //if (!isToSelfLookup || !createPropertyEnd1) if (createPropertyEnd2) { targetNavigationProperty = new NavigationProperty(targetElement.Partition); } if (sourceNavigationProperty != null) { sourceNavigationProperty.PersistentTypeOfNavigationProperty = sourceEntity; sourceNavigationProperty.Name = sourceInfo.PairTo.Value; sourceNavigationProperty.Multiplicity = targetInfo.Multiplicity; sourceNavigationProperty.PersistentTypeHasAssociations = typeAssociations; } if (targetNavigationProperty != null) { targetNavigationProperty.PersistentTypeOfNavigationProperty = targetEntity; targetNavigationProperty.Name = targetInfo.PairTo.Value; targetNavigationProperty.Multiplicity = sourceInfo.Multiplicity; targetNavigationProperty.PersistentTypeHasAssociations = typeAssociations; } typeAssociations.End1.PairTo.SetAsCustom(createPropertyEnd1 ? sourceNavigationProperty.Name : targetNavigationProperty.Name); typeAssociations.End2.PairTo.SetAsCustom(createPropertyEnd2 ? targetNavigationProperty.Name : sourceNavigationProperty.Name); return(typeAssociations); }
private static bool TestPersistentType(ModelElement sourceElement, out PersistentType persistentType, Func <PersistentType, bool> validateFunc) { persistentType = sourceElement as PersistentType; if (persistentType != null) { return(validateFunc(persistentType)); } return(false); }
protected override void OnValueChanged(PersistentType element, string oldValue, string newValue) { if (!element.Store.InUndoRedoOrRollback) { // Hard validation of the new name. if (string.IsNullOrEmpty(newValue)) { throw new ArgumentException("The Name is required and cannot be an empty string.", newValue); } // Raise the NameChanged event for derived classes to act upon. element.OnNameChanged(EventArgs.Empty); } // Always call the base class method. base.OnValueChanged(element, oldValue, newValue); }
internal static string BuildAssociationName(PersistentType sourceEntity, PersistentType targetEntity) { string result = string.Format("{0}{1}", sourceEntity.Name, targetEntity.Name); bool nameUsed = true; var associationsLink = PersistentTypeHasAssociations.GetLinksToPersistentTypeAssociations(sourceEntity); while (nameUsed) { nameUsed = associationsLink.Any(item => Util.StringEqual(item.Name, result, true)); if (nameUsed) { result += "_1"; } } return(result); }
private static string BuildNavigationPropertyName(PersistentType sourceEntity, PersistentType targetEntity, bool forSource) { PersistentType entity = forSource ? sourceEntity : targetEntity; PersistentType oppositeEntity = forSource ? targetEntity : sourceEntity; string navigationPropertyName = oppositeEntity.Name; bool nameUsed = true; while (nameUsed) { nameUsed = entity.NavigationProperties.Any(property => Util.StringEqual(property.Name, navigationPropertyName, true)); if (nameUsed) { navigationPropertyName += "_1"; } } return(navigationPropertyName); }
private string GetReturnTypeValue() { string result = string.Empty; if (this.PersistentTypeHasAssociations == null) { return(result); } PersistentType ownerPersistentType = this.PersistentTypeOfNavigationProperty; bool isSource = this.PersistentTypeHasAssociations.SourcePersistentType == ownerPersistentType; PersistentType targetPersistentType = isSource ? this.PersistentTypeHasAssociations.TargetPersistentType : this.PersistentTypeHasAssociations.SourcePersistentType; switch (this.Multiplicity) { case MultiplicityKind.Many: { result = this.TypedEntitySet == null ? OrmUtils.BuildXtensiveType(OrmType.EntitySet, targetPersistentType.Name) : string.Format("{0} ({1})", this.TypedEntitySet.Name, OrmUtils.BuildXtensiveType(OrmType.EntitySet, this.TypedEntitySet.ItemType == null ? string.Empty : this.TypedEntitySet.ItemType.Name)); break; } case MultiplicityKind.ZeroOrOne: case MultiplicityKind.One: { result = string.Format("Instance of {0}", targetPersistentType.Name); break; } } return(result); }
public static void Connect(ModelElement sourceElement, ModelElement targetElement) { PersistentType sourceEntity = (PersistentType)sourceElement; PersistentType targetEntity = (PersistentType)targetElement; if (sourceEntity is TypedEntitySet && targetEntity is Interface) { TypedEntitySetHasItemType connection = new TypedEntitySetHasItemType((TypedEntitySet)sourceEntity, (Interface)targetEntity); if (DomainClassInfo.HasNameProperty(connection)) { DomainClassInfo.SetUniqueName(connection); } return; } IAssociationInfo sourceInfo = new AssociationInfo { Multiplicity = MultiplicityKind.One, OnOwnerRemove = AssociationOnRemoveAction.Default, OnTargetRemove = AssociationOnRemoveAction.Default }; sourceInfo.PairTo.SetAsCustom(BuildNavigationPropertyName(sourceEntity, targetEntity, true)); IAssociationInfo targetInfo = new AssociationInfo { Multiplicity = MultiplicityKind.Many, OnOwnerRemove = AssociationOnRemoveAction.Default, OnTargetRemove = AssociationOnRemoveAction.Default }; targetInfo.PairTo.SetAsCustom(BuildNavigationPropertyName(sourceEntity, targetEntity, false)); string associationName = BuildAssociationName(sourceEntity, targetEntity); CreatePersistentTypesAssociation(sourceElement, targetElement, sourceInfo, targetInfo, associationName, true, true); }
public override void ExecCommand(MenuCommand menuCommand) { var currentSelection = GetCurrentSelectedPersistentType(); PersistentType currentPersistentType = currentSelection.CurrentPersistentType; EntityIndex newIndex = null; currentSelection.MakeActionWithinTransaction( string.Format("Add entity index into '{0}'", currentPersistentType.Name), () => { Interface ownerInterface = (Interface)currentPersistentType; newIndex = new EntityIndex(currentPersistentType.Partition); ownerInterface.Indexes.Add(newIndex); newIndex.Name = string.Format("Index{0}", ownerInterface.Indexes.Count); }); if (newIndex != null) { DiagramUtil.SelectCompartmentItem(currentSelection.DiagramDocView, currentPersistentType, "Indexes", newIndex); } }
public void HandleEvent(ElementEventArgs args) { if (args.EventType == ElementEventType.ElementPropertyChanged) { Func <Tuple <OrmAssociationEnd, OrmAssociationEnd> > getAssocEnd = () => { PersistentType currentOwner = this.PersistentTypeOfNavigationProperty; bool isSource = this.PersistentTypeHasAssociations.SourcePersistentType == currentOwner; OrmAssociationEnd assocEnd1 = isSource ? this.PersistentTypeHasAssociations.End2 : this.PersistentTypeHasAssociations.End1; OrmAssociationEnd assocEnd2 = isSource ? this.PersistentTypeHasAssociations.End1 : this.PersistentTypeHasAssociations.End2; return(new Tuple <OrmAssociationEnd, OrmAssociationEnd>(assocEnd1, assocEnd2)); }; var e = args.ElementPropertyChangedEventArgs; if (e.DomainProperty.Id == MultiplicityDomainPropertyId) { if (this.PersistentTypeHasAssociations != null) { var assocEnds = getAssocEnd(); assocEnds.Item1.Multiplicity = (MultiplicityKind)e.NewValue; } } else if (e.DomainProperty.Id == NameDomainPropertyId) { if (this.PersistentTypeHasAssociations != null) { var assocEnds = getAssocEnd(); assocEnds.Item2.PairTo.SetAsCustom(e.NewValue as string); } } } }
private static void CustomWritePropertiesAsElements(SerializationContext context, PersistentType element, XmlWriter writer) { // if (!context.Result.Failed) // { // element.DataContract.SerializeToXml(writer, "dataContract"); // } }
private static void CustomReadPropertiesFromElements(SerializationContext serializationContext, PersistentType element, XmlReader reader) { // while (!serializationContext.Result.Failed && !reader.EOF && reader.NodeType == global::System.Xml.XmlNodeType.Element) // { // switch (reader.LocalName) // { // case "dataContract": // if (reader.IsEmptyElement) // { // No serialized value, must be default one. // SerializationUtilities.Skip(reader); // Skip this tag. // } // else // { // DataContractDescriptor dataContractDescriptor = new DataContractDescriptor(); // dataContractDescriptor.DeserializeFromXml(reader); // element.DataContract = dataContractDescriptor; // // SerializationUtilities.SkipToNextElement(reader); // reader.SkipToNextElementFix(); // } // break; // default: // return; // Don't know this element. // } // } }
public void HandleEvent(ElementEventArgs args) { if (args.EventType == ElementEventType.CustomEvent) { object customEventArgs = args.CustomEventArgs[0]; OrmAssociationEnd eventFromAssocEnd = customEventArgs as OrmAssociationEnd; string changedProperty = (string)args.CustomEventArgs[1]; string calledFromEndId = (string)args.CustomEventArgs[2]; if (eventFromAssocEnd != null) { bool changeAll = string.IsNullOrEmpty(changedProperty); bool canChangeMultiplicity = changeAll || Util.StringEqual(changedProperty, "Multiplicity", true); bool canChangePairTo = changeAll || Util.StringEqual(changedProperty, "PairTo", true); bool eventFromEnd1 = Util.StringEqual(calledFromEndId, "End1", true); Func <bool, Tuple <NavigationProperty, OrmAssociationEnd> > getDefinitions = delegate(bool revert) { PersistentType persistentToFind = eventFromEnd1 ? (revert ? this.SourcePersistentType : this.TargetPersistentType) : (revert ? this.TargetPersistentType : this.SourcePersistentType); OrmAssociationEnd otherAssocEnd = eventFromEnd1 ? (revert ? this.End2 : eventFromAssocEnd) : (revert ? eventFromAssocEnd : this.End2); NavigationProperty navigationProperty = this.NavigationProperties.SingleOrDefault( navProperty => navProperty.PersistentTypeOfNavigationProperty == persistentToFind); return(new Tuple <NavigationProperty, OrmAssociationEnd>(navigationProperty, otherAssocEnd)); }; { if (canChangeMultiplicity) { bool revert = false; var definitions = getDefinitions(revert); var navigationProperty = definitions.Item1; var otherAssocEnd = definitions.Item2; if (navigationProperty != null) { this.Store.MakeActionWithinTransaction( string.Format("Updating multiplicity on navigation property '{0}' to value '{1}'", navigationProperty.Name, eventFromAssocEnd.Multiplicity), () => { navigationProperty.Multiplicity = otherAssocEnd.Multiplicity; }); } } if (canChangePairTo && !eventFromAssocEnd.PairTo.IsDefault()) { string newName = eventFromAssocEnd.PairTo.Value; bool revert = true; var definitions = getDefinitions(revert); var navigationProperty = definitions.Item1; if (navigationProperty != null) { this.Store.MakeActionWithinTransaction( string.Format("Updating name on navigation property '{0}' to value '{1}'", navigationProperty.Name, newName), () => { navigationProperty.Name = newName; }); } } } } } }
public override void ExecCommand(MenuCommand menuCommand) { CurrentModelSelection modelSelection = GetCurrentSelectedPersistentType(); EntityDiagram entityDiagram = modelSelection.GetFromSelection <EntityDiagram>(false).Single(); EntityModel entityModel = entityDiagram.Store.ElementDirectory.FindElements <EntityModel>().Single(); IEnumerable <Tuple <string, EntityKind> > existingTypeNames = entityDiagram.Store.ElementDirectory.FindElements <PersistentType>() .Where(item => item.TypeKind.In(PersistentTypeKind.Entity, PersistentTypeKind.Interface, PersistentTypeKind.Structure)) .Select(item => new Tuple <string, EntityKind>(item.Name, ConvertTypeKind(item.TypeKind))); Func <string, EntityKind, PersistentType> findTypeFunc = (typeName, typeKind) => entityDiagram.Store.ElementDirectory .FindElements <PersistentType>() .Where(item => item.Name == typeName && ConvertTypeKind(item.TypeKind) == typeKind) .SingleOrDefault(); FormAddPersistentType.ResultData resultData; if (FormAddPersistentType.DialogShow(existingTypeNames, out resultData)) { modelSelection.MakeActionWithinTransaction(string.Format("Add new {0} with name '{1}'", resultData.TypeKind, resultData.TypeName), delegate { PersistentType newEntity = null; switch (resultData.TypeKind) { case EntityKind.Entity: { newEntity = new Entity(entityModel.Partition); if (resultData.BaseTypeInfo.Item1.HasValue) { Entity entity = (Entity)newEntity; entity.BaseType = findTypeFunc(resultData.BaseTypeInfo.Item2, resultData.BaseTypeInfo.Item1.Value) as EntityBase; } break; } case EntityKind.Structure: { newEntity = new Structure(entityModel.Partition); if (resultData.BaseTypeInfo.Item1.HasValue) { Structure structure = (Structure)newEntity; structure.BaseType = findTypeFunc(resultData.BaseTypeInfo.Item2, resultData.BaseTypeInfo.Item1.Value) as EntityBase; } break; } case EntityKind.Interface: { newEntity = new Interface(entityModel.Partition); if (resultData.BaseTypeInfo.Item1.HasValue) { Interface @interface = (Interface)newEntity; Interface baseInterface = findTypeFunc(resultData.BaseTypeInfo.Item2, resultData.BaseTypeInfo.Item1.Value) as Interface; @interface.InheritedInterfaces.Add(baseInterface); } break; } } newEntity.Name = resultData.TypeName; entityModel.PersistentTypes.Add(newEntity); // create key property if (resultData.KeyPropertyInfo.Item1) { var newProperty = new ScalarProperty(newEntity.Partition); newProperty.Name = resultData.KeyPropertyInfo.Item2; newProperty.KeyAttribute.Enabled = true; Guid typeId = SystemPrimitiveTypesConverter.GetTypeId(resultData.KeyPropertyInfo.Item3); newProperty.Type = (DomainType)entityModel.GetDomainType(typeId); newEntity.Properties.Add(newProperty); } Owner.SelectModelElement((ModelElement)newEntity); }); } }
public override void ExecCommand(MenuCommand menuCommand) { CurrentModelSelection modelSelection = GetCurrentSelectedPersistentType(); EntityDiagram entityDiagram = modelSelection.GetFromSelection <EntityDiagram>(false).SingleOrDefault(); PersistentType persistentType = modelSelection.IsPersistentTypeSelected ? modelSelection.CurrentPersistentType : null; Store store = persistentType != null ? persistentType.Store : entityDiagram.Store; IEnumerable <PersistentTypeItem> existingTypeNames = store.ElementDirectory.FindElements <PersistentType>() .Where(item => item.TypeKind.In(PersistentTypeKind.Entity, PersistentTypeKind.Interface, PersistentTypeKind.Structure)) .Select(item => new PersistentTypeItem(item.Name, ModelUtil.ConvertTo(item.TypeKind), item.AllProperties.Select(prop => prop.Name).ToArray())); PersistentTypeItem persistentTypeEnd1 = persistentType == null ? null : new PersistentTypeItem( persistentType.Name, ModelUtil.ConvertTo(persistentType.TypeKind), persistentType.AllProperties.Select(prop => prop.Name). ToArray()); Func <PersistentTypeItem, PersistentType> findTypeFunc = (typeItemd) => store.ElementDirectory .FindElements <PersistentType>() .Where(item => item.Name == typeItemd.Name && ModelUtil.ConvertTo(item.TypeKind) == typeItemd.Kind) .SingleOrDefault(); IEnumerable <string> existingAssociations = store.ElementDirectory.FindElements <PersistentTypeHasAssociations>().Select(assoc => assoc.Name).ToArray(); if (existingTypeNames.Count() < 2) { MessageBox.Show("There must be at least 2 persistent types to make association.", "Add Association...", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //FormAddAssociation.ResultData resultData; //if (FormAddAssociation.DialogShow(existingTypeNames, existingAssociations, persistentTypeEnd1, out resultData)) FormAddAssociation.ResultData resultData; if (FormAddAssociation.DialogShow(existingTypeNames, existingAssociations, persistentTypeEnd1, out resultData)) { modelSelection.MakeActionWithinTransaction(string.Format("Add new association '{0}'", resultData.AssociationName), delegate { PersistentType sourcePersistentType = findTypeFunc(resultData.PersistentItem1.TypeItem); PersistentType targetPersistentType = findTypeFunc(resultData.PersistentItem2.TypeItem); IAssociationInfo sourceInfo = new AssociationInfo { Multiplicity = resultData.PersistentItem2.Multiplicity, OnOwnerRemove = AssociationOnRemoveAction.Default, OnTargetRemove = AssociationOnRemoveAction.Default, UseAssociationAttribute = resultData.PersistentItem2.UseAssociationAttribute }; sourceInfo.PairTo.SetAsCustom(resultData.PersistentItem1.PropertyName); IAssociationInfo targetInfo = new AssociationInfo { Multiplicity = resultData.PersistentItem1.Multiplicity, OnOwnerRemove = AssociationOnRemoveAction.Default, OnTargetRemove = AssociationOnRemoveAction.Default, UseAssociationAttribute = resultData.PersistentItem1.UseAssociationAttribute }; targetInfo.PairTo.SetAsCustom(resultData.PersistentItem2.PropertyName); if (resultData.SimpleMode && sourcePersistentType == targetPersistentType) { targetInfo = sourceInfo; targetPersistentType = sourcePersistentType; } var persistentTypesAssociation = AssociationConnectorBuilder.CreatePersistentTypesAssociation(sourcePersistentType, targetPersistentType, sourceInfo, targetInfo, resultData.AssociationName, true, !resultData.SimpleMode); Owner.SelectModelElement((ModelElement)persistentTypesAssociation); }); } }
/// <summary> /// Constructor /// Creates a PersistentTypeHasAssociations link in the same Partition as the given PersistentType /// </summary> /// <param name="source">PersistentType to use as the source of the relationship.</param> /// <param name="target">PersistentType to use as the target of the relationship.</param> public PersistentTypeHasAssociations(PersistentType source, PersistentType target) : base((source != null ? source.Partition : null), new[] { new RoleAssignment(SourcePersistentTypeDomainRoleId, source), new RoleAssignment(TargetPersistentTypeDomainRoleId, target) }, null) { Initialize(); }