private static async Task <bool> CopyToDestination(string filename, InRiverDestination dest)
        {
            if (!File.Exists(filename))
            {
                return(false);
            }

            Boolean result = false;

            string dir = await GetDestinationDirectory(dest);

            try
            {
                File.Copy(filename, dir + filename);
            }
            catch (UnauthorizedAccessException ex1)
            {
                return(result);
            }
            catch (DirectoryNotFoundException ex2)
            {
                return(result);
            }
            catch (Exception ex3)
            {
                return(result);
            }

            result = true;

            return(result);
        }
        public static async Task <string> GetDestinationDirectory(InRiverDestination dest)
        {
            string destDirectory = "";
            await Task.Run(() =>
            {
                //get destination folder
                List <CommonLookup> opts = CommonLookup.GetLookups(INRIVER_DESTINATIONS, true);

                switch (dest)
                {
                case InRiverDestination.Products:
                    destDirectory = opts.Where(o => o.Code == "Products").FirstOrDefault().CodeDesc;
                    break;

                case InRiverDestination.Parts:
                    destDirectory = opts.Where(o => o.Code == "Parts").FirstOrDefault().CodeDesc;
                    break;

                case InRiverDestination.StockPartRelationship:
                    destDirectory = opts.Where(o => o.Code == "StockPartRelation").FirstOrDefault().CodeDesc;
                    break;

                default:
                    destDirectory = opts.Where(o => o.Code == "Products").FirstOrDefault().CodeDesc;
                    break;
                }
            });

            return(destDirectory);
        }
        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;
        }
        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);
        }
        private static async Task <SqlDataReader> GetEntityData(SqlConnection connection, IEnumerable <InRiverProcessorItem> items, InRiverDestination dest)
        {
            SqlDataReader reader;

            string view = "select * from vw_InRiverItemTemplate";

            if (items != null)
            {
                string[] parts = items.Select(o => "'" + o.StockNoOrPart + "',").ToArray();
                view = view + " Where Part in (";
                var w = string.Concat(parts);
                w    = w.Substring(0, w.Length - 2);
                view = view + w;
                view = view + ") ";
            }

            SqlCommand cmd = new SqlCommand(view, connection);

            reader = await cmd.ExecuteReaderAsync();

            return(reader);
        }
        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);
        }