private void RefreshTableMetadata(TableModel table)
 {
     if (table.ObjectType == DbObjectType.Table)
         table.MetaData = DbMetadata.GetTable(table.Owner + "." + table.Name, "app", true, true, true, false, true);
     else
         table.MetaData = DbMetadata.GetView(table.Owner + "." + table.Name, "app");
 }
        public void Generate(TableModel md, MapGenSettings settings)
        {
            Model = md;
            Settings = settings;

            GenerateCode();
        }
        public void DrawTable(TableModel table, int? X = null, int? Y = null, bool addToRepository = false)
        {
            int maxWidth;
            EntitySymbol et = (EntitySymbol) TableShape.CreateInstance();
            AddcolumnsToEntitySymbol(et, table.MetaData, out maxWidth);
            et.Text = String.IsNullOrEmpty(table.UserMapping.ClassName) ? table.MetaData.Name : table.UserMapping.ClassName;

            //In 'Shape' form, only the Tag can be ecffectively used as identifier
            et.Tag = table.MetaData.Name;

            if (table.ShapePlacementInfo == null && X != null && Y != null)
            {
                et.X = X.Value;
                et.Y = Y.Value;

                et.Width = maxWidth + _defaultTableWidthOffset;
                et.Height = table.MetaData.Columns.Count*_defaultHeightForEachColumn + _defaultTableHeightOffset;
            }
            else if (table.ShapePlacementInfo == null && X == null && Y == null)
            {
                et.X = TableDiagram.Width/2;
                et.Y = TableDiagram.Height/2;
                et.Width = maxWidth + _defaultTableWidthOffset;
                et.Height = table.MetaData.Columns.Count*_defaultHeightForEachColumn + _defaultTableHeightOffset;
            }
            else if (table.ShapePlacementInfo != null)
            {
                et.X = table.ShapePlacementInfo.Location.X;
                et.Y = table.ShapePlacementInfo.Location.Y;

                et.Width = table.ShapePlacementInfo.Size.Width;
                et.Height = table.ShapePlacementInfo.Size.Height;
            }

            //After placing the object, registers the placement informations
            if (table.ShapePlacementInfo == null)
                table.ShapePlacementInfo = new ShapePlacementInformation
                {
                    Location = new Point(et.X, et.Y),
                    Size = new Size(et.Width, et.Height),
                    ShapeTag = (string) et.Tag
                };

            TableDiagram.Shapes.Add(et, Project.Repository.ObtainNewTopZOrder(TableDiagram));
            if (addToRepository)
                Repository.Insert((Shape) et, TableDiagram);
        }
        public void Init(DbRelationship rel)
        {
            lblRel.Text = String.Format("{0} ----- {1}", rel.OriginalTable, rel.RefTable);

            Relationship = rel;
            Model = null;

            cmbRelType.SelectedItem = cmbRelType.Items[cmbRelType.Items.IndexOf(rel.RelType.ToString())];
            cmbCascStyleDirect.SelectedItem = cmbCascStyleDirect.Items[cmbCascStyleDirect.Items.IndexOf(rel.CascadeStyleDirect.ToString())];
            cmbCascStyleInverse.SelectedItem = cmbCascStyleDirect.Items[cmbCascStyleDirect.Items.IndexOf(rel.CascadeStyleInverse.ToString())];
            chkGenerate.Checked = rel.Generate;
            chkGenInverse.Checked = rel.GenerateInverse;
            chkLazyDirect.Checked = rel.LazyLoadingDirect;
            chkLazyInverse.Checked = rel.LazyLoadingInverse;
            chkNullableDirect.Checked = rel.NullableDirect;

            AdjustDisplay();
        }
        public void Generate(string owner, string tableName, MapGenSettings settings)
        {
            Model = new TableModel {Owner = owner, Name = tableName, ObjectType = DbObjectType.Table};
            Settings = settings;

            Model.MetaData = DbMetadata.GetTable(owner + "." + tableName, "app", true, true, true, false, true);
            if (Model.MetaData == null)
            {
                Model.MetaData = DbMetadata.GetView(owner + "." + tableName, "app");
                if (Model.MetaData == null)
                    throw new Exception("Non è stata trovata nessuna tabella o vista col nome/schema fornito: " + owner +
                                        "." + tableName);

                Model.ObjectType = DbObjectType.View;
            }
            Model.UserMapping = null;

            GenerateCode();
        }
        public void Init(TableModel model)
        {
            lblTab.Text = String.Format("{0}", model.Name);

            Model = model;
            Relationship = null;
            txtClassName.Text = model.UserMapping.ClassName;
            txtCollectionName.Text = model.UserMapping.SetName;

            isView = model.ObjectType == DbObjectType.View;
            if (isView)
            {
                cmbFields.Items.Clear();
                cmbFields.Items.Add("");
                cmbFields.Items.AddRange(Model.MetaData.Columns.Select(t => t.Name).ToArray());
                if (String.IsNullOrEmpty(model.ForcedPrimaryKey))
                    cmbFields.SelectedItem = "";
                else
                    cmbFields.SelectedItem = model.ForcedPrimaryKey;
            }

            AdjustDisplay();
        }
        public void UpdateTableShape(TableModel newModel)
        {
            var toUpd = TableDiagram.Shapes.Single(t => (string) t.Tag == newModel.Name);

            ((EntitySymbol) toUpd).Text = String.IsNullOrEmpty(newModel.UserMapping.ClassName) ? ((EntitySymbol) toUpd).Text : newModel.UserMapping.ClassName;
            Repository.Update(toUpd);
        }
 private string BuildClassName(TableModel model)
 {
     return !String.IsNullOrEmpty(model.UserMapping.ClassName) ? model.UserMapping.ClassName : BuildClassName(model.Owner, model.Name);
 }
        /// <summary>
        /// Usage: String.Format(GetFileStructure(...), *CodiceGenerato*)
        /// </summary>
        /// <param name="tab"></param>
        /// <param name="nameSp"></param>
        /// <param name="toGen"></param>
        /// <param name="referencedNamespace"></param>
        /// <returns></returns>
        public string GetFileStructure(TableModel tab, string nameSp, CodeFileStructure toGen,
            string referencedNamespace)
        {
            StringBuilder builderStructure = new StringBuilder();

            if (toGen == CodeFileStructure.Domain)
                builderStructure.AppendFormat("using System;\r\n");

            if (toGen == CodeFileStructure.Domain)
            {
                //Horrible but works
                if (tab.GeneratedClass.Contains("IList"))
                    builderStructure.AppendFormat("using System.Collections.Generic;\r\n\r\n");
                else
                    builderStructure.AppendFormat("\r\n");
            }
            else
            {
                if (!String.IsNullOrEmpty(referencedNamespace))
                    builderStructure.AppendFormat("using {0};\r\n", referencedNamespace);

                //Horrible but works 2
                if (tab.GeneratedMapping.Contains("Generator"))
                    builderStructure.AppendFormat("using NHibernate.Mapping.ByCode;\r\n");

                builderStructure.AppendFormat("using NHibernate.Mapping.ByCode.Conformist;\r\n\r\n");
            }
            builderStructure.AppendFormat("namespace {0}\r\n", nameSp);
            builderStructure.AppendFormat("{{{{\r\n");
            builderStructure.Append("{0}");
            builderStructure.AppendFormat("}}}}");
            return builderStructure.ToString();
        }