Esempio n. 1
0
        public object Translate(TranslationContext context, ASTNode node)
        {
            // Model mapping
            var result = new CREFModel.RequestExpression();

            if (node.ResultType == null)
            {
                throw new InvalidOperationException("Clinical request expression has no result type.");
            }

            var cardinality = (RequestCardinality)Enum.Parse(typeof(RequestCardinality), node.Attributes["cardinality"].ToString(), true);
            result.Cardinality = cardinality == RequestCardinality.Single ? CREFModel.RequestCardinality.Single : CREFModel.RequestCardinality.Multiple;
            result.CardinalitySpecified = true;

            var requestListType = node.ResultType as ListType;
            var requestType = (requestListType == null ? node.ResultType : requestListType.ElementType) as ObjectType;
            if (requestType == null)
            {
                throw new InvalidOperationException(String.Format("Unable to determine request type from source type: '{0}'.", node.ResultType.Name));
            }

            result.Type = GetQueryType(context.TransformModelType(requestType));
            result.TypeSpecified = true;

            // Translate Codes
            var codes = node.Children.Where(n => n.Name == "codes").FirstOrDefault();
            if (codes != null)
            {
                var codesResult = context.TranslateNode(codes);
                result.Items.Add(codesResult);
            }

            // Translate DateRange
            var dateRange = node.Children.Where(n => n.Name == "dateRange").FirstOrDefault();
            if (dateRange != null)
            {
                var dateRangeResult = context.TranslateNode(dateRange);
                result.Items.Add(dateRangeResult);
            }

            // Validate idProperty, dateProperty, and codeProperty
            ValidateIdProperty(requestType, node.GetAttribute<string>("idProperty"));
            ValidateDateProperty(requestType, node.GetAttribute<string>("dateProperty"));
            ValidateCodeProperty(requestType, node.GetAttribute<string>("codeProperty"));

            // Add status filters if necessary
            var filterValue = GetStatusFilterValue(requestType);
            if (!String.IsNullOrEmpty(filterValue))
            {
                var filter = new CREFModel.FilterExpression();

                filter.Items.Add(result);

                var condition = new CREFModel.BinaryExpression();
                condition.Operator = CREFModel.BinaryOperator.opEqual;
                condition.OperatorSpecified = true;

                condition.Items.Add(new CREFModel.PropertyExpression() { Path = "Status" });
                condition.Items.Add(new CREFModel.ValueExpression() { Type = CREFModel.ValueType.String, TypeSpecified = true, Value = filterValue });

                filter.Items.Add(condition);

                return filter;
            }

            return result;
        }