/// <summary>
        /// Adds a new application.
        /// </summary>
        /// <param name="application">The new application.</param>
        public void Add(ApplicationDescription application)
        {
            VerifySchemaVersion();

            var result = StoredApplicationDescriptions.Add(application);
            Patch(result);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ApplicationModel"/> class.
        /// </summary>
        /// <param name="model">The object that handles the data storage.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="model"/> is <see langword="null" />.
        /// </exception>
        public ApplicationModel(ApplicationDescription model)
        {
            {
                Lokad.Enforce.Argument(() => model);
            }

            m_Model = model;
        }
        private ApplicationDescription Patch(ApplicationDescription description)
        {
            var result = StoredApplicationDescriptions.Find(description.Id) ?? StoredApplicationDescriptions.Add(description);
            if (!result.IsPatched && !result.IsPatching)
            {
                result.IsPatching = true;
                try
                {
                    var selectedMachineApplications = GetMachineApplicationByApplicationId(result.Id)
                        .Select(Patch)
                        .ToList();
                    result.MachineApplications = selectedMachineApplications;

                    var selectedTestApplications = GetTestApplicationsByApplicationId(result.Id)
                        .Select(id => TestApplication(id.Value))
                        .ToList();
                    result.TestApplications = selectedTestApplications;

                    result.IsPatched = true;
                }
                finally
                {
                    result.IsPatching = false;
                }
            }

            return result;
        }
        private ApplicationDescription GetStoredApplicationFromData(ApplicationDescription app)
        {
            var storedApp = GetApplicationsByName(
                app.Name,
                app.Version.Major,
                app.Version.Minor,
                app.Version.Build,
                app.Version.Revision)
                .FirstOrDefault();
            if (storedApp == null)
            {
                throw new UnknownApplicationException();
            }

            return Patch(storedApp);
        }
        /// <summary>
        /// Updates the application with the data from the given object.
        /// </summary>
        /// <param name="application">The application.</param>
        public void Update(ApplicationDescription application)
        {
            VerifySchemaVersion();

            var storedApplication = Application(application.Id);
            if (storedApplication != null)
            {
                if (!ReferenceEquals(storedApplication, application))
                {
                    storedApplication.Name = application.Name;
                    storedApplication.Version = application.Version;
                }

                var entry = Entry(storedApplication);
                entry.State = EntityState.Modified;
            }
        }
        private void SetApplicationsForEnvironment(XDocument xmlContent, int environmentId)
        {
            // Expecting the following
            // <applications>
            //     <application name="{APPLICATION_NAME}"
            //                  version="{APPLICATION_VERSION}" />
            //
            //      .... more applications here ...
            //
            // </applications>
            var applicationsNode = xmlContent.Descendants("applications").First();
            foreach (var applicationNode in applicationsNode.Descendants("application"))
            {
                var name = applicationNode.Attribute("name").Value;
                var version = new Version(applicationNode.Attribute("version").Value);
                var application = new ApplicationDescription
                    {
                        Name = name,
                        Version = version,
                    };

                var storedApplicationId = m_Context.Applications()
                    .Where(a => a.IsNewerOrEqualVersionOf(application))
                    .OrderBy(a => a.Version)
                    .Select(a => a.Id)
                    .First();

                var testApplication = new TestApplication
                    {
                        ApplicationId = storedApplicationId,
                        TestEnvironmentId = environmentId,
                    };

                m_Context.Add(testApplication);
                m_Context.StoreChanges();
            }
        }