Esempio n. 1
0
        private (IKey key1, IKey key2) LoadRelatedMovieKeys(XmlStructure structure, XElement element)
        {
            IKey sourceKey = LoadMovieKey(structure, element, structure.RelatedMovieSourceId);
            IKey targetKey = LoadMovieKey(structure, element, structure.RelatedMovieTargetId);

            return(sourceKey, targetKey);
        }
Esempio n. 2
0
        /// <summary>
        /// Saves content from <paramref name="model"/> to its storage file as XML.
        /// </summary>
        /// <param name="model">A library to save.</param>
        /// <returns>Continuation task.</returns>
        public Task SaveAsync(Library model)
        {
            Ensure.NotNull(model, "model");

            if (!string.IsNullOrEmpty(model.Configuration.FilePath))
            {
                XmlStructure structure = new XmlStructure();
                XDocument    document  = new XDocument();

                XElement rootElement = new XElement(structure.Root);
                document.Add(rootElement);
                SaveFields(rootElement, model.ConfigurationDefinition, model.Configuration);

                XElement moviesElement = new XElement(structure.Movies);
                rootElement.Add(moviesElement);

                XElement relatedMoviesElement = new XElement(structure.RelatedMovies);
                rootElement.Add(relatedMoviesElement);

                foreach (Movie movie in model.Movies)
                {
                    XElement movieElement = new XElement(structure.Movie);
                    moviesElement.Add(movieElement);
                    SaveMovie(structure, movieElement, movie);

                    SaveRelated(structure, relatedMoviesElement, movie);
                }

                document.Save(model.Configuration.FilePath);
            }

            return(Task.CompletedTask);
        }
Esempio n. 3
0
        private void SaveRelated(XmlStructure structure, XElement parentElement, Movie model)
        {
            string sourceId = GetMovieId(model.Key);

            foreach (IKey relatedKey in model.RelatedMovieKeys)
            {
                string targetId    = GetMovieId(relatedKey);
                bool   isContained = parentElement
                                     .Descendants(structure.RelatedMovie)
                                     .Any(e =>
                {
                    string sId = e.Attribute(structure.RelatedMovieSourceId)?.Value;
                    string tId = e.Attribute(structure.RelatedMovieTargetId)?.Value;

                    return(sId == sourceId && tId == targetId || sId == targetId && tId == sourceId);
                });

                if (!isContained)
                {
                    XElement element = new XElement(structure.RelatedMovie);
                    parentElement.Add(element);

                    element.SetAttributeValue(structure.RelatedMovieSourceId, sourceId);
                    element.SetAttributeValue(structure.RelatedMovieTargetId, targetId);
                }
            }
        }
Esempio n. 4
0
        private Movie LoadMovie(Library library, XmlStructure structure, XElement element)
        {
            IKey  key   = LoadMovieKey(structure, element, structure.MovieId);
            Movie model = new Movie(key, library);

            LoadFields(element, library.MovieDefinition, model);
            return(model);
        }
Esempio n. 5
0
        private IKey LoadMovieKey(XmlStructure structure, XElement element, XName attributeName)
        {
            string id = element.Attribute(attributeName)?.Value;

            if (id == null)
            {
                throw Ensure.Exception.InvalidOperation($"Missing attribute '{structure.MovieId}' on element '{element}'.");
            }

            IKey key = null;

            if (Guid.TryParse(id, out Guid guid))
            {
                key = GuidKey.Create(guid, "Movie");
            }
            else
            {
                key = StringKey.Create(id, "Movie");
            }

            return(key);
        }
Esempio n. 6
0
        /// <summary>
        /// Loads (adds) movies from file storage of <paramref name="model"/>.
        /// </summary>
        /// <param name="model">A library to load to.</param>
        /// <returns>Continuation task.</returns>
        public Task LoadAsync(Library model)
        {
            Ensure.NotNull(model, "model");

            if (!string.IsNullOrEmpty(model.Configuration.FilePath) && File.Exists(model.Configuration.FilePath))
            {
                XmlStructure structure = new XmlStructure();

                XDocument document = XDocument.Load(model.Configuration.FilePath);
                LoadFields(document.Root, model.ConfigurationDefinition, model.Configuration);

                XElement moviesRootElement = document.Descendants(structure.Movies).FirstOrDefault();
                if (moviesRootElement == null)
                {
                    structure = new XmlStructure(false);
                }

                moviesRootElement = document.Descendants(structure.Movies).FirstOrDefault();
                if (moviesRootElement != null)
                {
                    foreach (XElement movieElement in document.Descendants(structure.Movies).Descendants(structure.Movie))
                    {
                        model.Movies.Add(LoadMovie(model, structure, movieElement));
                    }

                    foreach (XElement relatedElement in document.Descendants(structure.RelatedMovies).Descendants(structure.RelatedMovie))
                    {
                        var   keys   = LoadRelatedMovieKeys(structure, relatedElement);
                        Movie movie1 = model.Movies.FindByKey(keys.key1);
                        if (movie1 != null)
                        {
                            movie1.RelatedMovieKeys.Add(keys.key2);
                        }
                    }
                }
            }

            return(Task.CompletedTask);
        }
Esempio n. 7
0
 private void SaveMovie(XmlStructure structure, XElement element, Movie model)
 {
     SaveMovieKey(element, structure.MovieId, model.Key);
     SaveFields(element, model.Library.MovieDefinition, model);
 }