new public void Insert(int link, LinkField val)
		{
			if (this._link != null) 
			{
				val.SetLink(this._link);
			}
			if ( this.Contains(val) ) 
			{
				this.Remove(val);
			}
			this.Insert(link, val);
		}
		public override int Add(LinkField val)
		{
			if (this._link != null) 
			{
				val.SetLink(this._link);
			}
			if ( !this.Contains(val) ) 
			{
				return base.Add(val);
			}
			return -1;
		}
		public static IList GetFields(LinkField sender) 
		{
			ArrayList list = new ArrayList();

			foreach (EntityField field in sender.Link.Entity.Fields)
			{
				list.Add(field.Name);
			}

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

			return list;
		}
		public static IList GetFields(LinkField sender) {
			ArrayList list = new ArrayList();

			foreach (MetadataFile tmpFiles in Project.CurrentProject.MetadataFiles) {
				foreach (MetadataEntityBase tmpEntityBase in tmpFiles.MetadataEntities) {
					if (tmpEntityBase.Name.Equals(sender.Link.TargetEntityName)) {
						foreach (Index index in ((Entity) tmpEntityBase).Indexes) {
							if (index.Name.Equals(sender.Link.TargetIndexName)) {
								foreach (IndexField field in index.Fields) {
									list.Add(field.Name);
								}
							}
						}

					}
				}
			}

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

			return list;
		}
Beispiel #5
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; }
				}
			}
		}
		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 cboTargetIndex_SelectionChangeCommitted(object sender, System.EventArgs e) {
			cboTargetIndex.Text = cboTargetIndex.SelectedItem.ToString();
			Index index = GetSelectedIndex();
			if (index != null) {
				_link.Fields.Clear();
				foreach (IndexField indexField in index.Fields) {
					LinkField linkField = new LinkField();
					linkField.TargetFieldName = indexField.Name;
					_link.Fields.Add(linkField);
				}
				RefreshLinkFieldsList();
			}
		}