/// <summary> <p>Creates skeletal source code (without correct data structure but no business
        /// logic) for all segments found in the normative database.  </p>
        /// </summary>
        public static void MakeAll(string baseDirectory, string version)
        {
            // make base directory
            if (!(baseDirectory.EndsWith("\\") || baseDirectory.EndsWith("/")))
            {
                baseDirectory += Path.DirectorySeparatorChar;
            }

            var targetDir =
                SourceGenerator.MakeDirectory(
                    Path.Combine(baseDirectory, PackageManager.GetVersionPackagePath(version), "Segment"));

            // get list of data types
            var conn = NormativeDatabase.Instance.Connection;
            var sql  =
                "SELECT seg_code, [section] from HL7Segments, HL7Versions where HL7Segments.version_id = HL7Versions.version_id AND hl7_version = '" +
                version + "'";
            DbCommand temp_OleDbCommand = conn.CreateCommand();

            temp_OleDbCommand.Connection  = conn;
            temp_OleDbCommand.CommandText = sql;
            var rs = temp_OleDbCommand.ExecuteReader();

            var segments = new List <string>();

            while (rs.Read())
            {
                var segName = Convert.ToString(rs[1 - 1]);
                if (char.IsLetter(segName[0]))
                {
                    segments.Add(AltSegName(segName));
                }
            }

            temp_OleDbCommand.Dispose();
            NormativeDatabase.Instance.ReturnConnection(conn);

            if (segments.Count == 0)
            {
                Log.Warn($"No version {version} segments found in database {conn.Database}");
            }

            for (var i = 0; i < segments.Count; i++)
            {
                try
                {
                    var segment = segments[i];
                    var source  = MakeSegment(segment, version);

                    var targetFile = Path.Combine(targetDir.ToString(), $"{GetSpecialFilename(segment)}.cs");

                    FileAbstraction.WriteAllBytes(targetFile, Encoding.UTF8.GetBytes($"{source}}}"));
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine($"Error creating source code for all segments: {e.Message}");
                    SupportClass.WriteStackTrace(e, Console.Error);
                }
            }
        }
Esempio n. 2
0
        private static void BuildFile(string dataType, string targetDir, string version)
        {
            var fileName = Path.Combine(targetDir, $"{dataType}.cs");

            FileAbstraction.WriteAllBytes(
                fileName,
                Encoding.UTF8.GetBytes(GetClassSource(dataType, version)));
        }
Esempio n. 3
0
        /// <summary> Creates source code for a specific message structure and
        /// writes it under the specified directory.
        /// throws IllegalArgumentException if there is no message structure
        /// for this message in the normative database.
        /// </summary>
        public static void Make(string message, string baseDirectory, string chapter, string version)
        {
            try
            {
                var segments = GetSegments(message, version);

                // System.out.println("Making: " + message + " with " + segments.length + " segments (not writing message code - just groups)");
                var group = GroupGenerator.GetGroupDef(segments, null, baseDirectory, version, message);
                var contents = group.Structures;

                // make base directory
                if (!(baseDirectory.EndsWith("\\") || baseDirectory.EndsWith("/")))
                {
                    baseDirectory += Path.DirectorySeparatorChar;
                }

                var targetDir =
                    SourceGenerator.MakeDirectory(
                        Path.Combine(baseDirectory, PackageManager.GetVersionPackagePath(version), "Message"));

                var targetFile = Path.Combine(targetDir.FullName, $"{message}.cs");

                var stringBuilder = new StringBuilder();

                stringBuilder.Append(MakePreamble(contents, message, chapter, version));
                stringBuilder.Append(MakeConstructor(contents, message, version));
                for (var i = 0; i < contents.Length; i++)
                {
                    var groupAccessor = GroupGenerator.MakeAccessor(@group, i);
                    stringBuilder.Append(groupAccessor);
                }

                // add implementation of model.control interface, if any
                stringBuilder.Append("}\r\n"); // End class
                stringBuilder.Append("}\r\n"); // End namespace

                FileAbstraction.WriteAllBytes(targetFile, Encoding.UTF8.GetBytes(stringBuilder.ToString()));
            }
            catch (Exception e)
            {
                Log.Error("Error while creating source code", e);

                Log.Warn("Warning: could not write source code for message structure " + message + " - " + e.GetType().FullName +
                            ": " + e.Message);
            }
        }
