Esempio n. 1
0
        /// <summary>
        /// Get all template Parties
        /// </summary>
        /// <param name="notificationTemplate">notification template entity</param>
        /// <param name="primaryEntity">primary record</param>
        /// <param name="parameters">list of parameters attribute</param>
        /// <param name="partyListParam">party list parameters in form of JSON string</param>
        /// <param name="service">organization service</param>
        /// <returns>list of party list</returns>
        internal static List <PartyList> GetTemplateParties(EntityCollection notificationTemplate, Entity primaryEntity, Dictionary <string, dynamic> parameters, string partyListParam, Dictionary <string, dynamic> globalConfig, IOrganizationService service)
        {
            List <PartyList> partylist = new List <PartyList>();

            // iterate over Notification template party query entity
            foreach (Entity notification in notificationTemplate.Entities)
            {
                int participationType = 0;

                // get participation type from notification template entity
                if (notification.Attributes.Contains("NotificationParty.didd_participationtype"))
                {
                    participationType = ((OptionSetValue)((AliasedValue)notification.Attributes["NotificationParty.didd_participationtype"]).Value).Value;
                }

                List <Guid> userPartyList = new List <Guid>();
                List <Guid> teamPartyList = new List <Guid>();
                if (notification.Contains("NotificationParty.didd_notificationtemplatepartyid"))
                {
                    // add users to partylist
                    userPartyList = GetUserPartyList((Guid)((AliasedValue)notification.Attributes["NotificationParty.didd_notificationtemplatepartyid"]).Value, service);
                    AddUserToPartyList(userPartyList, partylist, participationType);

                    // add team, to party list
                    teamPartyList = GetTeamPartyList((Guid)((AliasedValue)notification.Attributes["NotificationParty.didd_notificationtemplatepartyid"]).Value, service);
                    AddTeamToPartyList(teamPartyList, partylist, participationType, service);
                }

                // linked entity attrib -if participationtype is To then create task for user
                if (notification.Attributes.Contains("NotificationParty.didd_partylistquery"))
                {
                    string           query   = (string)((AliasedValue)notification.Attributes["NotificationParty.didd_partylistquery"]).Value;
                    EntityCollection entColl = NotificationDataContext.GetCustomPartyList(query, primaryEntity, parameters, service);
                    AddPartyListFromCustomQuery(entColl, partylist, participationType, service);
                }

                // get From party Type
                if (participationType == 273310003)
                {
                    GetFromPartyType(notification, globalConfig);
                }
            }

            // Add partylist from parameter
            AddPartyListFromParameters(partyListParam, partylist);

            return(partylist);
        }
