public IKey GetPrimaryKey(IDatamodel dataModel)
 {
     return(DataRecordManager.GetKey(dataModel, PrimaryKeyName));
 }
        /// <summary>
        /// Method for consuming and processing messages on the RabbitMQ queue.
        /// </summary>
        public void ConsumeMessage()
        {
            // Objects for working with Blaise data sets.
            IDataLink4 dl_source    = null;
            IDatamodel dm_source    = null;
            IDataLink4 dl_dest_sql  = null;
            IDataLink  dl_dest_file = null;

            // Functionality to be performed when a message is received. (digestion)
            consumer.Received += (model, ea) =>
            {
                // Extract the message and encode it.
                var body    = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                log.Info("Message received - " + message);

                MessageData data = null;
                try
                {
                    // Take the serialized JSON string and deserialize it into a MessageData object.
                    data = new JavaScriptSerializer().Deserialize <MessageData>(message);

                    // Connect to the Blaise source data set.
                    dl_source = GetDataLink(data.source_hostname, data.source_instrument, data.source_server_park);
                    dm_source = dl_source.Datamodel;

                    // Identify the primary key in the source data set.
                    var key = DataRecordManager.GetKey(dm_source, "PRIMARY");

                    // Assign the primary key the value of the 'serial_number' recieved from the RabbitMQ message.
                    key.Fields[0].DataValue.Assign(data.serial_number);

                    // Check if a case with this key exists in the source data set.
                    if (dl_source.KeyExists(key))
                    {
                        // Read in the case.
                        var case_record = dl_source.ReadRecord(key);

                        if (data.action == "delete")
                        {
                            dl_source.Delete(key);
                            SendStatus(MakeStatusJson(data, "Case Deleted"));
                        }
                        else
                        {
                            // Connect to the Blaise sql database destination data set  if provided in payload.
                            if ((data.dest_hostname != "" && data.dest_hostname != null) && (data.dest_server_park != "" && data.dest_server_park != null))
                            {
                                dl_dest_sql = GetDataLink(data.dest_hostname, data.dest_instrument, data.dest_server_park);

                                // Copy or move the case from the source to destination based on the 'action' received from the message.
                                switch (data.action)
                                {
                                // Copy action received.
                                case "copy":
                                    dl_dest_sql.Write(case_record);
                                    if (!dl_dest_sql.KeyExists(key))
                                    {
                                        SendStatus(MakeStatusJson(data, "Error"));
                                        log.Error(data.dest_instrument + " case " + data.serial_number + " NOT copied from " + data.source_server_park + "@" + data.source_hostname + " to " + data.dest_server_park + "@" + data.dest_hostname + ".");
                                    }
                                    if (dl_dest_sql.KeyExists(key))
                                    {
                                        SendStatus(MakeStatusJson(data, "Case Copied"));
                                        log.Info(data.dest_instrument + " case " + data.serial_number + " copied from " + data.source_server_park + "@" + data.source_hostname + " to " + data.dest_server_park + "@" + data.dest_hostname + ".");
                                    }
                                    break;

                                // Move action received.
                                case "move":
                                    dl_dest_sql.Write(case_record);
                                    dl_source.Delete(key);
                                    if (!dl_dest_sql.KeyExists(key))
                                    {
                                        SendStatus(MakeStatusJson(data, "Error"));
                                        log.Error(data.dest_instrument + " case " + data.serial_number + " NOT moved from " + data.source_server_park + "@" + data.source_hostname + " to " + data.dest_server_park + "@" + data.dest_hostname + ".");
                                    }
                                    if ((dl_dest_sql.KeyExists(key)) && (dl_source.KeyExists(key)))
                                    {
                                        SendStatus(MakeStatusJson(data, "Warn"));
                                        log.Warn(data.dest_instrument + " case " + data.serial_number + " copied from " + data.source_server_park + "@" + data.source_hostname + " to " + data.dest_server_park + "@" + data.dest_hostname + " but also still exists in " + data.source_server_park + "@" + data.source_hostname + ".");
                                    }
                                    if ((dl_dest_sql.KeyExists(key)) && (!dl_source.KeyExists(key)))
                                    {
                                        SendStatus(MakeStatusJson(data, "Case Moved"));
                                        log.Info(data.dest_instrument + " case " + data.serial_number + " moved from " + data.source_server_park + "@" + data.source_hostname + " to " + data.dest_server_park + "@" + data.dest_hostname + ".");
                                    }
                                    break;

                                // Invalid action received.
                                default:
                                    SendStatus(MakeStatusJson(data, "Invalid Action"));
                                    log.Error("Invalid action requested - " + data.action);
                                    break;
                                }
                            }

                            // Connect to the Blaise file database destination data set if provided in payload.
                            if (data.dest_filepath != "" && data.dest_filepath != null)
                            {
                                // Check destination file database exists, and if not create it.
                                if (!File.Exists(data.dest_filepath + "\\" + data.dest_instrument + ".bdbx"))
                                {
                                    // Get source BMI and BDI to setup new file destination data set.
                                    string sourceBMI = GetSourceBMI(data.source_hostname, data.source_server_park, data.source_instrument);
                                    string sourceBDI = GetSourceBDI(data.source_hostname, data.source_server_park, data.source_instrument);

                                    // Create source directory from destination file path recieved in payload.
                                    Directory.CreateDirectory(data.dest_filepath);

                                    // Copy BMI from source to destination file path recieved in payload.
                                    File.Copy(sourceBMI, data.dest_filepath + "\\" + data.dest_instrument + ".bmix", true);

                                    // Create destination file data set.
                                    IDataInterface di = DataInterfaceManager.GetDataInterface();
                                    di.ConnectionInfo.DataSourceType   = DataSourceType.Blaise;
                                    di.ConnectionInfo.DataProviderType = DataProviderType.BlaiseDataProviderForDotNET;
                                    di.DataPartitionType = DataPartitionType.Stream;
                                    IBlaiseConnectionStringBuilder csb = DataInterfaceManager.GetBlaiseConnectionStringBuilder();
                                    csb.DataSource = data.dest_filepath + "\\" + data.dest_instrument + ".bdbx";
                                    di.ConnectionInfo.SetConnectionString(csb.ConnectionString);
                                    di.DatamodelFileName = data.dest_filepath + "\\" + data.dest_instrument + ".bmix";
                                    di.FileName          = data.dest_filepath + "\\" + data.dest_instrument + ".bdix";
                                    di.CreateTableDefinitions();
                                    di.CreateDatabaseObjects(null, true);
                                    di.SaveToFile(true);
                                }

                                // Connect to the Blaise file database destination data set.
                                dl_dest_file = DataLinkManager.GetDataLink(data.dest_filepath + "\\" + data.dest_instrument + ".bdix");

                                // Copy or move the case from the source to destination based on the 'action' received from the message.
                                switch (data.action)
                                {
                                // Copy action received.
                                case "copy":
                                    dl_dest_file.Write(case_record);
                                    if (dl_dest_file.ReadRecord(key) == null)
                                    {
                                        SendStatus(MakeStatusJson(data, "Error"));
                                        log.Error(data.dest_instrument + " case " + data.serial_number + " NOT copied from " + data.source_server_park + "@" + data.source_hostname + " to " + data.dest_filepath + ".");
                                    }
                                    if (dl_dest_file.ReadRecord(key) != null)
                                    {
                                        SendStatus(MakeStatusJson(data, "Case Copied"));
                                        log.Info(data.dest_instrument + " case " + data.serial_number + " copied from " + data.source_server_park + "@" + data.source_hostname + " to " + data.dest_filepath + ".");
                                    }
                                    break;

                                // Move action received.
                                case "move":
                                    dl_dest_file.Write(case_record);
                                    dl_source.Delete(key);
                                    if (dl_dest_file.ReadRecord(key) == null)
                                    {
                                        SendStatus(MakeStatusJson(data, "Error"));
                                        log.Error(data.dest_instrument + " case " + data.serial_number + " NOT moved from " + data.source_server_park + "@" + data.source_hostname + " to " + data.dest_filepath + ".");
                                    }
                                    if ((dl_dest_file.ReadRecord(key) != null) && (dl_source.KeyExists(key)))
                                    {
                                        SendStatus(MakeStatusJson(data, "Warn"));
                                        log.Warn(data.dest_instrument + " case " + data.serial_number + " copied from " + data.source_server_park + "@" + data.source_hostname + " to " + data.dest_filepath + " but also still exists in " + data.source_server_park + "@" + data.source_hostname + ".");
                                    }
                                    if ((dl_dest_file.ReadRecord(key) != null) && (!dl_source.KeyExists(key)))
                                    {
                                        SendStatus(MakeStatusJson(data, "Case Moved"));
                                        log.Info(data.dest_instrument + " case " + data.serial_number + " moved from " + data.source_server_park + "@" + data.source_hostname + " to " + data.dest_filepath + ".");
                                    }
                                    break;

                                // Invalid action received.
                                default:
                                    SendStatus(MakeStatusJson(data, "Invalid Action"));
                                    log.Error("Invalid action requested - " + data.action);
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        SendStatus(MakeStatusJson(data, "Case NOT Found"));
                        log.Error("Case " + data.serial_number.ToString() + " doesn't exist in source database.");
                    }
                }
                catch (Exception e)
                {
                    SendStatus(MakeStatusJson(data, "Error"));
                    log.Error(e);
                }
                // Remove from queue when done processing
                channel.BasicAck(ea.DeliveryTag, false);
            };

            // Consume and process any messages already held on the queue.
            string queueName = ConfigurationManager.AppSettings["HandlerQueueName"];

            channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer);
        }
 public IKey GetKey(IDatamodel dataModel, string keyName)
 {
     return(DataRecordManager.GetKey(dataModel, keyName));
 }