Exemple #1
0
 public FileDatabaseItem([NotNull] FileDatabase database, [CanBeNull] FileDatabaseItem parent, Guid id, [NotNull] string name, string displayName, [NotNull] string icon16X16, [NotNull] string icon32X32, [NotNull] string template, Guid templateId, [NotNull] string path, int childCount, string mediaUrl)
 {
     Database    = database;
     Parent      = parent;
     Id          = id;
     Name        = name;
     DisplayName = displayName;
     Icon16X16   = icon16X16;
     Icon32X32   = icon32X32;
     Path        = path;
     ChildCount  = childCount;
     Template    = template;
     TemplateId  = templateId;
     MediaUrl    = mediaUrl;
 }
Exemple #2
0
        protected virtual void WriteItemHeader([NotNull] JsonTextWriter output, [NotNull] FileDatabaseItem item)
        {
            output.WritePropertyString("id", item.Id.ToString("B").ToUpperInvariant());
            output.WritePropertyString("uri", DatabaseName + "/" + item.Id.ToString("B").ToUpperInvariant() + "/en/1/");
            output.WritePropertyString("name", item.Name);
            output.WritePropertyString("displayName", item.DisplayName);
            output.WritePropertyString("database", DatabaseName);
            output.WritePropertyString("icon16x16", item.Icon16X16);
            output.WritePropertyString("icon32x32", item.Icon32X32);
            output.WritePropertyString("path", item.Path);
            output.WritePropertyString("longPath", item.Path);
            output.WritePropertyString("templateId", item.TemplateId.ToString("B").ToUpperInvariant());
            output.WritePropertyString("templateName", item.Template);
            output.WritePropertyString("childCount", item.ChildCount);

            if (item.Path.StartsWith("/sitecore/media library/"))
            {
                output.WritePropertyString("mediaurl", item.MediaUrl);
            }
        }
Exemple #3
0
        protected virtual void WriteItemFields(JsonTextWriter output, [NotNull] RequestParameters request, FileDatabaseItem item)
        {
            if (!request.Fields.Any())
            {
                return;
            }

            var includeAllFields    = request.Fields.Count == 1 && request.Fields.First().FieldName == "*";
            var includeSystemFields = request.IncludeSystemFields;
            var includeFieldInfo    = request.IncludeFieldInfo;

            if (includeFieldInfo)
            {
                output.WriteStartArray("fields");
            }
            else
            {
                output.WriteStartObject("fields");
            }

            foreach (var field in item.Fields)
            {
                if (includeAllFields)
                {
                    if (!includeSystemFields && field.Name.StartsWith("__"))
                    {
                        continue;
                    }
                }
                else if (!request.Fields.Any(f => string.Equals(f.FieldName, field.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    continue;
                }

                var value = field.Value;

                var fieldInfo = request.Fields.FirstOrDefault(f => string.Equals(f.FieldName, field.Name, StringComparison.OrdinalIgnoreCase)) ?? new FieldInfo(field.Name, string.Empty);
                foreach (var formatter in ContentDeliveryManager.FieldValueFormatters.OrderBy(f => f.Priority))
                {
                    if (!formatter.TryFormat(field, fieldInfo, value, out var formattedValue))
                    {
                        continue;
                    }

                    value = formattedValue;
                    break;
                }

                if (!request.IncludeEmptyFields && string.IsNullOrEmpty(value))
                {
                    continue;
                }

                if (includeFieldInfo)
                {
                    output.WriteStartObject();
                    output.WritePropertyString("id", field.Id.ToString("B").ToUpperInvariant());
                    output.WritePropertyString("uri", DatabaseName + "/" + item.Id.ToString("B").ToUpperInvariant() + "/en/1/" + field.Id.ToString("B").ToUpperInvariant());
                    output.WritePropertyString("name", field.Name);
                    output.WritePropertyString("displayName", field.DisplayName);
                    output.WritePropertyString("value", value);
                    output.WriteEndObject();
                }
                else
                {
                    output.WritePropertyString(field.Name, value);
                }
            }

            if (includeFieldInfo)
            {
                output.WriteEndArray();
            }
            else
            {
                output.WriteEndObject();
            }
        }
Exemple #4
0
        protected virtual void WriteItemChildren(JsonTextWriter output, RequestParameters request, FileDatabaseItem item, int children)
        {
            if (children == 0 || !item.Children.Any())
            {
                return;
            }

            output.WriteStartArray("children");

            foreach (var child in item.Children)
            {
                output.WriteStartObject();
                WriteItemHeader(output, child);
                WriteItemFields(output, request, child);
                WriteItemChildren(output, request, child, children - 1);
                output.WriteEndObject();
            }

            output.WriteEndArray();
        }
        protected void ReadItem([NotNull] JObject jobject, [CanBeNull] FileDatabaseItem parent)
        {
            var itemName        = jobject.GetPropertyValue("name");
            var itemDisplayName = jobject.GetPropertyValue("displayName");
            var icon16X16       = jobject.GetPropertyValue("icon16x16");
            var icon32X32       = jobject.GetPropertyValue("icon32x32");
            var template        = jobject.GetPropertyValue("templateName");
            var path            = jobject.GetPropertyValue("path");
            var mediaUrl        = jobject.GetPropertyValue("mediaUrl");

            var itemIdString = jobject.GetPropertyValue("id");

            if (!Guid.TryParse(itemIdString, out var itemId))
            {
                throw new InvalidOperationException("id is not a valid Guid");
            }

            var templateIdString = jobject.GetPropertyValue("templateId");

            if (!Guid.TryParse(templateIdString, out var templateId))
            {
                throw new InvalidOperationException("templateId is not a valid Guid");
            }

            var childCountString = jobject.GetPropertyValue("childCount");

            if (!int.TryParse(childCountString, out var childCount))
            {
                throw new InvalidOperationException("childCount is not a valid integer");
            }

            var item = new FileDatabaseItem(this, parent, itemId, itemName, itemDisplayName, icon16X16, icon32X32, template, templateId, path, childCount, mediaUrl);

            var fields = (JObject)jobject.Property("fields").Value;

            foreach (var property in fields.Properties())
            {
                var fieldName = property.Name;
                var value     = property.Value.ToString();

                var field = new FileDatabaseField(fieldName, value);

                item.Fields.Add(field);
            }

            Items.Add(item);

            var childrenProperty = jobject.Property("children");

            if (childrenProperty == null)
            {
                return;
            }

            var children = (JArray)childrenProperty.Value;

            foreach (var child in children.OfType <JObject>())
            {
                ReadItem(child, item);
            }
        }