Esempio n. 2
0
        /// <summary>
        /// Send Notifications to stake holders based on Configuration data
        /// </summary>
        /// <param name="pluginContext">execution context</param>
        /// <param name="service">organization service</param>
        /// <param name="tracing">tracing service</param>
        public void SendNotifications(IExecutionContext pluginContext, IOrganizationService service, ITracingService tracing)
        {
            if (tracing == null || pluginContext == null || service == null)
            {
                return;
            }

            tracing.Trace(string.Format(CultureInfo.InvariantCulture, "{0}: Entering NotifyStakeHolders.", "PostNotifyHelper"));
            tracingService = tracing;

            // get Notification global configuration data
            this.globalConfiguration = NotificationDataContext.GetNotificationGlobalData(service);

            // Extracting Input parameter from Action
            Guid   recordId             = new Guid((string)pluginContext.InputParameters["EntityRecordId"]);
            string filterCondition      = (string)pluginContext.InputParameters["FilterCondition"];
            string templateValue        = (string)pluginContext.InputParameters["TemplateId"];
            string entityLogicalName    = (string)pluginContext.InputParameters["EntityLogicalName"];
            string parameterCollections = (string)pluginContext.InputParameters["ParameterCollections"];
            string partyListParameters  = (string)pluginContext.InputParameters["PartyListParameters"];

            if (string.IsNullOrEmpty(parameterCollections))
            {
                parameterCollections = "[]";
            }

            if (string.IsNullOrEmpty(partyListParameters))
            {
                partyListParameters = "[]";
            }

            tracingService.Trace("Party List Parameters :- " + partyListParameters);
            Dictionary <string, dynamic> parameter = (Dictionary <string, dynamic>)JsonHelper.GetObject(parameterCollections, new Dictionary <string, dynamic>().GetType());
            Guid templateId = NotificationDataContext.GetNotificationTemplateId(templateValue, service);

            List <QueryFilter> filter = NotificationDataContext.GetFilterCriteria(filterCondition);

            Entity primaryEntity = NotificationDataContext.GetPrimaryEntity(entityLogicalName, recordId, service);

            // Get Notification Template Entity data
            EntityCollection notificationTemplate = NotificationDataContext.RetrieveNotificationTemplate(templateId, service);

            // Get Notification Template Query Entity data
            EntityCollection relatedQueries = NotificationDataContext.RetrieveRelatedQueries(templateId, service);
            int           countQueries      = relatedQueries.Entities.Count;
            List <Entity> relatedEntities   = new List <Entity>();

            // if no related entity is found, work on primary entity
            if (countQueries == 0)
            {
                relatedQueries            = new EntityCollection();
                relatedQueries.EntityName = entityLogicalName;
                relatedQueries.Entities.Add(primaryEntity);
                relatedEntities.AddRange(relatedQueries.Entities);
            }

            // Execute Related Queries
            for (int i = 0; i < countQueries; i++)
            {
                var relatedEntityColl = NotificationDataContext.RetrieveRelatedEntity(relatedQueries.Entities[i], primaryEntity, parameter, filter, service);

                if (relatedEntityColl.Entities.Count > 0)
                {
                    relatedEntities.AddRange(relatedEntityColl.Entities);
                }
            }

            // remove duplicates from related Query
            relatedEntities = relatedEntities.GroupBy(x => x.Id).Select(r => r.First()).ToList <Entity>();
            Guid?emailTemplateId     = null;
            int? emailTemplateOption = null;

            if (notificationTemplate[0].Contains("didd_emailtemplate") && notificationTemplate[0].Contains("didd_emailtemplateoption"))
            {
                emailTemplateId     = RenderEmailTemplate.GetTemplateId((string)notificationTemplate[0].Attributes["didd_emailtemplate"], service);
                emailTemplateOption = ((OptionSetValue)notificationTemplate[0].Attributes["didd_emailtemplateoption"]).Value;
            }

            if (relatedEntities == null || relatedEntities.Count == 0)
            {
                // no relatedentities records found
                return;
            }

            // Get setup data from notification template
            bool isTask      = (bool)notificationTemplate.Entities[0].Attributes["didd_task"];
            bool isEmail     = (bool)notificationTemplate.Entities[0].Attributes["didd_email"];
            bool isSendEmail = (bool)notificationTemplate.Entities[0].Attributes["didd_sendemail"];

            // Replace token in notification template
            NotificationTokenReplacer ndr = new NotificationTokenReplacer();

            ndr.ReplaceTemplateData(primaryEntity, relatedEntities, parameter, notificationTemplate.Entities[0], this.globalConfiguration["RecordBaseURL"]);

            // Get all party List defined in notification template party
            List <PartyList> partylist = NotificationPartyHandler.GetTemplateParties(notificationTemplate, primaryEntity, parameter, partyListParameters, this.globalConfiguration, service);

            if (isTask && isEmail)
            {
                // Create Email and task both and embed link of task into email
                EntityCollection entColl = NotificationPartyHandler.GenerateEmailAndTask(relatedEntities, partylist, this.globalConfiguration, emailTemplateId, emailTemplateOption, service);
                RenderEmailTemplate.ProcessEmail(entColl, isSendEmail, emailTemplateId, emailTemplateOption, service);
            }
            else if (isTask && !isEmail)
            {
                // Create task
                NotificationPartyHandler.GenerateTask(relatedEntities, partylist, service);
            }
            else if (!isTask && isEmail)
            {
                // Create Email
                EntityCollection entColl = NotificationPartyHandler.GenerateEmail(relatedEntities, partylist, this.globalConfiguration, emailTemplateId, emailTemplateOption, service);
                RenderEmailTemplate.ProcessEmail(entColl, isSendEmail, emailTemplateId, emailTemplateOption, service);
            }

            tracingService.Trace(string.Format(CultureInfo.InvariantCulture, "{0}: Exiting NotifyStakeHolders.", "PostNotifyHelper"));
        }