new public void Insert(int index, Link val)
		{
			if (this._entity != null) 
			{
				val.SetEntity(this._entity);
			}
			if ( this.Contains(val) ) 
			{
				this.Remove(val);
			}
			this.Insert(index, val);
		}
		new public int Add(Link val)
		{
			if (this._entity != null) 
			{
				val.SetEntity(this._entity);
			}
			if ( !this.Contains(val) ) 
			{
				return base.Add(val);
			}
			return -1;
		}
		public static IList GetIndexes(Link sourceLink) {
			ArrayList list = new ArrayList();

			foreach (MetadataFile tmpFiles in Project.CurrentProject.MetadataFiles) {
				foreach (MetadataEntityBase tmpEntityBase in tmpFiles.MetadataEntities) {
					if (tmpEntityBase.Name.Equals(sourceLink.TargetEntityName)) {
						foreach (Index tmpIndex in ((Entity) tmpEntityBase).Indexes) {
							list.Add(tmpIndex.Name);
						}
					}
				}
			}

			// Sort List
			list.Sort(0, list.Count, null);

			return list;
		}
		protected virtual string GetLinkType(Link link)
		{
			if (link.IsCollection)
				return this.FindEntity(link.TargetEntityName).CollectionName;
			else
				return link.TargetEntityName;
		}
		protected virtual string GetBasicLinkProperty(Link link)
		{
			string txt = string.Format(
				"public {0} {1} {{\r\n"
				, this.GetLinkType(link)
				, link.Name);
			txt += StringHelper.IndentBlock(string.Format(
				"get {{{0}}}\r\n"
				, FormatPropertyAccessorText(link, GetPropertyGetterText(link))));
			txt += StringHelper.IndentBlock(string.Format(
				"set {{{0}}}\r\n"
				, FormatPropertyAccessorText(link, GetPropertySetterText(link))));
			txt += "}";

			return txt;
		}
		protected string GetBasicLinkProperty(Link link, string indent)
		{
			return StringHelper.IndentBlock(
				GetBasicLinkProperty(link), 
				indent);
		}
		internal void SetLink(Link link) {
			Debug.Assert(link != null, "Link parameter should not be null.");
			_link = link;
		}
