コード例 #1
0
        public static void Import(out int count)
        {
            using (StreamWriter writer = new StreamWriter(@"C:\HCCCustomerOutput.txt"))
            {
                Console.SetOut(writer);

                count = 0;
                List <string> errorReport = new List <string>();

                try
                {
                    List <ImportedCustomer> impCusts = GetAll();

                    foreach (ImportedCustomer impCust in impCusts)
                    {
                        Console.WriteLine(count + " : ");
                        if (!impCust.IsValid)
                        {
                            impCust.Email = "admin" + count.ToString() + "@healthychefcreations.com";
                        }
                        else if (impCust.Email.Contains("info@healthychef") || impCust.Email.Contains("thehealthyassistant@earthlink"))
                        {
                            impCust.Email = "admin" + count.ToString() + "@healthychefcreations.com";
                        }

                        Console.WriteLine(impCust.Email);

                        if (impCust.IsValid)
                        {
                            count++;

                            string         userName    = impCust.Email.Trim().Split('@')[0] + DateTime.Now.ToString("yyyyMMddHHmmtt");
                            string         password    = OrderNumberGenerator.GenerateOrderNumber("?#?#?#?#");
                            string         aspUserName = Membership.GetUserNameByEmail(impCust.Email.Trim());
                            MembershipUser newUser     = null;

                            if (!string.IsNullOrWhiteSpace(aspUserName))
                            {
                                newUser = Membership.GetUser(aspUserName);
                            }

                            MembershipCreateStatus createResult = MembershipCreateStatus.UserRejected;

                            if (newUser == null)
                            {
                                newUser = Membership.CreateUser(userName, password, impCust.Email.Trim(), "import", "import", true, out createResult);

                                if (newUser != null)
                                {
                                    Console.WriteLine(newUser.UserName + "New user.");
                                }
                            }
                            else
                            {
                                Console.WriteLine(newUser.UserName + " Existing user.");
                                createResult = MembershipCreateStatus.Success;
                            }

                            if (newUser != null)
                            {
                                if (createResult == MembershipCreateStatus.Success)
                                {
                                    //Assign Customer role to newUser
                                    try
                                    {
                                        if (!Roles.IsUserInRole(newUser.UserName, "Customer"))
                                        {
                                            Roles.AddUserToRole(newUser.UserName, "Customer");
                                            Console.WriteLine(newUser.UserName + " Role assigned.");
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(newUser.UserName + " =Assign role failed." + ex.Message + ex.StackTrace);
                                    }
                                    //Send E-mail notification to account user
                                    //HealthyChef.Email.EmailController ec = new HealthyChef.Email.EmailController();
                                    //ec.SendMail_NewUserConfirmation(email, password);

                                    //Create a Healthy Chef profile for this new user
                                    hccUserProfile newProfile = hccUserProfile.GetBy((Guid)newUser.ProviderUserKey).SingleOrDefault(a => !a.ParentProfileID.HasValue);

                                    if (newProfile == null)
                                    {
                                        try
                                        {
                                            newProfile = new hccUserProfile
                                            {
                                                MembershipID   = (Guid)newUser.ProviderUserKey,
                                                CreatedBy      = (Membership.GetUser() == null ? Guid.Empty : (Guid)Membership.GetUser().ProviderUserKey),
                                                CreatedDate    = DateTime.Now,
                                                AccountBalance = 0.00m,
                                                IsActive       = true,
                                                FirstName      = impCust.FirstName.Trim(),
                                                LastName       = impCust.LastName.Trim(),
                                                ProfileName    = impCust.FirstName.Trim()
                                            };

                                            //Save all hccProfile information
                                            using (var cont = new healthychefEntities())
                                            {
                                                System.Data.EntityKey key = cont.CreateEntityKey("hccUserProfiles", newProfile);
                                                object oldObj;

                                                if (cont.TryGetObjectByKey(key, out oldObj))
                                                {
                                                    cont.ApplyCurrentValues("hccUserProfiles", newProfile);
                                                }
                                                else
                                                {
                                                    cont.hccUserProfiles.AddObject(newProfile);
                                                }

                                                cont.SaveChanges();
                                            }
                                            //cont.Refresh(System.Data.Objects.RefreshMode.StoreWins, newProfile);
                                            Console.WriteLine(newUser.UserName + " New profile.");
                                        }
                                        catch (Exception)
                                        {
                                            Console.WriteLine("=" + newUser.UserName + " Save Profile failed.");
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine(newUser.UserName + " Existing profile.");
                                        createResult = MembershipCreateStatus.Success;
                                    }

                                    if (newProfile != null && newProfile.UserProfileID > 0)
                                    {
                                        if (impCust.OtherDeliveryInfo != null && !string.IsNullOrWhiteSpace(impCust.OtherDeliveryInfo))
                                        {
                                            hccUserProfileNote shipNote = new hccUserProfileNote
                                            {
                                                DateCreated   = DateTime.Now,
                                                DisplayToUser = false,
                                                UserProfileID = newProfile.UserProfileID,
                                                IsActive      = true,
                                                Note          = impCust.OtherDeliveryInfo,
                                                NoteTypeID    = (int)Enums.UserProfileNoteTypes.ShippingNote
                                            };

                                            using (var cont = new healthychefEntities())
                                            {
                                                EntityKey key          = cont.CreateEntityKey("hccUserProfileNotes", shipNote);
                                                object    originalItem = null;

                                                if (cont.TryGetObjectByKey(key, out originalItem))
                                                {
                                                    cont.ApplyCurrentValues(key.EntitySetName, shipNote);
                                                }
                                                else
                                                {
                                                    cont.hccUserProfileNotes.AddObject(shipNote);
                                                }

                                                cont.SaveChanges();
                                            }
                                        }

                                        if (impCust.HowDidYouHear != null && !string.IsNullOrWhiteSpace(impCust.HowDidYouHear))
                                        {
                                            hccUserProfileNote hearNote = new hccUserProfileNote
                                            {
                                                DateCreated   = DateTime.Now,
                                                DisplayToUser = false,
                                                UserProfileID = newProfile.UserProfileID,
                                                IsActive      = true,
                                                Note          = impCust.HowDidYouHear,
                                                NoteTypeID    = (int)Enums.UserProfileNoteTypes.GeneralNote
                                            };

                                            using (var cont = new healthychefEntities())
                                            {
                                                EntityKey key          = cont.CreateEntityKey("hccUserProfileNotes", hearNote);
                                                object    originalItem = null;

                                                if (cont.TryGetObjectByKey(key, out originalItem))
                                                {
                                                    cont.ApplyCurrentValues(key.EntitySetName, hearNote);
                                                }
                                                else
                                                {
                                                    cont.hccUserProfileNotes.AddObject(hearNote);
                                                }

                                                cont.SaveChanges();
                                            }
                                        }

                                        try
                                        {
                                            //save Shipping Address
                                            hccAddress shipAddr = null;

                                            if (newProfile.ShippingAddressID.HasValue)
                                            {
                                                shipAddr = hccAddress.GetById(newProfile.ShippingAddressID.Value);
                                            }

                                            if (shipAddr != null)
                                            {
                                                try
                                                {
                                                    newProfile.ShippingAddressID = null;
                                                    //Save all hccProfile information
                                                    using (var cont = new healthychefEntities())
                                                    {
                                                        System.Data.EntityKey key1 = cont.CreateEntityKey("hccUserProfiles", newProfile);
                                                        object oldObj1;

                                                        if (cont.TryGetObjectByKey(key1, out oldObj1))
                                                        {
                                                            cont.ApplyCurrentValues("hccUserProfiles", newProfile);
                                                        }
                                                        else
                                                        {
                                                            cont.hccUserProfiles.AddObject(newProfile);
                                                        }
                                                        cont.SaveChanges();
                                                    }
                                                    //cont.Refresh(System.Data.Objects.RefreshMode.StoreWins, newProfile);
                                                    using (var cont = new healthychefEntities())
                                                    {
                                                        EntityKey key          = cont.CreateEntityKey("hccAddresses", shipAddr);
                                                        object    originalItem = null;

                                                        if (cont.TryGetObjectByKey(key, out originalItem))
                                                        {
                                                            cont.AttachTo(shipAddr.EntityKey.EntitySetName, shipAddr);
                                                            cont.DeleteObject(shipAddr);
                                                        }
                                                        cont.SaveChanges();
                                                    }

                                                    shipAddr = null;
                                                }
                                                catch (Exception ex)
                                                {
                                                    Console.WriteLine(newUser.UserName + " =Delete old shipping address failed." + ex.Message + ex.StackTrace);
                                                }
                                            }

                                            if (shipAddr == null)
                                            {
                                                shipAddr = new hccAddress();
                                            }

                                            if (impCust.ShippingAddress1 != null)
                                            {
                                                shipAddr.Address1 = (string.IsNullOrWhiteSpace(impCust.ShippingAddress1) ? "" : impCust.ShippingAddress1.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.Address1 = "";
                                            }

                                            if (impCust.ShippingAddress2 != null)
                                            {
                                                shipAddr.Address2 = (string.IsNullOrWhiteSpace(impCust.ShippingAddress2) ? "" : impCust.ShippingAddress2.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.Address2 = "";
                                            }

                                            if (impCust.ShippingAddress3 != null)
                                            {
                                                shipAddr.Address2 += " " + (string.IsNullOrWhiteSpace(impCust.ShippingAddress3) ? "" : impCust.ShippingAddress3.Trim());
                                            }

                                            shipAddr.AddressTypeID = (int)Enums.AddressType.Shipping;

                                            if (impCust.ShippingCity != null)
                                            {
                                                shipAddr.City = (string.IsNullOrWhiteSpace(impCust.ShippingCity) ? "" : impCust.ShippingCity.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.City = "";
                                            }

                                            shipAddr.Country = "US";

                                            if (impCust.FirstName != null)
                                            {
                                                shipAddr.FirstName = (string.IsNullOrWhiteSpace(impCust.FirstName) ? "" : impCust.FirstName.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.FirstName = "";
                                            }

                                            shipAddr.IsBusiness = false;

                                            if (impCust.LastName != null)
                                            {
                                                shipAddr.LastName = (string.IsNullOrWhiteSpace(impCust.LastName) ? "" : impCust.LastName.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.LastName = "";
                                            }

                                            if (impCust.Phone1 != null)
                                            {
                                                shipAddr.Phone = (string.IsNullOrWhiteSpace(impCust.Phone1) ? "" : impCust.Phone1.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.Phone = "";
                                            }

                                            if (impCust.Phone1Ext != null)
                                            {
                                                shipAddr.Phone += (string.IsNullOrWhiteSpace(impCust.Phone1Ext.Trim()) ? "" : " x" + impCust.Phone1Ext.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.Phone = "";
                                            }

                                            if (impCust.ShippingZipCode != null)
                                            {
                                                shipAddr.PostalCode = (string.IsNullOrWhiteSpace(impCust.ShippingZipCode) ? "" : impCust.ShippingZipCode.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.PostalCode = "";
                                            }

                                            if (impCust.ShippingState != null)
                                            {
                                                shipAddr.State = (string.IsNullOrWhiteSpace(impCust.ShippingState) ? "" : impCust.ShippingState.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.State = "";
                                            }

                                            if (impCust.ShipMethod == null)
                                            {
                                                shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.Delivery;
                                            }
                                            else if (impCust.ShipMethod.Trim() == "F")
                                            {
                                                shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.Delivery;
                                            }
                                            else if (impCust.ShipMethod.Trim() == "P")
                                            {
                                                shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.LocalPickUp;
                                            }
                                            else if (impCust.ShipMethod.Trim() == "D")
                                            {
                                                shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.LocalDelivery;
                                            }
                                            else
                                            {
                                                shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.Delivery;
                                            }

                                            if (shipAddr != null)
                                            {
                                                try
                                                {
                                                    using (var cont = new healthychefEntities())
                                                    {
                                                        EntityKey key          = cont.CreateEntityKey("hccAddresses", shipAddr);
                                                        object    originalItem = null;

                                                        if (cont.TryGetObjectByKey(key, out originalItem))
                                                        {
                                                            cont.hccAddresses.ApplyCurrentValues((hccAddress)originalItem);
                                                        }
                                                        else
                                                        {
                                                            cont.hccAddresses.AddObject(shipAddr);
                                                        }

                                                        cont.SaveChanges();
                                                    }
                                                    //cont.Refresh(System.Data.Objects.RefreshMode.StoreWins, shipAddr);
                                                }
                                                catch (Exception ex)
                                                {
                                                    Console.WriteLine(newUser.UserName + " =Shipping address save failed." + ex.Message + ex.StackTrace);
                                                }
                                            }

                                            if (shipAddr != null && shipAddr.AddressID > 0)
                                            {
                                                newProfile.ShippingAddressID = shipAddr.AddressID;
                                            }
                                            else
                                            {
                                                newProfile.ShippingAddressID = null;
                                            }

                                            using (var cont = new healthychefEntities())
                                            {
                                                System.Data.EntityKey upkey = cont.CreateEntityKey("hccUserProfiles", newProfile);
                                                object oldObj;

                                                if (cont.TryGetObjectByKey(upkey, out oldObj))
                                                {
                                                    cont.ApplyCurrentValues("hccUserProfiles", newProfile);
                                                }
                                                else
                                                {
                                                    cont.hccUserProfiles.AddObject(newProfile);
                                                }

                                                cont.SaveChanges();
                                            }

                                            Console.WriteLine(newUser.UserName + " Shipping address saved.");
                                        }
                                        catch (Exception ex)
                                        {
                                            Console.WriteLine(newUser.UserName + " =Shipping address not created." + ex.Message + ex.StackTrace);
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("User Profile for user: "******" ID not created.");
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("=New user for user: "******" not created.");
                                }
                            }
                            else
                            {
                                Console.WriteLine(createResult.ToString() + " : " + impCust.Email);
                            }
                        }
                        else
                        {
                            count++; Console.WriteLine("=Customer: " + impCust.FirstName + " " + impCust.LastName + " has no email address.");
                        }
                    }
                }
                catch (Exception ex) { Console.WriteLine("=" + ex.Message + " : " + ex.StackTrace); }
            }
        }