/// <summary>
        /// Create an issue alert entity (subscription) for the user.
        /// </summary>
        /// <param name="user">The user to create an issue alert entity (subsciption) for.</param>
        public void CreateAlert(EntityReference user)
        {
            user.ThrowOnNull("user");

            if (user.LogicalName != "contact")
            {
                throw new ArgumentException(string.Format("Value must have logical name {0}.", user.LogicalName), "user");
            }

            var serviceContext = Dependencies.GetServiceContextForWrite();

            var existingAlert = GetIssueAlertEntity(serviceContext, user);

            if (existingAlert != null)
            {
                return;
            }

            var alert = new Entity("adx_issuealert");

            alert["adx_subscriberid"] = user;
            alert["adx_issueid"]      = Issue;

            serviceContext.AddObject(alert);
            serviceContext.SaveChanges();
        }
        private static void Associate(CrmDbContext context, ContentMap map, EntityReference target, Relationship relationship, EntityReferenceCollection relatedEntities)
        {
            target.ThrowOnNull("target");
            relationship.ThrowOnNull("relationship");
            relatedEntities.ThrowOnNull("relatedEntities");

            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Target: LogicalName={0}, Id={1}", EntityNamePrivacy.GetEntityName(target.LogicalName), target.Id));
            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Relationship: SchemaName={0}, PrimaryEntityRole={1}", relationship.SchemaName, relationship.PrimaryEntityRole));

            foreach (var entity in relatedEntities)
            {
                ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Related: LogicalName={0}, Id={1}", EntityNamePrivacy.GetEntityName(entity.LogicalName), entity.Id));
            }

            // validate that the relationships do in fact exist by querying for the intersects

            var entities = map.Using(ContentMapLockType.Read, () => RetrieveIntersectEntities(context, map, target, relationship, relatedEntities));

            if (entities != null)
            {
                // add intersect entities to the content map

                map.Using(ContentMapLockType.Write, () => map.AddRange(entities));

                foreach (var added in entities)
                {
                    ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Added: LogicalName={0}, Id={1}", EntityNamePrivacy.GetEntityName(added.LogicalName), added.Id));
                }
            }
        }
        public override void Remove(EntityReference entity)
        {
            entity.ThrowOnNull("entity");

            var message = GetMessage(entity.LogicalName, entity.Id);

            Remove(message);
        }
        public override void Remove(EntityReference entity)
        {
            entity.ThrowOnNull("entity");

            base.Remove(entity);

            Refresh(entity);
        }
