public static AssociationHelperResult BuildAssociationsToPrefetch(IAssociationPrefetcherRequest request, ApplicationSchemaDefinition schema) { var schemaAssociations = schema.GetProperty(ApplicationSchemaPropertiesCatalog.PreFetchAssociations); var requestToFetch = request.AssociationsToFetch; var result = new AssociationHelperResult(); var toFetch = new HashedSet <string>(); var toAvoid = new HashedSet <string>(); result.ToFetch = toFetch; result.ToAvoid = toAvoid; if (schemaAssociations == null && requestToFetch == null) { toFetch.AddReturn(None); return(result); } if (All.Equals(schemaAssociations)) { toFetch.Add(None.Equals(requestToFetch) ? None : All); return(result); } if (All.Equals(requestToFetch)) { toFetch.AddReturn(All); return(result); } if (schemaAssociations != null) { toFetch.AddAll(schemaAssociations.Split(',')); if (AllButSchema.Equals(requestToFetch)) { toAvoid.AddAll(schemaAssociations.Split(',')); } } if (requestToFetch != null) { toFetch.AddAll(requestToFetch.Split(',')); } return(result); }
//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); }