private EntityMeta GenerateEntityMeta()
        {
            if (Type is EnumType)
            {
                var entityType = Type as EnumType;
                var entityMeta = new EnumMeta();

                entityMeta.ProjectName = Model.Project.Name;
                entityMeta.ModelName = Model.Name;

                entityMeta.EntityType = Enum.GetName(typeof(EntityType), entityType.EntityType);
                entityMeta.Access = Enum.GetName(typeof(AccessModifier), entityType.Access);
                entityMeta.Name = entityType.Name;
                entityMeta.ValuesCount = entityType.ValueCount;

                foreach (var value in entityType.Values)
                {
                    entityMeta.Values.Add(Enum.GetName(typeof(EnumValue), value));
                }

                return entityMeta;
            }
            else
            {
                var entityType = Type as CompositeType;
                var entityMeta = new CompositeMeta();

                entityMeta.ProjectName = Model.Project.Name;
                entityMeta.ModelName = Model.Name;

                entityMeta.EntityType = Enum.GetName(typeof(EntityType), entityType.EntityType);
                entityMeta.Access = Enum.GetName(typeof(AccessModifier), entityType.Access);
                entityMeta.Name = entityType.Name;
                entityMeta.NHMTableName = entityType.NHMTableName;
                entityMeta.IdentityGenerator = EnumExtensions.GetDescription((IdentityGeneratorType)Enum.Parse(typeof(IdentityGeneratorType), entityType.IdGenerator));

                if (entityType.SupportsFields)
                {
                    entityMeta.FieldsCount = entityType.FieldCount;

                    foreach (var field in entityType.Fields)
                    {
                        var fieldMeta = new FieldMeta();
                        fieldMeta.MemberType = Enum.GetName(typeof(MemberType), field.MemberType);
                        fieldMeta.Access = Enum.GetName(typeof(AccessModifier), field.Access);
                        fieldMeta.Name = field.Name;
                        fieldMeta.Type = field.Type;

                        entityMeta.Fields.Add(fieldMeta);
                    }
                }

                if (entityType.SupportsOperations)
                {
                    entityMeta.OperationsCount = entityType.OperationCount;

                    foreach (var operation in entityType.Operations)
                    {
                        var operationMeta = new OperationMeta();
                        operationMeta.MemberType = Enum.GetName(typeof(MemberType), operation.MemberType);
                        operationMeta.Access = Enum.GetName(typeof(AccessModifier), operation.Access);
                        operationMeta.Name = operation.Name;
                        operationMeta.Type = operation.Type;
                        operationMeta.NHMColumnName = operation.NHMColumnName;
                        operationMeta.Identity = operation.IsIdentity;
                        operationMeta.ManyToOne = operation.ManyToOne;
                        operationMeta.Unique = operation.IsUnique;
                        operationMeta.NotNull = operation.IsNotNull;

                        entityMeta.Operations.Add(operationMeta);
                    }
                }

                return entityMeta;
            }
        }