Exemple #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CategoryDataAdapter"/> class.
        /// </summary>
        /// <param name="category">Category Entity Reference</param>
        /// <param name="dependencies">Data Adapter Dependencies</param>
        public CategoryDataAdapter(EntityReference category, KnowledgeArticles.IDataAdapterDependencies dependencies)
        {
            category.ThrowOnNull("article");
            category.AssertLogicalName("knowledgearticle");
            dependencies.ThrowOnNull("dependencies");

            this.Category     = category;
            this.Dependencies = dependencies;
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="ideaForum">The idea forum to get and set data for.</param>
        /// <param name="dependencies">The dependencies to use for getting and setting data.</param>
        public IdeaForumDataAdapter(EntityReference ideaForum, IDataAdapterDependencies dependencies)
        {
            ideaForum.ThrowOnNull("ideaForum");
            ideaForum.AssertLogicalName("adx_ideaforum");
            dependencies.ThrowOnNull("dependencies");

            IdeaForum    = ideaForum;
            Dependencies = dependencies;
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="idea">The idea to get and set data for.</param>
        /// <param name="dependencies">The dependencies to use for getting and setting data.</param>
        public IdeaDataAdapter(EntityReference idea, IDataAdapterDependencies dependencies)
        {
            idea.ThrowOnNull("idea");
            idea.AssertLogicalName("adx_idea");
            dependencies.ThrowOnNull("dependencies");

            Idea         = idea;
            Dependencies = dependencies;
        }
        private Entity GetIssueAlertEntity(OrganizationServiceContext serviceContext, EntityReference user)
        {
            serviceContext.ThrowOnNull("serviceContext");
            user.ThrowOnNull("user");

            return(serviceContext.CreateQuery("adx_issuealert")
                   .FirstOrDefault(e => e.GetAttributeValue <EntityReference>("adx_subscriberid") == user &&
                                   e.GetAttributeValue <EntityReference>("adx_issueid") == Issue));
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="issue">The issue to get and set data for.</param>
        /// <param name="dependencies">The dependencies to use for getting and setting data.</param>
        public IssueDataAdapter(EntityReference issue, IDataAdapterDependencies dependencies)
        {
            issue.ThrowOnNull("issue");
            issue.AssertLogicalName("adx_issue");
            dependencies.ThrowOnNull("dependencies");

            Issue        = issue;
            Dependencies = dependencies;
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="article">Knowledge Article Entity Reference</param>
        /// <param name="code">Article language code</param>
        /// <param name="dependencies">Data Adapter Dependencies</param>
        public KnowledgeArticleDataAdapter(EntityReference article, string code, IDataAdapterDependencies dependencies)
        {
            article.ThrowOnNull("article");
            article.AssertLogicalName("knowledgearticle");
            dependencies.ThrowOnNull("dependencies");

            this.KnowledgeArticle = article;
            this.Dependencies     = dependencies;
            this.LanguageCode     = code;
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="issueForum">The issue forum to get and set data for.</param>
        /// <param name="dependencies">The dependencies to use for getting and setting data.</param>
        public IssueForumDataAdapter(EntityReference issueForum, IDataAdapterDependencies dependencies)
        {
            issueForum.ThrowOnNull("issueForum");
            issueForum.AssertLogicalName("adx_issueforum");
            dependencies.ThrowOnNull("dependencies");

            IssueForum   = issueForum;
            Dependencies = dependencies;
            Status       = new int[] { };
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="serviceContext">An <see cref="OrganizationServiceContext"/>.</param>
        /// <param name="securityProvider">An <see cref="ICrmEntitySecurityProvider"/>.</param>
        /// <param name="httpContext">An <see cref="HttpContextBase"/>.</param>
        /// <param name="website">An <see cref="EntityReference"/> to a website.</param>
        /// <param name="portalUser">An <see cref="EntityReference"/> to a portal user.</param>
        public DataAdapterDependencies(OrganizationServiceContext serviceContext, ICrmEntitySecurityProvider securityProvider,
                                       HttpContextBase httpContext, EntityReference website, EntityReference portalUser = null, RequestContext requestContext = null)
        {
            serviceContext.ThrowOnNull("serviceContext");
            securityProvider.ThrowOnNull("securityProvider");
            httpContext.ThrowOnNull("httpContext");
            website.ThrowOnNull("website");

            _serviceContext   = serviceContext;
            _securityProvider = securityProvider;
            _httpContext      = httpContext;
            _website          = website;
            _portalUser       = portalUser;
            _requestContext   = requestContext;
        }
        /// <summary>
        /// Delete an issue alert entity (subscription) for the user.
        /// </summary>
        /// <param name="user">The user to remove an issue alert entity (subsciption) for.</param>
        public void DeleteAlert(EntityReference user)
        {
            user.ThrowOnNull("user");

            var serviceContext = Dependencies.GetServiceContextForWrite();

            var existingAlert = GetIssueAlertEntity(serviceContext, user);

            if (existingAlert == null)
            {
                return;
            }

            serviceContext.DeleteObject(existingAlert);
            serviceContext.SaveChanges();
        }
        private void Disassociate(EntityReference target, Relationship relationship, EntityReferenceCollection relatedEntities)
        {
            target.ThrowOnNull("target");
            relationship.ThrowOnNull("relationship");
            relatedEntities.ThrowOnNull("relatedEntities");

            var contentMapProvider = GetContentMapProvider();

            if (contentMapProvider == null)
            {
                return;
            }

            var map = contentMapProvider.Using(m => m);

            RefreshIntersect(map, relationship);

            contentMapProvider.Disassociate(map, target, relationship, relatedEntities);
        }
        private void Disassociate(CrmDbContext context, ContentMap map, EntityReference target, Relationship relationship, EntityReferenceCollection relatedEntities)
        {
            target.ThrowOnNull("target");
            relationship.ThrowOnNull("relationship");
            relatedEntities.ThrowOnNull("relatedEntities");

            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Target: LogicalName={0}, Id={1}", EntityNamePrivacy.GetEntityName(target.LogicalName), target.Id));
            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Relationship: SchemaName={0}, PrimaryEntityRole={1}", relationship.SchemaName, relationship.PrimaryEntityRole));

            foreach (var entity in relatedEntities)
            {
                ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Related: LogicalName={0}, Id={1}", EntityNamePrivacy.GetEntityName(entity.LogicalName), entity.Id));
            }

            var entities = new List <EntityReference>();

            if (this.EventHubJobSettings.IsEnabled)
            {
                //logic to ignore to get Intersect entity which we already have in eventhub model.
                EntityReference intersectEntity = new EntityReference();
                intersectEntity.LogicalName = target.LogicalName;
                intersectEntity.Id          = target.Id;
                entities.Add(intersectEntity);
            }
            else
            {
                // validate that the relationships do in fact not exist by querying for the intersects
                entities = map.Using(ContentMapLockType.Read, () => RetrieveDisassociatedIntersectEntities(context, map, target, relationship, relatedEntities).ToList());
            }

            if (entities != null)
            {
                // add intersect entities to the content map

                map.Using(ContentMapLockType.Write, () => map.RemoveRange(entities));
            }
        }
        private static EntityNode Refresh(CrmDbContext context, ContentMap map, EntityReference reference)
        {
            reference.ThrowOnNull("reference");

            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("LogicalName={0}, Id={1}", EntityNamePrivacy.GetEntityName(reference.LogicalName), reference.Id));

            EntityDefinition ed;

            if (map.Solution.Entities.TryGetValue(reference.LogicalName, out ed))
            {
                // retrieve a fresh entity which also acts as a backend validation

                var fetch = ed.CreateFetch();

                Entity entity = null;

                try
                {
                    string primaryIdAttribute = EventHubBasedInvalidation.CrmChangeTrackingManager.Instance.TryGetPrimaryKey(reference.LogicalName);

                    // The condition for the filter on primary key
                    var primaryAttributeCondition = new Condition
                    {
                        Attribute = primaryIdAttribute,
                        Operator  = ConditionOperator.Equal,
                        Value     = reference.Id
                    };

                    var attributes = fetch.Entity.Attributes;
                    var fQuery     = new Fetch
                    {
                        Distinct  = true,
                        SkipCache = true,
                        Entity    = new FetchEntity
                        {
                            Name       = reference.LogicalName,
                            Attributes = attributes,
                            Filters    = new List <Filter>()
                            {
                                new Filter
                                {
                                    Type       = LogicalOperator.And,
                                    Conditions = new List <Condition>()
                                    {
                                        primaryAttributeCondition
                                    }
                                }
                            }
                        }
                    };

                    entity = context.Service.RetrieveSingle(fQuery, true, true);
                }
                catch (FaultException <OrganizationServiceFault> fe)
                {
                    // an exception occurs when trying to retrieve a non-existing entity

                    if (!fe.Message.EndsWith("Does Not Exist"))
                    {
                        throw;
                    }
                }

                // Check if the entity matches on the defined relationships.
                if (!ed.ShouldIncludeInContentMap(entity))
                {
                    return(null);
                }

                // check if the entity is inactive according to the definition
                var option = entity != null?entity.GetAttributeValue <OptionSetValue>("statecode") : null;

                var isActive = ed.ActiveStateCode == null || (option != null && ed.ActiveStateCode.Value == option.Value);

                var node = map.Using(ContentMapLockType.Write, () => entity != null
                                        ? (isActive
                                                ? map.Replace(entity)
                                                : map.Deactivate(reference))
                                        : map.Remove(reference));

                return(node);
            }

            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Unknown: logicalName={0}", EntityNamePrivacy.GetEntityName(reference.LogicalName)));

            return(null);
        }