Advertisement
Guest User

MITable

a guest
Apr 29th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.10 KB | None | 0 0
  1.     class MITable
  2.     {
  3.         private string tableName;
  4.         private string parentUniqueId;
  5.         private string parentUniqueFieldName;
  6.         private string uniqueIdFieldName;
  7.         private string uniqueId;
  8.         private string patId;
  9.         private string addEditDelete;
  10.         private string connectionString;
  11.         private List<MITableRow> tableRows;
  12.         private List<Property> properties;
  13.         private XElement xml;
  14.  
  15.         public MITable(string tableName, string uniqueIdFieldName, string parentUniqueId, string parentUniqueFieldName, string addEditDelete, string patId, string connectionString, List<Property> properties, string uniqueId = "")
  16.         {
  17.             this.tableRows = new List<MITableRow>();
  18.             this.tableName = tableName;
  19.             this.uniqueIdFieldName = uniqueIdFieldName;
  20.             this.parentUniqueId = parentUniqueId;
  21.             this.parentUniqueFieldName = parentUniqueFieldName;
  22.             this.addEditDelete = addEditDelete;
  23.             this.patId = patId;
  24.             this.connectionString = connectionString;
  25.             this.properties = properties;
  26.             this.uniqueId = uniqueId;
  27.  
  28.             GetAllTableRows();
  29.         }
  30.  
  31.         private void GetAllTableRows()
  32.         {
  33.                 using (OdbcConnection conn = new OdbcConnection(this.connectionString))
  34.                 {
  35.                     string query = "query here";
  36.  
  37.                     using (OdbcCommand command = new OdbcCommand(query, conn))
  38.                     {
  39.                         conn.Open();
  40.  
  41.                         using (OdbcDataReader reader = command.ExecuteReader())
  42.                         {
  43.                             while (reader.Read())
  44.                             {
  45.                                 MITableRow newRow = new MITableRow(this.tableName, this.uniqueIdFieldName, this.addEditDelete, this.patId, this.connectionString, this.properties);
  46.  
  47.                                 for (int i = 0; i < reader.FieldCount; i++)
  48.                                 {
  49.                                     newRow.properties[i].value = reader[i].ToString();
  50.                                 }
  51.  
  52.                                 this.tableRows.Add(newRow);
  53.                             }
  54.  
  55.                             reader.Close();
  56.                         }
  57.                     }
  58.                 }
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Edits the value of a property.
  63.         /// </summary>
  64.         /// <param name="fieldName"></param>
  65.         /// <param name="value"></param>
  66.         public void EditProperty(string fieldName, string value)
  67.         {
  68.             foreach (var item in this.properties)
  69.             {
  70.                 if (fieldName == item.name)
  71.                 {
  72.                     item.value = value;
  73.                 }
  74.             }
  75.         }
  76.  
  77.         /// <summary>
  78.         /// Adds a property to the MI table.
  79.         /// </summary>
  80.         /// <param name="property"></param>
  81.         public void AddProperty(Property property)
  82.         {
  83.             this.properties.Add(property);
  84.         }
  85.     }
  86.    
  87.    
  88.  
  89.     class MITableRow
  90.     {
  91.         private string tableName;
  92.         private string uniqueIdentifierFieldName;
  93.         private string uniqueIdentifier;
  94.         private string patId;
  95.         private string addEditDelete;
  96.         private string connectionString;
  97.         private XElement xml;
  98.         public List<Property> properties;
  99.  
  100.         public MITableRow(string tableName, string uniqueIdentifierFieldName, string addEditDelete, string patId, string connectionString, List<Property> properties, string uniqueIdentifier = "")
  101.         {
  102.             this.tableName = tableName;
  103.             this.uniqueIdentifierFieldName = uniqueIdentifierFieldName;
  104.             this.uniqueIdentifier = uniqueIdentifier;
  105.             this.connectionString = connectionString;
  106.             this.patId = patId;
  107.             this.addEditDelete = addEditDelete;
  108.             this.properties = properties;
  109.  
  110.             if (this.addEditDelete == "E" && this.uniqueIdentifier != "")
  111.             {
  112.                 //This row is being edited.
  113.                 GetByUniqueId();
  114.             }
  115.             else if (this.addEditDelete == "A")
  116.             {
  117.                 //This row is being added.
  118.                 this.uniqueIdentifier = GetNextUniqueIdentifier();
  119.             }
  120.             else if (this.addEditDelete == "D")
  121.             {
  122.                 //This row should be deleted
  123.             }
  124.             else
  125.             {
  126.                 //No Add/edit/delete method given.
  127.             }
  128.         }
  129.  
  130.         /// <summary>
  131.         /// Edits the value of a property.
  132.         /// </summary>
  133.         /// <param name="fieldName"></param>
  134.         /// <param name="value"></param>
  135.         public void EditProperty(string fieldName, string value)
  136.         {
  137.             foreach (var item in this.properties)
  138.             {
  139.                 if (fieldName == item.name)
  140.                 {
  141.                     item.value = value;
  142.                 }
  143.             }
  144.         }
  145.  
  146.         /// <summary>
  147.         /// Sets current objects property values for the patid and uniqueId listed.
  148.         /// </summary>
  149.         private void GetByUniqueId()
  150.         {
  151.             try
  152.             {
  153.                 using (OdbcConnection conn = new OdbcConnection(this.connectionString))
  154.                 {
  155.  
  156.                     string query = "QUERY HERE";
  157.  
  158.                     using (OdbcCommand command = new OdbcCommand(query, conn))
  159.                     {
  160.                         conn.Open();
  161.                         using (OdbcDataReader reader = command.ExecuteReader())
  162.                         {
  163.                             while (reader.Read())
  164.                             {
  165.                                 //Loop through the columns returned and add the values to the current items corresponding properties.
  166.                                 for (int i = 0; i < reader.FieldCount; i++)
  167.                                 {
  168.                                     this.properties[i].value = reader[i].ToString();
  169.                                 }
  170.                             }
  171.                             reader.Close();
  172.                         }
  173.                     }
  174.                 }
  175.             }
  176.             catch (Exception exc)
  177.             {
  178.                 Console.WriteLine(exc.ToString());
  179.             }
  180.         }
  181.  
  182.         /// <summary>
  183.         /// Gets the next available unique identifier code for the patient.
  184.         /// </summary>
  185.         /// <returns></returns>
  186.         private string GetNextUniqueIdentifier()
  187.         {
  188.             string uniqueId = "";
  189.             try
  190.             {
  191.                 using (OdbcConnection conn = new OdbcConnection(this.connectionString))
  192.                 {
  193.                     //ODBC cannot use named parameters so use ? as placeholders instead.
  194.                     using (OdbcCommand command = new OdbcCommand("query here", conn))
  195.                     {
  196.                         conn.Open();
  197.                         string result = command.ExecuteScalar().ToString();
  198.                         if (!string.IsNullOrEmpty(result))
  199.                         {
  200.                             uniqueId = result;
  201.                             string[] split = uniqueId.Split('.');
  202.                             uniqueId = split[0] + "." + (Convert.ToInt32(split[1]) + 1).ToString("D5");
  203.                         }
  204.                         else
  205.                         {
  206.                             //No record exists. Create the very first record.
  207.                             uniqueId = this.uniqueIdentifierFieldName.Substring(5, 3) + ".00001";
  208.                         }
  209.                     }
  210.                 }
  211.             }
  212.             catch (Exception exc)
  213.             {
  214.                 Console.WriteLine(exc.ToString());
  215.             }
  216.             return uniqueId;
  217.         }
  218.  
  219.         /// <summary>
  220.         /// Adds a property to the MI table.
  221.         /// </summary>
  222.         /// <param name="property"></param>
  223.         public void AddProperty(Property property)
  224.         {
  225.             this.properties.Add(property);
  226.         }
  227.  
  228.         /// <summary>
  229.         /// Gets the xml structure of the MI Table.
  230.         /// </summary>
  231.         /// <returns></returns>
  232.         public XElement GetXml()
  233.         {
  234.             //Set the base xml structure.
  235.             this.xml = new XElement("SYSTEM." + this.tableName,
  236.                 new XElement("rows.reference",
  237.                     new XElement("unique_identifier", this.uniqueIdentifier),
  238.                     new XElement("add_edit_delete", this.addEditDelete)
  239.                 )
  240.             );
  241.  
  242.             //Reference the xml element that the new properties should be added after.
  243.             XElement element = new XElement("temp", "");
  244.             element = this.xml.Element("SYSTEM." + this.tableName);
  245.             element = this.xml.Element("rows.reference");
  246.  
  247.             //Add all the properties to the xml structure.
  248.             foreach (var item in this.properties)
  249.             {
  250.                 switch (item.propertyType)
  251.                 {
  252.                     case "string":
  253.                         element.AddAfterSelf(new XElement(item.name, item.value));
  254.                         break;
  255.                     case "phone":
  256.                         string[] splitPhone = item.value.Split('-');
  257.                         element.AddAfterSelf(new XElement(item.name,
  258.                         new XElement("areacode", splitPhone[0]),
  259.                         new XElement("phonenumber", splitPhone[1] + "-" + splitPhone[2]),
  260.                         new XElement("extension", "")));
  261.                         break;
  262.                     default:
  263.                         break;
  264.                 }
  265.             }
  266.             return this.xml;
  267.         }
  268.     }
  269.  
  270.     class Property
  271.     {
  272.         public string name { get; set; }
  273.         public string value { get; set; }
  274.         public string propertyType { get; set; }
  275.  
  276.         public Property(string name, string propertyType = "string")
  277.         {
  278.             this.name = name;
  279.             this.propertyType = propertyType;
  280.         }
  281.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement