private void SetupControlState()
        {
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();
            var allTypes   = assemblies.SelectMany(x => x.GetTypes()).ToList();


            var extendableClrTypes = allTypes
                                     .HavingAttribute <WarpCoreEntityAttribute>()
                                     .Where(x => typeof(WarpCoreEntity).IsAssignableFrom(x));
            //.ToLookup(x => x.GetCustomAttribute<WarpCoreEntityAttribute>().TypeExtensionUid);

            List <EntityViewModel> vms = new List <EntityViewModel>();
            var allDynamicContentTypes = new ContentTypeMetadataRepository().Find();

            foreach (var t in allDynamicContentTypes)
            {
                vms.Add(new EntityViewModel {
                    DisplayName = t.Name, ContentTypeId = t.ContentId
                });
            }

            foreach (var t in extendableClrTypes)
            {
                var contentTypeId = t.GetCustomAttribute <WarpCoreEntityAttribute>().TypeExtensionUid;
                vms.Add(new EntityViewModel {
                    DisplayName = t.Name, ContentTypeId = contentTypeId
                });
            }


            var repo = new ContentInterfaceRepository();
            var contentInterfaceLookup = repo.Find().ToLookup(x => x.ContentTypeId);

            foreach (var entityViewModel in vms)
            {
                var appliedInterfaces = contentInterfaceLookup[entityViewModel.ContentTypeId];
                foreach (var contentInterface in appliedInterfaces)
                {
                    entityViewModel.EditableInterfaces.Add(new EntityInterfaceViewModel
                    {
                        InterfaceId = contentInterface.ContentId,
                        Name        = contentInterface.InterfaceName
                    });
                }
            }

            _controlState.Entities = vms.ToList();

            //var repo = new ContentInterfaceRepository();
            //var allEntities = repo.Find()
            //    .Select(x => new EntityViewModel
            //    {
            //        ContentTypeId = x.ContentTypeId,
            //        DisplayName = clrLookup[x.ContentTypeId].FirstOrDefault()?.Name

            //    });

            //_controlState.Entities = allEntities.ToList();
        }
Beispiel #2
0
        public static void RegisterExtensibleTypesWithApi(AppDomain domain)
        {
            var assemblies = domain.GetAssemblies();
            var allTypes   = assemblies.SelectMany(x => x.GetTypes()).ToList();

            var repositoryDescriptions = TypeSearcher.FindRepositoriesKnownToWarpCore(allTypes);

            foreach (var repositoryDescription in repositoryDescriptions)
            {
                BuildUpRepositoryMetadata(repositoryDescription);
            }

            //Break this one out later.
            var entities = TypeSearcher.FindEntitiesKnownToWarpCore(allTypes);

            var contentTypeMetadataRepository = new ContentTypeMetadataRepository();

            var typeExtensionRepo = new ContentInterfaceRepository();

            foreach (var entityType in entities)
            {
                var repositoryUid = entityType.GetCustomAttribute <WarpCoreEntityAttribute>();

                var preexistingContentType = contentTypeMetadataRepository.Find().SingleOrDefault(x => x.TypeResolverId == repositoryUid.TypeExtensionUid);
                if (preexistingContentType == null)
                {
                    preexistingContentType = new DynamicContentType
                    {
                        TypeResolverId = repositoryUid.TypeExtensionUid,
                    }
                }
                ;

                preexistingContentType.ContentNameSingular = repositoryUid.ContentNameSingular;
                preexistingContentType.ContentNamePlural   = repositoryUid.ContentNamePlural;

                if (string.IsNullOrWhiteSpace(preexistingContentType.ContentNameSingular))
                {
                    preexistingContentType.ContentNameSingular = entityType.Name;
                }

                if (string.IsNullOrWhiteSpace(preexistingContentType.ContentNamePlural))
                {
                    //todo: replace
                    //PluralizationService.CreateService(CultureInfo.CurrentCulture)
                    //    .Pluralize(preexistingContentType.ContentNameSingular);
                }
                preexistingContentType.SupportsCustomFields = repositoryUid.SupportsCustomFields;
                preexistingContentType.TitleProperty        = repositoryUid.TitleProperty;

                contentTypeMetadataRepository.Save(preexistingContentType);

                if (preexistingContentType.SupportsCustomFields)
                {
                    var preexisting = typeExtensionRepo.Find().SingleOrDefault(x =>
                                                                               x.ContentTypeId == repositoryUid.TypeExtensionUid &&
                                                                               x.InterfaceName == KnownTypeExtensionNames.CustomFields);
                    if (preexisting == null)
                    {
                        typeExtensionRepo.Save(new ContentInterface
                        {
                            ContentTypeId = repositoryUid.TypeExtensionUid,
                            InterfaceName = KnownTypeExtensionNames.CustomFields
                        });
                    }
                }
            }
        }