예제 #2
0
        protected override void WriteFileContent()
        {
            useLazyLoading          = Settings.Default.DefaultLazyFetching;
            useLowercaseUnderscored = Settings.Default.UseUnderscoreAndLowercaseInDB;

            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent = true;
            settings.NewLineOnAttributes = true;
            settings.Encoding            = System.Text.Encoding.Unicode;

            ClassType _class = (ClassType)Type;

            if (_class.IdGenerator == null)
            {
                idGeneratorType = EnumExtensions.GetDescription(Settings.Default.DefaultIdentityGenerator);
            }
            else
            {
                idGeneratorType = EnumExtensions.GetDescription((IdentityGeneratorType)Enum.Parse(typeof(IdentityGeneratorType), _class.IdGenerator));
            }

            using (XmlWriter xml = XmlWriter.Create(CodeBuilder, settings))
            {
                xml.WriteStartDocument();
                xml.WriteComment(
                    string.Format(
                        " This code was generated by {0} ",
                        GetVersionString()
                        ));
                xml.WriteStartElement("hibernate-mapping", "urn:nhibernate-mapping-2.2");
                xml.WriteAttributeString("assembly", ProjectName);
                xml.WriteAttributeString("namespace", RootNamespace);
                xml.WriteStartElement("class");
                xml.WriteAttributeString("name", _class.Name);
                xml.WriteAttributeString("table",
                                         string.Format("`{0}`",
                                                       PrefixedText(
                                                           useLowercaseUnderscored
                        ? LowercaseAndUnderscoredWord(_class.Name)
                        : string.IsNullOrEmpty(_class.NHMTableName)
                        ? _class.Name
                        : _class.NHMTableName
                                                           )));
                xml.WriteAttributeString("lazy", useLazyLoading.ToString().ToLower());

                List <Operation> ids = _class.Operations.Where(o => o is Property && o.IsIdentity).ToList <Operation>();

                if (ids.Count > 1)
                {
                    xml.WriteStartElement("composite-id");
                    foreach (var id in ids)
                    {
                        if (!string.IsNullOrEmpty(id.ManyToOne))
                        {
                            xml.WriteStartElement("key-many-to-one");
                            xml.WriteAttributeString("name", id.Name);
                            xml.WriteAttributeString("column",
                                                     string.Format("`{0}`",
                                                                   useLowercaseUnderscored
                                    ? LowercaseAndUnderscoredWord(id.Name)
                                    : string.IsNullOrEmpty(id.NHMColumnName)
                                    ? id.Name
                                    : id.NHMColumnName
                                                                   ));
                            xml.WriteAttributeString("class", id.Type);
                            xml.WriteEndElement();
                        }
                        else
                        {
                            xml.WriteStartElement("key-property");
                            xml.WriteAttributeString("name", id.Name);
                            xml.WriteAttributeString("column",
                                                     string.Format("`{0}`",
                                                                   useLowercaseUnderscored
                                    ? LowercaseAndUnderscoredWord(id.Name)
                                    : string.IsNullOrEmpty(id.NHMColumnName)
                                    ? id.Name
                                    : id.NHMColumnName
                                                                   ));
                            xml.WriteAttributeString("type", id.Type);
                            xml.WriteEndElement();
                        }
                    }
                    xml.WriteEndElement();
                }
                else if (ids.Count == 1)
                {
                    xml.WriteStartElement("id");
                    xml.WriteAttributeString("name", ids[0].Name);
                    xml.WriteAttributeString("column",
                                             string.Format("`{0}`",
                                                           useLowercaseUnderscored
                            ? LowercaseAndUnderscoredWord(ids[0].Name)
                            : string.IsNullOrEmpty(ids[0].NHMColumnName)
                            ? ids[0].Name
                            : ids[0].NHMColumnName
                                                           ));
                    xml.WriteAttributeString("type", ids[0].Type);
                    xml.WriteStartElement("generator");

                    if (_class.IdGenerator != "Custom")
                    {
                        xml.WriteAttributeString("class", idGeneratorType);
                    }

                    if (_class.IdGenerator == "HiLo")
                    {
                        HiLoIdentityGeneratorParameters hiLo = GeneratorParametersDeSerializer.Deserialize <HiLoIdentityGeneratorParameters>(_class.GeneratorParameters);

                        xml.WriteStartElement("param");
                        xml.WriteAttributeString("name", "table");
                        xml.WriteValue(hiLo.Table);
                        xml.WriteEndElement();

                        xml.WriteStartElement("param");
                        xml.WriteAttributeString("name", "column");
                        xml.WriteValue(hiLo.Column);
                        xml.WriteEndElement();

                        xml.WriteStartElement("param");
                        xml.WriteAttributeString("name", "max_lo");
                        xml.WriteValue(hiLo.MaxLo);
                        xml.WriteEndElement();

                        xml.WriteStartElement("param");
                        xml.WriteAttributeString("name", "where");
                        xml.WriteValue(hiLo.Where);
                        xml.WriteEndElement();
                    }
                    else if (_class.IdGenerator == "SeqHiLo")
                    {
                        SeqHiLoIdentityGeneratorParameters seqHiLo = GeneratorParametersDeSerializer.Deserialize <SeqHiLoIdentityGeneratorParameters>(_class.GeneratorParameters);

                        xml.WriteStartElement("param");
                        xml.WriteAttributeString("name", "sequence");
                        xml.WriteValue(seqHiLo.Sequence);
                        xml.WriteEndElement();

                        xml.WriteStartElement("param");
                        xml.WriteAttributeString("name", "max_lo");
                        xml.WriteValue(seqHiLo.MaxLo);
                        xml.WriteEndElement();
                    }
                    else if (_class.IdGenerator == "Sequence")
                    {
                        SequenceIdentityGeneratorParameters sequence = GeneratorParametersDeSerializer.Deserialize <SequenceIdentityGeneratorParameters>(_class.GeneratorParameters);

                        xml.WriteStartElement("param");
                        xml.WriteAttributeString("name", "sequence");
                        xml.WriteValue(sequence.Sequence);
                        xml.WriteEndElement();
                    }
                    else if (_class.IdGenerator == "UuidHex")
                    {
                        UuidHexIdentityGeneratorParameters uuidHex = GeneratorParametersDeSerializer.Deserialize <UuidHexIdentityGeneratorParameters>(_class.GeneratorParameters);

                        xml.WriteStartElement("param");
                        xml.WriteAttributeString("name", "format_value");
                        xml.WriteValue(uuidHex.Format);
                        xml.WriteEndElement();

                        xml.WriteStartElement("param");
                        xml.WriteAttributeString("name", "separator_value");
                        xml.WriteValue(uuidHex.Separator);
                        xml.WriteEndElement();
                    }
                    else if (_class.IdGenerator == "Foreign")
                    {
                        ForeignIdentityGeneratorParameters foreign = GeneratorParametersDeSerializer.Deserialize <ForeignIdentityGeneratorParameters>(_class.GeneratorParameters);

                        xml.WriteStartElement("param");
                        xml.WriteAttributeString("name", "property");
                        xml.WriteValue(foreign.Property);
                        xml.WriteEndElement();
                    }
                    else if (_class.IdGenerator == "Custom")
                    {
                        CustomIdentityGeneratorParameters custom = GeneratorParametersDeSerializer.Deserialize <CustomIdentityGeneratorParameters>(_class.GeneratorParameters);

                        xml.WriteAttributeString("class", custom.Class);

                        foreach (var param in custom.Parameters)
                        {
                            xml.WriteStartElement("param");
                            xml.WriteAttributeString("name", param.Name);
                            xml.WriteValue(param.Value);
                            xml.WriteEndElement();
                        }
                    }

                    xml.WriteEndElement();
                    xml.WriteEndElement();
                }

                foreach (var property in _class.Operations.Where(o => o is Property && !o.IsIdentity).ToList <Operation>())
                {
                    if (!string.IsNullOrEmpty(property.ManyToOne))
                    {
                        xml.WriteStartElement("many-to-one");
                        xml.WriteAttributeString("name", property.Name);
                        xml.WriteAttributeString("class", property.Type);
                        xml.WriteAttributeString("column",
                                                 string.Format("`{0}`",
                                                               useLowercaseUnderscored
                                ? LowercaseAndUnderscoredWord(property.Name)
                                : string.IsNullOrEmpty(property.NHMColumnName)
                                ? property.Name
                                : property.NHMColumnName
                                                               ));

                        if (property.IsUnique)
                        {
                            xml.WriteAttributeString("unique", "true");
                        }

                        xml.WriteAttributeString("not-null", property.IsNotNull.ToString().ToLower());
                        xml.WriteEndElement();
                    }
                    else
                    {
                        xml.WriteStartElement("property");
                        xml.WriteAttributeString("name", property.Name);
                        xml.WriteAttributeString("column",
                                                 string.Format("`{0}`",
                                                               useLowercaseUnderscored
                                ? LowercaseAndUnderscoredWord(property.Name)
                                : string.IsNullOrEmpty(property.NHMColumnName)
                                ? property.Name
                                : property.NHMColumnName
                                                               ));
                        xml.WriteAttributeString("type", property.Type);

                        if (property.IsUnique)
                        {
                            xml.WriteAttributeString("unique", "true");
                        }

                        xml.WriteAttributeString("not-null", property.IsNotNull.ToString().ToLower());
                        xml.WriteEndElement();
                    }
                }

                xml.WriteEndElement();
                xml.WriteEndElement();
                xml.WriteEndDocument();
            }
        }