Esempio n. 4
0
        public static void MakeAll(string baseDirectory, string version)
        {
            // make base directory
            if (!(baseDirectory.EndsWith("\\") || baseDirectory.EndsWith("/")))
            {
                baseDirectory += Path.DirectorySeparatorChar;
            }

            var targetDir =
                SourceGenerator.MakeDirectory(
                    Path.Combine(baseDirectory, PackageManager.GetVersionPackagePath(version), "EventMapping"));

            // get list of data types
            var conn = NormativeDatabase.Instance.Connection;
            var sql  =
                "SELECT * from HL7EventMessageTypes inner join HL7Versions on HL7EventMessageTypes.version_id = HL7Versions.version_id where HL7Versions.hl7_version = '" +
                version + "'";
            DbCommand temp_OleDbCommand = conn.CreateCommand();

            temp_OleDbCommand.Connection  = conn;
            temp_OleDbCommand.CommandText = sql;
            var rs = temp_OleDbCommand.ExecuteReader();

            var targetFile = Path.Combine(targetDir.FullName, "EventMap.properties");

            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine($"#event -> structure map for {version}");
            if (version == "2.1" || version == "2.2")
            {
                stringBuilder.AppendLine("#note: no mappings are defined for 2.1 and 2.2");
            }
            else
            {
                while (rs.Read())
                {
                    var messageType = $"{rs["message_typ_snd"]}_{rs["event_code"]}";
                    var structure   = (string)rs["message_structure_snd"];

                    stringBuilder.AppendLine($"{messageType} {structure}");
                }
            }

            FileAbstraction.WriteAllBytes(targetFile, Encoding.UTF8.GetBytes(stringBuilder.ToString()));
        }
        /// <summary> Creates source code for a Group and returns a GroupDef object that
        /// describes the Group's name, optionality, repeatability.  The source
        /// code is written under the given directory.
        /// The structures list may contain [] and {} pairs representing
        /// nested groups and their optionality and repeatability.  In these cases
        /// this method is called recursively.
        /// If the given structures list begins and ends with repetition and/or
        /// optionality markers the repetition and optionality of the returned
        /// GroupDef are set accordingly.
        /// <param name="structures">a list of the structures that comprise this group - must
        /// be at least 2 long
        /// </param>
        /// <param name="groupName">The group name</param>
        /// <param name="version">The version of message</param>
        /// <param name="baseDirectory">the directory to which files should be written
        /// </param>
        /// <param name="message">the message to which this group belongs
        /// </param>
        /// <throws>  HL7Exception if the repetition and optionality markers are not  </throws>
        /// </summary>
        public static GroupDef WriteGroup(
            IStructureDef[] structures,
            string groupName,
            string baseDirectory,
            string version,
            string message)
        {
            // make base directory
            if (!(baseDirectory.EndsWith("\\") || baseDirectory.EndsWith("/")))
            {
                baseDirectory += Path.DirectorySeparatorChar;
            }

            var targetDir =
                SourceGenerator.MakeDirectory(
                    Path.Combine(baseDirectory, PackageManager.GetVersionPackagePath(version), "Group"));

            // some group names are troublesome and have "/" which will cause problems when writing files
            groupName = groupName.Replace("/", "_");
            var group = GetGroupDef(structures, groupName, baseDirectory, version, message);

            var targetFile = Path.Combine(targetDir.FullName, $"{@group.Name}.cs");

            var stringBuilder = new StringBuilder();

            stringBuilder.Append(MakePreamble(group, version));
            stringBuilder.Append(MakeConstructor(group, version));

            var shallow = group.Structures;

            for (var i = 0; i < shallow.Length; i++)
            {
                stringBuilder.Append(MakeAccessor(group, i));
            }

            stringBuilder.Append("}\r\n"); // Closing class
            stringBuilder.Append("}\r\n"); // Closing namespace

            FileAbstraction.WriteAllBytes(targetFile, Encoding.UTF8.GetBytes(stringBuilder.ToString()));
            return(group);
        }
