コード例 #1
0
        /// <summary>
        /// A plug-in that creates a follow-up task activity when a new account is created.
        /// </summary>
        /// <remarks>Register this plug-in on the Create message, account entity,
        /// and asynchronous mode.
        /// </remarks>
        public void Execute(IServiceProvider serviceProvider)
        {
            //Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                                              serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            var service = serviceFactory.CreateOrganizationService(context.UserId);
            OrganizationServiceContext ctx = new OrganizationServiceContext(service);

            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.
                Entity entity = (Entity)context.InputParameters["Target"];

                if (entity.LogicalName != "gcbase_fundcentre")
                {
                    return;
                }

                FaultException ex1 = new FaultException();
                //throw new InvalidPluginExecutionException("test", ex1);

                if (entity.Attributes.Contains("gcbase_estimatedannualbudget") && entity.Attributes.Contains("gcbase_startdate") && entity.Attributes.Contains("gcbase_enddate"))
                {
                    try
                    {
                        //create budget lines
                        //throw new InvalidPluginExecutionException("testing", ex1);
                        int[] fiscalYears = new FiscalYear(entity.GetAttributeValue <DateTime>("gcbase_startdate"), entity.GetAttributeValue <DateTime>("gcbase_enddate")).getFiscalYears();

                        QueryExpression existingFundCentreBudgets = new QueryExpression
                        {
                            EntityName = "gcbase_fundcentrebudget",
                            ColumnSet  = new ColumnSet("gcbase_fundcentre", "gcbase_fiscalyear"),
                            Criteria   = new FilterExpression
                            {
                                Conditions =
                                {
                                    new ConditionExpression {
                                        AttributeName = "gcbase_fundcentre",
                                        Operator      = ConditionOperator.Equal,
                                        Values        =     { entity.Id}
                                    }
                                }
                            }
                        };

                        DataCollection <Entity> fundCentreBudgetsToDelete = service.RetrieveMultiple(existingFundCentreBudgets).Entities;

                        if (fundCentreBudgetsToDelete.Count > 0)
                        {
                            // here we should validate if we have projects pending instead of deleting budgets

                            var currentYears = fundCentreBudgetsToDelete.Select(s => (int)s.GetAttributeValue <OptionSetValue>("gcbase_fiscalyear").Value).ToArray();
                            var newYears     = fiscalYears.ToArray();
                            //newYears.Except(currentYears);

                            var illegalYears = currentYears.Except(newYears);

                            if (illegalYears.Count() > 0)
                            {
                                throw new InvalidPluginExecutionException(@"Cannot save your new start and end dates because there are budgets entered in
                                        fiscal years that fall outside of those dates. If you want to revise the dates please first delete the budgets in 
                                        fiscal years: " + string.Join("-", illegalYears) + " and try again!", ex1);
                            }
                            else
                            {
                                foreach (Entity fcb in fundCentreBudgetsToDelete)
                                {
                                    service.Delete("gcbase_fundcentrebudget", fcb.Id);
                                }
                            }
                        }

                        Array    values = Enum.GetValues(typeof(goal_fiscalyear));
                        string[] fys    = new string[fiscalYears.Count()];
                        int      index  = 0;

                        //throw new InvalidPluginExecutionException("testing", ex1);

                        //EntityReference fundRef = new EntityReference("gcbase_fund", preEntity.GetAttributeValue<EntityReference>("gcbase_fund").Id);
                        var fundEntity      = service.Retrieve("gcbase_fund", entity.GetAttributeValue <EntityReference>("gcbase_fund").Id, new ColumnSet("gcbase_name", "gcbase_fundid", "gcbase_fundtype"));
                        var fundTypeEntity  = service.Retrieve("gcbase_fundtype", fundEntity.GetAttributeValue <EntityReference>("gcbase_fundtype").Id, new ColumnSet("gcbase_name"));
                        var budgetEntryName = entity.GetAttributeValue <string>("gcbase_name") + "-" + fundEntity.GetAttributeValue <string>("gcbase_name") + "-" + fundTypeEntity.GetAttributeValue <string>("gcbase_name");
                        //  throw new InvalidPluginExecutionException(fiscalYears.Count().ToString(), ex1);
                        foreach (int year in fiscalYears)
                        {
                            var amount = entity.GetAttributeValue <Money>("gcbase_estimatedannualbudget");

                            if (amount.Value > 0)
                            {
                                Entity fundCentreBudget = new Entity("gcbase_fundcentrebudget");
                                // fundCentreBudget.Id = Guid.NewGuid();
                                fundCentreBudget["gcbase_amount"] = amount;
                                fys[index] = (string)Enum.GetName(typeof(goal_fiscalyear), year);
                                OptionSetValue fy = new OptionSetValue();
                                fy.Value = year;
                                fundCentreBudget["gcbase_fiscalyear"] = fy;

                                EntityReference fundCentre = new EntityReference("gcbase_fundcentre", entity.Id);
                                fundCentreBudget["gcbase_fundcentre"] = fundCentre;
                                fundCentreBudget["gcbase_name"]       = budgetEntryName + "-" + fy.Value;
                                // ctx.Attach(fundCentreBudget)
                                ctx.AddObject(fundCentreBudget);
                                ctx.SaveChanges();
                            }

                            index++;
                        }
                    }

                    catch (FaultException <OrganizationServiceFault> ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in the PostFundCentreCreate plug-in.", ex);
                    }

                    catch (Exception ex)
                    {
                        tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
                        throw;
                    }
                }
            }
        }
