Example #1
0
        private void MapTopicIndex(Cogs.Dto.TopicIndex dto, TopicIndex topicIndex)
        {
            topicIndex.Name        = dto.Name;
            topicIndex.Description = dto.Description;

            topicIndex.ItemTypeNames.AddRange(dto.ItemTypes);
        }
Example #2
0
        public CogsModel Build(Cogs.Dto.CogsDtoModel cogsDtoModel)
        {
            this.dto   = cogsDtoModel;
            this.model = new CogsModel();

            // Copy information about articles.
            model.ArticlesPath = dto.ArticlesPath;
            model.ArticleTocEntries.AddRange(dto.ArticleTocEntries);

            // Identification
            foreach (var id in dto.Identification)
            {
                var property = new Property();
                MapProperty(id, property);
                model.Identification.Add(property);
            }

            // Settings
            model.Settings = new Settings();
            foreach (var setting in dto.Settings)
            {
                switch (setting.Key)
                {
                case "Title":
                    model.Settings.Title = setting.Value;
                    break;

                case "ShortTitle":
                    model.Settings.ShortTitle = setting.Value;
                    break;

                case "Slug":
                    model.Settings.Slug = setting.Value;
                    break;

                case "Description":
                    model.Settings.Description = setting.Value;
                    break;

                case "Version":
                    model.Settings.Version = setting.Value;
                    break;

                case "Author":
                    model.Settings.Author = setting.Value;
                    break;

                case "Copyright":
                    model.Settings.Copyright = setting.Value;
                    break;

                case "NamespaceUrl":
                    model.Settings.NamespaceUrl = setting.Value;
                    break;

                case "NamespacePrefix":
                    model.Settings.NamespacePrefix = setting.Value;
                    break;

                default:
                    model.Settings.ExtraSettings.Add(setting.Key, setting.Value);
                    break;
                }
            }

            // Set defaults for well-known settings, if they are blank.
            if (string.IsNullOrWhiteSpace(model.Settings.Title))
            {
                model.Settings.Title = "Model Title";
            }
            if (string.IsNullOrWhiteSpace(model.Settings.ShortTitle))
            {
                model.Settings.ShortTitle = "Model";
            }
            if (string.IsNullOrWhiteSpace(model.Settings.Slug))
            {
                model.Settings.Slug = "model";
            }

            if (string.IsNullOrWhiteSpace(model.Settings.Description))
            {
                model.Settings.Description = "TODO";
            }

            if (string.IsNullOrWhiteSpace(model.Settings.Version))
            {
                model.Settings.Version = "0.1";
            }

            if (string.IsNullOrWhiteSpace(model.Settings.Author))
            {
                model.Settings.Author = "TODO";
            }

            if (string.IsNullOrWhiteSpace(model.Settings.Copyright))
            {
                model.Settings.Copyright = "TODO";
            }

            if (string.IsNullOrWhiteSpace(model.Settings.NamespaceUrl))
            {
                model.Settings.NamespaceUrl = "http://example.org/todo";
            }

            if (string.IsNullOrWhiteSpace(model.Settings.NamespacePrefix))
            {
                model.Settings.NamespacePrefix = "todo";
            }


            // First pass: create object stubs.
            string[] itemNames = dto.ItemTypes.Select(x => x.Name).ToArray();

            foreach (var itemTypeDto in dto.ItemTypes)
            {
                var itemType = new ItemType();
                MapDataType(itemTypeDto, itemType, true);
                model.ItemTypes.Add(itemType);

                // add identification to all base types in itemtypes
                if (string.IsNullOrEmpty(itemType.ExtendsTypeName))
                {
                    itemType.Properties.InsertRange(0, model.Identification);
                }
                else
                {
                    if (!itemNames.Contains(itemType.ExtendsTypeName))
                    {
                        string errorMessage = $"Item {itemType.Name} can not extend {itemType.ExtendsTypeName} because it is not an item type.";
                        throw new InvalidOperationException(errorMessage);
                    }
                }
            }

            foreach (var reusableTypeDto in dto.ReusableDataTypes)
            {
                var reusableType = new DataType();
                MapDataType(reusableTypeDto, reusableType, false);
                model.ReusableDataTypes.Add(reusableType);
            }

            foreach (var topicIndexDto in dto.TopicIndices)
            {
                var index = new TopicIndex();
                MapTopicIndex(topicIndexDto, index);
                model.TopicIndices.Add(index);
            }


            // Second pass: add references between items.
            foreach (var itemType in model.ItemTypes)
            {
                CreateRelationships(itemType);
            }

            foreach (var type in model.ReusableDataTypes)
            {
                CreateRelationships(type);
            }

            foreach (var index in model.TopicIndices)
            {
                foreach (var itemTypeName in index.ItemTypeNames)
                {
                    var includedType = GetTypeByName(itemTypeName);
                    index.ItemTypes.Add(includedType);
                }
            }

            // Third pass: look for relationships among items.
            // Related item types, based on following the properties' data types.
            foreach (var itemType in model.ItemTypes)
            {
                ProcessProperties(itemType.Properties, itemType.Relationships, new HashSet <string>());
            }

            return(model);
        }