Exemplo n.º 1
0
        protected virtual void OnSelecting(CrmMetadataDataSourceSelectingEventArgs args)
        {
            EventHandler <CrmMetadataDataSourceSelectingEventArgs> handler = base.Events[EventSelecting] as EventHandler <CrmMetadataDataSourceSelectingEventArgs>;

            if (handler != null)
            {
                handler(this, args);
            }
        }
Exemplo n.º 2
0
        protected override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
        {
            Tracing.FrameworkInformation("CrmMetadataDataSourceView", "ExecuteSelect", "Begin");

            if (CanSort)
            {
                arguments.AddSupportedCapabilities(DataSourceCapabilities.Sort);
            }

            if (CanPage)
            {
                arguments.AddSupportedCapabilities(DataSourceCapabilities.Page);
            }

            if (CanRetrieveTotalRowCount)
            {
                arguments.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount);
            }

            // merge SelectParameters
            IOrderedDictionary parameters = SelectParameters.GetValues(Context, Owner);

            string sortExpression = GetNonNullOrEmpty(
                arguments.SortExpression,
                parameters[_sortExpressionParameterName] as string,
                SortExpression);
            string entityName = GetNonNullOrEmpty(
                parameters[_entityNameParameterName] as string,
                EntityName);
            string attributeName = GetNonNullOrEmpty(
                parameters[_attributeNameParameterName] as string,
                AttributeName);

            var metadataFlags = MetadataFlags;

            if (parameters.Contains(_metadataFlagsParameterName))
            {
                metadataFlags = (parameters[_metadataFlagsParameterName] as string).ToEnum <EntityFilters>();
            }

            var entityFlags = EntityFlags;

            if (parameters.Contains(_entityFlagsParameterName))
            {
                entityFlags = (parameters[_entityFlagsParameterName] as string).ToEnum <EntityFilters>();
            }

            // raise pre-event
            CrmMetadataDataSourceSelectingEventArgs selectingArgs = new CrmMetadataDataSourceSelectingEventArgs(
                Owner,
                arguments,
                entityName,
                attributeName,
                metadataFlags,
                entityFlags,
                sortExpression);

            OnSelecting(selectingArgs);

            if (selectingArgs.Cancel)
            {
                Tracing.FrameworkInformation("CrmMetadataDataSourceView", "ExecuteSelect", "Cancel");
                return(null);
            }

            // merge event results
            arguments.RaiseUnsupportedCapabilitiesError(this);
            sortExpression = selectingArgs.SortExpression;
            entityName     = selectingArgs.EntityName;
            attributeName  = selectingArgs.AttributeName;
            metadataFlags  = selectingArgs.MetadataFlags;
            entityFlags    = selectingArgs.EntityFlags;

            if (CancelSelectOnNullParameter && string.IsNullOrEmpty(entityName) && string.IsNullOrEmpty(attributeName))
            {
                Tracing.FrameworkInformation("CrmMetadataDataSourceView", "ExecuteSelect", "CancelSelectOnNullParameter");
                return(null);
            }

            IEnumerable result       = null;
            int         rowsAffected = 0;

            try
            {
                if (Owner.CacheParameters.Enabled)
                {
                    var cacheKey = GetCacheKey(metadataFlags, entityFlags, entityName, attributeName, sortExpression);

                    result = ObjectCacheManager.Get(cacheKey,
                                                    cache =>
                    {
                        var metadata = ExecuteSelect(entityName, attributeName, sortExpression, entityFlags, metadataFlags, out rowsAffected);
                        return(metadata);
                    },
                                                    (cache, metadata) =>
                    {
                        if (metadata != null)
                        {
                            var dependencies = GetCacheDependencies(metadata);
                            cache.Insert(cacheKey, metadata, dependencies);
                        }
                    });
                }
                else
                {
                    result = ExecuteSelect(entityName, attributeName, sortExpression, entityFlags, metadataFlags, out rowsAffected);
                }
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                Tracing.FrameworkError("CrmMetadataDataSourceView", "ExecuteSelect", "{0}\n\n{1}", ex.Detail.InnerXml, ex);
            }
            catch (Exception e)
            {
                Tracing.FrameworkError("CrmMetadataDataSourceView", "ExecuteSelect", "Exception: {0}", e);

                // raise post-event with exception
                CrmMetadataDataSourceStatusEventArgs selectedExceptionArgs = new CrmMetadataDataSourceStatusEventArgs(0, e);
                OnSelected(selectedExceptionArgs);

                if (!selectedExceptionArgs.ExceptionHandled)
                {
                    throw;
                }

                return(result);
            }

            // raise post-event
            CrmMetadataDataSourceStatusEventArgs selectedArgs = new CrmMetadataDataSourceStatusEventArgs(rowsAffected, null);

            OnSelected(selectedArgs);

            Tracing.FrameworkInformation("CrmMetadataDataSourceView", "ExecuteSelect", "End");

            return(result);
        }