private UserProfile GetOwner(IContentBase source, MapperContext context)
        {
            var profile = source.GetCreatorProfile(userService);

            return(profile == null ? null : context.Map <IProfile, UserProfile>(profile));
        }
Пример #2
0
    public UserProfile?GetOwner(IContentBase source, MapperContext context)
    {
        IProfile?profile = source.GetCreatorProfile(_userService);

        return(profile == null ? null : context.Map <IProfile, UserProfile>(profile));
    }
Пример #3
0
        private Document GetDocumentToIndex(IContentBase content, SearchField[] searchFields)
        {
            try
            {
                var c = new Document();

                var type = content.GetType();

                foreach (var field in GetStandardUmbracoFields())
                {
                    try
                    {
                        object propertyValue = null;

                        // handle special case properties
                        switch (field.Name)
                        {
                        case "SearchablePath":
                            propertyValue = content.Path.TrimStart('-');
                            break;

                        case "Path":
                            propertyValue = content.Path.Split(',');
                            break;

                        case "CreatorName":
                            propertyValue = content.GetCreatorProfile(UmbracoContext.Current.Application.Services.UserService).Name;
                            break;

                        case "ParentID":
                            propertyValue = content.ParentId;
                            break;

                        default:
                            // try get model property
                            PropertyInfo modelProperty;
                            if (!_propertyCache.ContainsKey(type.Name))
                            {
                                _propertyCache[type.Name] = new Dictionary <string, PropertyInfo>();
                            }

                            var cache = _propertyCache[type.Name];

                            if (cache.ContainsKey(field.Name))
                            {
                                modelProperty = cache[field.Name];
                            }
                            else
                            {
                                modelProperty     = type.GetProperty(field.Name);
                                cache[field.Name] = modelProperty;
                            }

                            if (modelProperty != null)
                            {
                                propertyValue = modelProperty.GetValue(content);
                            }
                            else
                            {
                                // try get umbraco property
                                if (content.HasProperty(field.Name))
                                {
                                    propertyValue = content.GetValue(field.Name);
                                }
                            }
                            break;
                        }

                        // handle datatypes
                        switch (field.Type.ToString())
                        {
                        case "Edm.String":
                            propertyValue = propertyValue?.ToString();
                            break;

                        case "Edm.Boolean":
                            bool.TryParse((propertyValue ?? "False").ToString(), out var val);
                            propertyValue = val;
                            break;
                        }

                        if (propertyValue?.ToString().IsNullOrWhiteSpace() == false)
                        {
                            c[field.Name] = propertyValue;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }
                }

                switch (type.Name)
                {
                case "Media":
                    c["IsMedia"] = true;
                    break;

                case "Content":
                    c["IsContent"] = true;
                    break;

                case "Member":
                    c["IsMember"] = true;
                    break;
                }

                bool cancelIndex = AzureSearch.FireContentIndexing(
                    new AzureSearchEventArgs()
                {
                    Item  = content,
                    Entry = c
                });

                if (cancelIndex)
                {
                    // cancel was set in an event, so we don't index this item.
                    return(null);
                }

                var umbracoFields  = searchFields.Where(x => !x.IsComputedField()).ToArray();
                var computedFields = searchFields.Where(x => x.IsComputedField()).ToArray();

                c = FromUmbracoContentBase(c, content, umbracoFields);
                c = FromComputedFields(c, content, computedFields);

                // todo: content isn't actually indexed at this point, consider moving the event to the callback from azure after sending to index
                AzureSearch.FireContentIndexed(
                    new AzureSearchEventArgs()
                {
                    Item  = content,
                    Entry = c
                });

                return(c);
            }
            catch (Exception ex)
            {
                throw;
            }
        }