Пример #1
0
        private JSchema AttachFieldData(Quantumart.QP8.BLL.Field qpField, JSchema schema)
        {
            schema.Description =
                !IsHtmlWhiteSpace(qpField.Description)
                    ? qpField.Description
                    : !IsHtmlWhiteSpace(qpField.FriendlyName)
                        ? qpField.FriendlyName
                        : schema.Description;
            
            switch (qpField.ExactType)
            {
                case FieldExactTypes.StringEnum:
                    foreach (var item in qpField.StringEnumItems.Where(item => !item.Invalid))
                    {
                        schema.Enum.Add(item.Value);

                        if (item.IsDefault.GetValueOrDefault())
                        {
                            schema.Default = item.Value;
                        }
                    }
                    break;
            }
            
            return schema;
        }
Пример #2
0
        public DefinitionTreeNode(Quantumart.QP8.BLL.Field fieldNotInDef, string parentPath, string ownPath, bool notInDefinition, bool isFromRelatedContent, IFieldService fieldService)
        {
            Id = ownPath ?? parentPath + Separator + fieldNotInDef.Id;

            text = fieldNotInDef.Name;

            using (fieldService.CreateQpConnectionScope())
            {
                if (isFromRelatedContent)
                {
                    if (!fieldNotInDef.Aggregated)
                    {
                        text += " из контента " + fieldNotInDef.Content.Name;
                    }
                }
            }

            expanded = false;

            //ноды которые не включены в описание не надо позволять раскрывать пока их не включат
            hasChildren = false;

            NotInDefinition = notInDefinition;

            MissingInQp = false;

            if (fieldNotInDef.RelationType != RelationType.None || fieldNotInDef.IsClassifier)
            {
                imageUrl = "images/icons/relation.gif";
                IconName = "link";
            }
        }
Пример #3
0
        private ArticleField DeserializeField(Field fieldInDef, Quantumart.QP8.BLL.Field qpField, IProductDataSource productDataSource, DBConnector connector, Context context)
        {
            ArticleField field;

            if (fieldInDef is BackwardRelationField)
            {
                field = DeserializeBackwardField((BackwardRelationField)fieldInDef, productDataSource, connector, context);
            }
            else if (fieldInDef is EntityField)
            {
                field = DeserializeEntityField((EntityField)fieldInDef, qpField, productDataSource, connector, context);
            }
            else if (fieldInDef is ExtensionField)
            {
                field = DeserializeExtensionField((ExtensionField)fieldInDef, qpField, productDataSource, connector, context);
            }
            else if (fieldInDef is PlainField)
            {
                field = DeserializePlainField(qpField, productDataSource, connector);
            }
            else
            {
                throw new Exception("Неподдерживаемый тип поля: " + fieldInDef.GetType().Name);
            }

            field.ContentId = qpField.ContentId;

            field.FieldId = qpField.Id;

            field.FieldName = string.IsNullOrEmpty(fieldInDef.FieldName) ? qpField.Name : fieldInDef.FieldName;

            field.FieldDisplayName = qpField.DisplayName;

            return(field);
        }
Пример #4
0
        private JSchema GetPlainFieldSchema(Quantumart.QP8.BLL.Field qpField)
        {
            var schema = new JSchema();

            switch (qpField.ExactType)
            {
                case FieldExactTypes.Numeric:
                    schema.Type = qpField.IsInteger ? JSchemaType.Integer : JSchemaType.Number;
                    break;

                case FieldExactTypes.File:
                    schema.Type = JSchemaType.Object;
                    schema.AllowAdditionalProperties = false;

                    schema.Required.Add("Name");
                    schema.Required.Add("AbsoluteUrl");
                    schema.Required.Add("FileSizeBytes");

                    schema.Properties["Name"] = new JSchema { Type = JSchemaType.String };

                    schema.Properties["AbsoluteUrl"] = new JSchema { Type = JSchemaType.String };

                    schema.Properties["FileSizeBytes"] = new JSchema
                    {
                        Type = JSchemaType.Integer,
                        Minimum = 0,
                        ExclusiveMinimum = false
                    };
                    break;

                case FieldExactTypes.Boolean:
                    schema.Type = JSchemaType.Boolean;
                    break;

                case FieldExactTypes.O2MRelation:
                    schema.Type = JSchemaType.Integer;
                    break;

                default:
                    schema.Type = JSchemaType.String;
                    break;
            }

            return AttachFieldData(qpField, schema);
        }
