Ejemplo n.º 1
0
        private static void LoadViews(string rootFolder, DiskModel results)
        {
            var folder = Path.Combine(rootFolder, FOLDER_VW);

            //Load Views
            if (Directory.Exists(folder))
            {
                var fList = Directory.GetFiles(folder, "*.configuration.xml");
                foreach (var f in fList)
                {
                    results.Views.Add(GetObject <nHydrate.ModelManagement.View.configuration>(f));
                }

                fList = Directory.GetFiles(folder, "*.sql");
                foreach (var f in fList)
                {
                    var fi = new FileInfo(f);
                    if (fi.Name.ToLower().EndsWith(".sql"))
                    {
                        var name = fi.Name.Substring(0, fi.Name.Length - 4).ToLower();
                        var item = results.Views.FirstOrDefault(x => x.name.ToLower() == name);
                        if (item != null)
                        {
                            item.sql = File.ReadAllText(f);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private static void LoadEntities(string rootFolder, DiskModel results)
        {
            var folder = Path.Combine(rootFolder, FOLDER_OLD_ET);

            if (!Directory.Exists(folder))
            {
                folder = Path.Combine(rootFolder, FOLDER_ET);
                if (!Directory.Exists(folder))
                {
                    return;
                }
            }

            //Load Entities
            foreach (var f in Directory.GetFiles(folder, "*.configuration.xml"))
            {
                results.Entities.Add(GetObject <Entity.configuration>(f));
            }

            //Load Indexes
            foreach (var f in Directory.GetFiles(folder, "*.indexes.xml"))
            {
                results.Indexes.Add(GetObject <nHydrate.ModelManagement.Index.configuration>(f));
            }

            //Clean up indexes
            var indexIdList = new List <string>();

            foreach (var i1 in results.Indexes)
            {
                var newList = new List <Index.configurationIndex>();
                foreach (var i2 in i1.index)
                {
                    if (!indexIdList.Contains(i2.id))
                    {
                        indexIdList.Add(i2.id);
                        newList.Add(i2);
                    }
                }
                i1.index = newList.ToArray();
            }

            //Load Relations
            foreach (var f in Directory.GetFiles(folder, "*.relations.xml"))
            {
                results.Relations.Add(GetObject <Relation.configuration>(f));
            }

            //Load Static Data
            foreach (var f in Directory.GetFiles(folder, "*.staticdata.xml"))
            {
                results.StaticData.Add(GetObject <StaticData.configuration>(f));
            }
        }
Ejemplo n.º 3
0
        public static void Save(string rootFolder, string modelName, DiskModel model)
        {
            var folderName  = modelName.Replace(".nhydrate", ".model");
            var modelFolder = GetModelFolder(rootFolder, folderName);

            var generatedFileList = new List <string>();

            SaveViews(modelFolder, model, generatedFileList); //must come before entities
            SaveEntities(modelFolder, model, generatedFileList);

            //Save the global model properties
            RemoveNullStrings(model.ModelProperties);
            SaveObject(model.ModelProperties, Path.Combine(modelFolder, "model.xml"), generatedFileList);

            //Do not remove diagram file
            generatedFileList.Add(Path.Combine(modelFolder, "diagram.xml"));

            RemoveOrphans(modelFolder, generatedFileList);

            try
            {
                var compressedFile = Path.Combine(rootFolder, modelName + ".zip");
                if (File.Exists(compressedFile))
                {
                    File.Delete(compressedFile);
                    System.Threading.Thread.Sleep(300);
                }

                //Create ZIP file with entire model folder
                System.IO.Compression.ZipFile.CreateFromDirectory(modelFolder, compressedFile, System.IO.Compression.CompressionLevel.Fastest, true);

                //Now add the top level model artifacts
                var artifacts = Directory.GetFiles(rootFolder, $"{modelName}.*").ToList();
                artifacts.RemoveAll(x => x == compressedFile);
                using (var zipToOpen = System.IO.Compression.ZipFile.Open(compressedFile, System.IO.Compression.ZipArchiveMode.Update))
                {
                    foreach (var ff in artifacts)
                    {
                        var fi = new FileInfo(ff);
                        zipToOpen.CreateEntryFromFile(ff, fi.Name);
                    }
                }
            }
            catch (Exception ex)
            {
                //Do Nothing
            }
        }
Ejemplo n.º 4
0
        private static void SaveEntities(string rootFolder, DiskModel model, List <string> generatedFileList)
        {
            var folder = Path.Combine(rootFolder, FOLDER_ET);

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            //Save Entities
            foreach (var obj in model.Entities)
            {
                var f = Path.Combine(folder, obj.name + ".configuration.xml");
                SaveObject(obj, f, generatedFileList);
            }

            //Save Indexes
            foreach (var obj in model.Indexes)
            {
                var entity = model.Entities.FirstOrDefault(x => x.id == obj.id);
                var f      = Path.Combine(folder, entity.name + ".indexes.xml");
                SaveObject(obj, f, generatedFileList);
            }

            //Save Relations
            foreach (var obj in model.Relations)
            {
                var entity = model.Entities.FirstOrDefault(x => x.id == obj.id);
                var f      = Path.Combine(folder, entity.name + ".relations.xml");
                SaveObject(obj, f, generatedFileList);
            }

            //Save Static Data
            foreach (var obj in model.StaticData)
            {
                var entity = model.Entities.FirstOrDefault(x => x.id == obj.id);
                var f      = Path.Combine(folder, entity.name + ".staticdata.xml");
                SaveObject(obj, f, generatedFileList);
            }

            WriteReadMeFile(folder, generatedFileList);
        }
Ejemplo n.º 5
0
        private static void ConvertViewsOld2Yaml(string rootFolder, DiskModel model, DiskModelYaml model2)
        {
            var folder = Path.Combine(rootFolder, FOLDER_VW);

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            //Views
            foreach (var obj in model.Views)
            {
                var newView = model2.Views.AddItem(new ViewYaml
                {
                    CodeFacade             = obj.codefacade,
                    GeneratesDoubleDerived = obj.generatesdoublederived != 0,
                    Id      = obj.id.ToGuid(),
                    Sql     = obj.sql,
                    Name    = obj.name,
                    Schema  = obj.schema,
                    Summary = obj.summary,
                });

                //Fields
                foreach (var ff in obj.fieldset)
                {
                    newView.Fields.AddItem(new ViewFieldYaml
                    {
                        CodeFacade   = ff.codefacade,
                        Datatype     = ff.datatype.ToEnum <DataTypeConstants>(),
                        Default      = ff.@default,
                        Id           = ff.id.ToGuid(),
                        IsPrimaryKey = ff.isprimarykey != 0,
                        Length       = ff.length,
                        Name         = ff.name,
                        Nullable     = ff.nullable != 0,
                        Scale        = ff.scale,
                        Summary      = ff.summary,
                    });
                }
            }
        }
Ejemplo n.º 6
0
        private static void SaveViews(string rootFolder, DiskModel model, List <string> generatedFileList)
        {
            var folder = Path.Combine(rootFolder, FOLDER_VW);

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            //Save Views
            foreach (var obj in model.Views)
            {
                var f = Path.Combine(folder, obj.name + ".configuration.xml");
                SaveObject(obj, f, generatedFileList);

                var f1 = Path.Combine(folder, obj.name + ".sql");
                WriteFileIfNeedBe(f1, obj.sql, new List <string>());
            }

            WriteReadMeFile(folder, generatedFileList);
        }
Ejemplo n.º 7
0
        private static void LoadEntities(string rootFolder, DiskModel results)
        {
            var folder = Path.Combine(rootFolder, FOLDER_ET);

            if (!Directory.Exists(folder))
            {
                return;
            }

            //Load Entities
            var fList = Directory.GetFiles(folder, "*.configuration.xml");

            foreach (var f in fList)
            {
                results.Entities.Add(GetObject <Entity.configuration>(f));
            }

            //Load Indexes
            fList = Directory.GetFiles(folder, "*.indexes.xml");
            foreach (var f in fList)
            {
                results.Indexes.Add(GetObject <nHydrate.ModelManagement.Index.configuration>(f));
            }

            //Load Relations
            fList = Directory.GetFiles(folder, "*.relations.xml");
            foreach (var f in fList)
            {
                results.Relations.Add(GetObject <Relation.configuration>(f));
            }

            //Load Static Data
            fList = Directory.GetFiles(folder, "*.staticdata.xml");
            foreach (var f in fList)
            {
                results.StaticData.Add(GetObject <StaticData.configuration>(f));
            }
        }
Ejemplo n.º 8
0
        public static DiskModel Load(string rootFolder, string modelName, out bool wasLoaded)
        {
            wasLoaded = false;
            var modelFile = Path.Combine(rootFolder, modelName);
            var fi        = new FileInfo(modelFile);
            var showError = (fi.Length > 10); //New file is small so show no error if creating new

            var folderName  = modelName.Replace(".nhydrate", ".model");
            var modelFolder = GetModelFolder(rootFolder, folderName);

            //If the model folder does NOT exist
            if (!Directory.Exists(modelFolder))
            {
                if (showError)
                {
                    //Try to use the ZIP file
                    var compressedFile = Path.Combine(rootFolder, modelName + ".zip");
                    if (!File.Exists(compressedFile))
                    {
                        throw new Exception("The model folder was not found and the ZIP file is missing. One of these must exist to continue.");
                    }

                    //Unzip the whole file
                    ExtractToDirectory(compressedFile, rootFolder, false);
                }
            }
            else
            {
                wasLoaded = true;
            }

            var results = new DiskModel();

            LoadEntities(modelFolder, results);
            LoadViews(modelFolder, results);

            //Save the global model properties
            var globalFile = Path.Combine(modelFolder, "model.xml");

            if (File.Exists(globalFile))
            {
                results.ModelProperties = GetObject <ModelProperties>(globalFile);
            }

            RemoveNullStrings(results.ModelProperties);

            #region Clean up
            //Ensure all arrays are not null
            foreach (var obj in results.Entities)
            {
                if (obj.fieldset == null)
                {
                    obj.fieldset = new Entity.configurationField[0];
                }
            }
            foreach (var obj in results.Indexes)
            {
                if (obj.index == null)
                {
                    obj.index = new Index.configurationIndex[0];
                }
                foreach (var obj2 in obj.index)
                {
                    if (obj2.indexcolumnset == null)
                    {
                        obj2.indexcolumnset = new Index.configurationIndexColumn[0];
                    }
                }
            }
            foreach (var obj in results.Relations)
            {
                if (obj.relation == null)
                {
                    obj.relation = new Relation.configurationRelation[0];
                }
                foreach (var obj2 in obj.relation)
                {
                    if (obj2.relationfieldset == null)
                    {
                        obj2.relationfieldset = new Relation.configurationRelationField[0];
                    }
                }
            }
            foreach (var obj in results.StaticData)
            {
                if (obj.data == null)
                {
                    obj.data = new StaticData.configurationData[0];
                }
            }
            foreach (var obj in results.Views)
            {
                if (obj.fieldset == null)
                {
                    obj.fieldset = new View.configurationField[0];
                }
            }
            #endregion

            return(results);
        }
Ejemplo n.º 9
0
        private static void ConvertEntitiesOld2Yaml(string rootFolder, DiskModel model, DiskModelYaml model2)
        {
            var folder = Path.Combine(rootFolder, FOLDER_ET);

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            foreach (var obj in model.Entities)
            {
                var newEntity = model2.Entities.AddItem(new EntityYaml
                {
                    AllowCreateAudit       = obj.allowcreateaudit != 0,
                    AllowModifyAudit       = obj.allowmodifyaudit != 0,
                    AllowTimestamp         = obj.allowtimestamp != 0,
                    CodeFacade             = obj.codefacade,
                    GeneratesDoubleDerived = obj.generatesdoublederived != 0,
                    Id            = obj.id.ToGuid(),
                    Immutable     = obj.immutable != 0,
                    TypedTable    = obj.typedentity.ToEnum <TypedTableConstants>(),
                    IsAssociative = obj.isassociative != 0,
                    IsTenant      = obj.isTenant != 0,
                    Name          = obj.name,
                    Schema        = obj.schema,
                    Summary       = obj.summary,
                });

                #region Fields
                foreach (var ff in obj.fieldset.OrderBy(x => x.sortorder))
                {
                    newEntity.Fields.AddItem(new EntityFieldYaml
                    {
                        CodeFacade       = ff.codefacade,
                        DataFormatString = ff.dataformatstring,
                        Datatype         = ff.datatype.ToEnum <DataTypeConstants>(),
                        Default          = ff.@default,
                        DefaultIsFunc    = ff.defaultisfunc != 0,
                        Formula          = ff.formula,
                        Id           = ff.id.ToGuid(),
                        Identity     = ff.identity.ToEnum <IdentityTypeConstants>(),
                        IsCalculated = ff.Iscalculated != 0,
                        IsIndexed    = ff.isindexed != 0,
                        IsPrimaryKey = ff.isprimarykey != 0,
                        IsReadonly   = ff.isreadonly != 0,
                        IsUnique     = ff.isunique != 0,
                        Length       = ff.length,
                        Name         = ff.name,
                        Nullable     = ff.nullable != 0,
                        Obsolete     = ff.obsolete != 0,
                        Scale        = ff.scale,
                        Summary      = ff.summary,
                    });
                }
                #endregion

                #region Indexes
                foreach (var ii in model.Indexes.Where(x => x.id == obj.id))
                {
                    foreach (var ifield in ii.index)
                    {
                        var newIndex = newEntity.Indexes.AddItem(new IndexYaml
                        {
                            Clustered    = ifield.clustered != 0,
                            Id           = ifield.id.ToGuid(),
                            ImportedName = ifield.importedname,
                            IndexType    = (IndexTypeConstants)ifield.indextype,
                            IsUnique     = ifield.isunique != 0,
                            Summary      = ifield.summary,
                        });

                        foreach (var i2 in ifield.indexcolumnset.OrderBy(x => x.sortorder))
                        {
                            newIndex.Fields.AddItem(new IndexFieldYaml
                            {
                                Ascending = i2.ascending != 0,
                                FieldId   = i2.fieldid.ToGuid(),
                                FieldName = newEntity.Fields.FirstOrDefault(x => x.Id == i2.fieldid.ToGuid())?.Name,
                                //Id = i2.id.ToGuid(),
                            });
                        }
                    }
                }
                #endregion

                #region Static data
                foreach (var sd in model.StaticData.Where(x => x.id == obj.id))
                {
                    foreach (var dd in sd.data.OrderBy(x => x.orderkey))
                    {
                        newEntity.StaticData.AddItem(new StaticDataYaml
                        {
                            ColumnId  = dd.columnkey.ToGuid(),
                            Value     = dd.value,
                            SortOrder = dd.orderkey,
                        });
                    }
                }
                #endregion
            }

            #region Relations (After all entities are loaded)

            foreach (var obj in model.Entities)
            {
                var newEntity = model2.Entities.First(x => x.Id == new Guid(obj.id));
                foreach (var rr in model.Relations.Where(x => x.id == obj.id))
                {
                    foreach (var relation in rr.relation)
                    {
                        var entity      = model.Entities.FirstOrDefault(x => x.id == rr.id);
                        var entity2     = model.Entities.FirstOrDefault(x => x.id == relation.childid);
                        var newRelation = newEntity.Relations.AddItem(new RelationYaml
                        {
                            Id = relation.id.ToGuid(),
                            ForeignEntityName = entity2.name,
                            ForeignEntityId   = entity2.id.ToGuid(),
                        });
                        newRelation.IsEnforced   = relation.isenforced != 0;
                        newRelation.DeleteAction = relation.deleteaction.ToEnum <DeleteActionConstants>();
                        newRelation.RoleName     = relation.rolename;
                        newRelation.Summary      = relation.summary;
                        foreach (var fsi in relation.relationfieldset)
                        {
                            var newRelationField = new RelationFieldYaml
                            {
                                //Id = fsi.id.ToGuid(),
                                PrimaryFieldId   = fsi.sourcefieldid.ToGuid(),
                                PrimaryFieldName = entity.fieldset.FirstOrDefault(x => x.id == fsi.sourcefieldid)?.name,
                                ForeignFieldId   = fsi.targetfieldid.ToGuid(),
                                ForeignFieldName = entity2.fieldset.FirstOrDefault(x => x.id == fsi.targetfieldid)?.name,
                            };
                            newRelation.Fields.AddItem(newRelationField);
                        }
                    }
                }
            }
            #endregion
        }
Ejemplo n.º 10
0
        public static DiskModel Load(string rootFolder, string modelName, out bool wasLoaded)
        {
            wasLoaded = false;
            if (modelName.EndsWith(".yaml"))
            {
                return(null);
            }
            var modelFile = Path.Combine(rootFolder, modelName);
            var fi        = new FileInfo(modelFile);
            var showError = (fi.Length > 10); //New file is small so show no error if creating new

            var folderName  = modelName.Replace(".nhydrate", ".model");
            var modelFolder = GetModelFolder(rootFolder, folderName);

            //If the model folder does NOT exist
            if (!Directory.Exists(modelFolder))
            {
                if (showError)
                {
                    throw new Exception("The model folder was not found.");
                }
            }
            else
            {
                wasLoaded = true;
            }

            //Remove old ZIP file. It is no longer used
            try
            {
                var compressedFile = Path.Combine(rootFolder, modelName + ".zip");
                if (File.Exists(compressedFile))
                {
                    File.Delete(compressedFile);
                }
            }
            catch { }

            var results = new DiskModel();

            LoadEntities(modelFolder, results);
            LoadViews(modelFolder, results);

            //Load the global model properties
            var globalFile = Path.Combine(modelFolder, "model.xml");

            if (File.Exists(globalFile))
            {
                results.ModelProperties = GetObject <ModelProperties>(globalFile);
            }

            RemoveNullStrings(results.ModelProperties);

            #region Clean up
            //Ensure all arrays are not null
            results.Entities.ForEach(x => x.fieldset = x.fieldset.OrDefault());
            results.Entities.Where(x => x.fieldset == null).ToList().ForEach(x => x.fieldset = x.fieldset.OrDefault());
            results.Indexes.Where(x => x.index == null).ToList().ForEach(x => x.index        = x.index.OrDefault());
            results.Indexes.SelectMany(x => x.index).Where(x => x.indexcolumnset == null).ToList().ForEach(x => x.indexcolumnset = x.indexcolumnset.OrDefault());
            results.Relations.Where(x => x.relation == null).ToList().ForEach(x => x.relation = x.relation.OrDefault());
            results.Relations.SelectMany(x => x.relation).Where(x => x.relationfieldset == null).ToList().ForEach(x => x.relationfieldset = x.relationfieldset.OrDefault());
            results.StaticData.Where(x => x.data == null).ToList().ForEach(x => x.data    = x.data.OrDefault());
            results.Views.Where(x => x.fieldset == null).ToList().ForEach(x => x.fieldset = x.fieldset.OrDefault());
            #endregion

            return(results);
        }