Example #1
0
        private static void LoadEntityComposites(string folder, Entity entity)
        {
            XmlDocument document = null;
            var fileName = Path.Combine(folder, entity.Name + ".composites.xml");
            if (!File.Exists(fileName)) return;
            try
            {
                document = new XmlDocument();
                document.Load(fileName);
            }
            catch (Exception ex)
            {
                //Do Nothing
                MessageBox.Show("The file '" + fileName + "' is not valid and could not be loaded!", "Load Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            foreach (XmlNode n in document.DocumentElement)
            {
                var newComposite = new Composite(entity.Partition);
                entity.Composites.Add(newComposite);

                newComposite.Summary = XmlHelper.GetNodeValue(document.DocumentElement, "summary", newComposite.Summary);
                newComposite.CodeFacade = XmlHelper.GetAttributeValue(n, "codefacade", newComposite.CodeFacade);
                newComposite.IsGenerated = XmlHelper.GetAttributeValue(n, "isgenerated", newComposite.IsGenerated);
                newComposite.Name = XmlHelper.GetAttributeValue(n, "name", newComposite.Name);

                var columnsNode = n.SelectSingleNode("columnset");
                if (columnsNode != null)
                {
                    foreach (XmlNode m in columnsNode.ChildNodes)
                    {
                        var newField = new CompositeField(entity.Partition);
                        newComposite.Fields.Add(newField);
                        newField.FieldId = XmlHelper.GetAttributeValue(m, "fieldid", newField.FieldId);
                    }
                }

            }

        }
		private void cmdOK_Click(object sender, EventArgs e)
		{
			using (var transaction = _store.TransactionManager.BeginTransaction(Guid.NewGuid().ToString()))
			{
				_composite.Fields.Clear();
				foreach (Field column in lstMembers.CheckedItems)
				{
					var newField = new CompositeField(_model.Partition);
					newField.FieldId = column.Id;
					_composite.Fields.Add(newField);
				}
				transaction.Commit();
			}

			this.DialogResult = DialogResult.OK;
			this.Close();
		}
        public static void ImportLegacyModel(nHydrateModel model, Store store, nHydrateDiagram diagram, string legacyFileName)
        {
            diagram.IsLoading = true;
            model.IsLoading = true;
            try
            {
                var addedElements = new List<ModelElement>();
                using (var transaction = store.TransactionManager.BeginTransaction(Guid.NewGuid().ToString()))
                {
                    //The loading assemblies have changed names, so we must manually intervene
                    var newFile = legacyFileName + ".converting";
                    nHydrate.Generator.nHydrateGeneratorProject project = null;
                    try
                    {
                        File.Copy(legacyFileName, newFile);
                        var fileText = File.ReadAllText(newFile);
                        fileText = fileText.Replace("Widgetsphere.Generator.", "nHydrate.Generator.");
                        fileText = fileText.Replace("WidgetsphereGeneratorProject", "nHydrateGeneratorProject");
                        File.WriteAllText(newFile, fileText);
                        System.Threading.Thread.Sleep(500);
                        project = nHydrate.Generator.Common.GeneratorFramework.GeneratorHelper.OpenModel(newFile) as nHydrate.Generator.nHydrateGeneratorProject;
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show("An error occurred while importing the legacy modelRoot.", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                        return;
                    }
                    finally
                    {
                        if (File.Exists(newFile))
                            File.Delete(newFile);
                    }

                    try
                    {
                        var oldModel = project.Model as nHydrate.Generator.Models.ModelRoot;

                        model.TransformNames = oldModel.TransformNames;
                        model.CompanyName = oldModel.CompanyName;
                        model.ProjectName = oldModel.ProjectName;
                        model.Copyright = oldModel.Copyright;
                        model.CreatedByColumnName = oldModel.Database.CreatedByColumnName;
                        model.CreatedDateColumnName = oldModel.Database.CreatedDateColumnName;
                        model.DefaultNamespace = oldModel.DefaultNamespace;
                        model.ModifiedByColumnName = oldModel.Database.ModifiedByColumnName;
                        model.ModifiedDateColumnName = oldModel.Database.ModifiedDateColumnName;
                        model.SQLServerType = (DatabaseTypeConstants)Enum.Parse(typeof(DatabaseTypeConstants), oldModel.SQLServerType.ToString());
                        model.StoredProcedurePrefix = oldModel.StoredProcedurePrefix;
                        model.TenantPrefix = oldModel.TenantPrefix;
                        model.AllowMocks = oldModel.AllowMocks;
                        model.TimestampColumnName = oldModel.Database.TimestampColumnName;
                        model.UseUTCTime = oldModel.UseUTCTime;
                        model.Version = oldModel.Version;

                        #region Load Tables
                        foreach (var table in oldModel.Database.Tables.ToList())
                        {
                            var newEntity = new Entity(model.Partition);
                            model.Entities.Add(newEntity);
                            addedElements.Add(newEntity);
                            newEntity.AllowAuditTracking = table.AllowAuditTracking;
                            newEntity.AllowCreateAudit = table.AllowCreateAudit;
                            newEntity.AllowModifyAudit = table.AllowModifiedAudit;
                            newEntity.AllowTimestamp = table.AllowTimestamp;
                            newEntity.CodeFacade = table.CodeFacade;
                            newEntity.Summary = table.Description;
                            newEntity.Immutable = table.Immutable;
                            newEntity.IsAssociative = table.AssociativeTable;
                            newEntity.IsGenerated = table.Generated;
                            newEntity.TypedEntity = (TypedEntityConstants)Enum.Parse(typeof(TypedEntityConstants), table.TypedTable.ToString(), true);
                            newEntity.Name = table.Name;
                            newEntity.Schema = table.DBSchema;
                            //DO NOT IMPORT METADATA

                            #region Load the columns
                            var maxSortOrder = 1;
                            foreach (var column in table.GetColumns())
                            {
                                var newField = new Field(model.Partition);
                                newEntity.Fields.Add(newField);
                                newField.CodeFacade = column.CodeFacade;
                                newField.Collate = column.Collate;
                                newField.DataType = (DataTypeConstants)Enum.Parse(typeof(DataTypeConstants), column.DataType.ToString());
                                newField.Default = column.Default;
                                newField.Summary = column.Description;
                                newField.Formula = column.Formula;
                                newField.FriendlyName = column.FriendlyName;
                                newField.Identity = (IdentityTypeConstants)Enum.Parse(typeof(IdentityTypeConstants), column.Identity.ToString());
                                newField.IsCalculated = column.ComputedColumn;
                                newField.IsGenerated = column.Generated;
                                newField.IsPrimaryKey = column.PrimaryKey;
                                newField.SortOrder = maxSortOrder++;

                                //Do not reset as it creates 2 indexes with PK
                                if (!newField.IsPrimaryKey)
                                    newField.IsIndexed = column.IsIndexed;

                                newField.IsReadOnly = column.IsReadOnly;
                                newField.IsUnique = column.IsUnique;
                                newField.Length = column.Length;
                                newField.Max = column.Max;
                                newField.Min = column.Min;
                                newField.Name = column.Name;
                                newField.Nullable = column.AllowNull;
                                newField.Scale = column.Scale;
                                newField.IsBrowsable = column.IsBrowsable;
                                newField.ValidationExpression = column.ValidationExpression;
                            }
                            #endregion

                            #region Composites
                            foreach (var component in table.ComponentList.ToList())
                            {
                                var newComposite = new Composite(model.Partition);
                                newEntity.Composites.Add(newComposite);
                                addedElements.Add(newComposite);
                                newComposite.CodeFacade = component.CodeFacade;
                                newComposite.Summary = component.Description;
                                newComposite.Name = component.Name;
                                newComposite.IsGenerated = component.Generated;

                                //Add the fields by looking them up in the new table
                                foreach (var column in component.GetColumns())
                                {
                                    var checkColumn = table.GetColumns().FirstOrDefault(x => x.Key == column.Key);
                                    if (checkColumn != null)
                                    {
                                        var tableField = newEntity.Fields.FirstOrDefault(x => x.Name == checkColumn.Name);
                                        if (tableField != null)
                                        {
                                            var newField = new CompositeField(model.Partition);
                                            newComposite.Fields.Add(newField);
                                            newField.FieldId = tableField.Id;
                                        }
                                    }
                                }

                            }
                            #endregion

                            #region Load Static Data
                            var index = 1;
                            foreach (nHydrate.Generator.Models.RowEntry data in table.StaticData)
                            {
                                var isProcessed = false;
                                foreach (nHydrate.Generator.Models.CellEntry cellEntry in data.CellEntries)
                                {
                                    var column = cellEntry.ColumnRef.Object as nHydrate.Generator.Models.Column;
                                    if (column != null)
                                    {
                                        var newColumn = newEntity.Fields.FirstOrDefault(x => x.Name == column.Name);
                                        if (newColumn != null)
                                        {
                                            var newData = new StaticData(model.Partition);
                                            newEntity.StaticDatum.Add(newData);
                                            newData.ColumnKey = newColumn.Id;
                                            newData.Value = cellEntry.Value;
                                            newData.OrderKey = index;
                                            isProcessed = true;
                                        }
                                    }
                                }
                                if (isProcessed)
                                    index++;
                            }
                            #endregion

                        }

                        foreach (var table in oldModel.Database.Tables.ToList())
                        {
                            if (table.ParentTable != null)
                            {
                                var child = model.Entities.FirstOrDefault(x => x.Name == table.Name);
                                var parent = model.Entities.FirstOrDefault(x => x.Name == table.ParentTable.Name);
                                child.ParentInheritedEntity = parent;
                            }
                        }

                        #endregion

                        #region Load Relations
                        foreach (nHydrate.Generator.Models.Table table in oldModel.Database.Tables)
                        {
                            foreach (nHydrate.Generator.Models.Relation relation in table.GetRelations())
                            {
                                var entity1 = model.Entities.First(x => x.Name == relation.ParentTable.Name);
                                var entity2 = model.Entities.First(x => x.Name == relation.ChildTable.Name);
                                if (entity2.ParentInheritedEntity != entity1)
                                {
                                    entity1.ChildEntities.Add(entity2);
                                    var newRelation = entity1.RelationshipList.First(x =>
                                        x.SourceEntity == entity1 &&
                                        x.TargetEntity == entity2 &&
                                        !x.FieldMapList().Any());
                                    newRelation.RoleName = relation.RoleName;

                                    foreach (nHydrate.Generator.Models.ColumnRelationship relationColumn in relation.ColumnRelationships)
                                    {
                                        var column1 = entity1.Fields.First(x => x.Name == relationColumn.ParentColumn.Name);
                                        var column2 = entity2.Fields.First(x => x.Name == relationColumn.ChildColumn.Name);
                                        var newRelationField = new RelationField(model.Partition);
                                        model.RelationFields.Add(newRelationField);
                                        newRelationField.SourceFieldId = column1.Id;
                                        newRelationField.TargetFieldId = column2.Id;
                                        newRelationField.RelationID = newRelation.Id;
                                    }
                                }
                            }
                        }
                        #endregion

                        #region Load Views
                        foreach (nHydrate.Generator.Models.CustomView item in oldModel.Database.CustomViews)
                        {
                            var newitem = new nHydrate.Dsl.View(model.Partition);
                            model.Views.Add(newitem);
                            addedElements.Add(newitem);
                            newitem.CodeFacade = item.CodeFacade;
                            newitem.Summary = item.Description;
                            newitem.IsGenerated = item.Generated;
                            newitem.Name = item.Name;
                            newitem.Schema = item.DBSchema;
                            newitem.SQL = item.SQL;

                            foreach (var column in item.GetColumns())
                            {
                                var newColumn = new ViewField(model.Partition);
                                newitem.Fields.Add(newColumn);
                                newColumn.DataType = (DataTypeConstants)Enum.Parse(typeof(DataTypeConstants), column.DataType.ToString());
                                newColumn.Default = column.Default;
                                newColumn.Summary = column.Description;
                                newColumn.FriendlyName = column.FriendlyName;
                                newColumn.Length = column.Length;
                                newColumn.IsGenerated = column.Generated;
                                //newColumn.Max = column.ma;
                                //newColumn.Min = column.;
                                newColumn.Name = column.Name;
                                newColumn.Nullable = column.AllowNull;
                                newColumn.Scale = column.Scale;
                            }
                        }
                        #endregion

                        #region Load Stored Procedures
                        foreach (nHydrate.Generator.Models.CustomStoredProcedure item in oldModel.Database.CustomStoredProcedures)
                        {
                            var newitem = new StoredProcedure(model.Partition);
                            model.StoredProcedures.Add(newitem);
                            addedElements.Add(newitem);
                            newitem.CodeFacade = item.CodeFacade;
                            newitem.Summary = item.Description;
                            newitem.IsGenerated = item.Generated;
                            newitem.IsExisting = item.IsExisting;
                            newitem.Name = item.Name;
                            newitem.Schema = item.DBSchema;
                            newitem.SQL = item.SQL;
                            newitem.DatabaseObjectName = item.DatabaseObjectName;

                            foreach (var parameter in item.GetParameters())
                            {
                                var newParameter = new StoredProcedureParameter(model.Partition);
                                newitem.Parameters.Add(newParameter);
                                newParameter.Nullable = parameter.AllowNull;
                                newParameter.DataType = (nHydrate.Dsl.DataTypeConstants)Enum.Parse(typeof(nHydrate.Dsl.DataTypeConstants), parameter.DataType.ToString());
                                newParameter.Default = parameter.Default;
                                newParameter.Summary = parameter.Description;
                                newParameter.IsGenerated = parameter.Generated;
                                newParameter.IsOutputParameter = parameter.IsOutputParameter;
                                newParameter.Length = parameter.Length;
                                newParameter.Scale = parameter.Scale;
                                newParameter.Name = parameter.Name;
                            }

                            foreach (var column in item.GetColumns())
                            {
                                var newColumn = new StoredProcedureField(model.Partition);
                                newitem.Fields.Add(newColumn);
                                newColumn.DataType = (DataTypeConstants)Enum.Parse(typeof(DataTypeConstants), column.DataType.ToString());
                                newColumn.Default = column.Default;
                                newColumn.Summary = column.Description;
                                newColumn.FriendlyName = column.FriendlyName;
                                newColumn.Length = column.Length;
                                //newColumn.Max = column.ma;
                                //newColumn.Min = column.;
                                newColumn.Name = column.Name;
                                newColumn.Nullable = column.AllowNull;
                                newColumn.Scale = column.Scale;
                            }

                        }
                        #endregion

                        nHydrateSerializationHelper.LoadInitialIndexes(model);

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }

                }

                using (var transaction = store.TransactionManager.BeginTransaction(Guid.NewGuid().ToString()))
                {
                    diagram.AutoLayoutShapeElements(
                        diagram.NestedChildShapes.Where(x => addedElements.Contains(x.ModelElement)).ToList(),
                        Microsoft.VisualStudio.Modeling.Diagrams.GraphObject.VGRoutingStyle.VGRouteTreeNS,
                        Microsoft.VisualStudio.Modeling.Diagrams.GraphObject.PlacementValueStyle.VGPlaceNS,
                        true);
                    transaction.Commit();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                diagram.IsLoading = false;
                model.IsLoading = false;
            }

        }