private string SaveDeployState(DeployState state)
 {
     if (state.Build == null)
     {
         throw new ArgumentNullException("Missing build");
     }
     if (state.Branch == null)
     {
         throw new ArgumentNullException("Missing branch");
     }
     if (state.Component == null)
     {
         throw new ArgumentNullException("Missing component");
     }
     if (state.Environment == null)
     {
         throw new ArgumentNullException("Missing environment");
     }
     if (state.MachineList == null)
     {
         throw new ArgumentNullException("Missing machineList");
     }
     if (state.DeployBatchRequestItemId == null)
     {
         throw new ArgumentNullException("Missing deployBatchRequestItemId");
     }
     var sqlDeployState = new SqlDeployState
     {
         ID = state.Id,
         ProjectID = state.ProjectId,
         BranchID = state.Branch.Id,
         BranchJson = state.Branch.ToJson(),
         BuildID = state.Build.Id,
         BuildJson = state.Build.ToJson(),
         ComponentID = state.Component.Id,
         ComponentJson = state.Component.ToJson(),
         EnvironmentID = state.Environment.Id,
         EnvironmentName = state.Environment.EnvironmentName,
         EnvironmentJson = state.Environment.ToJson(),
         DeployBatchRequestItemID = state.DeployBatchRequestItemId,
         DeploymentCompleteDateTimeUtc = state.DeploymentCompleteDateTimeUtc,
         DeploymentStartedDateTimeUtc = state.DeploymentStartedDateTimeUtc,
         ErrorDetails = state.ErrorDetails,
         SortableVersion = state.Build.SortableVersion,
         EnumDeployStatusID = (int)state.Status,
         CreatedByUserName = state.CreatedByUserName,
         CreatedDateTimeUtc = state.CreatedDateTimeUtc,
         UpdatedByUserName = state.UpdatedByUserName,
         UpdatedDateTimeUtc = state.UpdatedDateTimeUtc,
         SubmittedDateTimeUtc = state.SubmittedDateTimeUtc
     };
     if(state.MessageList != null)
     {
         foreach(var message in state.MessageList)
         {
             if(string.IsNullOrEmpty(message.Id))
             {
                 message.Id = Guid.NewGuid().ToString();
             }
         }
         sqlDeployState.MessageListJson = state.MessageList.ToJson();
     }
     var sqlMachineList = new List<SqlDeployStateMachine>();
     foreach (var machine in state.MachineList)
     {
         var sqlMachine = new SqlDeployStateMachine
         {
             ID = machine.Id,
             DeployStateID = sqlDeployState.ID,
             MachineID = machine.Id,
             MachineName = machine.MachineName,
             MachineJson = machine.ToJson(),
             CreatedByUserName = _userIdentity.UserName,
             CreatedDateTimeUtc = DateTime.UtcNow,
             UpdatedByUserName = _userIdentity.UserName,
             UpdatedDateTimeUtc = DateTime.UtcNow
         };
         sqlMachineList.Add(sqlMachine);
     }
     using (var db = _sqlConnectionInfo.GetDB())
     {
         if (string.IsNullOrEmpty(sqlDeployState.ID) || !DeployStateExists(sqlDeployState.ID))
         {
             if (string.IsNullOrEmpty(sqlDeployState.ID))
             {
                 sqlDeployState.ID = Guid.NewGuid().ToString();
             }
             db.Insert("DeployState", "ID", false, sqlDeployState);
             foreach (var machine in sqlMachineList)
             {
                 machine.ID = Guid.NewGuid().ToString();
                 machine.DeployStateID = sqlDeployState.ID;
                 db.Insert("DeployStateMachine", "ID", false, machine);
             }
         }
         else 
         {
             db.Update("DeployState", "ID", sqlDeployState);
         }
     }
     return sqlDeployState.ID;
 }
        private ComponentDeployHistory PopulateComponentDeployHistory(SqlDeployState dbState, List<SqlDeployStateMachine> dbMachineList)
        {
            DeployBuild build = null;
            if(!string.IsNullOrEmpty(dbState.BuildJson))
            {
                build = JsonConvert.DeserializeObject<DeployBuild>(dbState.BuildJson);
            }
            DeployEnvironment environment = null;
            if(!string.IsNullOrEmpty(dbState.EnvironmentJson))
            {
                environment = JsonConvert.DeserializeObject<DeployEnvironment>(dbState.EnvironmentJson);
            }
            DeployProjectBranch branch = null;
            if(!string.IsNullOrEmpty(dbState.BranchJson))
            {
                branch = JsonConvert.DeserializeObject<DeployProjectBranch>(dbState.BranchJson);
            }
            DeployComponent component = null;
            if(!string.IsNullOrEmpty(dbState.ComponentJson))
            {
                component = JsonConvert.DeserializeObject<DeployComponent>(dbState.ComponentJson);
            }
            var machineList = (from m in dbMachineList
                                where m.DeployStateID == dbState.ID && !string.IsNullOrEmpty(m.MachineJson)
                                select JsonConvert.DeserializeObject<DeployMachine>(m.MachineJson)
                              ).ToList();

            return new ComponentDeployHistory
            {
                DeployStateId = dbState.ID,
                DeployBatchRequestItemId = dbState.DeployBatchRequestItemID,
                ProjectId = dbState.ProjectID, 
                ProjectName = (build != null)
                                ? build.ProjectName
                                : null,
                BuildId = dbState.BuildID,
                ProjectBranchId = dbState.BranchID,
                ProjectBranchName = (branch != null)
                                    ? branch.BranchName
                                    : null,
                ProjectComponentId = dbState.ComponentID,
                ProjectComponentName = (component != null)
                                        ? component.ComponentName
                                        : null,
                Status = (EnumDeployStatus)dbState.EnumDeployStatusID,
                StatusDisplayValue = EnumHelper.GetDisplayValue((EnumDeployStatus)dbState.EnumDeployStatusID),
                EnvironmentId = dbState.EnvironmentID,
                EnvironmentName = (environment != null) 
                                    ? environment.EnvironmentName 
                                    : null,
                DeploymentStartedDateTimeUtc = dbState.DeploymentStartedDateTimeUtc,
                DeploymentCompleteDateTimeUtc = dbState.DeploymentCompleteDateTimeUtc,
                FileId = (build != null)
                            ? build.FileId
                            : null,
                Version = (build != null)
                            ? build.Version
                            : null,
                SortableVersion = (build != null)
                            ? DeployBuild.GetSortableVersion(build.Version)
                            : null,
                MachineList = machineList,
                ErrorDetails = dbState.ErrorDetails,
            };
        }
 private DeployState PopulateDeployState(SqlDeployState item, bool loadChildren)
 {
     var returnValue = new DeployState();
     PopulateDeployState(item, loadChildren, returnValue);
     return returnValue;
 }
 private void PopulateDeployState(SqlDeployState source, bool loadChildren, DeployStateSummary target)
 {
     target.Id = source.ID;
     target.DeployBatchRequestItemId = source.DeployBatchRequestItemID;
     target.DeploymentStartedDateTimeUtc = source.DeploymentStartedDateTimeUtc;
     target.Status = (EnumDeployStatus)source.EnumDeployStatusID;
     target.ErrorDetails = source.ErrorDetails;
     target.ProjectId = source.ProjectID;
     target.DeploymentCompleteDateTimeUtc = source.DeploymentCompleteDateTimeUtc;
     target.SubmittedDateTimeUtc = source.SubmittedDateTimeUtc;
     target.CreatedByUserName = source.CreatedByUserName;
     target.CreatedDateTimeUtc = source.CreatedDateTimeUtc;
     target.UpdatedByUserName = source.UpdatedByUserName;
     target.UpdatedDateTimeUtc = source.UpdatedDateTimeUtc;
     if(!string.IsNullOrEmpty(source.BranchJson))
     {
         target.Branch = JsonConvert.DeserializeObject<DeployProjectBranch>(source.BranchJson);
     }
     if(!string.IsNullOrEmpty(source.BuildJson))
     {
         target.Build = JsonConvert.DeserializeObject<DeployBuild>(source.BuildJson);
     }
     if(!string.IsNullOrEmpty(source.ComponentJson))
     {
         target.Component = JsonConvert.DeserializeObject<DeployComponent>(source.ComponentJson);
     }
     if(!string.IsNullOrEmpty(source.EnvironmentJson))
     {
         target.Environment = JsonConvert.DeserializeObject<DeployEnvironment>(source.EnvironmentJson);
     }
     if(!string.IsNullOrEmpty(source.MessageListJson) && target is DeployState)
     {
         var deployState = (DeployState)target;
         deployState.MessageList = JsonConvert.DeserializeObject<List<DeployStateMessage>>(source.MessageListJson);
     }
     if(loadChildren)
     {
         PopulateDeployStateChildren(target);
     }
 }
        public DeployState CreateDeployState(DeployBuild build, DeployProjectBranch branch, DeployEnvironment environment, DeployComponent component, IEnumerable<DeployMachine> machineList, string deployBatchRequestItemId)
        {
            if (build == null)
            {
                throw new ArgumentNullException("Missing build");
            }
            if (branch == null)
            {
                throw new ArgumentNullException("Missing branch");
            }
            if (component == null)
            {
                throw new ArgumentNullException("Missing component");
            }
            if (environment == null)
            {
                throw new ArgumentNullException("Missing environment");
            }
            if (machineList == null)
            {
                throw new ArgumentNullException("Missing machineList");
            }
            if (deployBatchRequestItemId == null)
            {
                throw new ArgumentNullException("Missing deployBatchRequestItemId");
            }
            var sqlDeployState = new SqlDeployState
            {
                ID = Guid.NewGuid().ToString(),
                ProjectID = build.ProjectId,
                BranchID = branch.Id,
                BranchJson = branch.ToJson(),
                BuildID = build.Id,
                BuildJson = build.ToJson(),
                ComponentID = component.Id,
                ComponentJson = component.ToJson(),
                EnvironmentID = environment.Id,
                EnvironmentName = environment.EnvironmentName,
                EnvironmentJson = environment.ToJson(),
                DeployBatchRequestItemID = deployBatchRequestItemId,
                DeploymentCompleteDateTimeUtc = null,
                DeploymentStartedDateTimeUtc = null,
                ErrorDetails = null,
                SortableVersion = build.SortableVersion,
                EnumDeployStatusID = (int)EnumDeployStatus.NotStarted,
                CreatedByUserName = _userIdentity.UserName,
                CreatedDateTimeUtc = DateTime.UtcNow,
                UpdatedByUserName = _userIdentity.UserName,
                UpdatedDateTimeUtc = DateTime.UtcNow,
                SubmittedDateTimeUtc = DateTime.UtcNow
            };
            var branchJson = branch.ToJson();
            var buildJson = build.ToJson();
            var componentJson = component.ToJson();
            var environmentJson = environment.ToJson();
            using(var db = _sqlConnectionInfo.GetDB())
            {
                var deployStateSql = PetaPoco.Sql.Builder
                            .Append("INSERT INTO DeployState (ID, DeployBatchRequestItemID, EnumDeployStatusID, ProjectID, BranchID, BuildID, EnvironmentID, EnvironmentName, ComponentID,")
                                            .Append("BranchJson, BuildJson, EnvironmentJson, ComponentJson, SubmittedDateTimeUtc, DeploymentStartedDateTimeUtc, DeploymentCompleteDateTimeUtc, ")
                                            .Append("ErrorDetails, SortableVersion, CreatedByUserName, CreatedDateTimeUtc, UpdatedByUserName, UpdatedDateTimeUtc)")
                            .Append("VALUES (@ID, @DeployBatchRequestItemID, @EnumDeployStatusID, @ProjectID, @BranchID, @BuildID, @EnvironmentID, @EnvironmentName, @ComponentID,", sqlDeployState)
                                .Append("@BranchJson, @BuildJson, @EnvironmentJson, @ComponentJson, @SubmittedDateTimeUtc, @DeploymentStartedDateTimeUtc, @DeploymentCompleteDateTimeUtc,", sqlDeployState)
                                .Append("@ErrorDetails, @SortableVersion, @CreatedByUserName, @CreatedDateTimeUtc, @UpdatedByUserName, @UpdatedDateTimeUtc)", sqlDeployState);
                db.Execute(deployStateSql);

                foreach(var machine in machineList)
                {
                    var sqlMachine = new SqlDeployStateMachine
                    {
                        ID = Guid.NewGuid().ToString(),
                        DeployStateID = sqlDeployState.ID,
                        MachineID = machine.Id,  
                        MachineName = machine.MachineName,
                        MachineJson = machine.ToJson(),
                        CreatedByUserName = _userIdentity.UserName,
                        CreatedDateTimeUtc = DateTime.UtcNow,
                        UpdatedByUserName = _userIdentity.UserName,
                        UpdatedDateTimeUtc = DateTime.UtcNow
                    };
                    var machineSql = PetaPoco.Sql.Builder  
                            .Append("INSERT INTO DeployStateMachine (ID, DeployStateID, MachineID, MachineName, MachineJson, CreatedByUserName, CreatedDateTimeUtc, UpdatedByUserName, UpdatedDateTimeUtc)")
                            .Append("VALUES (@ID, @DeployStateID, @MachineID, @MachineName, @MachineJson, @CreatedByUserName, @CreatedDateTimeUtc, @UpdatedByUserName, @UpdatedDateTimeUtc)", sqlMachine);
                    db.Execute(machineSql);
                }
            }
            return GetDeployState(sqlDeployState.ID);
        }