public void CreateRelationship(CreatePropertyCommand createPropertyCommand, object sender, Puzzle.SideFX.Framework.Execution.ExecutionCancelEventArgs e)
        {
            IEngine engine = sender as IEngine;
            ISchemaService schemaService = engine.GetService<ISchemaService>();
            IDatabaseService databaseService = engine.GetService<IDatabaseService>();

            string className = createPropertyCommand.ClassName;
            string propertyName = createPropertyCommand.Name;

            string propertyType = createPropertyCommand.Type.ToString();

            string columnName = createPropertyCommand.ColumnName;
            DbType columnType = DbType.Int32; //TODO: Get the column of the identity property

            string tableName = schemaService.GetTableForClass(className);

            switch (createPropertyCommand.Multiplicity)
            {
                case Multiplicity.OneToMany:
                case Multiplicity.OneToOne:

                    //Add a property to the class
                    schemaService.CreateProperty(className, propertyName, propertyType);

                    //Set the nullability of the property
                    schemaService.SetPropertyMetaData(className, propertyName, PropertyMetaData.Nullable, createPropertyCommand.Nullable);

                    //Add a column to the table
                    schemaService.CreateColumn(tableName, columnName, columnType);

                    //Set the nullability of the column
                    schemaService.SetColumnMetaData(tableName, columnName, ColumnMetaData.Nullable, createPropertyCommand.Nullable);

                    //Map the property to the column in the schema
                    schemaService.MapPropertyToColumn(className, propertyName, tableName, columnName);

                    break;

                case Multiplicity.ManyToMany:

                    //Add a property to the class
                    schemaService.CreateListProperty(className, propertyName, propertyType);

                    //Add a many-many table
                    //schemaService.CreateTable(tableName, columnName, columnType);

                    break;

                case Multiplicity.ManyToOne:

                    //Add a property to the class
                    schemaService.CreateListProperty(className, propertyName, propertyType);

                    //Add a column to the table
                    //schemaService.CreateColumn(tableName, columnName, columnType);

                    break;

            }
        }
        public void CreateProperty(CreatePropertyCommand createPropertyCommand, object sender, ExecutionCancelEventArgs e)
        {
            IEngine engine = sender as IEngine;
            ISchemaService schemaService = engine.GetService<ISchemaService>();
            IDatabaseService databaseService = engine.GetService<IDatabaseService>();

            string className = createPropertyCommand.ClassName;
            string propertyName = createPropertyCommand.Name;

            string propertyType = createPropertyCommand.Type.ToString();

            string columnName = createPropertyCommand.ColumnName;
            DbType columnType = databaseService.GetDbType(createPropertyCommand.Type, createPropertyCommand.StringLength); ;

            string tableName = schemaService.GetTableForClass(className);

            //Add a property to the class
            schemaService.CreateProperty(className, propertyName, propertyType);

            //Set the nullability of the property
            schemaService.SetPropertyMetaData(className, propertyName, PropertyMetaData.Nullable, createPropertyCommand.Nullable);

            //Add a column to the table
            schemaService.CreateColumn(tableName, columnName, columnType);

            //Set the nullability of the column
            schemaService.SetColumnMetaData(tableName, columnName, ColumnMetaData.Nullable, createPropertyCommand.Nullable);

            //Map the property to the column in the schema
            schemaService.MapPropertyToColumn(className, propertyName, tableName, columnName);
        }
        public void CreateProperty(CreatePropertyCommand createPropertyCommand, object sender, ExecutionCancelEventArgs e)
        {
            IEngine          engine          = sender as IEngine;
            ISchemaService   schemaService   = engine.GetService <ISchemaService>();
            IDatabaseService databaseService = engine.GetService <IDatabaseService>();

            string className    = createPropertyCommand.ClassName;
            string propertyName = createPropertyCommand.Name;

            string propertyType = createPropertyCommand.Type.ToString();

            string columnName = createPropertyCommand.ColumnName;
            DbType columnType = databaseService.GetDbType(createPropertyCommand.Type, createPropertyCommand.StringLength);;

            string tableName = schemaService.GetTableForClass(className);

            //Add a property to the class
            schemaService.CreateProperty(className, propertyName, propertyType);

            //Set the nullability of the property
            schemaService.SetPropertyMetaData(className, propertyName, PropertyMetaData.Nullable, createPropertyCommand.Nullable);

            //Add a column to the table
            schemaService.CreateColumn(tableName, columnName, columnType);

            //Set the nullability of the column
            schemaService.SetColumnMetaData(tableName, columnName, ColumnMetaData.Nullable, createPropertyCommand.Nullable);

            //Map the property to the column in the schema
            schemaService.MapPropertyToColumn(className, propertyName, tableName, columnName);
        }
        public void CreateRelationship(CreatePropertyCommand createPropertyCommand, object sender, Puzzle.SideFX.Framework.Execution.ExecutionCancelEventArgs e)
        {
            IEngine          engine          = sender as IEngine;
            ISchemaService   schemaService   = engine.GetService <ISchemaService>();
            IDatabaseService databaseService = engine.GetService <IDatabaseService>();

            string className    = createPropertyCommand.ClassName;
            string propertyName = createPropertyCommand.Name;

            string propertyType = createPropertyCommand.Type.ToString();

            string columnName = createPropertyCommand.ColumnName;
            DbType columnType = DbType.Int32; //TODO: Get the column of the identity property

            string tableName = schemaService.GetTableForClass(className);

            switch (createPropertyCommand.Multiplicity)
            {
            case Multiplicity.OneToMany:
            case Multiplicity.OneToOne:

                //Add a property to the class
                schemaService.CreateProperty(className, propertyName, propertyType);

                //Set the nullability of the property
                schemaService.SetPropertyMetaData(className, propertyName, PropertyMetaData.Nullable, createPropertyCommand.Nullable);

                //Add a column to the table
                schemaService.CreateColumn(tableName, columnName, columnType);

                //Set the nullability of the column
                schemaService.SetColumnMetaData(tableName, columnName, ColumnMetaData.Nullable, createPropertyCommand.Nullable);

                //Map the property to the column in the schema
                schemaService.MapPropertyToColumn(className, propertyName, tableName, columnName);

                break;

            case Multiplicity.ManyToMany:

                //Add a property to the class
                schemaService.CreateListProperty(className, propertyName, propertyType);

                //Add a many-many table
                //schemaService.CreateTable(tableName, columnName, columnType);

                break;

            case Multiplicity.ManyToOne:

                //Add a property to the class
                schemaService.CreateListProperty(className, propertyName, propertyType);

                //Add a column to the table
                //schemaService.CreateColumn(tableName, columnName, columnType);

                break;
            }
        }
        public void CreateRelationship(CreatePropertyCommand createPropertyCommand, object sender, ExecutionCancelEventArgs e)
        {
            //IDatabaseService databaseService = engine.GetService<IDatabaseService>();
            //ISqlService sqlService = engine.GetService<ISqlService>();
            //ISchemaService schemaService = engine.GetService<ISchemaService>();

            ////Begins a new transaction or gets the current transaction
            //IDbTransaction transaction = databaseService.EnsureTransaction();

            //string tableName = schemaService.GetTableForClass(createPropertyCommand.ClassName);
            //DbType dbType = databaseService.GetDbType(createPropertyCommand.Type, createPropertyCommand.StringLength);

            ////Create the database table with the primary key column
            //sqlService.CreateColumn(transaction, tableName, createPropertyCommand.ColumnName, dbType, createPropertyCommand.Nullable);
        }
        public void CreateRelationship(CreatePropertyCommand createPropertyCommand, object sender, ExecutionCancelEventArgs e)
        {
            //IDatabaseService databaseService = engine.GetService<IDatabaseService>();
            //ISqlService sqlService = engine.GetService<ISqlService>();
            //ISchemaService schemaService = engine.GetService<ISchemaService>();

            ////Begins a new transaction or gets the current transaction
            //IDbTransaction transaction = databaseService.EnsureTransaction();

            //string tableName = schemaService.GetTableForClass(createPropertyCommand.ClassName);
            //DbType dbType = databaseService.GetDbType(createPropertyCommand.Type, createPropertyCommand.StringLength);

            ////Create the database table with the primary key column
            //sqlService.CreateColumn(transaction, tableName, createPropertyCommand.ColumnName, dbType, createPropertyCommand.Nullable);
        }
        public void OnExecuting(object sender, ExecutionCancelEventArgs e)
        {
            IEngine engine = sender as IEngine;
            CreatePropertyCommand createPropertyCommand = CreatePropertyCommand.Evaluate(engine, e.Command);

            if (createPropertyCommand == null)
            {
                return;
            }

            if (createPropertyCommand.Multiplicity == Multiplicity.None)
            {
                CreateProperty(createPropertyCommand, sender, e);
            }
            else
            {
                CreateRelationship(createPropertyCommand, sender, e);
            }
        }
        public static CreatePropertyCommand Evaluate(IEngine engine, Command command)
        {
            if (command.Parameters.Count < 3)
                return null;

            if (command.Parameters[0].Name.ToLower() != "create")
                return null;
            if (command.Parameters[0].Value != null)
                return null;

            Multiplicity multiplicity = Multiplicity.None;

            switch (command.Parameters[1].Name.ToLower())
            {
                case "property":
                case "relationship":
                    break;
                case "one-one":
                case "oneone":
                case "one-to-one":
                case "onetoone":
                    multiplicity = Multiplicity.OneToOne;
                    break;
                case "one-many":
                case "onemany":
                case "one-to-many":
                case "onetomany":
                case "reference":
                    multiplicity = Multiplicity.OneToMany;
                    break;
                case "many-one":
                case "manyone":
                case "many-to-one":
                case "manytoone":
                case "list":
                    multiplicity = Multiplicity.ManyToOne;
                    break;
                case "many-many":
                case "manymany":
                case "many-to-many":
                case "manytomany":
                    multiplicity = Multiplicity.ManyToMany;
                    break;
                default:
                    return null;
            }
            if (command.Parameters[1].Value != null)
                return null;

            string name = command.Parameters[2].Name;
            if (string.IsNullOrEmpty(name))
                return null;  // register exception...

            string[] parts = name.Split(".".ToCharArray());
            if (parts.Length != 2)
                return null;  // register exception...

            string className = parts[0];
            string propertyName = parts[1];

            if (string.IsNullOrEmpty(className))
                return null;  // register exception...

            if (string.IsNullOrEmpty(propertyName))
                return null;  // register exception...

            ISchemaService schemaService = engine.GetService<ISchemaService>();
            IObjectService objectService = engine.GetService<IObjectService>();

            CreatePropertyCommand createPropertyCommand = new CreatePropertyCommand(className, propertyName);
            createPropertyCommand.Multiplicity = multiplicity;
            createPropertyCommand.Type = typeof(string);
            createPropertyCommand.StringLength = 255;

            bool list = false;

            IList<Parameter> parameters = command.Parameters[2].Value as IList<Parameter>;
            if (parameters != null)
            {
                foreach (Parameter parameter in parameters)
                {
                    if (parameter.Value != null)
                    {
                        switch (parameter.Name.ToLower())
                        {
                            case "type":
                                if (schemaService.HasClass(parameter.Value.ToString()))
                                    createPropertyCommand.Type = objectService.GetTypeByName(parameter.Value.ToString());
                                else
                                    createPropertyCommand.Type = Type.GetType(parameter.Value.ToString());
                                break;
                            case "list":
                                list = true;
                                break;
                            case "multiplicity":
                            case "ref":
                            case "reference":
                            case "referencetype":
                                if (createPropertyCommand.Multiplicity == Multiplicity.None)
                                {
                                    string refType = parameter.Value.ToString();
                                    switch (refType.ToLower())
                                    {
                                        case "one-one":
                                        case "oneone":
                                        case "one-to-one":
                                        case "onetoone":
                                            createPropertyCommand.Multiplicity = Multiplicity.OneToOne;
                                            break;
                                        case "one-many":
                                        case "onemany":
                                        case "one-to-many":
                                        case "onetomany":
                                            createPropertyCommand.Multiplicity = Multiplicity.OneToMany;
                                            break;
                                        case "many-one":
                                        case "manyone":
                                        case "many-to-one":
                                        case "manytoone":
                                            createPropertyCommand.Multiplicity = Multiplicity.ManyToOne;
                                            break;
                                        case "many-many":
                                        case "manymany":
                                        case "many-to-many":
                                        case "manytomany":
                                            createPropertyCommand.Multiplicity = Multiplicity.ManyToMany;
                                            break;
                                        default:
                                            createPropertyCommand.Multiplicity = Multiplicity.None;
                                            break;
                                    }
                                }
                                list = true;
                                break;
                            case "length":
                                createPropertyCommand.StringLength = Convert.ToInt32(parameter.Value.ToString());
                                break;
                            case "column":
                            case "columnname":
                                createPropertyCommand.ColumnName = parameter.Value.ToString();
                                break;
                            case "null":
                            case "nullable":
                                createPropertyCommand.Nullable = Convert.ToBoolean(parameter.Value.ToString());
                                break;
                            case "inverse":
                                createPropertyCommand.Inverse = parameter.Value.ToString();
                                break;
                        }
                    }
                }
            }

            if (createPropertyCommand.Multiplicity == Multiplicity.None)
            {
                if (schemaService.HasClass(objectService.GetTypeName(createPropertyCommand.Type)))
                {
                    if (list)
                        createPropertyCommand.Multiplicity = Multiplicity.ManyToOne;
                    else
                        createPropertyCommand.Multiplicity = Multiplicity.OneToMany;
                }
            }

            if (createPropertyCommand.Multiplicity != Multiplicity.None)
            {
                if (string.IsNullOrEmpty(createPropertyCommand.Inverse))
                    createPropertyCommand.Inverse = "InverseOf" + name;
            }

            return createPropertyCommand;
        }
        public static CreatePropertyCommand Evaluate(IEngine engine, Command command)
        {
            if (command.Parameters.Count < 3)
            {
                return(null);
            }

            if (command.Parameters[0].Name.ToLower() != "create")
            {
                return(null);
            }
            if (command.Parameters[0].Value != null)
            {
                return(null);
            }

            Multiplicity multiplicity = Multiplicity.None;

            switch (command.Parameters[1].Name.ToLower())
            {
            case "property":
            case "relationship":
                break;

            case "one-one":
            case "oneone":
            case "one-to-one":
            case "onetoone":
                multiplicity = Multiplicity.OneToOne;
                break;

            case "one-many":
            case "onemany":
            case "one-to-many":
            case "onetomany":
            case "reference":
                multiplicity = Multiplicity.OneToMany;
                break;

            case "many-one":
            case "manyone":
            case "many-to-one":
            case "manytoone":
            case "list":
                multiplicity = Multiplicity.ManyToOne;
                break;

            case "many-many":
            case "manymany":
            case "many-to-many":
            case "manytomany":
                multiplicity = Multiplicity.ManyToMany;
                break;

            default:
                return(null);
            }
            if (command.Parameters[1].Value != null)
            {
                return(null);
            }

            string name = command.Parameters[2].Name;

            if (string.IsNullOrEmpty(name))
            {
                return(null);  // register exception...
            }
            string[] parts = name.Split(".".ToCharArray());
            if (parts.Length != 2)
            {
                return(null);  // register exception...
            }
            string className    = parts[0];
            string propertyName = parts[1];

            if (string.IsNullOrEmpty(className))
            {
                return(null);  // register exception...
            }
            if (string.IsNullOrEmpty(propertyName))
            {
                return(null);  // register exception...
            }
            ISchemaService schemaService = engine.GetService <ISchemaService>();
            IObjectService objectService = engine.GetService <IObjectService>();

            CreatePropertyCommand createPropertyCommand = new CreatePropertyCommand(className, propertyName);

            createPropertyCommand.Multiplicity = multiplicity;
            createPropertyCommand.Type         = typeof(string);
            createPropertyCommand.StringLength = 255;

            bool list = false;

            IList <Parameter> parameters = command.Parameters[2].Value as IList <Parameter>;

            if (parameters != null)
            {
                foreach (Parameter parameter in parameters)
                {
                    if (parameter.Value != null)
                    {
                        switch (parameter.Name.ToLower())
                        {
                        case "type":
                            if (schemaService.HasClass(parameter.Value.ToString()))
                            {
                                createPropertyCommand.Type = objectService.GetTypeByName(parameter.Value.ToString());
                            }
                            else
                            {
                                createPropertyCommand.Type = Type.GetType(parameter.Value.ToString());
                            }
                            break;

                        case "list":
                            list = true;
                            break;

                        case "multiplicity":
                        case "ref":
                        case "reference":
                        case "referencetype":
                            if (createPropertyCommand.Multiplicity == Multiplicity.None)
                            {
                                string refType = parameter.Value.ToString();
                                switch (refType.ToLower())
                                {
                                case "one-one":
                                case "oneone":
                                case "one-to-one":
                                case "onetoone":
                                    createPropertyCommand.Multiplicity = Multiplicity.OneToOne;
                                    break;

                                case "one-many":
                                case "onemany":
                                case "one-to-many":
                                case "onetomany":
                                    createPropertyCommand.Multiplicity = Multiplicity.OneToMany;
                                    break;

                                case "many-one":
                                case "manyone":
                                case "many-to-one":
                                case "manytoone":
                                    createPropertyCommand.Multiplicity = Multiplicity.ManyToOne;
                                    break;

                                case "many-many":
                                case "manymany":
                                case "many-to-many":
                                case "manytomany":
                                    createPropertyCommand.Multiplicity = Multiplicity.ManyToMany;
                                    break;

                                default:
                                    createPropertyCommand.Multiplicity = Multiplicity.None;
                                    break;
                                }
                            }
                            list = true;
                            break;

                        case "length":
                            createPropertyCommand.StringLength = Convert.ToInt32(parameter.Value.ToString());
                            break;

                        case "column":
                        case "columnname":
                            createPropertyCommand.ColumnName = parameter.Value.ToString();
                            break;

                        case "null":
                        case "nullable":
                            createPropertyCommand.Nullable = Convert.ToBoolean(parameter.Value.ToString());
                            break;

                        case "inverse":
                            createPropertyCommand.Inverse = parameter.Value.ToString();
                            break;
                        }
                    }
                }
            }

            if (createPropertyCommand.Multiplicity == Multiplicity.None)
            {
                if (schemaService.HasClass(objectService.GetTypeName(createPropertyCommand.Type)))
                {
                    if (list)
                    {
                        createPropertyCommand.Multiplicity = Multiplicity.ManyToOne;
                    }
                    else
                    {
                        createPropertyCommand.Multiplicity = Multiplicity.OneToMany;
                    }
                }
            }

            if (createPropertyCommand.Multiplicity != Multiplicity.None)
            {
                if (string.IsNullOrEmpty(createPropertyCommand.Inverse))
                {
                    createPropertyCommand.Inverse = "InverseOf" + name;
                }
            }

            return(createPropertyCommand);
        }