Пример #5
0
        /// <param name="qpField">Can be null</param>
        /// <exception cref="NotSupportedException" />
        /// <exception cref="InvalidOperationException" />
        private JSchema GetFieldSchema(
            Field field, Quantumart.QP8.BLL.Field qpField, SchemaContext context, bool forList)
        {
            if (qpField == null && !(field is BaseVirtualField))
            {
                qpField = _fieldService.Read(field.FieldId);
            }

            if (field is PlainField)
            {
                return GetPlainFieldSchema(qpField);
            }
            else if (field is BackwardRelationField backwardRelationalField)
            {
                JSchema backwardItemSchema = GetSchemaRecursive(backwardRelationalField.Content, context, forList);

                return AttachFieldData(qpField, new JSchema
                {
                    Type = JSchemaType.Array, Items = { backwardItemSchema }
                });
            }
            else if (field is EntityField entityField)
            {
                Content fieldContent = entityField.Content;

                if (qpField.RelationType == Quantumart.QP8.BLL.RelationType.OneToMany)
                {
                    return AttachFieldData(qpField, GetSchemaRecursive(fieldContent, context, forList));
                }
                else
                {
                    JSchema arrayItemSchema = GetSchemaRecursive(fieldContent, context, forList);

                    return AttachFieldData(qpField, new JSchema
                    {
                        Type = JSchemaType.Array, Items = { arrayItemSchema }
                    });
                }
            }
            else
            {
                throw new NotSupportedException($"Field type {field.GetType()} is not supported");
            }
        }
Пример #6
0
        private ArticleField DeserializeExtensionField(ExtensionField fieldInDef, Quantumart.QP8.BLL.Field qpField, IProductDataSource productDataSource, DBConnector connector, Context context)
        {
            var extensionArticleField = new ExtensionArticleField();

            string fieldName = string.IsNullOrEmpty(fieldInDef.FieldName) ? qpField.Name : fieldInDef.FieldName;

            string contentName = productDataSource.GetString(fieldName);

            if (!string.IsNullOrEmpty(contentName))
            {
                Models.Configuration.Content valueDef = fieldInDef.ContentMapping.Values.FirstOrDefault(x => _contentService.Read(x.ContentId).NetName == contentName);

                if (valueDef == null)
                {
                    throw new Exception(
                              $"'{contentName}' value is not found in an available extension content list, id = {fieldInDef.FieldId}");
                }

                extensionArticleField.Value        = valueDef.ContentId.ToString();
                extensionArticleField.SubContentId = valueDef.ContentId;

                IProductDataSource extensionDataSource = productDataSource.GetExtensionContainer(fieldName, contentName);

                extensionArticleField.Item = DeserializeArticle(extensionDataSource, valueDef, connector, context);

                if (extensionArticleField.Item.Id == productDataSource.GetArticleId())
                {
                    var id = GetExtensionId(connector, valueDef.ContentId, fieldInDef.FieldId, extensionArticleField.Item.Id);

                    if (id.HasValue)
                    {
                        extensionArticleField.Item.Id = id.Value;
                    }
                    else
                    {
                        context.AddExtensionArticle(extensionArticleField.Item.Id, extensionArticleField.Item);
                        extensionArticleField.Item.Id = default(int);
                    }
                }
            }

            return(extensionArticleField);
        }
Пример #7
0
        private ArticleField DeserializeEntityField(EntityField fieldInDef, Quantumart.QP8.BLL.Field qpField, IProductDataSource productDataSource, DBConnector connector, Context context)
        {
            string fieldName = fieldInDef.FieldName ?? qpField.Name;

            ArticleField articleField;

            if (qpField.RelationType == RelationType.OneToMany)
            {
                articleField = new SingleArticleField
                {
                    Item         = DeserializeArticle(productDataSource.GetContainer(fieldName), fieldInDef.Content, connector, context),
                    Aggregated   = qpField.Aggregated,
                    SubContentId = fieldInDef.Content.ContentId
                };
            }
            else if (qpField.RelationType == RelationType.ManyToMany || qpField.RelationType == RelationType.ManyToOne)
            {
                var multiArticleField = new MultiArticleField {
                    SubContentId = fieldInDef.Content.ContentId
                };

                var containersCollection = productDataSource.GetContainersCollection(fieldName);

                if (containersCollection != null)
                {
                    foreach (Article article in containersCollection.Select(x => DeserializeArticle(x, fieldInDef.Content, connector, context)))
                    {
                        multiArticleField.Items.Add(article.Id, article);
                    }
                }

                articleField = multiArticleField;
            }
            else
            {
                throw new Exception(string.Format("Field definition id={0} has EntityField type but its RelationType is not valid", fieldInDef.FieldId));
            }

            return(articleField);
        }
