/// <summary>
        /// Get the Machine Login Credential from the Machine
        /// </summary>
        /// <param name="machine">Machine</param>
        /// <returns>Machine Login Credential</returns>
        public MachineLoginCredential getLogin(CloudWebPortal.Areas.Aneka.Models.Machine machine)
        {
            MachineLoginCredential login = null;
            var logins = db.MachineLoginCredentials.ToList();
            foreach (var loginEntity in logins)
            {
                foreach (var machineEntity in loginEntity.Machines)
                {
                    if (machineEntity.MachineId == machine.MachineId)
                        login = loginEntity;
                }
            }

            return login;
        }
 /// <summary>
 /// Convert the Machine entity from CloudWebPortal.Areas.Aneka.Models.Machine to Aneka.PAL.Management.Model.Machine by passing the Machine Login Credential
 /// </summary>
 /// <param name="machine">CloudWebPortal.Areas.Aneka.Models.Machine</param>
 /// <param name="login">Machine Login Credential</param>
 /// <returns>Aneka.PAL.Management.Model.Machine</returns>
 public global::Aneka.PAL.Management.Model.Machine ToAnekaPALMachineWithLogin(CloudWebPortal.Areas.Aneka.Models.Machine machine, MachineLoginCredential login)
 {
     global::Aneka.PAL.Management.Model.Machine result = new global::Aneka.PAL.Management.Model.Machine(machine.IP);
     result.DaemonUri = ResolveURL(string.Format("tcp://{0}:{1}/daemon", result.Address, machine.Daemon.Port));
     if (login != null)
         result.UserAccount = new global::Aneka.PAL.Management.Model.UserAccount(login.Username, login.Password);
     result.Platform = machine.Platform.Platform;
     result.HomeDirectory = machine.Daemon.Directory + "\\LocalRepository\\Container";
     return result;
 }
        public ActionResult CreatePost(CloudWebPortal.Areas.Aneka.Models.Machine machine, Daemon daemon, bool InstallDaemon, int? ResourcePool, int? LoginCredential, int? PlatformID, int? TypeID, int?[] SoftwareAppliancesList)
        {
            MachinesManagement machineManagment = new MachinesManagement();
            DaemonsManagement daemonManagement = new DaemonsManagement();

            if (!ResourcePool.HasValue)
                ModelState.AddModelError(String.Empty, "Please Select A Resource Pool");

            if (!PlatformID.HasValue)
                ModelState.AddModelError(String.Empty, "Machine Platform Is Requared");
            else
                machine.Platform = db.MachinePlatforms.Find(PlatformID);

            if (!TypeID.HasValue)
                ModelState.AddModelError(String.Empty, "Machine Type Is Requared");
            else
                machine.Type = db.MachineTypes.Find(TypeID);

            if (ModelState.IsValid)
            {
                machine.StatusEnum = DaemonProbeStatus.Unknown;

                //Create Machine and Daemon
                machine.Daemon = daemon;
                db.Machines.Add(machine);
                db.SaveChanges();

                //Add To A Resource Pool
                if (ResourcePool.HasValue)
                {
                    db.ResourcePools.Find(ResourcePool).Machines.Add(machine);
                    db.SaveChanges();
                }

                //Link Login Credential
                MachineLoginCredential login = null;
                if (LoginCredential.HasValue)
                {
                    login = db.MachineLoginCredentials.Find(LoginCredential);
                    login.Machines.Add(machine);
                    db.SaveChanges();
                }

                //Add Software Appliances
                if (SoftwareAppliancesList != null)
                {
                    machine.SoftwareAppliances = new List<SoftwareAppliance>();
                    foreach (int appID in SoftwareAppliancesList)
                    {
                        var app = db.SoftwareAppliances.Find(appID);
                        machine.SoftwareAppliances.Add(app);
                    }
                    db.SaveChanges();
                }

                machine.StatusEnum = machineManagment.UpdateMachineStatus(machine.MachineId, login).Status;

                if (InstallDaemon && machine.StatusEnum == DaemonProbeStatus.ServiceNotInstalled && login != null)
                {
                    UpdateInProgressMessage(machine.MachineId, "Installing Daemon");

                    AsynchronousCallback installingDaemonThread = new AsynchronousCallback(daemonManagement.InstallDaemon);
                    installingDaemonThread.BeginInvoke(machine.MachineId, System.Web.HttpContext.Current.Server.MapPath("~/"), null, null);
                }
                else
                {
                    if (machine.StatusEnum == DaemonProbeStatus.ServiceStarted && login != null)
                    {
                        FetchExistCloud(machine.MachineId);
                    }
                }

                return RedirectToAction("ManageMachinesandResourcePools", "Infrastructure");
            }

            return View("~/Views/Infrastructure/ManageMachinesandResourcePools.cshtml", machine);
        }
 /// <summary>
 /// Convert the Machine entity from CloudWebPortal.Areas.Aneka.Models.Machine to Aneka.PAL.Management.Model.Machine without passing the Machine Login Credential
 /// </summary>
 /// <param name="machine">CloudWebPortal.Areas.Aneka.Models.Machine</param>
 /// <param name="login">Machine Login Credential</param>
 /// <returns>Aneka.PAL.Management.Model.Machine</returns>
 public global::Aneka.PAL.Management.Model.Machine ToAnekaPALMachine(CloudWebPortal.Areas.Aneka.Models.Machine machine)
 {
     return ToAnekaPALMachineWithLogin(machine, getLogin(machine));
 }
        public ActionResult EditPost(CloudWebPortal.Areas.Aneka.Models.Machine machine, Daemon daemon, int? ResourcePool, int? LoginCredential, int? PlatformID, int? TypeID, int?[] SoftwareAppliancesList)
        {
            MachinesManagement machineManagment = new MachinesManagement();

            if(machine.StatusEnum == DaemonProbeStatus.ServiceStarted)
                ModelState.AddModelError(String.Empty, "Editing the machine while the daemon is running is not supported yet, please stop the daemon first");

            if (!ResourcePool.HasValue)
                ModelState.AddModelError(String.Empty, "Please Select A Resource Pool");
            if (!PlatformID.HasValue)
                ModelState.AddModelError(String.Empty, "Machine Platform Is Requared");
            if (!TypeID.HasValue)
                ModelState.AddModelError(String.Empty, "Machine Type Is Requared");

            if (ModelState.IsValid)
            {
                db.Entry(machine).State = EntityState.Modified;
                db.SaveChanges();

                machine.Daemon = daemon;
                db.SaveChanges();

                var machine2 = db.Machines.Include(x => x.Platform).Include(x => x.Type).Where(x => x.MachineId == machine.MachineId).First();
                if (machine2.Platform == null)
                    machine2.Platform = new MachinePlatform();
                if (machine2.Type == null)
                    machine2.Type = new MachineType();
                MachinePlatform platform = db.MachinePlatforms.Find(PlatformID);
                machine.Platform = platform;
                MachineType type = db.MachineTypes.Find(TypeID);
                machine.Type = type;
                db.SaveChanges();
                machine2 = null;

                //Resource Pool List
                var ResourcePoolsList = db.ResourcePools.ToList();
                for(int ResourcePoolIndex = 0; ResourcePoolIndex < ResourcePoolsList.Count(); ResourcePoolIndex++)
                    if (ResourcePoolsList[ResourcePoolIndex].Machines.Where(x => x.MachineId == machine.MachineId).Count() > 0)
                        db.ResourcePools.ToList()[ResourcePoolIndex].Machines.Remove(machine);
                db.SaveChanges();
                if (ResourcePool.HasValue)
                {
                    db.ResourcePools.Find(ResourcePool).Machines.Add(machine);
                    db.SaveChanges();
                }

                //Login List
                var MachineLoginCredentialList = db.MachineLoginCredentials.ToList();
                for(int MachineLoginCredentialIndex = 0; MachineLoginCredentialIndex < MachineLoginCredentialList.Count(); MachineLoginCredentialIndex++)
                    if (MachineLoginCredentialList[MachineLoginCredentialIndex].Machines.Where(x => x.MachineId == machine.MachineId).Count() > 0)
                        db.MachineLoginCredentials.ToList()[MachineLoginCredentialIndex].Machines.Remove(machine);
                db.SaveChanges();
                MachineLoginCredential login = null;
                if (LoginCredential.HasValue)
                {
                    login = db.MachineLoginCredentials.Find(LoginCredential);
                    login.Machines.Add(machine);
                    db.SaveChanges();
                }

                //SoftwareAppliances List
                machine2 = db.Machines.Include(x => x.SoftwareAppliances).Where(x => x.MachineId == machine.MachineId).First();

                if(machine2.SoftwareAppliances == null)
                    machine2.SoftwareAppliances = new List<SoftwareAppliance>();

                foreach (SoftwareAppliance app in db.SoftwareAppliances.ToList())
                    machine2.SoftwareAppliances.Remove(app);
                db.SaveChanges();

                if (SoftwareAppliancesList != null)
                {
                    foreach (int appID in SoftwareAppliancesList)
                    {
                        var app = db.SoftwareAppliances.Find(appID);
                        machine2.SoftwareAppliances.Add(app);
                    }
                    db.SaveChanges();
                }

                machineManagment.UpdateMachineStatus(machine.MachineId, login);

                return RedirectToAction("ManageMachinesandResourcePools", "Infrastructure");
            }
            return View("~/Views/Infrastructure/ManageMachinesandResourcePools.cshtml", machine);
        }