Example #1
0
        public override PathData ResolvePath(ContentItem startingPoint, string path)
        {
            if (path.Contains("virtual-grouping"))
            {
                var parentPath = startingPoint.FindPath(path);
                if (parentPath.StopItem != null)
                {
                    var parent = parentPath.StopItem;

                    if (parent == null)
                    {
                        return(base.ResolvePath(startingPoint, path));
                    }

                    var attributes = map.GetOrCreateDefinition(parent).GetCustomAttributes <GroupChildrenAttribute>();
                    foreach (var attribute in attributes)
                    {
                        var name     = HttpUtility.UrlDecode(parentPath.Argument).Trim('/');
                        var argument = name.Split('/')[1];

                        var group = Sources.GetChildren(new Query {
                            Parent = parent, OnlyPages = null
                        }).FirstOrDefault(i => i.Name == name);
                        if (group != null)
                        {
                            return(new PathData(group));
                        }
                    }
                }
            }
            return(base.ResolvePath(startingPoint, path));
        }
Example #2
0
            public override IEnumerable <ItemDefinition> GetAllowedDefinitions(ContentItem parentItem, string zoneName, System.Security.Principal.IPrincipal user)
            {
                yield return(map.GetOrCreateDefinition(typeof(OtherItem)).Define());

                yield return(map.GetOrCreateDefinition(typeof(BaseItem)).Define());

                yield return(map.GetOrCreateDefinition(typeof(SuperficialItem)).Define());
            }
Example #3
0
        public string GetDiscriminator(Type value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            ItemDefinition definition = map.GetOrCreateDefinition(value);

            if (definition == null)
            {
                throw new ArgumentException("Could not find the definition associated with the type '" + value.FullName + "'. Please ensure this is a non-abstract class deriving from N2.ContentItem and that it is decorated by the [Definition] attribute.");
            }
            return(definition.Discriminator);
        }
Example #4
0
        /// <summary>Checks the root node in the database. Throws an exception if there is something really wrong with it.</summary>
        /// <returns>A diagnostic string about the root node.</returns>
        public override string CheckRootItem()
        {
            int         rootID   = host.DefaultSite.RootItemID;
            ContentItem rootItem = persister.Get(rootID);

            if (rootItem != null)
            {
                return(String.Format("Root node OK, id: {0}, name: {1}, type: {2}, discriminator: {3}, published: {4} - {5}",
                                     rootItem.ID, rootItem.Name, rootItem.GetContentType(),
                                     map.GetOrCreateDefinition(rootItem), rootItem.Published, rootItem.Expires));
            }
            else
            {
                return("No root item found with the id: " + rootID);
            }
        }
Example #5
0
        public virtual void MapTypes(List <Type> allTypes, NHibernate.Cfg.Configuration cfg, Func <string, string> formatter)
        {
            var m = new HbmMapping();

            m.Items = allTypes.Select(t =>
            {
                var sc                = new HbmSubclass();
                sc.name               = GetName(t);
                sc.extends            = GetName(t.BaseType);
                sc.discriminatorvalue = map.GetOrCreateDefinition(t).Discriminator ?? t.Name;
                sc.lazy               = false;
                sc.lazySpecified      = true;

                var propertyMappings = GetPersistables(t)
                                       .Select(p => p.Attribute.GetPropertyMapping(p.DeclaringProperty, formatter))
                                       .ToList();
                if (propertyMappings.Count > 0)
                {
                    if (sc.Items == null)
                    {
                        sc.Items = propertyMappings.ToArray();
                    }
                    else
                    {
                        sc.Items = sc.Items.Union(propertyMappings).ToArray();
                    }
                }

                return(sc);
            }).ToArray();
            var dbg = m.AsString();

            cfg.AddDeserializedMapping(m, "N2");
        }
Example #6
0
        public virtual IEnumerable <ItemDefinition> Register(DefinitionMap map)
        {
            var registration = new ContentRegistration <T>(map.GetOrCreateDefinition(RegisteredType));

            registration.IsDefined = true;
            RegisterDefinition(registration);
            return(new [] { registration.Finalize() });
        }
Example #7
0
        public void ExportedImportedItem_AutoGeneratedProperties_AreImported()
        {
            var item = definitions.CreateInstance <XmlableItem2>(null);

            item.AutoPropertyString = "Hello World!";

            // simulate saving proxied items - this is most likely the case in normal operation
            var pf  = new N2.Persistence.Proxying.InterceptingProxyFactory();
            var map = new DefinitionMap();

            pf.Initialize(new [] { map.GetOrCreateDefinition(typeof(XmlableItem2)) });
            pf.OnSaving(item);

            var readItem = ExportAndImport(item, ExportOptions.Default);

            Assert.That(readItem.AutoPropertyString, Is.EqualTo(item.AutoPropertyString));
        }
Example #8
0
        private IEnumerable <T> GetBehviors <T>(ContentItem item) where T : class
        {
            if (item == null)
            {
                return(Enumerable.Empty <T>());
            }

            var behaviors = definitionMap.GetOrCreateDefinition(item).GetCustomAttributes <T>();

            if (generalBehaviors.Length > 0)
            {
                behaviors = behaviors.Union(generalBehaviors.OfType <T>());
            }
            if (item is T)
            {
                behaviors = behaviors.Union(new[] { item as T });
            }
            return(behaviors);
        }