Пример #8
0
        /// <exception cref="NotSupportedException" />
        /// <exception cref="InvalidOperationException" />
        private void MergeExtensionFieldSchema(
            ExtensionField field, Quantumart.QP8.BLL.Field qpField, JSchema contentSchema, SchemaContext context)
        {
            JSchema extensionSchema = AttachFieldData(qpField, new JSchema { Type = JSchemaType.String });

            foreach (Content content in field.ContentMapping.Values)
            {
                var qpContent = _contentService.Read(content.ContentId);
                if (!String.IsNullOrWhiteSpace(qpContent.NetName))
                {
                    extensionSchema.Enum.Add(qpContent.NetName);
                }
            }

            contentSchema.Properties.Add(field.FieldName, extensionSchema);

            var fieldGroups = field.ContentMapping.Values
                .SelectMany(extContent =>
                {
                    var fields = extContent.Fields
                        .Select(extField => new
                        {
                            extField.FieldName,

                            FieldSchema = GetFieldSchema(
                                extField, _fieldService.Read(extField.FieldId), context, false)
                        });

                    if (extContent.LoadAllPlainFields)
                    {
                        var qpFields = _fieldService.List(extContent.ContentId).ToArray();

                        var fieldsToAdd = qpFields
                            .Where(f => f.RelationType == Quantumart.QP8.BLL.RelationType.None
                                && extContent.Fields.All(y => y.FieldId != f.Id))
                            .Where(f => !context.IgnoredFields
                                .Any(t => t.Item1.Equals(extContent) && t.Item2.FieldId == f.Id));

                        fields = fields.Concat(fieldsToAdd.Select(extQpField => new
                        {
                            FieldName = extQpField.Name,
                            FieldSchema = GetPlainFieldSchema(extQpField)
                        }));
                    }

                    return fields;
                })
                .GroupBy(x => x.FieldName);

            foreach (var fieldGroup in fieldGroups)
            {
                JSchema[] groupSchemas = fieldGroup.Select(pair => pair.FieldSchema).ToArray();

                JSchema sameNameExtensionFieldsSchema;
                if (groupSchemas.Length > 1)
                {
                    sameNameExtensionFieldsSchema = new JSchema { Type = JSchemaType.Object };

                    foreach (JSchema extFieldSchema in groupSchemas)
                    {
                        sameNameExtensionFieldsSchema.OneOf.Add(extFieldSchema);
                    }
                }
                else
                {
                    sameNameExtensionFieldsSchema = groupSchemas[0];
                }

                contentSchema.Properties[fieldGroup.Key] = sameNameExtensionFieldsSchema;
            }
        }
Пример #9
0
        private ArticleField DeserializePlainField(Quantumart.QP8.BLL.Field plainFieldFromQP, IProductDataSource productDataSource, DBConnector connector)
        {
            var field = new PlainArticleField
            {
                PlainFieldType = (PlainFieldType)plainFieldFromQP.ExactType
            };

            switch (field.PlainFieldType)
            {
            case PlainFieldType.Date:
            case PlainFieldType.DateTime:

                DateTime?dt = productDataSource.GetDateTime(plainFieldFromQP.Name);

                if (dt.HasValue)
                {
                    field.Value = dt.ToString();

                    field.NativeValue = dt;
                }
                break;

            case PlainFieldType.Numeric:
            case PlainFieldType.O2MRelation:
            {
                object number;
                if (plainFieldFromQP.IsInteger ||
                    plainFieldFromQP.RelationType == RelationType.OneToMany)
                {
                    number = productDataSource.GetInt(plainFieldFromQP.Name);
                }
                else
                {
                    var dec = productDataSource.GetDecimal(plainFieldFromQP.Name);
                    number = !plainFieldFromQP.IsDecimal && dec.HasValue ? (object)Convert.ToDouble(dec) : dec;
                }

                if (number != null)
                {
                    field.Value = number.ToString();

                    field.NativeValue = number;
                }
            }
            break;

            case PlainFieldType.Image:
                string imageUrl = productDataSource.GetString(plainFieldFromQP.Name);

                if (imageUrl != null)
                {
                    string imageName = Common.GetFileNameByUrl(connector, plainFieldFromQP.Id, imageUrl);
                    field.NativeValue = field.Value = imageName;
                }
                break;

            case PlainFieldType.File:
                if (productDataSource is EditorJsonProductDataSource)
                {
                    string fileName = productDataSource.GetString(plainFieldFromQP.Name);
                    field.NativeValue = field.Value = fileName;
                }
                else
                {
                    IProductDataSource fileContainer = productDataSource.GetContainer(plainFieldFromQP.Name);

                    if (fileContainer != null)
                    {
                        string fileUrl  = fileContainer.GetString("AbsoluteUrl");
                        string fileName = Common.GetFileNameByUrl(connector, plainFieldFromQP.Id, fileUrl);
                        field.NativeValue = field.Value = fileName;
                    }
                }
                break;

            case PlainFieldType.Boolean:
            {
                decimal?number = productDataSource.GetDecimal(plainFieldFromQP.Name);

                if (number != null)
                {
                    field.Value = number.ToString();

                    field.NativeValue = number;
                }
            }
            break;

            default:
                field.NativeValue = field.Value = productDataSource.GetString(plainFieldFromQP.Name);
                break;
            }

            field.Value = field.Value ?? string.Empty;

            return(field);
        }