Ejemplo n.º 1
0
        /// <summary>
        /// Fetch results for calculated fields.
        /// </summary>
        /// <remarks>
        /// Processes all specified fields for all specified entities.
        /// </remarks>
        /// <param name="fieldIDs">The calculated fields to evaluate.</param>
        /// <param name="entityIDs">The root entities for which the calculation is being evaluated.</param>
        /// <param name="settings">Additional settings.</param>
        /// <returns>Calculation results.</returns>
        public IReadOnlyCollection <CalculatedFieldResult> GetCalculatedFieldValues(IReadOnlyCollection <long> fieldIDs, IReadOnlyCollection <long> entityIDs, CalculatedFieldSettings settings)
        {
            if (fieldIDs == null)
            {
                throw new ArgumentNullException("fieldIDs");
            }
            if (entityIDs == null)
            {
                throw new ArgumentNullException("entityIDs");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            // Create the evaluators for this field
            IReadOnlyCollection <CalculatedFieldMetadata> fieldsMetadata;

            fieldsMetadata = _metadataProvider.GetCalculatedFieldMetadata(fieldIDs, settings);

            // Create result container
            List <CalculatedFieldResult> results = new List <CalculatedFieldResult>(fieldIDs.Count);

            // Create expression evaluator settings
            EvaluationSettings evalSettings = CreateEvaluationSettings(settings);

            // Fill
            foreach (long fieldId in fieldIDs)
            {
                CalculatedFieldMetadata fieldMetadata;
                IReadOnlyCollection <CalculatedFieldSingleResult> fieldResults;
                CalculatedFieldResult result;

                // Find the evaluator
                // (Deemed cheaper to just scan, rather than use a dictionary)
                fieldMetadata = fieldsMetadata.First(fe => fe.CalculatedFieldId == fieldId);

                if (fieldMetadata.Exception != null)
                {
                    result = new CalculatedFieldResult(fieldId, fieldMetadata.Exception);
                }
                else
                {
                    // And get the values
                    fieldResults = GetSingleFieldValues(fieldMetadata, entityIDs, evalSettings);
                    result       = new CalculatedFieldResult(fieldId, fieldResults);
                }

                results.Add(result);
            }

            return(results);
        }
        /// <summary>
        /// Fetch results for calculated fields.
        /// </summary>
        /// <remarks>
        /// Extension method for processing a single field.
        /// Script errors cause null result.
        /// </remarks>
        /// <param name="provider">The calculated field provider.</param>
        /// <param name="fieldId">The calculated field to evaluate.</param>
        /// <param name="entityIds">The root entities for which the calculation is being evaluated.</param>
        /// <param name="settings">Additional settings.</param>
        /// <returns>Calculation results.</returns>
        public static CalculatedFieldResult GetCalculatedFieldValues(this ICalculatedFieldProvider provider, long fieldId, IReadOnlyCollection <long> entityIds, CalculatedFieldSettings settings)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");                // assert false
            }
            var fields = new[] { fieldId };

            // Call provider
            var results = provider.GetCalculatedFieldValues(fields, entityIds, settings);

            // Extract results
            CalculatedFieldResult result = results.FirstOrDefault();

            if (result == null)
            {
                throw new Exception("Unexpected null result.");             // assert false .. provider isn't following the rules
            }
            return(result);
        }