Example #1
0
        public void ValidateEntities(ValidationContext context)
        {
            var timer = nHydrate.Dsl.Custom.DebugHelper.StartTimer();

            try
            {
                #region Check for zero tables
                if (this.Entities.Count(x => x.IsGenerated) == 0)
                {
                    context.LogError(ValidationHelper.ErrorTextNoTables, string.Empty, this);
                    return;
                }
                #endregion

                #region Verify that the name is valid
                foreach (var item in this.Entities.Where(x => x.IsGenerated))
                {
                    if (!ValidationHelper.ValidCodeIdentifier(item.PascalName) || !ValidationHelper.ValidEntityName(item.PascalName))
                    {
                        context.LogError(string.Format(ValidationHelper.ErrorTextInvalidIdentifier, item.Name), string.Empty, this);
                    }

                    foreach (var field in item.Fields)
                    {
                        if (!ValidationHelper.ValidCodeIdentifier(field.PascalName))
                        {
                            context.LogError(string.Format(ValidationHelper.ErrorTextInvalidIdentifier, field.Name), string.Empty, this);
                        }
                    }
                }
                #endregion

                #region Check for duplicate names
                var nameList = new Hashtable();
                foreach (var table in this.Entities.Where(x => x.IsGenerated))
                {
                    var name = table.Name.ToLower();
                    if (nameList.ContainsKey(name))
                    {
                        context.LogError(string.Format(ValidationHelper.ErrorTextDuplicateName, table.Name), string.Empty, this);
                    }
                    else
                    {
                        nameList.Add(name, string.Empty);
                    }
                }
                #endregion

                #region Check for duplication relationship names

                var duplicateList = new Dictionary <string, RelationshipChecker>();
                var relationList  = this.Store.ElementDirectory.AllElements.Where(x => x is EntityHasEntities).ToList().Cast <EntityHasEntities>();
                foreach (var relation in relationList)
                {
                    var childTable     = relation.TargetEntity;
                    var entity         = relation.SourceEntity;
                    var relationFields = this.RelationFields.Where(x => x.RelationID == relation.Id).ToList();

                    if (childTable != null && entity != null && childTable.IsGenerated && entity.IsGenerated)
                    {
                        var key = string.Empty;
                        if (StringHelper.Match(entity.Name, childTable.Name, true))
                        {
                            if (string.Compare(entity.Name, childTable.Name, false) < 0)
                            {
                                key = childTable.Name + "|" + relation.RoleName + "|" + entity.Name;
                            }
                            else
                            {
                                key = entity.Name + "|" + relation.RoleName + "|" + childTable.Name;
                            }
                        }
                        else
                        {
                            if (string.Compare(entity.Name, childTable.Name, false) < 0)
                            {
                                key = entity.Name + "|" + relation.RoleName + "|" + childTable.Name;
                            }
                            else
                            {
                                key = childTable.Name + "|" + relation.RoleName + "|" + entity.Name;
                            }
                        }

                        if (duplicateList.ContainsKey(key))
                        {
                            if (StringHelper.Match(entity.Name, childTable.Name, true))
                            {
                                duplicateList[key].TableList.Add(entity);
                            }
                            else
                            {
                                duplicateList[key].TableList.Add(childTable);
                            }
                        }
                        else
                        {
                            var rc = new RelationshipChecker(relation);
                            if (string.Compare(entity.Name, childTable.Name, true) < 0)
                            {
                                rc.TableList.Add(childTable);
                            }
                            else
                            {
                                rc.TableList.Add(entity);
                            }
                            duplicateList.Add(key, rc);
                        }

                        //Verify that a FK has an index on it
                        foreach (var field in relationFields)
                        {
                            var targetField = field.GetTargetField(relation);
                            if (targetField != null)
                            {
                                if (!childTable.Indexes.SelectMany(x => x.FieldList.Select(z => z.Id)).Any(x => x == targetField.Id))
                                {
                                    context.LogWarning(string.Format(ValidationHelper.ErrorTextFKNeedIndex, childTable.Name + "." + targetField.Name), string.Empty, this);
                                }
                            }
                        }
                    }
                }

                foreach (var key in duplicateList.Keys)
                {
                    if (duplicateList[key].TableList.Count > 1)
                    {
                        var t1 = duplicateList[key].Relationship.SourceEntity.Name;
                        var t2 = duplicateList[key].Relationship.TargetEntity.Name;
                        context.LogError(string.Format(ValidationHelper.ErrorTextConflictingRelationships, "'" + t1 + "' and '" + t2 + "'"), string.Empty, this);
                    }
                }

                #endregion

                #region Check for duplicate codefacades
                if (context.CurrentViolations.Count == 0)
                {
                    nameList = new Hashtable();
                    foreach (var table in this.Entities.Where(x => x.IsGenerated))
                    {
                        var name = table.PascalName.ToLower();
                        if (nameList.ContainsKey(name))
                        {
                            context.LogError(string.Format(ValidationHelper.ErrorTextDuplicateCodeFacade, table.Name), string.Empty, this);
                        }
                        else
                        {
                            nameList.Add(name, string.Empty);
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                nHydrate.Dsl.Custom.DebugHelper.StopTimer(timer, "Model Validate - Entities");
            }
        }
        public override MessageCollection Verify()
        {
            try
            {
                var retval = new MessageCollection();
                retval.AddRange(base.Verify());

                var tableCollection = (TableCollection)this.Object;

                #region Check for zero tables
                if (tableCollection.Count == 0)
                {
                    retval.Add(MessageTypeConstants.Error, ValidationHelper.ErrorTextNoTables, this);
                }
                #endregion

                #region Check for duplicate names
                var nameList = new Hashtable();
                foreach (Table table in tableCollection)
                {
                    if (table.Generated)
                    {
                        var name = table.Name.ToLower();
                        if (nameList.ContainsKey(name))
                        {
                            retval.Add(MessageTypeConstants.Error, string.Format(ValidationHelper.ErrorTextDuplicateName, table.Name), table.Controller);
                        }
                        else
                        {
                            nameList.Add(name, string.Empty);
                        }
                    }
                }
                #endregion

                #region Check for duplication relationship names

                var duplicateList = new Dictionary <string, RelationshipChecker>();
                foreach (Table table in tableCollection)
                {
                    foreach (Reference reference in table.Relationships)
                    {
                        var relation   = (Relation)reference.Object;
                        var childTable = (Table)relation.ChildTableRef.Object;
                        if (childTable != null)
                        {
                            var key = string.Empty;
                            if (StringHelper.Match(table.Name, childTable.Name, true))
                            {
                                if (string.Compare(table.Name, childTable.Name, false) < 0)
                                {
                                    key = childTable.Name + "|" + relation.RoleName + "|" + table.Name;
                                }
                                else
                                {
                                    key = table.Name + "|" + relation.RoleName + "|" + childTable.Name;
                                }
                            }
                            else
                            {
                                if (string.Compare(table.Name, childTable.Name, false) < 0)
                                {
                                    key = table.Name + "|" + relation.RoleName + "|" + childTable.Name;
                                }
                                else
                                {
                                    key = childTable.Name + "|" + relation.RoleName + "|" + table.Name;
                                }
                            }

                            if (duplicateList.ContainsKey(key))
                            {
                                if (StringHelper.Match(table.Name, childTable.Name, true))
                                {
                                    duplicateList[key].TableList.Add(table);
                                }
                                else
                                {
                                    duplicateList[key].TableList.Add(childTable);
                                }
                            }
                            else
                            {
                                var rc = new RelationshipChecker(relation);
                                if (string.Compare(table.Name, childTable.Name, true) < 0)
                                {
                                    rc.TableList.Add(childTable);
                                }
                                else
                                {
                                    rc.TableList.Add(table);
                                }
                                duplicateList.Add(key, rc);
                            }
                        }
                    }
                }

                foreach (var key in duplicateList.Keys)
                {
                    if (duplicateList[key].TableList.Count > 1)
                    {
                        var t1 = ((Table)duplicateList[key].Relationship.ChildTableRef.Object).Name;
                        var t2 = ((Table)duplicateList[key].Relationship.ParentTableRef.Object).Name;
                        retval.Add(MessageTypeConstants.Error, string.Format(ValidationHelper.ErrorTextConflictingRelationships, "'" + t1 + "' and '" + t2 + "'"), duplicateList[key].TableList[0].Controller);
                    }
                }

                #endregion

                #region Check for duplicate codefacades
                if (retval.Count == 0)
                {
                    nameList = new Hashtable();
                    foreach (Table table in tableCollection)
                    {
                        if (table.Generated)
                        {
                            var name = table.PascalName.ToLower();
                            if (nameList.ContainsKey(name))
                            {
                                retval.Add(MessageTypeConstants.Error, string.Format(ValidationHelper.ErrorTextDuplicateCodeFacade, table.Name), table.Controller);
                            }
                            else
                            {
                                nameList.Add(name, string.Empty);
                            }
                        }
                    }
                }
                #endregion

                return(retval);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                Application.DoEvents();
            }
        }
		public override MessageCollection Verify()
		{
			try
			{
				var retval = new MessageCollection();
				retval.AddRange(base.Verify());

				var tableCollection = (TableCollection)this.Object;

				#region Check for zero tables
				if (tableCollection.Count == 0)
				{
					retval.Add(MessageTypeConstants.Error, ValidationHelper.ErrorTextNoTables, this);
				}
				#endregion

				#region Check for duplicate names
				var nameList = new Hashtable();
				foreach (Table table in tableCollection)
				{
					if (table.Generated)
					{
						var name = table.Name.ToLower();
						if (nameList.ContainsKey(name))
							retval.Add(MessageTypeConstants.Error, string.Format(ValidationHelper.ErrorTextDuplicateName, table.Name), table.Controller);
						else
							nameList.Add(name, string.Empty);
					}
				}
				#endregion

				#region Check for duplication relationship names

				var duplicateList = new Dictionary<string, RelationshipChecker>();
				foreach (Table table in tableCollection)
				{
					foreach (Reference reference in table.Relationships)
					{
						var relation = (Relation)reference.Object;
						var childTable = (Table)relation.ChildTableRef.Object;
						if (childTable != null)
						{
							var key = string.Empty;
							if (StringHelper.Match(table.Name, childTable.Name, true))
							{
								if (string.Compare(table.Name, childTable.Name, false) < 0)
									key = childTable.Name + "|" + relation.RoleName + "|" + table.Name;
								else
									key = table.Name + "|" + relation.RoleName + "|" + childTable.Name;
							}
							else
							{
								if (string.Compare(table.Name, childTable.Name, false) < 0)
									key = table.Name + "|" + relation.RoleName + "|" + childTable.Name;
								else
									key = childTable.Name + "|" + relation.RoleName + "|" + table.Name;
							}

							if (duplicateList.ContainsKey(key))
							{
								if (StringHelper.Match(table.Name, childTable.Name, true))
									duplicateList[key].TableList.Add(table);
								else duplicateList[key].TableList.Add(childTable);
							}
							else
							{
								var rc = new RelationshipChecker(relation);
								if (string.Compare(table.Name, childTable.Name, true) < 0)
									rc.TableList.Add(childTable);
								else rc.TableList.Add(table);
								duplicateList.Add(key, rc);
							}
						}

					}

				}

				foreach (var key in duplicateList.Keys)
				{
					if (duplicateList[key].TableList.Count > 1)
					{
						var t1 = ((Table)duplicateList[key].Relationship.ChildTableRef.Object).Name;
						var t2 = ((Table)duplicateList[key].Relationship.ParentTableRef.Object).Name;
						retval.Add(MessageTypeConstants.Error, string.Format(ValidationHelper.ErrorTextConflictingRelationships, "'" + t1 + "' and '" + t2 + "'"), duplicateList[key].TableList[0].Controller);
					}
				}

				#endregion

				#region Check for duplicate codefacades
				if (retval.Count == 0)
				{
					nameList = new Hashtable();
					foreach (Table table in tableCollection)
					{
						if (table.Generated)
						{
							var name = table.PascalName.ToLower();
							if (nameList.ContainsKey(name))
								retval.Add(MessageTypeConstants.Error, string.Format(ValidationHelper.ErrorTextDuplicateCodeFacade, table.Name), table.Controller);
							else
								nameList.Add(name, string.Empty);
						}
					}
				}
				#endregion

				return retval;

			}
			catch (Exception ex)
			{
				throw;
			}
			finally
			{
				Application.DoEvents();
			}

		}
Example #4
0
        public void ValidateEntities(ValidationContext context)
        {
            var timer = nHydrate.Dsl.Custom.DebugHelper.StartTimer();
            try
            {
                #region Check for zero tables
                if (this.Entities.Count(x => x.IsGenerated) == 0)
                {
                    context.LogError(ValidationHelper.ErrorTextNoTables, string.Empty, this);
                    return;
                }
                #endregion

                #region Verify that the name is valid
                foreach (var item in this.Entities.Where(x => x.IsGenerated))
                {
                    if (!ValidationHelper.ValidCodeIdentifier(item.PascalName) || !ValidationHelper.ValidEntityName(item.PascalName))
                    {
                        context.LogError(string.Format(ValidationHelper.ErrorTextInvalidIdentifier, item.Name), string.Empty, this);
                    }

                    foreach (var field in item.Fields)
                    {
                        if (!ValidationHelper.ValidCodeIdentifier(field.PascalName))
                        {
                            context.LogError(string.Format(ValidationHelper.ErrorTextInvalidIdentifier, field.Name), string.Empty, this);
                        }
                    }

                }
                #endregion

                #region Check for duplicate names
                var nameList = new Hashtable();
                foreach (var table in this.Entities.Where(x => x.IsGenerated))
                {
                    var name = table.Name.ToLower();
                    if (nameList.ContainsKey(name))
                        context.LogError(string.Format(ValidationHelper.ErrorTextDuplicateName, table.Name), string.Empty, this);
                    else
                        nameList.Add(name, string.Empty);
                }
                #endregion

                #region Check for duplication relationship names

                var duplicateList = new Dictionary<string, RelationshipChecker>();
                var relationList = this.Store.ElementDirectory.AllElements.Where(x => x is EntityHasEntities).ToList().Cast<EntityHasEntities>();
                foreach (var relation in relationList)
                {
                    var childTable = relation.TargetEntity;
                    var entity = relation.SourceEntity;
                    var relationFields = this.RelationFields.Where(x => x.RelationID == relation.Id).ToList();

                    if (childTable != null && entity != null && childTable.IsGenerated && entity.IsGenerated)
                    {
                        var key = string.Empty;
                        if (StringHelper.Match(entity.Name, childTable.Name, true))
                        {
                            if (string.Compare(entity.Name, childTable.Name, false) < 0)
                                key = childTable.Name + "|" + relation.RoleName + "|" + entity.Name;
                            else
                                key = entity.Name + "|" + relation.RoleName + "|" + childTable.Name;
                        }
                        else
                        {
                            if (string.Compare(entity.Name, childTable.Name, false) < 0)
                                key = entity.Name + "|" + relation.RoleName + "|" + childTable.Name;
                            else
                                key = childTable.Name + "|" + relation.RoleName + "|" + entity.Name;
                        }

                        if (duplicateList.ContainsKey(key))
                        {
                            if (StringHelper.Match(entity.Name, childTable.Name, true))
                                duplicateList[key].TableList.Add(entity);
                            else duplicateList[key].TableList.Add(childTable);
                        }
                        else
                        {
                            var rc = new RelationshipChecker(relation);
                            if (string.Compare(entity.Name, childTable.Name, true) < 0)
                                rc.TableList.Add(childTable);
                            else rc.TableList.Add(entity);
                            duplicateList.Add(key, rc);
                        }

                        //Verify that a FK has an index on it
                        foreach (var field in relationFields)
                        {
                            var targetField = field.GetTargetField(relation);
                            if (targetField != null)
                            {
                                if (!childTable.Indexes.SelectMany(x => x.FieldList.Select(z => z.Id)).Any(x => x == targetField.Id))
                                    context.LogWarning(string.Format(ValidationHelper.ErrorTextFKNeedIndex, childTable.Name + "." + targetField.Name), string.Empty, this);
                            }
                        }
                    }

                }

                foreach (var key in duplicateList.Keys)
                {
                    if (duplicateList[key].TableList.Count > 1)
                    {
                        var t1 = duplicateList[key].Relationship.SourceEntity.Name;
                        var t2 = duplicateList[key].Relationship.TargetEntity.Name;
                        context.LogError(string.Format(ValidationHelper.ErrorTextConflictingRelationships, "'" + t1 + "' and '" + t2 + "'"), string.Empty, this);
                    }
                }

                #endregion

                #region Check for duplicate codefacades
                if (context.CurrentViolations.Count == 0)
                {
                    nameList = new Hashtable();
                    foreach (var table in this.Entities.Where(x => x.IsGenerated))
                    {
                        var name = table.PascalName.ToLower();
                        if (nameList.ContainsKey(name))
                            context.LogError(string.Format(ValidationHelper.ErrorTextDuplicateCodeFacade, table.Name), string.Empty, this);
                        else
                            nameList.Add(name, string.Empty);
                    }
                }
                #endregion

            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                nHydrate.Dsl.Custom.DebugHelper.StopTimer(timer, "Model Validate - Entities");
            }

        }