public void NoneRequestOverriding()
        {
            var result = AssociationHelper.BuildAssociationsToPrefetch(new DetailRequest {
                AssociationsToFetch = "#none"
            }, WithProperty("#all")).ToFetchList;

            Assert.AreEqual("#none", result[0]);
        }
        public void TestMerge()
        {
            var result = AssociationHelper.BuildAssociationsToPrefetch(new DetailRequest {
                AssociationsToFetch = "x"
            }, WithProperty("y")).ToFetchList;

            Assert.AreEqual("y", result[0]);
            Assert.AreEqual("x", result[1]);
        }
        public void RequestNullReturnApp()
        {
            var result = AssociationHelper.BuildAssociationsToPrefetch(_blankRequest, WithProperty("#all")).ToFetchList;

            Assert.AreEqual("#all", result.First());
            result = AssociationHelper.BuildAssociationsToPrefetch(_blankRequest, WithProperty("fromlocation,xxx")).ToFetchList;
            Assert.AreEqual("fromlocation", result[0]);
            Assert.AreEqual("xxx", result[1]);
        }
        public void TestAllButSchema2()
        {
            var result = AssociationHelper.BuildAssociationsToPrefetch(new DetailRequest {
                AssociationsToFetch = AssociationHelper.AllButSchema
            }, new ApplicationSchemaDefinition());

            Assert.IsTrue(result.ShouldResolve("x"));
            Assert.IsTrue(result.ShouldResolve("z"));
            Assert.IsTrue(result.ShouldResolve("y"));
        }
        public void TestAllButSchema()
        {
            var result = AssociationHelper.BuildAssociationsToPrefetch(new DetailRequest {
                AssociationsToFetch = AssociationHelper.AllButSchema
            }, WithProperty("y"));

            Assert.IsTrue(result.ShouldResolve("x"));
            Assert.IsTrue(result.ShouldResolve("z"));
            Assert.IsFalse(result.ShouldResolve("y"));
        }
        public void BothNulls()
        {
            var result = AssociationHelper.BuildAssociationsToPrefetch(_blankRequest, new ApplicationSchemaDefinition()).ToFetchList;

            Assert.AreEqual("#none", result[0]);
        }
        //TODO: add locale,and format options
        public IDictionary <string, BaseAssociationUpdateResult> BuildAssociationOptions(AttributeHolder dataMap, ApplicationMetadata application, IAssociationPrefetcherRequest request)
        {
            var associationsToFetch = AssociationHelper.BuildAssociationsToPrefetch(request, application.Schema);

            if (associationsToFetch.IsNone)
            {
                return(new Dictionary <string, BaseAssociationUpdateResult>());
            }


            IDictionary <string, BaseAssociationUpdateResult> associationOptionsDictionary = new ConcurrentDictionary <string, BaseAssociationUpdateResult>();
            var before = LoggingUtil.StartMeasuring(Log, "starting association options fetching for application {0} schema {1}", application.Name, application.Schema.Name);

            var associations = application.Schema.Associations;
            var tasks        = new List <Task>();
            var ctx          = ContextLookuper.LookupContext();

            #region associations

            foreach (var applicationAssociation in associations)
            {
                if (!associationsToFetch.ShouldResolve(applicationAssociation.AssociationKey))
                {
                    Log.Debug("ignoring association fetching: {0}".Fmt(applicationAssociation.AssociationKey));
                    continue;
                }

                //only resolve the association options for non lazy associations or lazy loaded with value set.
                SearchRequestDto search;
                if (!applicationAssociation.IsLazyLoaded())
                {
                    search = new SearchRequestDto();
                }
                else if (dataMap != null && dataMap.GetAttribute(applicationAssociation.Target) != null)
                {
                    //if the field has a value, fetch only this single element, for showing eventual extra label fields... ==> lookup with a selected value
                    search = new SearchRequestDto();
                    var toAttribute    = applicationAssociation.EntityAssociation.PrimaryAttribute().To;
                    var prefilledValue = dataMap.GetAttribute(applicationAssociation.Target).ToString();
                    search.AppendSearchEntry(toAttribute, prefilledValue);
                }
                else
                {
                    //lazy association with no default value
                    continue;
                }
                var association = applicationAssociation;

                tasks.Add(Task.Factory.NewThread(c => {
                    //this will avoid that one thread impacts any other, for ex: changing metadataid of the query
                    var perThreadContext = ctx.ShallowCopy();
                    Quartz.Util.LogicalThreadContext.SetData("context", perThreadContext);
                    var associationOptions = _associationOptionResolver.ResolveOptions(application, dataMap, association, search);
                    associationOptionsDictionary.Add(association.AssociationKey, new BaseAssociationUpdateResult(associationOptions));
                }, ctx));
            }
            #endregion

            #region optionfields
            foreach (var optionField in application.Schema.OptionFields)
            {
                if (!associationsToFetch.ShouldResolve(optionField.AssociationKey))
                {
                    Log.Debug("ignoring association fetching: {0}".Fmt(optionField.AssociationKey));
                    continue;
                }

                if (optionField.ProviderAttribute == null)
                {
                    //if there´s no provider, there´s nothing to do --> static list
                    continue;
                }
                var field = optionField;
                tasks.Add(Task.Factory.NewThread(c => {
                    Quartz.Util.LogicalThreadContext.SetData("context", c);
                    var associationOptions = _dynamicOptionFieldResolver.ResolveOptions(application, field, dataMap);
                    if (associationOptionsDictionary.ContainsKey(field.AssociationKey))
                    {
                        associationOptionsDictionary.Remove(field.AssociationKey);
                    }
                    associationOptionsDictionary.Add(field.AssociationKey, new BaseAssociationUpdateResult(associationOptions));
                }, ctx));
            }
            #endregion

            Task.WaitAll(tasks.ToArray());
            if (Log.IsDebugEnabled)
            {
                var keys = String.Join(",", associationOptionsDictionary.Keys.Where(k => associationOptionsDictionary[k].AssociationData != null));
                Log.Debug(LoggingUtil.BaseDurationMessageFormat(before, "Finished execution of options fetching. Resolved collections: {0}", keys));
            }


            return(associationOptionsDictionary);
        }