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 <InRiverServiceResult> CheckSQLForItems(IEnumerable <InRiverProcessorItem> items, InRiverDestination dest)
        {
            InRiverServiceResult sr = new InRiverServiceResult();

            IEnumerable <Task> tasks = from InRiverProcessorItem in items select CheckItem(InRiverProcessorItem, dest);

            await Task.WhenAll(tasks);

            return(sr);
        }
        public static async Task <InRiverServiceResult> ProcessProductsAll()
        {
            //only handles stock numbers

            string filename = Guid.NewGuid().ToString() + ".xlsx";

            InRiverServiceResult sr = new InRiverServiceResult();

            sr.ProcessTask = "InRiver.ProcessProductsAll";

            //Get headers and related data fields
            List <CommonLookup> headerfields = Task.Run(() => CommonLookup.GetLookups(PRODUCT_TEMPLATE, true)).Result;

            //get data from view dbo.usp_InRiverProductTemplate


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

                SqlDataReader reader = await GetEntityProductTemplateData(connection, null);

                XLWorkbook workbook = null;

                workbook = await BuildWorkbook(headerfields, reader, connection, filename);

                reader.Close();

                Boolean fileCopied = await CopyToDestination(filename, InRiverDestination.Products);

                if (fileCopied)
                {
                    sr.LastProcessed = sr.Processed;
                    sr.FileName      = filename;
                    sr.LogDate       = DateTime.Now;
                }

                sr.Save(connection);
            }

            return(sr);
        }
        private static async Task <InRiverServiceResult> ProcessStockNumberUpdates(IEnumerable <InRiverProcessorItem> items)
        {
            InRiverServiceResult sr = new InRiverServiceResult();

            string filename = Guid.NewGuid().ToString() + ".xlsx";

            //Get headers and related data fields
            List <CommonLookup> headerfields = CommonLookup.GetLookups(PRODUCT_TEMPLATE, true);

            //get data from view dbo.usp_InRiverProductTemplate
            XLWorkbook workbook = null;

            using (SqlConnection connection = new SqlConnection(APIModels.Data.ConnectionManager.ProductDataHubConnectionString))
            {
                SqlDataReader reader = await GetEntityProductTemplateData(connection, items);

                workbook = await BuildWorkbook(headerfields, reader, connection, filename);

                reader.Close();

                Boolean fileCopied = await CopyToDestination(filename, InRiverDestination.Products);

                if (fileCopied)
                {
                    await UpdateState(items, InRiverState.Complete);

                    sr.LastProcessed = sr.Processed;
                    sr.FileName      = filename;
                    sr.LogDate       = DateTime.Now;
                }

                sr.Save(connection);
            }


            await UpdateState(items, InRiverState.Complete);

            return(sr);
        }
        private static async Task <InRiverServiceResult> ProcessPartUpdates(IEnumerable <InRiverProcessorItem> items)
        {
            InRiverServiceResult sr = new InRiverServiceResult();

            string filename = Guid.NewGuid().ToString() + ".xlsx";

            sr.ProcessTask = "InRiver.ProcessPartsUpdates";

            //Get headers and related data fields
            List <CommonLookup> headerfields = Task.Run(() => CommonLookup.GetLookups(ITEM_TEMPLATE, true)).Result;

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

                SqlDataReader reader = await GetEntityItemTemplateData(connection, null);

                XLWorkbook workbook = null;

                workbook = await BuildWorkbook(headerfields, reader, connection, filename);

                reader.Close();

                Boolean fileCopied = await CopyToDestination(filename, InRiverDestination.Parts);

                if (fileCopied)
                {
                    await UpdateState(items, InRiverState.Complete);

                    sr.LastProcessed = sr.Processed;
                    sr.FileName      = filename;
                    sr.LogDate       = DateTime.Now;
                }

                sr.Save(connection);
            }

            return(sr);
        }
        private static async Task <InRiverServiceResult> ProcessDataUpdates(IEnumerable <InRiverProcessorItem> items, InRiverDestination dest)
        {
            InRiverServiceResult sr = new InRiverServiceResult();

            if (items == null || !items.Any())
            {
                return(sr);
            }

            string filename = Guid.NewGuid().ToString() + ".xlsx";

            string template = PRODUCT_TEMPLATE;

            //set defaults based on dest type
            switch (dest)
            {
            case InRiverDestination.Products:
                sr.ProcessTask = "InRiver.ProcessStockNoUpdates";
                template       = PRODUCT_TEMPLATE;
                break;

            case InRiverDestination.Parts:
                sr.ProcessTask = "InRiver.ProcessPartsUpdates";
                template       = ITEM_TEMPLATE;
                break;

            case InRiverDestination.StockPartRelationship:
                sr.ProcessTask = "InRiver.ProcessRelationships";
                template       = RELATIONSHIP_TEMPLATE;
                break;

            default:
                sr.ProcessTask = "InRiver.ProcessStockNoUpdates";
                template       = PRODUCT_TEMPLATE;
                break;
            }

            //Get headers and related data fields
            List <CommonLookup> headerfields = Task.Run(() => CommonLookup.GetLookups(template, true)).Result;

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

                Object readerObject = null;

                if (dest == InRiverDestination.Products)
                {
                    readerObject = await GetEntityProductTemplateData(connection, items);
                }

                if (dest == InRiverDestination.Parts)
                {
                    readerObject = await GetEntityItemTemplateData(connection, items);
                }

                if (dest == InRiverDestination.StockPartRelationship)
                {
                    readerObject = await GetEntityRelationshipTemplateData(connection, items);
                }

                if (readerObject != null)
                {
                    SqlDataReader reader = (SqlDataReader)readerObject;

                    XLWorkbook workbook = null;

                    workbook = await BuildWorkbook(headerfields, reader, connection, filename);

                    reader.Close();

                    Boolean fileCopied = await CopyToDestination(filename, dest);

                    if (fileCopied)
                    {
                        await UpdateState(items, InRiverState.Complete);

                        sr.LastProcessed = sr.Processed;
                        sr.FileName      = filename;
                        sr.LogDate       = DateTime.Now;
                        sr.ProcessStatus = "Finished";
                        await UpdateState(items, InRiverState.Complete);
                    }
                    else
                    {
                        sr.FileName      = filename;
                        sr.LogDate       = DateTime.Now;
                        sr.ProcessStatus = "Failure";
                        sr.ErrorMessage  = "File Not Copied";
                        await UpdateState(items, InRiverState.Failure);
                    }

                    sr.Save(connection);
                }
            }

            return(sr);
        }