コード例 #2
0
        private void createOrUpdateBudgetLineItems(FundCentreParamaters param, IOrganizationService service, Entity preEntity, Entity entity)
        {
            //using (var ctx = new OrganizationServiceContext(service)) {
            OrganizationServiceContext ctx = new OrganizationServiceContext(service);
            FaultException             ex1 = new FaultException();

            //DELETE existing budget lines
            QueryExpression existingFundCentreBudgets = new QueryExpression
            {
                EntityName = "gcbase_fundcentrebudget",
                ColumnSet  = new ColumnSet("gcbase_fundcentre", "gcbase_fiscalyear"),
                Criteria   = new FilterExpression
                {
                    Conditions =
                    {
                        new ConditionExpression {
                            AttributeName = "gcbase_fundcentre",
                            Operator      = ConditionOperator.Equal,
                            Values        =     { param.id}
                        }
                    }
                }
            };



            if (param.startdate.HasValue && param.enddate.HasValue)
            {
                int[] fiscalYears = new FiscalYear(param.startdate.Value, param.enddate.Value).getFiscalYears();


                DataCollection <Entity> fundCentreBudgetsToDelete = service.RetrieveMultiple(existingFundCentreBudgets).Entities;
                if (fundCentreBudgetsToDelete.Count > 0)
                {
                    // here we should validate if we have projects pending instead of deleting budgets

                    var currentYears = fundCentreBudgetsToDelete.Select(s => (int)s.GetAttributeValue <OptionSetValue>("gcbase_fiscalyear").Value).ToArray();
                    var newYears     = fiscalYears.ToArray();
                    //newYears.Except(currentYears);

                    var illegalYears = currentYears.Except(newYears);

                    if (illegalYears.Count() > 0)
                    {
                        throw new InvalidPluginExecutionException(@"Cannot save your new start and end dates because there are budgets entered in
                        fiscal years that fall outside of those dates. If you want to revise the dates please first delete the budgets in 
                        fiscal years: " + string.Join("-", illegalYears) + " and try again!", ex1);
                    }
                    else
                    {
                        foreach (Entity fcb in fundCentreBudgetsToDelete)
                        {
                            service.Delete("gcbase_fundcentrebudget", fcb.Id);
                        }
                    }
                }

                Array    values = Enum.GetValues(typeof(goal_fiscalyear));
                string[] fys    = new string[fiscalYears.Count()];
                int      index  = 0;

                //QueryExpression fundTypeQry = new QueryExpression
                //{
                //    EntityName = "gcbase_fundtype",
                //    ColumnSet = new ColumnSet("gcbase_name"),
                //    Criteria = new FilterExpression
                //    {
                //        Conditions = {
                //            new ConditionExpression("gcbase_name", ConditionOperator.Equal, "Contribution")
                //        }
                //    }
                //};
                //DataCollection<Entity> fundTypes = service.RetrieveMultiple(fundTypeQry).Entities;
                //Guid contribution = new Guid();

                //foreach (var ft in fundTypes)
                //{
                //    if (ft.Attributes["gcbase_name"].ToString() != "Grant")
                //    {
                //        contribution = ft.Id;
                //    }
                //}


                // throw new InvalidPluginExecutionException(grant.ToString() + contribution.ToString(), ex1);


                foreach (int year in fiscalYears)
                {
                    if (param.amount.Value > 0)
                    {
                        Entity fundCentreBudget = new Entity("gcbase_fundcentrebudget");
                        // fundCentreBudget.Id = Guid.NewGuid();
                        fundCentreBudget["gcbase_amount"] = (Money)param.amount;
                        fys[index] = (string)Enum.GetName(typeof(goal_fiscalyear), year);
                        OptionSetValue fy = new OptionSetValue();
                        fy.Value = year;
                        fundCentreBudget["gcbase_fiscalyear"] = fy;

                        EntityReference fundCentre = new EntityReference("gcbase_fundcentre", param.id);
                        fundCentreBudget["gcbase_fundcentre"] = fundCentre;
                        fundCentreBudget["gcbase_name"]       = param.name + "-" + fy.Value;
                        // ctx.Attach(fundCentreBudget)
                        ctx.AddObject(fundCentreBudget);
                        ctx.SaveChanges();
                    }

                    index++;
                }
            }
        }