/
MappingFileGenerator

MappingFileGenerator

Plugin was written by Heron Brink, however it will now maintained by Geoff Davis to help plugin developers write simple NHibernate mapping files.

General Info

LicenseAs-is, use at your own risk
Typefree
Current version1.0

Plugin Description

It runs as a separate .exe that loads in a plugin and generates a mapping file.

Using the plugin

Details on how to use the tool is available in: /wiki/spaces/SD550/pages/521932381 under Adding persistence using NHibernate.

Source Code

Source code available in SourceCommunity\SourcePlugins\MappingFileGenerator

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using TIME.Core.Metadata;
namespace RiverSystem.Plugins.MappingFileGenerator
{
    public partial class MappingFileGenerator : Form
    {
        private XmlDocument doc;
        public MappingFileGenerator()
        {
            InitializeComponent();
        }
        private void fileChooser_OpenButtonClicked(object sender, TIME.Winforms.Filesystem.FileChooserEventArgs fcea)
        {
            Assembly ass = Assembly.LoadFrom(fcea.FullName);
            Type[] types = ass.GetTypes();
            bool foundSomething = false;
            var fieldsAndProperties = new Dictionary<string, string>();
            foreach (var tpe in types)
            {
                fieldsAndProperties.Clear();
                foreach (MemberInfo mi in
                    tpe.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    if (mi.IsDefined(typeof(ParameterAttribute), true) && (mi.DeclaringType == tpe))
                    {
                        if (mi is FieldInfo)
                            fieldsAndProperties.Add(mi.Name, "field");
                        else if (mi is PropertyInfo)
                            fieldsAndProperties.Add(mi.Name, "property");
                        if (mi.Name.Length > 28)
                            lblColumnLengthWarning.Visible = true;
                    }
                }
                if (!foundSomething)
                {
                    doc = new XmlDocument();
                    string root = @"<?xml version=""1.0"" encoding=""utf-8"" ?><hibernate-mapping> </hibernate-mapping>";
                    doc.LoadXml(root);
                    XmlElement rootElement = doc.DocumentElement;
                    rootElement.SetAttribute("xmlns", "urn:nhibernate-mapping-2.2");
                    rootElement.SetAttribute("assembly", ass.GetName().Name);
                    rootElement.SetAttribute("assembly", ass.GetName().Name);
                    rootElement.SetAttribute("auto-import", "false");
                    foundSomething = true;
                }
                /*
                 * <subclass name="RiverSystem.Catchments.Models.CatchmentModels.AbstractCatchmentModel, RiverSystem" lazy="false" discriminator-value="AbstractCatchmentModel">
      <join table="AbstractCatchmentModel" fetch="select" >
        <key column="SubclassKey"  />
        <many-to-one name="Subcatchment" column="Subcatchment" cascade="save-update" access="property" fetch="select" />
      </join>
    </subclass>
                 */
                if (tpe.BaseType != null)
                {
                    if (!tpe.BaseType.FullName.ToLower().Contains("system.windows.forms")
                        && !tpe.BaseType.FullName.ToLower().Contains("system.windows.forms.usercontrol")
                        && !tpe.BaseType.FullName.ToLower().Contains("system.object")
                        && !tpe.BaseType.FullName.ToLower().Contains("system.drawing.design.uitypeeditor")
                        && !tpe.BaseType.FullName.ToLower().Contains("system.enum")
                        && !tpe.BaseType.FullName.ToLower().Contains("system.valuetype")
                        && !tpe.BaseType.FullName.ToLower().Contains("system.multicastdelegate")
                        && !tpe.BaseType.FullName.ToLower().Contains("riversystem.scenariotask")
                        && (fieldsAndProperties.Count > 0))
                    {
                        XmlElement subClassElem = doc.CreateElement("subclass");
                        subClassElem.SetAttribute("name", tpe.Namespace + "." + tpe.Name);
                        subClassElem.SetAttribute("lazy", "false");
                        subClassElem.SetAttribute("discriminator-value", tpe.Name);
                        subClassElem.SetAttribute("extends", tpe.BaseType.FullName + ", " + tpe.BaseType.Assembly.GetName().Name);
                        XmlElement joinElem = doc.CreateElement("join");
                        string tableName = (tpe.Name.Length < 26) ? tpe.Name : tpe.Name.Substring(0, 25);
                        joinElem.SetAttribute("table", tableName);
                        joinElem.SetAttribute("fetch", "select");
                        subClassElem.AppendChild(joinElem);
                        XmlElement element = doc.CreateElement("key");
                        element.SetAttribute("column", "SubclassKey");
                        joinElem.AppendChild(element);
                        foreach (KeyValuePair<string, string> kvp in fieldsAndProperties)
                        {
                            element = doc.CreateElement("property");
                            element.SetAttribute("name", kvp.Key);
                            element.SetAttribute("column", kvp.Key);
                            element.SetAttribute("access", kvp.Value);
                            joinElem.AppendChild(element);
                        }
                        //Add the node to the document.
                        doc.DocumentElement.AppendChild(subClassElem);
                    }
                }
            }
            if (foundSomething)
            {
                TextWriter sw = new StringWriter();
                XmlTextWriter writer = new XmlTextWriter(sw);
                writer.Formatting = Formatting.Indented;
                doc.WriteTo(writer);
                writer.Flush();
                rtb.Text = sw.ToString();
            }
            else
            {
                rtb.Text = "No classes with properties/fields marked as 'Parameter' found";
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (saveFileDlg.ShowDialog() == DialogResult.OK)
            {
                doc.Save(saveFileDlg.FileName);
            }
        }
        private string TruncateUnique(string theString, List<String> theList, int maxLen)
        {
            if (theString.Length <= maxLen)
            {
                theList.Add(theString);
                return theString;
            }
            theString = theString.Substring(0, maxLen);
            int suffix = 0;
            string tmp = theString;
            while (theList.Contains(tmp))
            {
                suffix++;
                int numToReplace = 1 + (int)Math.Log10(suffix);
                tmp = theString.Substring(0, maxLen - numToReplace) + suffix.ToString();
            }
            theList.Add(tmp);
            return tmp;
        }
    }
}

Related content

Gauge node
Read with this
MappingFileGenerator
MappingFileGenerator
More like this
Plugin not loading issues
Plugin not loading issues
More like this
Plugin not loading issues
Plugin not loading issues
More like this
Plugin not loading issues
Plugin not loading issues
More like this
Associate Scenarios
Associate Scenarios
More like this