public void DeleteInstruction()
 {
     if (InstructionsCollection.Count > 0)
     {
         InstructionsCollection.RemoveAt(InstructionsCollection.Count - 1);
     }
 }
Esempio n. 2
0
        public IActionResult Create([FromBody] NewBatchObject newbatch)
        {
            if (!ModelState.IsValid)
            {
                { return(NotFound()); }
            }
            if (newbatch.HRToken.Trim() == "")
            {
                { return(NotFound("Invalid Token")); }
            }
            if (newbatch.Instructions.Count() < 1)
            {
                return(BadRequest("No Instructions found"));
            }
            if (newbatch.Ent_IDs.Count < 1)
            {
                return(BadRequest("No Entities found"));
            }

            // Check if HR is up
            var hruri      = _configuration.GetSection("HumanReview:uri").Value + "auth/token";
            var hrResponse = Common.ServerStatusBy(hruri);

            // We expect a 400 - Bad Request, anything else specifically 404 Not Found, return an error
            if (hrResponse.StatusCode == 404)
            {
                { return(NotFound("Error: Human Review Service unavailable")); }
            }

            int        BatchIdOutput = 0;
            string     ReturnMessage = "";
            ReturnData retValue;

            var InstructionList = new InstructionsCollection();

            foreach (var ins in newbatch.Instructions)
            {
                InstructionList.Add(ins);
            }
            var EntitiesList = new EntitiesCollection();

            foreach (var ent in newbatch.Ent_IDs)
            {
                EntitiesList.Add(ent);
            }


            #region  Save Editorial Data
            try
            {
                using (var sqlCmd = _context.Database.GetDbConnection().CreateCommand())
                {
                    string sp = "usp_CreateBatch_ins";

                    sqlCmd.CommandText = sp;
                    sqlCmd.CommandType = CommandType.StoredProcedure;

                    sqlCmd.Parameters.Add(new SqlParameter("@BatchName", SqlDbType.VarChar, 500)
                    {
                        Value = newbatch.BatchName
                    });
                    sqlCmd.Parameters.Add(new SqlParameter("@Description", SqlDbType.VarChar, 1000)
                    {
                        Value = newbatch.Description
                    });
                    sqlCmd.Parameters.Add(new SqlParameter("@StartDate", SqlDbType.Date)
                    {
                        Value = newbatch.StartDate
                    });
                    sqlCmd.Parameters.Add(new SqlParameter("@DueDate", SqlDbType.Date)
                    {
                        Value = newbatch.DueDate
                    });
                    sqlCmd.Parameters.Add(new SqlParameter("@PriorityCollectionItemID", SqlDbType.Int)
                    {
                        Value = newbatch.PriorityCollectionItemID
                    });
                    sqlCmd.Parameters.Add(new SqlParameter("@StatusCollectionItemID", SqlDbType.Int)
                    {
                        Value = newbatch.StatusCollectionItemID
                    });

                    sqlCmd.Parameters.Add(new SqlParameter("@Instructions", SqlDbType.Structured)
                    {
                        Value = InstructionList
                    });
                    sqlCmd.Parameters.Add(new SqlParameter("@Ent_IDs", SqlDbType.Structured)
                    {
                        Value = EntitiesList
                    });

                    sqlCmd.Parameters.Add(new SqlParameter("@Username", SqlDbType.VarChar, 50)
                    {
                        Value = newbatch.Username
                    });
                    sqlCmd.Parameters.Add(new SqlParameter("@OriginalCount", SqlDbType.Int)
                    {
                        Value = newbatch.OriginalCount
                    });
                    sqlCmd.Parameters.Add(new SqlParameter("@Operation", SqlDbType.VarChar, 500)
                    {
                        Value = newbatch.Operation
                    });

                    // output params
                    SqlParameter Results  = new SqlParameter("@Results", SqlDbType.VarChar, 500);
                    SqlParameter NewBWQID = new SqlParameter("@NewBWQID", SqlDbType.Int);
                    Results.Direction  = ParameterDirection.Output;
                    NewBWQID.Direction = ParameterDirection.Output;

                    sqlCmd.Parameters.Add(Results);
                    sqlCmd.Parameters.Add(NewBWQID);

                    if (sqlCmd.Connection.State != ConnectionState.Open)
                    {
                        sqlCmd.Connection.Open();
                    }

                    var execResult = sqlCmd.ExecuteNonQuery();
                    ReturnMessage = Results.Value.ToString();

                    if (NewBWQID.Value.ToString() == "") // no records created
                    {
                        return(BadRequest(new { batch = BatchIdOutput, message = ReturnMessage }));
                    }

                    BatchIdOutput = Convert.ToInt32(NewBWQID.Value);
                }
            }
            catch (Exception e)
            {
                // TODO: log
                return(BadRequest(new { batch = BatchIdOutput, message = ReturnMessage + " " + e.Message }));
            }
            #endregion

            //Human Review Entries
            #region Create Human Review Entries
            try
            {
                var BWQEntities = _context.BWQEntities.Where(t => t.BWQID == BatchIdOutput);
                var Modules     = _context.ApplicationModules;
                var QueueGuid   = (from mods in Modules
                                   where mods.ModuleName == "BWQ"
                                   select mods.QueueGuid).FirstOrDefault();

                foreach (var bwqent in BWQEntities)
                {
                    var BWQEntityInstructionsObject = _context.BWQInstructions
                                                      .Where(t => t.BWQEntitiesID == bwqent.BWQEntitiesID);

                    var thisInstruction = BWQEntityInstructionsObject
                                          .Select(x => new
                    {
                        BWQInstructionsID = x.BWQInstructionsID,
                        BWQEntitiesID     = x.BWQEntitiesID,
                        BWQDispositionsID = x.BWQDispositionsID,
                        BWQFieldSelectID  = x.BWQFieldSelectID,
                        Instructions      = x.Instructions,
                        CreatedBy         = x.CreatedBy,
                        DateCreatedUTC    = x.DateCreatedUTC,
                        UpdatedBy         = x.UpdatedBy,
                        LastUpdatedUTC    = x.LastUpdatedUTC,
                        BWQEntities       = "",
                        BWQDispositions   = "",
                        BWQFieldSelect    = ""
                    });


                    var JsonData = JsonConvert.SerializeObject(thisInstruction);

                    WorkItemRequest HRCreateRequest = new WorkItemRequest();
                    HRCreateRequest.name                 = "Instruction Entries for BWQ Entity " + bwqent.BWQEntitiesID + " and Profile ID " + bwqent.MMMEntityID;
                    HRCreateRequest.description          = "Instruction Entries for BWQ Entity " + bwqent.BWQEntitiesID + " and Profile " + bwqent.MMMEntityID;
                    HRCreateRequest.queueGuid            = QueueGuid;
                    HRCreateRequest.statusDetailTypeGuid = _configuration.GetSection("HumanReview:statusDetailTypeGuid_ins").Value;
                    HRCreateRequest.reviewTypeGuid       = _configuration.GetSection("HumanReview:reviewTypeGuid_ins").Value;
                    HRCreateRequest.formDefinitionJson   = JsonData;
                    HRCreateRequest.isActive             = true;

                    //var returnGuid = getWorkItemForEntityAsync(HRCreateRequest, newbatch.HRToken);

                    var returnGuid = Common.getWorkItemAsync(HRCreateRequest, newbatch.HRToken, _configuration);

                    /*
                     * if (returnGuid.value.workItemGuid == null)
                     * {
                     *  var workitemnotcreated = new
                     *  {
                     *      Success = false,
                     *      Message = "WorkItem not created"
                     *  };
                     *  return BadRequest(workitemnotcreated);
                     *  // TODO: LOG THIS ERROR
                     * }
                     */

                    if (returnGuid.value.workItemGuid == null)
                    {
                        var workitemnotcreated = new
                        {
                            Success = false,
                            Message = "WorkItem not created"
                        };

                        // delete the BWQ entry created above if WorkItemGuid is not created
                        using (var sqlCmd = _context.Database.GetDbConnection().CreateCommand())
                        {
                            string sp = "usp_DeleteBatch_del";

                            sqlCmd.CommandText = sp;
                            sqlCmd.CommandType = CommandType.StoredProcedure;
                            sqlCmd.Parameters.Add(new SqlParameter("@BWQID", SqlDbType.VarChar, 10)
                            {
                                Value = BatchIdOutput.ToString()
                            });
                            // output params
                            SqlParameter Results = new SqlParameter("@Results", SqlDbType.VarChar, 500);
                            Results.Direction = ParameterDirection.Output;
                            sqlCmd.Parameters.Add(Results);


                            if (sqlCmd.Connection.State != ConnectionState.Open)
                            {
                                sqlCmd.Connection.Open();
                            }

                            var execResult = sqlCmd.ExecuteNonQuery();
                            ReturnMessage = Results.Value.ToString();
                        }

                        return(Json(workitemnotcreated + " - " + ReturnMessage));
                    }

                    bwqent.WorkItemID = new Guid(returnGuid.value.workItemGuid);
                }
                ;

                retValue = _context.SaveData();

                if (retValue.Message != "Success")
                {
                    return(Json(retValue));
                }                          // return error if workitemid was not updated for this bwq entity
            }
            catch (Exception e)
            {
                return(BadRequest(new { batch = BatchIdOutput, message = e.Message }));
            }
            #endregion

            return(Ok(new { batch = BatchIdOutput, message = ReturnMessage }));
            //return Ok();
        }