Beispiel #8
0
		public void AutoFillLinks(DataSource ds)
		{
			// Create Object's
			Link oLink;
			DataView dbLinkSchemas;
			Entity oTargetEntity = null;
			Index oTargetIndex = null;
			IndexField oTargetIndexField = null;
			LinkField oSourceLinkField = null;
			Boolean bIndexExists = false;

			Trace.Unindent();
				Trace.WriteLine(String.Format("Create Links from Entity \"{0}\"", this.Name));
			Trace.Indent();

			// Get Schemas
			dbLinkSchemas = GetDBLinkSchema(ds);

			// Lopping on Fields
			foreach (EntityField tmpField in this.Fields)
			{
				// Clear Flag
				bIndexExists = false;

				if (tmpField.KeyField) // PK
				{
					Trace.WriteLine(String.Format("Parsing PK \"{0}\"", tmpField.Name));

					// Filter Indexes
					dbLinkSchemas.RowFilter = String.Format("PK_COLUMN_NAME = '{0}' AND PK_TABLE_NAME = '{1}'", tmpField.DBColumn, this.DBEntityName);

					// Looping on FK's
					foreach (DataRowView tmpDataRowView in dbLinkSchemas)
					{
						Trace.WriteLine(String.Format("Parsing FK \"{0}\"", tmpDataRowView["FK_NAME"]));

						// Get Target Entity
						oTargetEntity = Project.CurrentProject.GetEntity(tmpDataRowView["FK_TABLE_NAME"].ToString());

						if (oTargetEntity == null)
						{
							Trace.WriteLine(String.Format("Entity for Table \"{0}\" not Found.", tmpDataRowView["FK_TABLE_NAME"]));
							continue;
						}

						// Check if the Link Alread Exists
						foreach (Link tmpLink in this.Links)
						{
							if (tmpLink.Fields.Count.Equals(1) && tmpLink.Fields[0].Name.Equals(tmpDataRowView["FK_COLUMN_NAME"].ToString()))
							{
								Trace.WriteLine(String.Format("Link por Field \"{0}\" Alread Exists", tmpField.Name));
								bIndexExists = true;
								break;
							}
						}

						if (bIndexExists) { break; }

						// Lopping on TargetEntity to Find the Correct Index
						foreach (Index tmpIndex in oTargetEntity.Indexes)
						{
							if (tmpIndex.Fields.Count.Equals(1) && tmpIndex.Fields[0].Name.EndsWith(tmpDataRowView["FK_COLUMN_NAME"].ToString()))
							{
								oTargetIndex = tmpIndex;
								break;
							}
						}

						// Check if Exists
						if (oTargetIndex == null)
						{
							Trace.WriteLine(String.Format("Creating Target Index for FK \"{0}\"", tmpDataRowView["FK_NAME"]));

							// Create Target Index
							oTargetIndex = new Index();
								oTargetIndex.SelectBy = true;
								oTargetIndex.DeleteBy = false;
								oTargetIndex.Name = tmpDataRowView["FK_COLUMN_NAME"].ToString();
								oTargetIndex.Unique = false;
								oTargetIndex.DBName = tmpDataRowView["FK_COLUMN_NAME"].ToString();
								oTargetIndex.PrimaryKey = false;

							// Create Target Index Field
							oTargetIndexField = new IndexField();
								oTargetIndexField.Name = tmpDataRowView["FK_COLUMN_NAME"].ToString();
								oTargetIndexField.PartialTextMatch = false;

							// Add Target Index Field on Target Index
							oTargetIndex.Fields.Add(oTargetIndexField);

							// Add Target Index on Target Entity
							oTargetEntity.Indexes.Add(oTargetIndex);
						}

						// Create Link
						oLink = new Link();
							oLink.TargetEntityName = oTargetEntity.Name;
							oLink.TargetIndexName = oTargetIndex.Name;
							oLink.IsCollection = true;
							oLink.IsProperty = true;
							oLink.ReadOnly = true;

						// Check if the Target Index is Unique
						if (oTargetIndex.Unique) // 1-N Relation
						{
							oLink.Name = oTargetEntity.Name; // N-M Relation
						}
						else { oLink.Name = oTargetEntity.PluralName; }
							
						// Check Delete Relation Action
						if (tmpDataRowView["DELETE_RULE"].ToString().Equals("NO ACTION"))
						{
							oLink.CascadeDelete = false;
						}
						else { oLink.CascadeUpdate = true; }

						// Check Update Relation Action
						if (tmpDataRowView["UPDATE_RULE"].ToString().Equals("NO ACTION"))
						{
							oLink.CascadeDelete = false;
						}
						else { oLink.CascadeUpdate = true; }

						// Create Link Field
						oSourceLinkField = new LinkField();
							oSourceLinkField.SourceFieldName = this.Fields.GetFieldFromDBColumn(tmpDataRowView["PK_COLUMN_NAME"].ToString()).Name;
							oSourceLinkField.TargetFieldName = oTargetEntity.Fields.GetFieldFromDBColumn(tmpDataRowView["FK_COLUMN_NAME"].ToString()).Name;

						// Add Link Field on Link
						oLink.Fields.Add(oSourceLinkField);

						// Add Link on Entity
						this.Links.Add(oLink);
					}

					if (bIndexExists) { continue; }
				}
			}
		}
		internal void RemoveLinkFromList(TreeNode rootNode, Link link)
		{
			// Looping on Child Nodes
			foreach (TreeNode tmpNode in rootNode.Nodes)
			{
				RemoveLinkFromList(tmpNode, link);
			}

			// Verify
			if (rootNode.Tag.Equals(link))
			{
				// Try to Select Previous Item
				if (rootNode.PrevVisibleNode != null && rootNode.NextVisibleNode != null && rootNode.NextVisibleNode.Tag is Link)
				{
					rootNode.TreeView.SelectedNode = rootNode.NextVisibleNode;
				}
				else { rootNode.TreeView.SelectedNode = rootNode.PrevVisibleNode; }

				// Remove
				rootNode.Parent.Nodes.Remove(rootNode);
			}
		}
		private void FillLinks(string sourcePrefix, string targetPrefix, Entity entity, bool excludeSourceFields)
		{
			DataView linkSchema = GetDBLinkSchema(entity);
			linkSchema.RowFilter = string.Format(
				"{0}TABLE_NAME = '{1}'"
				, sourcePrefix
				, entity.DBEntityName);
			linkSchema.Table.Columns.Add("Key", typeof(string));
			ArrayList keys = new ArrayList();
			foreach (DataRowView row in linkSchema)
			{
				row["Key"] = row[sourcePrefix + "NAME"] + "_" + row[targetPrefix + "NAME"];
				if (row[sourcePrefix + "NAME"] is string && !keys.Contains(row[sourcePrefix + "NAME"] as string))
				{
					keys.Add(row["Key"] as string);
				}
			}

			foreach (string key in keys)
			{
				_dblinks.Add(key);
				linkSchema.RowFilter = string.Format(
					"{2}TABLE_NAME = '{0}' and Key = '{1}'"
					, entity.DBEntityName
					, key
					, sourcePrefix);
				Entity linkedEntity = FindDBEntity(linkSchema[0][targetPrefix + "TABLE_NAME"] as string);
				if (linkedEntity == null) continue;
				Index linkedIndex = linkedEntity.Indexes.FindByDBName(linkSchema[0][targetPrefix + "NAME"] as string);
				if (linkedIndex == null) continue;
				Link link = entity.Links.FindByDBName(key);
				bool isCollection = (!linkedIndex.Unique && !linkedIndex.PrimaryKey);
				if (link == null)
				{
					link = new Link();
					link.IsExcluded = !this.OrmConfiguration.AutoEnableMappedLinks;
					link.DBName = key;
					link.IsProperty = true;
					if (isCollection)
						link.Name = linkedEntity.PluralName;
					else
						link.Name = linkedEntity.Name;
					entity.Links.Add(link);
				}
				link.IsCollection = isCollection;
				link.IsConstrained = (sourcePrefix == "FK_");
				link.TargetEntityName = linkedEntity.Name;
				link.TargetIndexName = linkedIndex.Name;
				link.Fields.Clear();
				foreach (DataRowView row in linkSchema)
				{
					LinkField field = new LinkField();
					field.SourceFieldName = entity.Fields.GetFieldFromDBColumn(row[sourcePrefix + "COLUMN_NAME"] as string).Name;
					field.TargetFieldName = linkedEntity.Fields.GetFieldFromDBColumn(row[targetPrefix + "COLUMN_NAME"] as string).Name;
					link.Fields.Add(field);
					if (excludeSourceFields && !link.IsExcluded)
					{
						entity.Fields.GetFieldFromName(field.SourceFieldName)
							.IsExcluded = true;
					}
				}
			}
		}
		private void designView_OnInsert(object sender, EventArgs e)
		{
			if (this.designView.CurrentSelectedNode != null)
			{
				if (this.designView.CurrentSelectedNode.Tag is EntityFieldCollection)
				{
					EntityField oEntity = new EntityField();
					oEntity.Name = Const_DefaultNewFieldText;

					((EntityFieldCollection) this.designView.CurrentSelectedNode.Tag).Add(oEntity);

					this.designView.RefreshFieldsList(this.designView.CurrentSelectedNode, this.designView.CurrentEntity, true);
				}
				else if (this.designView.CurrentSelectedNode.Tag is IndexCollection)
				{
					Index oIndex = new Index();
					oIndex.Name = Const_DefaultNewIndexText;

					((IndexCollection) this.designView.CurrentSelectedNode.Tag).Add(oIndex);

					this.designView.RefreshIndexesList(this.designView.CurrentSelectedNode, this.designView.CurrentEntity, true);
				}
				else if (this.designView.CurrentSelectedNode.Tag is LinkCollection)
				{
					Link oLink = new Link();
					oLink.Name = Const_DefaultNewLinkText;

					((LinkCollection) this.designView.CurrentSelectedNode.Tag).Add(oLink);

					this.designView.RefreshLinksList(this.designView.CurrentSelectedNode, this.designView.CurrentEntity, true);
				}
			}
		}
		private void btnAddLink_Click(object sender, EventArgs e)
		{
			LinkCollection Links = (LinkCollection) GetSelectedItemOfType(typeof (LinkCollection));
			Link oLink = new Link();
			Links.Add(oLink);
			IObjectEditor editor = new ORLinkEditor(oLink);
			ObjectEditorManager.OpenObjectEditorDialog(editor);
			this.designView.RefreshLinksList(this.GetSelectedNodeOfType(typeof(LinkCollection)), this.designView.CurrentEntity, false);
		}
		public ORLinkEditor(Link link) : this() {
			_link = link;
		}