private void buttonFetch_Click(object sender, EventArgs e)
        {
            string      csprojFilename = textBoxCsProjFile.Text;
            XmlDocument doc            = new XmlDocument();

            doc.LoadXml(fileController.ReadAllText(csprojFilename));
            ArchAngel.Providers.EntityModel.Model.MappingLayer.MappingSet mappingSet = null;

            GetProject(mappingSet, doc, csprojFilename);
        }
        private XmlDocument GetCSProjDocument(PreGenerationData data, out string filename)
        {
            var doc = new XmlDocument();

            var projectName = data.UserOptions.GetValueOrDefault("ProjectName") as string;

            if (string.IsNullOrEmpty(projectName) == false)
            {
                // Search for the project as named by the ProjectName User Option.
                filename = Path.Combine(data.OutputFolder, projectName + ".csproj");

                if (fileController.FileExists(filename))
                {
                    doc.LoadXml(fileController.ReadAllText(filename));
                    return(doc);
                }
            }

            // If we get to this point, we couldn't find the project in the default location,
            // so we search for the first project that has any *.hbm.xml files in it.
            var csProjFilenames = fileController.FindAllFilesLike(data.OutputFolder, "*.csproj", SearchOption.AllDirectories);

            foreach (var csprojFilename in csProjFilenames)
            {
                doc = new XmlDocument();
                doc.LoadXml(fileController.ReadAllText(csprojFilename));
                var hbmFiles = ProjectLoader.GetHBMFilesFromCSProj(new CSProjFile(doc, csprojFilename), fileController);

                if (hbmFiles.Any())
                {
                    filename = csprojFilename;
                    return(doc);
                }
            }

            filename = "";
            return(null);
        }
        public IEnumerable <IncludedFile> LoadStaticFilenames(string folder)
        {
            CheckFolder(folder, "StaticFiles");

            string filename = folder.PathSlash(ProjectSerialiserV1.StaticFilesDetailsFileName);

            if (controller.FileExists(filename) == false)
            {
                throw new FileNotFoundException(string.Format("Could not find Outputs file at path {0}", filename));
            }
            if (controller.CanReadFile(filename) == false)
            {
                throw new IOException(string.Format("Cannot read from file {0}: Access Denied.", filename));
            }

            string xml = controller.ReadAllText(filename);

            var files = ReadStaticFilesDetails(xml.GetXmlDocRoot());

            SetFullFileNamesForIncludedFiles(files, folder);
            return(files);
        }
예제 #4
0
        public LoadResult LoadEntityModelFromCSProj(string csprojFilePath, NHConfigFile nhConfigFile)
        {
            _progress.SetCurrentState("Loading Entities From Visual Studio Project", ProgressState.Normal);

            EntityLoader entityLoader = new EntityLoader(new FileController());

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(fileController.ReadAllText(csprojFilePath));
            CSProjFile csProjFile = new CSProjFile(doc, csprojFilePath);
            var        hbmFiles   = GetHBMFilesFromCSProj(csProjFile);

            if (IsFluentProject(csProjFile))
            {
                ArchAngel.Interfaces.SharedData.CurrentProject.SetUserOption("UseFluentNHibernate", true);
                string tempFluentPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Visual NHibernate" + Path.DirectorySeparatorChar + "Temp" + Path.DirectorySeparatorChar + "FluentTemp");
                var    fluentHbmFiles = GetHBMFilesForFluentFromCSProj(csProjFile, tempFluentPath);
                // Combine the actual HBM files with the ones derived from FluentNH
                hbmFiles = hbmFiles.Union(fluentHbmFiles);
            }
            else
            {
                ArchAngel.Interfaces.SharedData.CurrentProject.SetUserOption("UseFluentNHibernate", false);
            }

            var csFiles  = GetCSharpFilesFromCSProj(doc, csprojFilePath);
            var nhvFiles = GetNHVFilesFromCSProj(doc, csprojFilePath);

            //NHConfigFile nhConfigFile = GetNhConfigFile(csProjFile, fileController);

            var databaseConnector = nhConfigFile == null ? null : nhConfigFile.DatabaseConnector;

            //////// GFH
            // We need to fetch ALL tables, because HBM mappings don't include association tables, or at least it's difficult to find them.
            List <SchemaData> tablesToFetch = null;           // entityLoader.GetTablesFromHbmFiles(hbmFiles);

            IDatabaseLoader loader   = null;
            IDatabase       database = null;

            if (databaseConnector != null)
            {
                database = GetDatabase(databaseConnector, out loader, tablesToFetch);
            }

            _progress.SetCurrentState("Parsing your existing Model Project", ProgressState.Normal);
            var parseResults = ParseResults.ParseCSharpFiles(csFiles);

            _progress.SetCurrentState("Loading Mapping Information From NHibernate Mapping Files", ProgressState.Normal);
            var mappingSet = entityLoader.GetEntities(hbmFiles, parseResults, database);

            entityLoader.ApplyConstraints(mappingSet, nhvFiles, parseResults);

            #region Create References

            // Get a set of all Guids for tables that we will want to create references from
            HashSet <Guid> existingTables = new HashSet <Guid>(database.Tables.Select(t => t.InternalIdentifier));

            foreach (var mappedTable in mappingSet.Mappings.Select(m => m.FromTable))
            {
                existingTables.Add(mappedTable.InternalIdentifier);
            }

            HashSet <Guid> processedRelationships = new HashSet <Guid>();
            foreach (var table in database.Tables)
            {
                foreach (var directedRel in table.DirectedRelationships)
                {
                    var relationship = directedRel.Relationship;

                    if (processedRelationships.Contains(relationship.InternalIdentifier))
                    {
                        continue;                         // Skip relationships that have already been handled.
                    }
                    if (relationship.MappedReferences().Any())
                    {
                        continue;                         // Skip relationships that have been mapped by the user.
                    }
                    if (existingTables.Contains(directedRel.ToTable.InternalIdentifier) == false)
                    {
                        continue;                         // Skip relationships that have tables that have no mapped Entity
                    }
                    if (relationship.PrimaryTable.MappedEntities().FirstOrDefault() == null ||
                        relationship.ForeignTable.MappedEntities().FirstOrDefault() == null)
                    {
                        continue;
                    }
                    ArchAngel.Providers.EntityModel.Controller.MappingLayer.MappingProcessor.ProcessRelationshipInternal(mappingSet, relationship, new ArchAngel.Providers.EntityModel.Controller.MappingLayer.OneToOneEntityProcessor());
                    processedRelationships.Add(relationship.InternalIdentifier);
                }
            }
            #endregion

            foreach (var entity in mappingSet.EntitySet.Entities)
            {
                foreach (var reference in entity.References)
                {
                    if (!mappingSet.EntitySet.References.Contains(reference))
                    {
                        mappingSet.EntitySet.AddReference(reference);
                    }
                }
            }

            LoadResult result = new LoadResult();
            result.MappingSet     = mappingSet;
            result.DatabaseLoader = loader;
            result.NhConfigFile   = nhConfigFile;
            result.CsProjFile     = csProjFile;
            return(result);
        }