Esempio n. 3
0
        public object CreateNewBatch(NewBatchObject newbatch, IConfiguration configuration)
        {
            bool DataValid = ValidateBatchData(newbatch, configuration);

            if (DataValid == false)
            {
                return(null);
            }

            InstructionsCollection InstructionList = new InstructionsCollection();

            foreach (var ins in newbatch.Instructions)
            {
                InstructionList.Add(ins);
            }

            EntitiesCollection EntitiesList = new EntitiesCollection();

            foreach (var ent in newbatch.Ent_IDs)
            {
                EntitiesList.Add(ent);
            }

            int BatchIdOutput = 0;  // Will contain the ID of the created BWQ entry

            #region  Save Editorial Data

            using (var sqlCmd = _context.Database.GetDbConnection().CreateCommand())
            {
                string sp = "usp_CreateBatch_ins";

                sqlCmd.CommandText = sp;
                sqlCmd.CommandType = CommandType.StoredProcedure;

                sqlCmd.Parameters.Add(new SqlParameter("@BatchName", SqlDbType.VarChar, 500)
                {
                    Value = newbatch.BatchName
                });
                sqlCmd.Parameters.Add(new SqlParameter("@Description", SqlDbType.VarChar, 1000)
                {
                    Value = newbatch.Description
                });
                sqlCmd.Parameters.Add(new SqlParameter("@StartDate", SqlDbType.Date)
                {
                    Value = newbatch.StartDate
                });
                sqlCmd.Parameters.Add(new SqlParameter("@DueDate", SqlDbType.Date)
                {
                    Value = newbatch.DueDate
                });
                sqlCmd.Parameters.Add(new SqlParameter("@PriorityCollectionItemID", SqlDbType.Int)
                {
                    Value = newbatch.PriorityCollectionItemID
                });
                sqlCmd.Parameters.Add(new SqlParameter("@StatusCollectionItemID", SqlDbType.Int)
                {
                    Value = newbatch.StatusCollectionItemID
                });

                sqlCmd.Parameters.Add(new SqlParameter("@Instructions", SqlDbType.Structured)
                {
                    Value = InstructionList
                });
                sqlCmd.Parameters.Add(new SqlParameter("@Ent_IDs", SqlDbType.Structured)
                {
                    Value = EntitiesList
                });

                sqlCmd.Parameters.Add(new SqlParameter("@Username", SqlDbType.VarChar, 50)
                {
                    Value = newbatch.Username
                });
                sqlCmd.Parameters.Add(new SqlParameter("@OriginalCount", SqlDbType.Int)
                {
                    Value = newbatch.OriginalCount
                });
                sqlCmd.Parameters.Add(new SqlParameter("@Operation", SqlDbType.VarChar, 500)
                {
                    Value = newbatch.Operation
                });

                // output params
                SqlParameter Results  = new SqlParameter("@Results", SqlDbType.VarChar, 500);
                SqlParameter NewBWQID = new SqlParameter("@NewBWQID", SqlDbType.Int);

                Results.Direction  = ParameterDirection.Output;
                NewBWQID.Direction = ParameterDirection.Output;

                sqlCmd.Parameters.Add(Results);
                sqlCmd.Parameters.Add(NewBWQID);

                if (sqlCmd.Connection.State != ConnectionState.Open)
                {
                    sqlCmd.Connection.Open();
                }

                var execResult = sqlCmd.ExecuteNonQuery();

                if (NewBWQID.Value.ToString() == "") // failed to save Editorial Data
                {
                    return(null);
                }

                BatchIdOutput = Convert.ToInt32(NewBWQID.Value);
            }
            #endregion



            #region Human Review Entries

            // Once the batch is created in Editorial we need to Update the entries with WorkItem Guids from Human Review

            var BWQEntities = _context.BWQEntities.Where(t => t.BWQID == BatchIdOutput);
            var QueueGuid   = (from module in _context.ApplicationModules
                               where module.ModuleName == EditorialModules.BWQ
                               select module.QueueGuid).FirstOrDefault();

            int WorkItemCount = 0;

            // Loop the Entities for this BWQID
            foreach (var bwqentity in BWQEntities)
            {
                var bwqInstruction = _context.BWQInstructions
                                     .Where(t => t.BWQEntitiesID == bwqentity.BWQEntitiesID);

                // Serialize the corresponding Instruction for the BWQ Entity
                var JsonData = JsonConvert.SerializeObject(bwqInstruction);

                var QueueGuidString = QueueGuid.ToString();

                var HRCreateRequest = Helper.BuildHRWorkItemRequest(EditorialModules.BWQ,
                                                                    QueueGuidString, JsonData,
                                                                    configuration, null, HRRequestMode.Create);

                var GuidResult = Helper.GetHRWorkItem((WorkItemRequest)HRCreateRequest, newbatch.HRToken, configuration);

                if (GuidResult.Value.workItemGuid == null) // Delete the BWQ Entries above if Human Review WorkItems were not created
                {
                    using (var sqlCmd = _context.Database.GetDbConnection().CreateCommand())
                    {
                        string sp = "usp_DeleteBatch_del";

                        sqlCmd.CommandText = sp;
                        sqlCmd.CommandType = CommandType.StoredProcedure;
                        sqlCmd.Parameters.Add(new SqlParameter("@BWQID", SqlDbType.VarChar, 10)
                        {
                            Value = BatchIdOutput.ToString()
                        });
                        // output params
                        SqlParameter Results = new SqlParameter("@Results", SqlDbType.VarChar, 500)
                        {
                            Direction = ParameterDirection.Output
                        };
                        sqlCmd.Parameters.Add(Results);

                        if (sqlCmd.Connection.State != ConnectionState.Open)
                        {
                            sqlCmd.Connection.Open();
                        }

                        var execResult = sqlCmd.ExecuteNonQuery();

                        return(null);
                    }
                }

                bwqentity.WorkItemID = new Guid(GuidResult.Value.workItemGuid);
                WorkItemCount       += 1;
            }

            if (WorkItemCount > 0)
            {
                _context.SaveChanges();
            }

            #endregion

            return(BatchIdOutput);
        }
Esempio n. 4
0
 public Project()
 {
     Context =  new RestController();
     Instructions = new InstructionsCollection();
 }
 public void Close()
 {
     InstructionsCollection.Clear();
 }
 public void AddInstruction()
 {
     InstructionsCollection.Add(new Instruction());
 }