/// <summary>
        /// Fetch results for calculated fields.
        /// </summary>
        /// <remarks>
        /// Extension method for processing a single entity and 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="entityId">The root entity for which the calculation is being evaluated.</param>
        /// <param name="settings">Additional settings.</param>
        /// <returns>Calculation results.</returns>
        public static object GetCalculatedFieldValue(this ICalculatedFieldProvider provider, long fieldId, long entityId, CalculatedFieldSettings settings)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");                // assert false
            }
            var entities = new[] { entityId };

            // Call provider
            var results = provider.GetCalculatedFieldValues(fieldId, entities, settings);

            // Extract results
            if (results == null)
            {
                throw new Exception("Unexpected null result.");             // assert false .. provider isn't following the rules
            }
            // Parse exception means there are no individual results
            if (results.ParseException != null)
            {
                return(null);
            }

            CalculatedFieldSingleResult singleResult = results.Entities.FirstOrDefault();

            if (singleResult == null)
            {
                throw new Exception("Could not load calculated field for entity. Likely cause that entity could not be loaded.");
            }

            // Return result
            return(singleResult.Result);
        }
Пример #2
0
        /// <summary>
        /// Get the calculated field value for the specified entities.
        /// </summary>
        /// <param name="fieldMetadata">The metadata for the calculated field.</param>
        /// <param name="entityIDs">Entities</param>
        /// <param name="evalSettings">Evaluation settings</param>
        /// <returns>List of results.</returns>
        private IReadOnlyCollection <CalculatedFieldSingleResult> GetSingleFieldValues(CalculatedFieldMetadata fieldMetadata, IReadOnlyCollection <long> entityIDs, EvaluationSettings evalSettings)
        {
            if (fieldMetadata == null)
            {
                throw new ArgumentNullException("fieldMetadata");
            }
            if (entityIDs == null)
            {
                throw new ArgumentNullException("entityIDs");
            }

            // Handle statically broken calculated-fields
            // (may be null due to not being a calculated field, or the script failing to compile)
            if (fieldMetadata.Expression == null)
            {
                // If the calculated field was somehow invalid, return null for values.
                return(entityIDs.Select(id => new CalculatedFieldSingleResult(id, null)).ToArray());
            }

            // Load entities
            IReadOnlyCollection <IEntity> entities;

            entities = _entityRepository.Get(entityIDs);

            var results = new List <CalculatedFieldSingleResult>(entities.Count);

            // Perform calculations
            foreach (IEntity entity in entities)
            {
                ExpressionRunResult         runResult;
                CalculatedFieldSingleResult singleResult;

                evalSettings.ContextEntity = entity;

                try
                {
                    runResult    = _expressionRunner.Run(fieldMetadata.Expression, evalSettings);
                    singleResult = new CalculatedFieldSingleResult(entity.Id, runResult.Value);
                }
                catch (EvaluationException ex)
                {
                    singleResult = new CalculatedFieldSingleResult(entity.Id, ex);
                }

                results.Add(singleResult);
            }

            return(results);
        }