public static ISimulationProducerContract Get(string nameOrID)
        {
            Guid simulationId = Guid.Empty;
            bool isGuid       = Guid.TryParse(nameOrID, out simulationId);

            ProducerSimulationContract sc = new ProducerSimulationContract();

            using (var db = new ProducerContext())
            {
                Simulation obj = null;
                if (isGuid == true)
                {
                    obj = db.Simulations.SingleOrDefault(s => s.Id == simulationId);
                }
                else
                {
                    obj = db.Simulations
                          .OrderByDescending(q => q.Count).First(s => s.Name == nameOrID);
                }

                if (obj == null)
                {
                    return(null);
                }

                sc.simId      = obj.Id;
                sc.name       = obj.Name;
                sc.nameIsGuid = isGuid;
            }

            return(sc);
        }
        public static ISimulationProducerContract Create(string simNameOrID, string simName, string applicationName)
        {
            Guid simulationId = Guid.Empty;
            bool isGuid       = Guid.TryParse(simNameOrID, out simulationId);

            ProducerSimulationContract sc = new ProducerSimulationContract();

            string owner = Turbine.Producer.Container.GetAppContext().UserName;

            using (var db = new ProducerContext())
            {
                Debug.WriteLine("Owner: " + owner, "ProducerSimulationContract.Create");
                User       user      = db.Users.Single <User>(s => s.Name == owner);
                Simulation obj       = null;
                Guid       simIdUsed = Guid.Empty;

                if (isGuid == true)
                {
                    simIdUsed = simulationId;
                    obj       = db.Simulations.SingleOrDefault(s => s.Id == simulationId);
                }
                else
                {
                    simIdUsed = Guid.NewGuid();
                    obj       = db.Simulations
                                .OrderBy(q => q.Count).FirstOrDefault(s => s.Name == simNameOrID);
                }

                if (obj != null)
                {
                    throw new InvalidStateChangeException(String.Format(
                                                              "Simulation with Name or Id '{0}' already exists", simNameOrID));
                }

                Application app = db.Applications.SingleOrDefault <Application>(s => s.Name == applicationName);
                if (app == null)
                {
                    throw new ArgumentException(String.Format(
                                                    "Application '{0}' does not exist", applicationName));
                }

                Debug.WriteLine("create simulation", "ProducerSimulationContract");
                var sim = new Simulation()
                {
                    Name        = simName,
                    Id          = simIdUsed,
                    User        = user,
                    Application = app,
                    Create      = DateTime.UtcNow,
                    Update      = DateTime.UtcNow
                };
                db.Simulations.Add(sim);
                db.SaveChanges();
            }
            using (var db = new ProducerContext())
            {
                Debug.WriteLine("create simulation inputs", "ProducerSimulationContract");
                Simulation sim = null;

                if (isGuid == true)
                {
                    sim = db.Simulations.Single <Simulation>(s => s.Id == simulationId);
                }
                else
                {
                    sim = db.Simulations.Single <Simulation>(s => s.Name == simNameOrID);
                }

                foreach (var input in sim.Application.InputFileTypes)
                {
                    Debug.WriteLine("create simulation input " + input.Name, "ProducerSimulationContract");
                    if ("any".Equals(input.Name))
                    {
                        continue;
                    }
                    var ssi = new SimulationStagedInput()
                    {
                        Id         = Guid.NewGuid(),
                        Name       = input.Name,
                        Content    = new Byte[] { },
                        Hash       = "",
                        Simulation = sim
                    };
                    sim.SimulationStagedInputs.Add(ssi);
                }

                sc.simId      = sim.Id;
                sc.name       = sim.Name;
                sc.nameIsGuid = isGuid;

                Debug.WriteLine("create save", "ProducerSimulationContract");
                db.SaveChanges();
            }

            return(sc);
        }
        /// <summary>
        /// Copy Session to a new session with the desired job state.
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public Dictionary <string, string> Copy(Guid sessionId, string state)
        {
            ISimulationProducerContract contract = null;

            List <int> jobList = new List <int>();

            Guid   sessionIdNew = Guid.NewGuid();
            string owner        = Container.GetAppContext().UserName;

            using (var db = new ProducerContext())
            {
                User user    = db.Users.Single <User>(s => s.Name == owner);
                var  session = new Session()
                {
                    Id     = sessionIdNew,
                    User   = user,
                    Create = DateTime.UtcNow
                };
                db.Sessions.Add(session);

                db.SaveChanges();
            }


            using (var db = new ProducerContext())
            {
                var sessionJobs = db.Jobs.Where(j => j.SessionId == sessionId).Where(j => j.State == state);

                Debug.WriteLine("Simulation Name: " + sessionJobs.First().Simulation.Name, "ProducerSessionContract.Copy");

                contract = ProducerSimulationContract.Get(sessionJobs.First().Simulation.Name);


                foreach (var job in sessionJobs)
                {
                    Debug.WriteLine("job: " + job.Count.ToString(), "ProducerSessionContract.Copy");
                    var input = Newtonsoft.Json.Linq.JObject.Parse(job.Process.Input);

                    Dictionary <String, Object> dd = new Dictionary <string, object>();
                    foreach (var v in input)
                    {
                        Debug.WriteLine("VALUE: " + v);
                        Debug.WriteLine("Key: " + v.Key + ", Value: " + v.Value);
                        dd[v.Key] = v.Value;
                    }

                    Debug.WriteLine("Creating jobContract", "ProducerSessionContract.Copy");
                    IJobProducerContract jobContract;
                    jobContract = contract.NewJob(sessionIdNew, job.Initialize, job.Reset, job.Visible);
                    jobContract.Process.Input = dd;

                    Debug.WriteLine("Adding jobContract Count to jobList", "ProducerSessionContract.Copy");
                    jobList.Add(jobContract.Id);
                }
            }

            var d = new Dictionary <string, string>();

            d.Add("NewSessionGuid", sessionIdNew.ToString());
            d.Add("NewJobIds", string.Join(",", jobList.ToArray()));

            return(d);
        }