Esempio n. 1
0
 public static ModelRegistry GetInstance(IGeneratorConfiguration configuration)
 {
     if (_modelRegistry == null)
     {
         _modelRegistry = new ModelRegistry(configuration);
     }
     return(_modelRegistry);
 }
Esempio n. 2
0
        private void ConvertTemplate(TemplateData template)
        {
            _log.Debug($"Converting template {template.Title}");
            ViewDefinition viewDefinition;

            if (this.ContainsView(template.Id))
            {
                _log.Debug($"schema {template.Title} was already in the model registry");
                viewDefinition = Views.First(m => m.TcmUri == template.Id);
            }
            else
            {
                _log.Debug($"creating new ViewDefinition for template {template.Title}");
                viewDefinition        = new ViewDefinition();
                viewDefinition.Title  = template.Title;
                viewDefinition.TcmUri = template.Id;
                if (string.IsNullOrEmpty(template.Metadata))
                {
                    viewDefinition.ViewName = MakeCsName(template.Title, true);
                }
                else
                {
                    viewDefinition.ViewName = GetFieldValue(template.Metadata, "view");
                }

                if (template is ComponentTemplateData)
                {
                    viewDefinition.ViewPurpose = ViewPurpose.Entity;
                    UsedItemsFilterData filter = new UsedItemsFilterData()
                    {
                        ItemTypes = new[] { ItemType.Schema }
                    };

                    var associatedSchemas = Client.GetList(template.Id, filter).Cast <SchemaData>();

                    var allAssociatedModels = associatedSchemas?.Select(s => ModelRegistry.GetViewModelDefinition(s.Id)).Where(a => a.Purpose == SchemaPurpose.Component);

                    if (allAssociatedModels.Count() > 1)
                    {
                        _log.Warn($"found component template {template.Title} ({template.Id}) with multiple associated schemas. We will use the base class {Config.BaseClasses.FirstOrDefault()} instead");
                        viewDefinition.ModelTypeName = Config.BaseClasses.FirstOrDefault();
                        //_log.Info($"Template {template.Title} is linked to more than one schema. We need to merge models before we can continue");
                        //var mergedModel = ModelRegistry.MergeModelDefinitions(allAssociatedModels);
                        //viewDefinition.AssociatedModelDefinition = mergedModel;
                        //viewDefinition.ModelTypeName = mergedModel.TypeName;
                    }
                    else if (allAssociatedModels.Count() == 0)
                    {
                        _log.Warn($"found component template {template.Title} ({template.Id}) without any associated schema. We will use the base class {Config.BaseClasses.FirstOrDefault()} instead");
                        viewDefinition.ModelTypeName = Config.BaseClasses.FirstOrDefault();
                    }
                    else
                    {
                        viewDefinition.AssociatedModelDefinition = allAssociatedModels.First();
                        viewDefinition.ModelTypeName             = viewDefinition.AssociatedModelDefinition.TypeName;
                    }
                }
                else // this is a page template, so it has its own associated model definition
                {
                    viewDefinition.AssociatedModelDefinition = ModelRegistry.GetViewModelDefinition(template.Id);
                    viewDefinition.ModelTypeName             = viewDefinition.AssociatedModelDefinition.TypeName;
                    viewDefinition.ViewPurpose = ViewPurpose.Page;
                }
            }

            currentView = viewDefinition;

            if (this.ContainsView(template.Id))
            {
                Views.Add(viewDefinition);
            }
            else
            {
                Views.Add(viewDefinition);
            }
        }