public static async Task <InRiverServiceResult> ProcessUpdates()
        {
            InRiverServiceResult sr = new InRiverServiceResult();

            await Task.Run(() =>
            {
                ClearExistingFiles();
            });

            var unprocessed = await InRiverProcessorItem.GetUnprocessed();

            var unprocessedStockNumbers = unprocessed.Where(o => o.CompanyID == 9);

            var unprocessedParts = unprocessed.Where(o => o.CompanyID != 9);

            await CheckSQLForItems(unprocessedStockNumbers, InRiverDestination.Products);

            await CheckSQLForItems(unprocessedParts, InRiverDestination.Parts);

            await UpdateState(unprocessed, InRiverState.InProcess);

            await ProcessDataUpdates(unprocessedStockNumbers, InRiverDestination.Products);

            await ProcessDataUpdates(unprocessedParts, InRiverDestination.Parts);

            //Process Relationships for any Parts
            await ProcessDataUpdates(unprocessedParts, InRiverDestination.StockPartRelationship);

            return(sr);
        }
        private static async Task CheckItem(InRiverProcessorItem item, InRiverDestination dest)
        {
            string sql = "";

            switch (dest)
            {
            case InRiverDestination.Products:
                sql = String.Format("Select count(StockNo) from ProductMaster where StockNo='{0}'", item.StockNoOrPart);
                break;

            case InRiverDestination.Parts:
                sql = String.Format("Select count(Part) from Part where CompanyId={0} and Part='{1}'", item.CompanyID, item.StockNoOrPart);
                break;
            }

            using (SqlConnection connection = new SqlConnection(APIModels.Data.ConnectionManager.ProductDataHubConnectionString))
            {
                connection.Open();

                SqlCommand cmd = new SqlCommand(sql, connection);

                object result = await cmd.ExecuteScalarAsync();

                if (result != null)
                {
                    int cnt = 0;
                    int.TryParse(result.ToString(), out cnt);

                    if (cnt == 0)
                    {
                        await Task.Run(() =>
                        {
                            //pull from Unidata to SQL
                            if (dest == InRiverDestination.Products)
                            {
                                Console.WriteLine("Adding Product " + item.StockNoOrPart);

                                ProductMaster pm = new ProductMaster(item.StockNoOrPart);

                                pm.Save(APIModels.Data.ConnectionManager.ProductDataHubConnectionString);
                            }
                            else
                            {
                                Console.WriteLine("Adding Part " + item.StockNoOrPart);

                                SellingPart sp = new SellingPart(item.CompanyID, item.StockNoOrPart);

                                sp.SaveSQL(APIModels.Data.ConnectionManager.ProductDataHubConnectionString);
                            }
                        });
                    }
                }
            }

            return;
        }
        public static async Task UpdateState(IEnumerable <InRiverProcessorItem> unprocessed, InRiverState state)
        {
            unprocessed.ForEach(o => o.InRiverState = state);

            IEnumerable <Task <long> > tasks = from InRiverProcessorItem in unprocessed select InRiverProcessorItem.SaveAsync();

            await Task.WhenAll(tasks);
        }
 public static async Task <long> PushItemForUpdate(int companyid, string item)
 {
     return(await InRiverProcessorItem.PushItemForUpdate(companyid, item));
 }