Esempio n. 6
0
        /// <summary>
        /// Creates source code for a single data type in the HL7 normative database.
        /// </summary>
        /// <param name="targetDirectory">the directory into which the file will be written.</param>
        /// <param name="dataType">the name (e.g. ST, ID, etc.) of the data type to be created.</param>
        /// <param name="version">the HL7 version of the intended data type.</param>
        public static void Make(FileInfo targetDirectory, string dataType, string version)
        {
            Console.Out.WriteLine(" Writing " + Path.Combine(targetDirectory.FullName, dataType));

            // make sure that targetDirectory is a directory ...
            if (!Directory.Exists(targetDirectory.FullName))
            {
                throw new IOException("Can't create file in " + targetDirectory + " - it is not a directory.");
            }

            // get any components for this data type
            var conn = NormativeDatabase.Instance.Connection;
            var stmt = TransactionManager.Manager.CreateStatement(conn);
            var sql  = new StringBuilder();

            // this query is adapted from the XML SIG informative document
            sql.Append(
                "SELECT HL7DataStructures.data_structure, HL7DataStructureComponents.seq_no, HL7DataStructures.description, HL7DataStructureComponents.table_id,  ");
            sql.Append(
                "HL7Components.description, HL7Components.table_id, HL7Components.data_type_code, HL7Components.data_structure ");
            sql.Append(
                "FROM HL7Versions LEFT JOIN (HL7DataStructures LEFT JOIN (HL7DataStructureComponents LEFT JOIN HL7Components ");
            sql.Append("ON HL7DataStructureComponents.comp_no = HL7Components.comp_no AND ");
            sql.Append("HL7DataStructureComponents.version_id = HL7Components.version_id) ");
            sql.Append("ON HL7DataStructures.version_id = HL7DataStructureComponents.version_id ");
            sql.Append("AND HL7DataStructures.data_structure = HL7DataStructureComponents.data_structure) ");
            sql.Append("ON HL7DataStructures.version_id = HL7Versions.version_id ");
            sql.Append("WHERE HL7DataStructures.data_structure = '");
            sql.Append(dataType);
            sql.Append("' AND HL7Versions.hl7_version = '");
            sql.Append(version);
            sql.Append("' ORDER BY HL7DataStructureComponents.seq_no");
            DbCommand temp_OleDbCommand;

            temp_OleDbCommand             = stmt;
            temp_OleDbCommand.CommandText = sql.ToString();
            var rs = temp_OleDbCommand.ExecuteReader();

            var    dataTypes    = new ArrayList(20);
            var    descriptions = new ArrayList(20);
            var    tables       = new ArrayList(20);
            string description  = null;

            while (rs.Read())
            {
                if (description == null)
                {
                    description = Convert.ToString(rs[3 - 1]);
                }

                var de = Convert.ToString(rs[5 - 1]);
                var dt = Convert.ToString(rs[8 - 1]);
                var ta = -1;
                if (!rs.IsDBNull(4 - 1))
                {
                    ta = rs.GetInt32(4 - 1);
                }

                // trim all CE_x to CE
                if (dt != null)
                {
                    if (dt.StartsWith("CE", StringComparison.Ordinal))
                    {
                        dt = "CE";
                    }
                }

                dataTypes.Add(dt);
                descriptions.Add(de);
                tables.Add(ta);
            }

            if (dataType.ToUpper().Equals("TS"))
            {
                dataTypes[0] = "TSComponentOne";
            }

            rs.Close();
            stmt.Dispose();
            NormativeDatabase.Instance.ReturnConnection(conn);

            // if there is only one component make a Primitive, otherwise make a Composite
            string source = null;

            if (dataTypes.Count == 1)
            {
                if (dataType.Equals("FT") || dataType.Equals("ST") || dataType.Equals("TX") || dataType.Equals("NM") ||
                    dataType.Equals("SI") || dataType.Equals("TN") || dataType.Equals("GTS"))
                {
                    source = MakePrimitive(dataType, description, version);
                }
                else
                {
                    source = null; // note: IS, ID, DT, DTM, and TM are coded manually
                }
            }
            else if (dataTypes.Count > 1)
            {
                var numComponents = dataTypes.Count;

                // copy data into arrays ...
                var type  = new string[numComponents];
                var desc  = new string[numComponents];
                var table = new int[numComponents];
                for (var i = 0; i < numComponents; i++)
                {
                    type[i]  = (string)dataTypes[i];
                    desc[i]  = (string)descriptions[i];
                    table[i] = (int)tables[i];
                }

                source = MakeComposite(dataType, description, type, desc, table, version);
            }
            else
            {
                // no components?
                // throw new DataTypeException("The data type " + dataType + " could not be found");
                Console.WriteLine("No components for " + dataType);
            }

            // System.out.println(source);

            // write to file ...
            if (source != null)
            {
                var targetFile = Path.Combine(targetDirectory.ToString(), $"{dataType}.cs");

                FileAbstraction.WriteAllBytes(targetFile, Encoding.UTF8.GetBytes($"{source}}}"));
            }
            else
            {
                Console.WriteLine("No Source for " + dataType);
            }
        }