private void GetProject(ArchAngel.Providers.EntityModel.Model.MappingLayer.MappingSet mappingSet, XmlDocument csprojDocument, string filename)
        {
            var hbmFiles = ProjectLoader.GetHBMFilesFromCSProj(new CSProjFile(csprojDocument, filename), fileController);

            // Load HBMs
            var mappings = hbmFiles.Select(f => MappingFiles.Version_2_2.Utility.Open(f)).ToList();

            // Parse the CSharp files
            var          csharpFiles  = ProjectLoader.GetCSharpFilesFromCSProj(new CSProjFile(csprojDocument, filename), fileController);
            ParseResults parseResults = ParseResults.ParseCSharpFiles(csharpFiles);

            //foreach (ArchAngel.Providers.CodeProvider.DotNet.Class c in parseResults.parsedClasses)
            //{
            //}
            //mappingSet.CodeParseResults = parseResults;

            // Clear the current mapped class.
            //mappingSet.EntitySet.Entities.ForEach(e => e.MappedClass = null);

            // Map the Entity objects to the parsed Class
            var entities = mappingSet.EntitySet.Entities.ToDictionary(e => e.Name);

            foreach (var hm in mappings)
            {
                foreach (var hClass in hm.Classes())
                {
                    var fullClassName  = HibernateMappingHelper.ResolveFullClassName(hClass, hm);
                    var shortClassName = HibernateMappingHelper.ResolveShortClassName(hClass, hm);

                    // try find the entity
                    Entity entity;
                    if (entities.TryGetValue(shortClassName, out entity))
                    {
                        // try find class in parse results
                        var parsedClass = parseResults.FindClass(fullClassName, entity.Properties.Select(p => p.Name).ToList());
                        entity.MappedClass = parsedClass;
                    }
                    else
                    {
                        //Log.InfoFormat("Could not find entity for class named {0} in the NHibernate project on disk.", shortClassName);
                    }
                }
            }
        }
예제 #2
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);
        }
        public void InitialiseEntityModel(ArchAngel.Providers.EntityModel.ProviderInfo providerInfo, PreGenerationData data)
        {
            providerInfo.MappingSet.CodeParseResults = null;
            // Clear the current mapped class.
            providerInfo.MappingSet.EntitySet.Entities.ForEach(e => e.MappedClass = null);

            // Find the csproj file we are going to use
            string filename;
            var    csprojDocument = GetCSProjDocument(data, out filename);

            if (csprojDocument == null)
            {
                return;
            }

            CSProjFile csproj   = new CSProjFile(csprojDocument, filename);
            var        hbmFiles = ProjectLoader.GetHBMFilesFromCSProj(csproj, fileController);

            // Load HBMs
            foreach (string hbmFilePath in hbmFiles)
            {
                if (!File.Exists(hbmFilePath))
                {
                    throw new FileNotFoundException(string.Format("A file is defined is your csproj file [{0}], but it cannot be found: [{1}]", filename, hbmFilePath), hbmFilePath);
                }
            }
            var mappings = hbmFiles.Select(f => MappingFiles.Version_2_2.Utility.Open(f)).ToList();

            // Parse the CSharp files
            var csharpFiles  = ProjectLoader.GetCSharpFilesFromCSProj(csproj, fileController);
            var parseResults = ParseResults.ParseCSharpFiles(csharpFiles);

            providerInfo.MappingSet.CodeParseResults = parseResults;

            // Clear the current mapped class.
            providerInfo.MappingSet.EntitySet.Entities.ForEach(e => e.MappedClass = null);

            // Map the Entity objects to the parsed Class
            var entities = providerInfo.MappingSet.EntitySet.Entities.ToDictionary(e => e.Name);

            foreach (var hm in mappings)
            {
                foreach (var hClass in hm.Classes())
                {
                    var fullClassName  = HibernateMappingHelper.ResolveFullClassName(hClass, hm);
                    var shortClassName = HibernateMappingHelper.ResolveShortClassName(hClass, hm);

                    // try find the entity
                    Entity entity;
                    if (entities.TryGetValue(shortClassName, out entity))
                    {
                        // try find class in parse results
                        var parsedClass = parseResults.FindClass(fullClassName, entity.Properties.Select(p => p.Name).ToList());
                        entity.MappedClass = parsedClass;
                    }
                    else
                    {
                        Log.InfoFormat("Could not find entity for class named {0} in the NHibernate project on disk.", shortClassName);
                    }
                }
            }
            // Now, try to map entities that haven't been found yet
            foreach (var entity in entities.Select(v => v.Value).Where(e => e.MappedClass == null))
            {
                string entityName = entity.Name;
                // try find class in parse results
                var parsedClass = parseResults.FindClass(entityName, entity.Properties.Select(p => p.Name).ToList());
                entity.MappedClass = parsedClass;
            }
        }