static void TimeStartedProperty_Setting(object sender, ValueTranslationEventArgs e)
 {
     if (TimeStartedProperty.GetValue((ServiceInstance)e.Entity) > DateTime.MinValue)
     {
         e.Cancel = true;
     }
 }
        /// <summary>
        /// Shortcut for mapping a list from a subquery.
        /// </summary>
        public static Mapping <ParentT> MapListFromSubquery <ParentT, ItemT>(
            this Mapping <ParentT> mapping,
            EntityProperty <ParentT, List <ItemT> > listProperty,
            string subqueryName,
            Action <Mapping <ParentT> > parentMappingFunction,
            Action <Mapping <ItemT> > itemMappingFunction
            )
        {
            mapping.Map <List <ItemT> >(listProperty, list => list
                                        .Subquery <ItemT>(subqueryName, subquery => subquery
                                                          .Map <ParentT>("parent", parentMappingFunction)
                                                          .Map <ItemT>("item", itemMappingFunction)
                                                          .Do(context =>
            {
                var parent = context.GetVariable <ParentT>("parent");
                var item   = context.GetVariable <ItemT>("item");
                var l      = listProperty.GetValue(parent);
                if (l == null)
                {
                    l = new List <ItemT>();
                    listProperty.SetValue(parent, l);
                }

                l.Add(item);

                // This has no real value but helps makes sense of this cruel world
                context.Target = item;
            })
                                                          )
                                        );

            return(mapping);
        }
        /// <summary>
        /// Shortcut for mapping a collection from a subquery.
        /// </summary>
        public static Mapping <ParentT> MapListFromSubquery <ParentT, ItemT>(
            this Mapping <ParentT> mapping,
            EntityProperty <ParentT, List <ItemT> > listProperty,
            string subqueryName,
            Action <Mapping <ParentT> > parentMappingFunction,
            Action <Mapping <ItemT> > itemMappingFunction
            )
        {
            mapping.Map <List <ItemT> >(listProperty, list => list
                                        .Subquery <ItemT>(subqueryName, subquery => subquery

                                                          .WhenInbound(inbound => inbound
                                                                       .Map <ParentT>("parent", parentMappingFunction)
                                                                       .Map <ItemT>("item", itemMappingFunction)
                                                                       .Do(context =>
            {
                var parent = context.GetVariable <ParentT>("parent");
                var item   = context.GetVariable <ItemT>("item");
                var l      = listProperty.GetValue(parent);
                if (l == null)
                {
                    l = new List <ItemT>();
                    listProperty.SetValue(parent, l);
                }

                l.Add(item);

                // This has no real purpose but helps makes sense of this cruel world
                context.MappedValue = item;
            })
                                                                       )
                                                          .WhenOutbound(outbound =>
            {
                // Define the outbound source from the context, since in outbound mode we have the parent context
                // TODO: check significance of IsDeferred
                outbound.OutboundSource(context =>
                {
                    List <ItemT> l = list.FromContext(context);
                    if (l == null)
                    {
                        return(null);
                    }
                    else
                    {
                        return(l.GetEnumerator());
                    }
                });

                // Apply the item mappings to the outbound definition
                itemMappingFunction(outbound);
            }
                                                                        )
                                                          )
                                        );

            return(mapping);
        }
        /// <summary>
        /// Shortcut for mapping a dictionary from a subquery.
        /// </summary>
        public static Mapping <ParentT> MapDictionaryFromSubquery <ParentT, KeyT, ValueT>(
            this Mapping <ParentT> mapping,
            EntityProperty <ParentT, Dictionary <KeyT, ValueT> > dictionaryProperty,
            string subqueryName,
            Action <Mapping <ParentT> > parentMappingFunction,
            Action <Mapping <KeyT> > keyMappingFunction,
            Action <Mapping <ValueT> > valueMappingFunction
            )
        {
            mapping.Map <Dictionary <KeyT, ValueT> >(dictionaryProperty, dictionary => dictionary
                                                     .Subquery(subqueryName, subquery => subquery
                                                               .Map <ParentT>("parent", parentMappingFunction)
                                                               .Map <KeyT>("key", keyMappingFunction)
                                                               .Map <ValueT>("value", valueMappingFunction)
                                                               .Do(context => dictionaryProperty.GetValue(context.GetVariable <ParentT>("parent")).Add(
                                                                       context.GetVariable <KeyT>("key"),
                                                                       context.GetVariable <ValueT>("value")
                                                                       )
                                                                   )
                                                               )
                                                     );

            return(mapping);
        }
