Пример #1
0
    private void CreateKPI(int organizationID, int areaID, int projectID, int activityID, int personID)
    {
        PopulateDatabaseDSTableAdapters.QueriesTableAdapter qta =
            new PopulateDatabaseDSTableAdapters.QueriesTableAdapter();

        int kpisToGenerate = r.Next(0, Convert.ToInt32(KPIDropDownList.SelectedValue));

        for (int i = 0; i < kpisToGenerate; i++)
        {
            KPITest         theTest       = kpiTests[r.Next(0, kpiTests.Count)];                                                     // Randomply select a test KPI
            bool            hasTarget     = r.Next(0, 10) < Convert.ToInt32(KPITargetsDropDownList.SelectedValue) ? true : false;    // 60% of the time the KPI has a target
            bool            hasCategories = r.Next(0, 10) < Convert.ToInt32(KPICategoriesDropDownList.SelectedValue) ? true : false; // 80% of the time the KPI doesn not have a target
            int             noCategories  = r.Next(1, Convert.ToInt32(CategoriesDropDownList.SelectedValue));
            List <Category> cat           = new List <Category>();
            decimal?        target        = null;
            if (hasCategories)
            {
                for (int j = 0; j < noCategories; j++)
                {
                    List <CategoryItem> items = new List <CategoryItem>();
                    foreach (CategoryItem catItem in categories[j].Items)
                    {
                        decimal?cattarget = null;
                        if (hasTarget)
                        {
                            cattarget = (Decimal)(r.NextDouble() * 500);
                        }
                        items.Add(new CategoryItem(catItem.ItemID, catItem.ItemName, cattarget));
                    }
                    cat.Add(new Category(categories[j].ID, categories[j].ID, items));
                }
            }

            if (hasTarget && !hasCategories)
            {
                target = (Decimal)(r.NextDouble() * 500);
            }

            DateTime startDate = DateTime.MinValue;

            if (hasTarget)
            {
                startDate = DateTime.Today.Subtract(new TimeSpan(r.Next(5, 350), 0, 0, 0, 0));
            }

            string unitID = "";
            switch (theTest.Type)
            {
            case "GENINT": unitID = "INT"; break;

            case "GENPER": unitID = "PERCENT"; break;

            case "GENTIME": unitID = "TIME"; break;

            case "GENDEC": unitID = "DECIMAL"; break;

            default:
                unitID = ""; break;
            }

            string reportUnit = reportingUnits[r.Next(0, reportingUnits.Length)];

            int currencyIndex = r.Next(0, currencies.Length);
            int kpiID         = KPIBLL.CreateKPI(organizationID,
                                                 areaID,
                                                 projectID,
                                                 activityID,
                                                 personID,
                                                 theTest.Name,
                                                 unitID,
                                                 theTest.Direction,
                                                 theTest.Strategy,
                                                 startDate,
                                                 reportUnit,
                                                 hasTarget ? r.Next(0, 350) : 0,
                                                 currencies[currencyIndex].Currency,
                                                 currencies[currencyIndex].Unit,
                                                 theTest.Type,
                                                 cat.ToArray(),
                                                 target,
                                                 currentUsers[r.Next(0, currentUsers.Length)]);

            // Make between 0 and X people owners of the KPI
            string[] owners = GetRandomSetOfUsers(r.Next(0, Convert.ToInt32(AllowedOwnDropDownList.SelectedValue) + 1), null);
            foreach (string myOwner in owners)
            {
                qta.InsertObjectPermissions("KPI", kpiID, myOwner, "OWN");
            }

            bool allowAllViewKPIs = false;

            // X% of the time we will allow Everyone to view the KPI
            allowAllViewKPIs = (r.Next(0, 100) < Convert.ToInt32(PublicKPIDropDownList.SelectedValue));

            if (allowAllViewKPIs)
            {
                qta.InsertObjectPublic("KPI", kpiID, "VIEW_KPI");
            }

            // Get between 0 and X random people to give them permissons to View or Enter Data on this object
            string[] others = GetRandomSetOfUsers(r.Next(0, Convert.ToInt32(AllowedViewKPIDropdownList.SelectedValue) + 1), owners);

            foreach (string myOthers in others)
            {
                StringBuilder permissions = new StringBuilder();
                // Get the permissons that we will give them.  They can be one of
                // of VIEW_KPI or ENTER_DATA but not both.  VIEW_KPI cannot be added
                // if these permissons are public as above.

                // 50% of the time give them VIEW_KPI
                if (r.Next(0, 100) < 10 && !allowAllViewKPIs)
                {
                    permissions.Append("VIEW_KPI");
                }
                else if ((r.Next(0, 100) < 50))
                {
                    // and if we did not give them VIEW_KPI 50% of the time we will give them ENTER_DATA
                    permissions.Append("ENTER_DATA");
                }

                if (permissions.Length > 0)
                {
                    qta.InsertObjectPermissions("KPI", kpiID, myOthers, permissions.ToString());
                }
            }

            // X% of the time, lets generate data for the KPI
            if (r.Next(0, 10) < Convert.ToInt32(MeasurementsDropDownList.SelectedValue))
            {
                List <KPIMeasurement> measurements = new List <KPIMeasurement>();

                int reportPeriods = r.Next(0, Convert.ToInt32(MeasurementsToGenerateDropDownList.SelectedValue));   // Generate MAX X reports

                for (int j = 0; j < reportPeriods; j++)
                {
                    if (hasCategories)
                    {
                        foreach (Category catX in cat)
                        {
                            foreach (CategoryItem catXItem in catX.Items)
                            {
                                measurements.Add(
                                    new KPIMeasurement(
                                        kpiID,
                                        DateTime.Today.Subtract(new TimeSpan(r.Next(0, 300), 0, 0, 0)),
                                        GenerateMeasurement(theTest.Type),
                                        catX.ID,
                                        catXItem.ItemID));
                            }
                        }
                    }
                    else
                    {
                        measurements.Add(
                            new KPIMeasurement(
                                kpiID,
                                DateTime.Today.Subtract(new TimeSpan(r.Next(0, 300), 0, 0, 0)),
                                GenerateMeasurement(theTest.Type),
                                "",
                                ""));
                    }
                }

                KPIBLL.InsertKPIMeasurements(measurements);
            }
        }
    }