Example #9
0
        private TemplateDefinition CreateTemplateInfo(ContentItem template)
        {
            var info = new TemplateDefinition
            {
                Name            = template.Name,
                Title           = template.Title,
                Description     = template.GetDetail(TemplateDescription, ""),
                TemplateUrl     = template.Url,
                Definition      = map.GetOrCreateDefinition(template.GetContentType(), template.Name),
                TemplateFactory = () =>
                {
                    var clone = template.Clone(true);
                    clone.SetDetail(TemplateDescription, null, typeof(string));
                    clone.Title       = "";
                    clone.Name        = null;
                    clone.TemplateKey = template.Name;
                    return(clone);
                },
                OriginalFactory = () => template
            };

            return(info);
        }
        public virtual void MapTypes(List <Type> allTypes, NHibernate.Cfg.Configuration cfg, Func <string, string> formatter)
        {
            var m = new HbmMapping();

            m.Items = allTypes.Select(t =>
            {
                var sc                = new HbmSubclass();
                sc.name               = GetName(t);
                sc.extends            = GetName(t.BaseType);
                sc.discriminatorvalue = map.GetOrCreateDefinition(t).Discriminator ?? t.Name;
                sc.lazy               = false;
                sc.lazySpecified      = true;

                var propertyMappings = GetPersistables(t)
                                       .Select(p => p.Attribute.GetPropertyMapping(p.DeclaringProperty, formatter))
                                       .ToList();
                if (propertyMappings.Count > 0)
                {
                    if (sc.Items == null)
                    {
                        sc.Items = propertyMappings.ToArray();
                    }
                    else
                    {
                        sc.Items = sc.Items.Union(propertyMappings).ToArray();
                    }
                }
                logger.DebugFormat("Generating subclass {0} with discriminator {1} extending {2} with {3} items ({4} property mappings)", sc.name, sc.discriminatorvalue, sc.extends, sc.Items != null ? sc.Items.Length.ToString() : "(null)", propertyMappings.Count);
                return(sc);
            }).ToArray();
            if (Debugger.IsAttached)
            {
                var dbg = m.AsString();
            }
            cfg.AddDeserializedMapping(m, "N2");
        }
 public void TestFixtureSetUp()
 {
     factory = new InterceptingProxyFactory();
     map     = new DefinitionMap();
     factory.Initialize(new[] { typeof(InterceptableItem), typeof(InterceptableInheritorItem), typeof(IgnoringItem) }.Select(t => map.GetOrCreateDefinition(t)));
 }
Example #12
0
        /// <summary>Gets a container or null if no container exists.</summary>
        /// <param name="containerContainer"></param>
        /// <returns></returns>
        public virtual T Get(ContentItem containerContainer, string name = null)
        {
            if (Navigate)
            {
                var q = containerContainer.Children.Query().OfType <T>();
                if (!string.IsNullOrEmpty(name))
                {
                    q = q.Where(i => string.Equals(i.Name, name, StringComparison.InvariantCultureIgnoreCase));
                }
                return(q.FirstOrDefault());
            }
            else
            {
                var parameters = Parameter.Equal("Parent", containerContainer) & Parameter.TypeEqual(map.GetOrCreateDefinition(typeof(T)).Discriminator);
                if (!string.IsNullOrEmpty(name))
                {
                    parameters.Add(Parameter.Like("Name", name));
                }

                var items = repository.Find(parameters.Take(1));
                return(items.OfType <T>().FirstOrDefault());
            }
        }
        public void Containerns_AreInherited()
        {
            var d = map.GetOrCreateDefinition(typeof(ChildContentItem));

            d.Containers.Count.ShouldBe(2);
        }
Example #14
0
        /// <summary>Gets the definition for a certain item type.</summary>
        /// <param name="itemType">The type of item whose definition we want.</param>
        /// <returns>The definition matching a certain item type.</returns>
        public virtual ItemDefinition GetDefinition(Type itemType)
        {
            if (itemType == null)
            {
                return(null);
            }

            var e = new DefinitionEventArgs
            {
                ContentType = itemType,
                Definition  = GetDefinitions().FirstOrDefault(d => d.ItemType == itemType) ?? map.GetOrCreateDefinition(itemType)
            };

            if (DefinitionResolving != null)
            {
                DefinitionResolving(this, e);
            }

            return(e.Definition);
        }
Example #15
0
        /// <summary>Gets a container or null if no container exists.</summary>
        /// <param name="containerContainer"></param>
        /// <param name="name">optional name</param>
        /// <returns></returns>
        public virtual T Get(ContentItem containerContainer, string name = null)
        {
            if (Navigate)
            {
                var q = containerContainer.Children.Query().OfType <T>();
                if (!string.IsNullOrEmpty(name))
                {
                    q = q.Where(i => string.Equals(i.Name, name, StringComparison.InvariantCultureIgnoreCase));
                }
                return(q.FirstOrDefault());
            }

            // load only once as this hardly ever changes at runtime
            var key = name ?? "<null>";

            if (cache.ContainsKey(key))
            {
                // object may have been updated in the meantime, e.g. trash purge - better get from repo
                var item = repository.Get(cache[key]) as T;         // get should be faster than query below with a chance of hitting NH cache
                if (item != null)
                {
                    return(item);
                }
            }

            var parameters = Parameter.Equal("Parent", containerContainer) & Parameter.TypeEqual(map.GetOrCreateDefinition(typeof(T)).Discriminator);

            if (!string.IsNullOrEmpty(name))
            {
                parameters.Add(Parameter.Like("Name", name));
            }

            var items  = repository.Find(parameters.Take(1));
            var result = items.OfType <T>().FirstOrDefault();

            if (result != null)
            {
                cache[key] = result.ID;
            }

            return(result);
        }