Example #5
0
        /// <summary>
        /// Evaluates the property expansion, returning more details.
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        private TokenEvaluationResults EvaluateInternal(string propertyName, EvaluateFlags flags, bool useAdvancedMethod)
        {
            if (propertyName == null)
            {
                throw new ArgumentNullException("propertyName");
            }
            if (propertyName.Length == 0)
            {
                throw new ArgumentOutOfRangeException("'propertyName' is zero-length.");
            }

            // mbr - 13-06-2007 - force string?
            bool forceString = false;

            if ((int)(flags & EvaluateFlags.ConvertResultToString) != 0)
            {
                forceString = true;
            }

            // check...
            if (Host == null)
            {
                throw new InvalidOperationException("'Host' is null.");
            }

            // walk...
            try
            {
                // mbr - 02-02-2006 - if the context is dictionary, then go in one level...
                object useObject  = null;
                string toEvaluate = propertyName;
                if (Context is IDictionary)
                {
                    // trim...
                    int    index     = propertyName.IndexOf(".");
                    string remaining = null;
                    if (index != -1)
                    {
                        propertyName = toEvaluate.Substring(0, index).Trim();
                        remaining    = toEvaluate.Substring(index + 1).Trim();
                    }

                    // get...
                    useObject = ((IDictionary)this.Context)[propertyName];
                    if (useObject == null)
                    {
                        var e = new TokenEvaluatorEventArgs(propertyName);
                        this.OnTokenNotFound(e);

                        // return...
                        if (e.WasResultSet)
                        {
                            // mbr - 2013-06-07 - step through into the remaining items...
                            //return new TokenEvaluationResults(null, DoStringConversion(e.Result));
                            useObject = e.Result;
                        }
                        else
                        {
                            // mbr - 2009-04-24 - if we are ignoring missing values, send back an error...
                            if ((int)(flags & EvaluateFlags.IgnoreMissingValues) != 0)
                            {
                                return(new TokenEvaluationResults(useObject, null, null, null, string.Format("(Couldn't evaluate [{0}])", propertyName)));
                            }
                            else
                            {
                                return(null);
                            }
                        }
                    }

                    // anything else?
                    if (remaining == null || remaining.Length == 0)
                    {
                        if (forceString)
                        {
                            return(new TokenEvaluationResults(useObject, null, DoStringConversion(useObject)));
                        }
                        else
                        {
                            return(new TokenEvaluationResults(useObject, null, useObject));
                        }
                    }

                    // flip...
                    toEvaluate = remaining;
                }
                else
                {
                    useObject = this.Context;
                }

                // mbr - 25-07-2008 - ok, if we have an entity and a single part expression, look for the field, otherwise use data binder...
                if (useAdvancedMethod && useObject is Entity && toEvaluate.IndexOf(".") == -1)
                {
                    // find that member!
                    EntityType et = EntityType.GetEntityType(useObject, OnNotFound.ThrowException);
                    if (et == null)
                    {
                        throw new InvalidOperationException("et is null.");
                    }

                    // we're looking for a value...
                    object       value      = null;
                    EntityMember usedMember = null;

                    // find...
                    EntityField field = et.Fields[toEvaluate];
                    if (field != null)
                    {
                        usedMember = field;
                        value      = field.GetValue(useObject);
                    }
                    else
                    {
                        EntityProperty prop = et.Properties[toEvaluate];
                        if (prop != null)
                        {
                            usedMember = prop;
                            value      = prop.GetValue(useObject);
                        }
                        else
                        {
//							ChildToParentEntityLink link = (ChildToParentEntityLink)et.ChildLinks[toEvaluate];
//							if(link != null)
//							{
//								usedMember = link;
//								value = link.GetValue(useObject);
//							}
//							else
                            value = DataBinder.Eval(useObject, toEvaluate);
                        }
                    }

                    // convert...
                    if (forceString)
                    {
                        value = DoStringConversion(value);
                    }

                    // return the value...
                    return(new TokenEvaluationResults(useObject, usedMember, value));
                }
                else
                {
                    // eval from there...
                    object value = DataBinder.Eval(useObject, toEvaluate);
                    if (forceString)
                    {
                        value = DoStringConversion(value);
                    }

                    // return the value...
                    return(new TokenEvaluationResults(useObject, null, value));
                }
            }
            catch (Exception ex)
            {
                if (this.Log.IsWarnEnabled)
                {
                    this.Log.Warn(string.Format("Failed to evaluate '{0}'.", propertyName), ex);
                }

                // mbr - 25-07-2008 - changed to use object...
                //return string.Format("(Couldn't evaluate [{0}])", propertyName);
                return(new TokenEvaluationResults(null, null, null, ex, string.Format("(Couldn't evaluate [{0}])", propertyName)));
            }
        }