Exemple #1
0
        public static void Main(string[] args)
        {
            string msg = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));

            logger.Info(msg);
            logger.Info("-------------------------------------------------------");
            try
            {
                List <WineDetails> WineList = new List <WineDetails>();
                string             str      = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
                int       maxEnoID          = 0;
                int       maxPPId           = 0;
                DataTable dt = new DataTable();
                using (SqlConnection con = new SqlConnection(str))
                {
                    using (SqlCommand cmd = new SqlCommand("CheckMaxWineId", con))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Connection  = con;
                        con.Open();
                        logger.Info("Connection opened");
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        DataSet        ds = new DataSet();
                        da.Fill(ds);
                        logger.Info("Dataset obtained");
                        if (ds != null && ds.Tables.Count > 0)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                maxEnoID = Convert.ToInt32(ds.Tables[2].Rows[0]["MaxId"]);
                            }
                            if (ds.Tables[1].Rows.Count > 0)
                            {
                                maxPPId = Convert.ToInt32(ds.Tables[3].Rows[0]["MaxId"]);
                            }

                            dt = ds.Tables[0];
                            dt.Merge(ds.Tables[1]);

                            if (dt.Rows.Count > 0)
                            {
                                foreach (DataRow dr in dt.Rows)
                                {
                                    WineDetails WineObj = new WineDetails();
                                    WineObj.WineId   = Convert.ToInt32(dr["WineId"]);
                                    WineObj.WineName = dr["WineName"].ToString();
                                    WineObj.Vintage  = dr["Vintage"].ToString();
                                    WineObj.Store    = Convert.ToInt32(dr["store"]);
                                    WineList.Add(WineObj);
                                }
                                con.Close();
                            }
                        }
                    }
                }

                Program p       = new Program();
                int     success = 0;
                foreach (WineDetails obj in WineList)
                {
                    Image img = p.GetFile(obj.WineName, obj.Vintage);
                    logger.Info("Obtained Image for " + obj.WineName + ". Uploading Image..");
                    success = p.UploadImage(img, obj.WineId, obj.Store);
                }

                if (WineList.Count > 0)
                {
                    using (SqlConnection con = new SqlConnection(str))
                    {
                        if (maxEnoID > 0)
                        {
                            using (SqlCommand cmd = new SqlCommand("update updateWine set MaxWineID=@wineId where storeId = 1", con))
                            {
                                cmd.Parameters.AddWithValue("@wineId", maxEnoID);
                                cmd.Connection = con;
                                con.Open();
                                logger.Info("Updating maximum wine id for Wall DB");
                                cmd.ExecuteNonQuery();
                                con.Close();
                            }
                        }
                        if (maxPPId > 0)
                        {
                            using (SqlCommand cmd = new SqlCommand("update updateWine set MaxWineID=@wineId where storeId = 2", con))
                            {
                                cmd.Parameters.AddWithValue("@wineId", maxPPId);
                                cmd.Connection = con;
                                con.Open();
                                logger.Info("Updating maximum wine id for Point pleasent DB");
                                cmd.ExecuteNonQuery();
                                con.Close();
                            }
                        }
                    }
                }

                p.getImagesFromDrive();
            }
            catch (Exception ex)
            {
                string path    = ConfigurationManager.AppSettings["ErrorLog"];
                string message = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));
                message += Environment.NewLine;
                message += "-----------------------------------------------------------";
                message += Environment.NewLine;
                message += string.Format("Message: {0}", ex.Message);
                message += Environment.NewLine;
                message += string.Format("StackTrace: {0}", ex.StackTrace);
                message += Environment.NewLine;
                message += string.Format("Source: {0}", ex.Source);
                message += Environment.NewLine;
                message += string.Format("TargetSite: {0}", ex.TargetSite.ToString());
                message += Environment.NewLine;
                message += "-----------------------------------------------------------";
                message += Environment.NewLine;
                System.IO.Directory.CreateDirectory(path);
                using (StreamWriter writer = new StreamWriter(path + "Error.txt", true))
                {
                    writer.WriteLine(message);
                    writer.Close();
                }
            }
        }
        public static void Main(string[] args)
        {
            string msg = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));

            logger.Info(msg);
            logger.Info("-------------------------------------------------------");
            try
            {
                List <WineDetails> WineList = new List <WineDetails>();
                string             str      = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
                int       maxEnoID          = 0;
                int       maxPPId           = 0;
                DataTable dt = new DataTable();
                using (SqlConnection con = new SqlConnection(str))
                {
                    using (SqlCommand cmd = new SqlCommand("CheckMaxWineIdAlt", con)) //Stored Procedure that brings the new wines from both the clients
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Connection  = con;
                        con.Open();
                        logger.Info("Connection opened");
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        DataSet        ds = new DataSet();
                        da.Fill(ds);
                        logger.Info("Dataset obtained");
                        if (ds != null && ds.Tables.Count > 0)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                maxEnoID = Convert.ToInt32(ds.Tables[2].Rows[0]["MaxId"]);
                            }
                            if (ds.Tables[1].Rows.Count > 0)
                            {
                                maxPPId = Convert.ToInt32(ds.Tables[3].Rows[0]["MaxId"]);
                            }

                            dt = ds.Tables[0];
                            dt.Merge(ds.Tables[1]);

                            if (dt.Rows.Count > 0)
                            {
                                foreach (DataRow dr in dt.Rows)
                                {
                                    WineDetails WineObj = new WineDetails();
                                    WineObj.BarCode = dr["BarCode"].ToString();
                                    //  WineObj.WineId = Convert.ToInt32(dr["WineId"]);
                                    WineObj.WineName = dr["WineName"].ToString();
                                    WineObj.Vintage  = dr["Vintage"].ToString();
                                    WineObj.Store    = Convert.ToInt32(dr["store"]);
                                    WineList.Add(WineObj);
                                }
                                con.Close();
                            }
                        }
                    }
                }

                Program p       = new Program();
                int     success = 0;
                foreach (WineDetails obj in WineList)
                {
                    string Wine    = obj.WineName.Replace("&", " and ");
                    string locName = "";

                    //Changing the names for location Wines
                    if (Wine.Contains("Location"))
                    {
                        for (int i = 8; i < Wine.Length; i++)
                        {
                            if ((Wine[i] > 96 && Wine[i] <= 122) || (Wine[i] >= 65 && Wine[i] <= 90))
                            {
                                locName = "Location " + Wine[i];
                                i++;
                                while (i < Wine.Length)
                                {
                                    if ((Wine[i] > 96 && Wine[i] <= 122) || (Wine[i] >= 65 && Wine[i] <= 90))
                                    {
                                        locName += Wine[i];
                                        i++;
                                    }
                                    else
                                    {
                                        locName += " ";
                                        i++;
                                    }
                                }
                                break;
                            }
                        }
                    }
                    Image img;
                    if (locName != "")
                    {
                        Wine = locName.Trim();
                    }
                    if (Wine.Contains("Location"))
                    {
                        img = p.getLocationWineImages(Wine); //Function to get Images for location Wines
                    }
                    else
                    {
                        img = p.GetFile(Wine, obj.Vintage);    //Function to get get Images for non location Wines
                    }
                    //img = p.getLocationWineImages(Wine);
                    logger.Info("Obtained Image for " + obj.WineName + ". Uploading Image..");

                    //Function that uploads the images to the blob
                    success = p.UploadImage(img, obj.BarCode, obj.Store);
                }

                if (WineList.Count > 0)
                {
                    using (SqlConnection con = new SqlConnection(str))
                    {
                        //Updates the max wine id for wall client
                        if (maxEnoID > 0)
                        {
                            using (SqlCommand cmd = new SqlCommand("update updateWine set MaxWineID=@wineId where storeId = 1", con))
                            {
                                cmd.Parameters.AddWithValue("@wineId", maxEnoID);
                                cmd.Connection = con;
                                con.Open();
                                logger.Info("Updating maximum wine id for Wall DB");
                                cmd.ExecuteNonQuery();
                                con.Close();
                            }
                        }
                        //Updates the max wine id for PointPleasant client
                        if (maxPPId > 0)
                        {
                            using (SqlCommand cmd = new SqlCommand("update updateWine set MaxWineID=@wineId where storeId = 2", con))
                            {
                                cmd.Parameters.AddWithValue("@wineId", maxPPId);
                                cmd.Connection = con;
                                con.Open();
                                logger.Info("Updating maximum wine id for Point pleasent DB");
                                cmd.ExecuteNonQuery();
                                con.Close();
                            }
                        }
                    }
                    //Inserts the winedetails to newly inserted wines
                    p.InsertWineDetails(WineList);
                }
                //Gets and uploads images to blob from google drive
                p.getImagesFromDrive();
            }
            catch (Exception ex)
            {
                string path    = ConfigurationManager.AppSettings["ErrorLog"];
                string message = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));
                message += Environment.NewLine;
                message += "-----------------------------------------------------------";
                message += Environment.NewLine;
                message += string.Format("Message: {0}", ex.Message);
                message += Environment.NewLine;
                message += string.Format("StackTrace: {0}", ex.StackTrace);
                message += Environment.NewLine;
                message += string.Format("Source: {0}", ex.Source);
                message += Environment.NewLine;
                message += string.Format("TargetSite: {0}", ex.TargetSite.ToString());
                message += Environment.NewLine;
                message += "-----------------------------------------------------------";
                message += Environment.NewLine;
                System.IO.Directory.CreateDirectory(path);
                using (StreamWriter writer = new StreamWriter(path + "Error.txt", true))
                {
                    writer.WriteLine(message);
                    writer.Close();
                }
            }
        }