/// <summary>
 /// Adds all available transports to a user with address to be filled out later.
 /// Created: 2009-07-28, John Morgan
 /// Modified: 2009-08-13, John Morgan
 /// Altered to only add transports that do not exist
 /// </summary>
 /// <param name="userName"></param>
 /// <returns>UserEntity</returns>
 private UserEntity AddTransportsToUser(string userName)
 {
     CriticalResultsEntityManager manager = new CriticalResultsEntityManager();
     TransportEntity[] transports = manager.GetAllTransports();
     UserEntity user = manager.GetUser(userName);
     foreach (TransportEntity transport in transports)
     {
         bool transportFound = false;
         foreach (UserTransportEntity t in user.UserTransports)
         {
             if (t.Transport.Name == transport.Name)
                 transportFound = true;
         }
         if(!transportFound)
             manager.CreateUserTransport(userName, transport.Name, "");
     }
     return manager.GetUser(userName);
 }
 /// <summary>
 /// Add a transport to all users
 /// Created: 2009-07-27 John Morgan
 /// </summary>
 /// <param name="transportName"></param>
 /// <returns></returns>
 private bool AddUsersToTransport(string transportName)
 {
     CriticalResultsEntityManager manager = new CriticalResultsEntityManager();
     UserEntity[] users = manager.GetAllUsers();
     foreach (UserEntity user in users)
     {
         manager.CreateUserTransport(user.UserName, transportName, "");
     }
     return true;
 }
        public User CreateANCRUser(User user)
        {
            CriticalResultsEntityManager manager = new CriticalResultsEntityManager();
            UserEntity userEntity = manager.CreateUserEntity(user.ToEntity());

            AuditEvent("CreateUser:Success", user.UserName);

            LevelEntity[] levels = manager.GetLevels();
            List<string> levelNames = new List<string>();
            foreach (LevelEntity level in levels)
            {
                levelNames.Add(level.Name);
            }

            TransportEntity[] transports = manager.GetAllTransports();
            foreach (TransportEntity transport in transports)
            {
                bool addTransport = true;
                foreach (UserTransportEntity userTransport in userEntity.UserTransports)
                {
                    if (userTransport.Transport.Name == transport.Name)
                    {
                        addTransport = false;
                    }
                }
                if (addTransport)
                {
                    if (user.Transports != null)
                    {
                        bool transportFound = false;
                        for (int i = 0; i < user.Transports.Length; i++)
                        {
                            if (user.Transports[i].Transport.Name == transport.Name)
                            {
                                if (user.Transports[i].Address == null)
                                {
                                    user.Transports[i].Address = "";
                                }
                                manager.CreateUserTransport(userEntity.UserName, transport.Name, user.Transports[i].Address);
                                manager.UpdateLevelsToUserTransport(user.UserName, transport.Name, user.Transports[i].Address, levelNames.ToArray());
                                transportFound = true;

                            }
                        }
                        if (!transportFound)
                        {
                            manager.CreateUserTransport(userEntity.UserName, transport.Name, "");
                            manager.UpdateLevelsToUserTransport(user.UserName, transport.Name, "", levelNames.ToArray());
                        }
                    }
                    else
                    {
                        manager.CreateUserTransport(userEntity.UserName, transport.Name, "");
                        manager.UpdateLevelsToUserTransport(user.UserName, transport.Name, "", levelNames.ToArray());
                    }
                }
            }

            userEntity = manager.GetUser(user.UserName);

            //bool passwordFound = false;
            //foreach (UserEntryEntity uee in e.UserEntries)
            //{
            //    if (uee.Type == "AuthExt" && uee.Key == "ANCR")
            //        passwordFound = true;
            //}
            //if(!passwordFound)
            //    GeneratePassword(user.UserName);

            User u = new User(userEntity);
            u.ResolveRoles();
            u.ResolveTransports();
            u.ResolveEntries();
            return u;
        }