Esempio n. 1
0
        protected void Page_Init(object sender, EventArgs e)
        {
            string tagSetIdsString = Request["ctl00$PlaceHolderMain$HiddenTagSetIdentifiers"];

            // Find our tag ids, either from previously hidden var or load from org

            if (String.IsNullOrEmpty(tagSetIdsString))
            {
                this._tagSetIds = FinancialTransactionTagSets.ForOrganization(CurrentOrganization).Identities;
            }
            else
            {
                string[] tagSetIdStrings = tagSetIdsString.Split(',');
                this._tagSetIds = new int[tagSetIdStrings.Length];

                for (int index = 0; index < tagSetIdStrings.Length; index++)
                {
                    this._tagSetIds[index] = Int32.Parse(tagSetIdStrings[index]);
                }
            }

            // Construct data source

            List <TagSetDataSourceItem> dataSourceVisibleTags = new List <TagSetDataSourceItem>();
            List <TagSetDataSourceItem> dataSourceForcedTags  = new List <TagSetDataSourceItem>();

            foreach (int tagSetId in this._tagSetIds)
            {
                TagSetDataSourceItem item = new TagSetDataSourceItem
                {
                    TagSetId            = tagSetId,
                    TagSetLocalizedName =
                        FinancialTransactionTagSetType.GetLocalizedName(
                            FinancialTransactionTagSet.FromIdentity(tagSetId).
                            FinancialTransactionTagSetTypeId)
                };

                FinancialTransactionTagSet tagSet = FinancialTransactionTagSet.FromIdentity(tagSetId);

                if (tagSet.VisibilityLevel <= 1)
                {
                    dataSourceVisibleTags.Add(item);

                    if (!tagSet.AllowUntagged)
                    {
                        dataSourceForcedTags.Add(item);
                    }
                }
            }

            // Bind data

            this.RepeaterTagLabels.DataSource      = dataSourceVisibleTags;
            this.RepeaterTagDrop.DataSource        = dataSourceVisibleTags;
            this.RepeaterTagDropScript.DataSource  = dataSourceVisibleTags;
            this.RepeaterErrorCheckTags.DataSource = dataSourceForcedTags;

            this.RepeaterTagLabels.DataBind();
            this.RepeaterTagDrop.DataBind();
            this.RepeaterTagDropScript.DataBind();
            this.RepeaterErrorCheckTags.DataBind();

            // Write set list back to hidden variable

            List <string> tagSetIdStringList = new List <string>();

            foreach (int tagSetId in this._tagSetIds)
            {
                tagSetIdStringList.Add(tagSetId.ToString(CultureInfo.InvariantCulture));
            }

            this.HiddenTagSetIdentifiers.Value = String.Join(",", tagSetIdStringList.ToArray());
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "application/json";

            int tagSetId = Int32.Parse(Request.QueryString["TagSetId"]);

            FinancialTransactionTagSet tagSet = FinancialTransactionTagSet.FromIdentity(tagSetId);

            if (tagSet.OrganizationId != CurrentOrganization.Identity)
            {
                throw new UnauthorizedAccessException();
            }

            // Is this stuff in cache already?

            string cacheKey = "FinancialTransactionTagTypes-Json-" +
                              tagSetId.ToString((CultureInfo.InvariantCulture));

            string tagsJson =
                (string)Cache[cacheKey];

            if (tagsJson != null)
            {
                Response.Output.WriteLine(tagsJson);
                Response.End();
                return;
            }

            // Not in cache. Construct.

            // Get accounts

            FinancialTransactionTagTypes tagTypes = FinancialTransactionTagTypes.ForSet(tagSet);

            // Build tree (there should be a template for this)

            Dictionary <int, List <FinancialTransactionTagType> > treeMap =
                new Dictionary <int, List <FinancialTransactionTagType> >();

            foreach (FinancialTransactionTagType tagType in tagTypes)
            {
                if (!treeMap.ContainsKey(tagType.ParentIdentity))
                {
                    treeMap[tagType.ParentIdentity] = new List <FinancialTransactionTagType>();
                }

                treeMap[tagType.ParentIdentity].Add(tagType);
            }

            int renderRootNodeId = 0;

            if (treeMap[0].Count == 1 && treeMap.ContainsKey(treeMap[0][0].Identity))
            {
                // assume there's a master root like "Costs"; bypass it

                renderRootNodeId = treeMap[0][0].Identity;
            }

            tagsJson = RecurseTreeMap(treeMap, renderRootNodeId);

            Cache.Insert(cacheKey, tagsJson, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
            // cache lasts for five minutes, no sliding expiration
            Response.Output.WriteLine(tagsJson);

            Response.End();
        }