예제 #3
0
        protected override bool GenerateProjectFiles(string location)
        {
            try
            {
                string templateDir  = Path.Combine(Application.StartupPath, "Templates");
                string templateFile = Path.Combine(templateDir, "csproj.template");
                string projectFile  = Path.Combine(location, RelativeProjectFileName);

                using (StreamReader reader = new StreamReader(templateFile))
                    using (StreamWriter writer = new StreamWriter(
                               projectFile, false, reader.CurrentEncoding))
                    {
                        while (!reader.EndOfStream)
                        {
                            string line = reader.ReadLine();

                            line = line.Replace("${RootNamespace}", RootNamespace);
                            line = line.Replace("${AssemblyName}", AssemblyName);

                            if (line.Contains("${DotNetVersion}"))
                            {
                                line = Regex.Replace(line, @"\${DotNetVersion}", EnumExtensions.GetDescription(dotNetVersion));

                                if (line.Length == 0)
                                {
                                    continue;
                                }
                            }

                            if (line.Contains("${SourceFile}"))
                            {
                                foreach (string fileName in FileNames)
                                {
                                    if ((new Regex(@"\.cs$").IsMatch(fileName)))
                                    {
                                        string newLine = line.Replace("${SourceFile}", fileName);
                                        writer.WriteLine(newLine);
                                    }
                                }
                            }
                            else if (line.Contains("${HbmXmlFile}"))
                            {
                                foreach (string fileName in FileNames)
                                {
                                    if ((new Regex(@"\.hbm\.xml$").IsMatch(fileName)))
                                    {
                                        string newLine = line.Replace("${HbmXmlFile}", fileName);
                                        writer.WriteLine(newLine);
                                    }
                                }
                            }
                            else if (line.Contains("${OtherFile}"))
                            {
                                foreach (string fileName in FileNames)
                                {
                                    if (!(new Regex(@"\.hbm\.xml$|\.cs$").IsMatch(fileName)))
                                    {
                                        string newLine = line.Replace("${OtherFile}", fileName);
                                        writer.WriteLine(newLine);
                                    }
                                }
                            }
                            else
                            {
                                writer.WriteLine(line);
                            }
                        }
                    }

                return(true);
            }
            catch
            {
                return(false);
            }
        }