protected void btnlnkfavorite_Click(object sender, EventArgs e)
        {
            int FlagId = Convert.ToInt16(Session["FlagId"].ToString());
            String FlagType = Session["FlagType"].ToString();
            if (FlagType == "Flag")
            {
                // check for flag
                _sqlHelper.FavoriteFlag(FlagId, _user.ExternalId);

                JArray _links = (JArray) Session["HomeLinks"];

                String UserCustomLink = _inBloomApi.GetCustomLink(_links);
                String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                String FlagUserId = Session["CustomUserId"].ToString();
                bool IsAdminUser = (bool) Session["CustomIsAdminUser"];
                bool IsPublic = (bool) Session["CustomIsPublic"];

                IList<Flag> _flag = (IList<Flag>) _sqlHelper.GetFlag(FlagId);

                FlagCls[] _flagCls = new FlagCls[1];
                _flagCls[0] = new FlagCls();
                _flagCls[0].FlagId = _flag[0].FlagId;
                _flagCls[0].FlagName = _flag[0].FlagName;
                _flagCls[0].FlagDescription = _flag[0].FlagDescription;
                _flagCls[0].FlagKeyword = _flag[0].Keyword;
                _flagCls[0].IsPublic = _flag[0].IsPublic;
                _flagCls[0].IsFavorite = _flag[0].IsFavorite;
                //_flagCls[0].IsDeleted =(bool) _flag[0].IsDeleted;
                _flagCls[0].ConditionId = _flag[0].ConditionId;
                _flagCls[0].DataElementId = _flag[0].DataElementId;
                _flagCls[0].ValueSet1 = _flag[0].ValueSet1;
                _flagCls[0].ValueSet2 = _flag[0].ValueSet2;
                _flagCls[0].UserId = _flag[0].UserId;
                _flagCls[0].CreatedBy = _flag[0].CreatedBy;
                _flagCls[0].CreatedDate = _flag[0].CreatedDate;
                _flagCls[0].ModifiedBy = _user.ExternalId;
                _flagCls[0].ModifiedDate = DateTime.Now;

                _inBloomApi.UpateFlag(FlagId, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, FlagUserId, _flagCls, _user);

                if (_sqlHelper.IsFav(FlagId) || _sqlHelper.IsInPublicFavorite(FlagId, _user.ExternalId))
                {
                    btnlnkfavorite.Text = "Remove Favorite";
                    btnlnkfavorite.CssClass = "";
                    btnlnkfavorite.Attributes.Add("onclick", "showunfavmsg();");

                }
                else
                {
                    btnlnkfavorite.Text = "Add to Favorite";
                    btnlnkfavorite.CssClass = "unfavorite_result";
                    btnlnkfavorite.Attributes.Add("onclick", "showfavmsg();");

                }

            }
            else if (FlagType == "AggregateFlag")
            {
                try
                {
                    _sqlHelper.AggregatetFavoriteFlag(FlagId, _user.ExternalId);

                    _user = (User) Session["UserDetail"];

                    JArray _links = (JArray) Session["HomeLinks"];

                    String GetCustomLink = _inBloomApi.GetCustomLink(_links);
                    String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                    String FlagUserId = Session["CustomUserId"].ToString();
                    bool IsAdminUser = (bool) Session["CustomIsAdminUser"];
                    bool IsPublic = (bool) Session["CustomIsPublic"];

                    String UserId = _user.ExternalId;

                    AggregateCls[] _aggregateCls = new AggregateCls[1];

                    IList<AggregateFlag> _aggregateFlag = _sqlHelper.GetAggregateFlag(FlagId);

                    _aggregateCls[0] = new AggregateCls();
                    _aggregateCls[0].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                    _aggregateCls[0].AggregateFlagDescription = _aggregateFlag[0].AggregateFlagDescription;
                    _aggregateCls[0].AggregateFlagName = _aggregateFlag[0].AggregateFlagName;
                    _aggregateCls[0].Keyword = _aggregateFlag[0].Keyword;

                    _aggregateCls[0].IsPublic = _aggregateFlag[0].IsPublic;
                    _aggregateCls[0].IsFavorite = _aggregateFlag[0].IsFavorite;
                    _aggregateCls[0].UserId = _aggregateFlag[0].UserId;
                    _aggregateCls[0].CreatedBy = _aggregateFlag[0].CreatedBy;
                    _aggregateCls[0].ModifiedBy = _user.ExternalId;
                    _aggregateCls[0].CreatedDate = _aggregateFlag[0].CreatedDate;
                    _aggregateCls[0].ModifiedDate = DateTime.Now;

                    int[] flagId = _sqlHelper.GetAllFlagIdOfAggregateFlagByAggregateFlagId(FlagId);
                    FlagForAggregate[] _flagForAggregateList = new FlagForAggregate[flagId.Count()];
                    for (int j = 0; j < flagId.Count(); j++)
                    {
                        _flagForAggregateList[j] = new FlagForAggregate();

                        _flagForAggregateList[j].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                        _flagForAggregateList[j].FlagId = flagId[j];
                        _flagForAggregateList[j].CreatedBy = _aggregateFlag[0].CreatedBy;
                        _flagForAggregateList[j].CreatedDate = _aggregateFlag[0].CreatedDate;
                        _flagForAggregateList[j].ModifiedBy = _aggregateFlag[0].ModifiedBy;
                        _flagForAggregateList[j].ModifiedDate = _aggregateFlag[0].ModifiedDate;

                    }

                    _aggregateCls[0].FlagForAggregate = _flagForAggregateList;

                    _inBloomApi.UpateAggregateFlag(FlagId, GetCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _aggregateCls, _user);

                    if (_sqlHelper.IsAggregateIsFav(FlagId) || _sqlHelper.IsAggregateInPublicFavorite(FlagId, _user.ExternalId))
                    {
                        btnlnkfavorite.Text = "Remove Favorite";
                        btnlnkfavorite.CssClass = "";
                        btnlnkfavorite.Attributes.Add("onclick", "showunfavmsg();");

                    }
                    else
                    {
                        btnlnkfavorite.Text = "Add to Favorite";
                        btnlnkfavorite.CssClass = "unfavorite_result";
                        btnlnkfavorite.Attributes.Add("onclick", "showfavmsg();");

                    }

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            UpdateMasterFlagList();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                String AggregateFlagName = txtFlagName.Text.Trim();
                String AggregateDescription = txtDescription.Text.Trim();
                String AggregateKeyword = txtFlag.Text.Trim();

                bool FlagValue = false;
                bool IsAdded = false;

                if (Session["CopyAggregateFlag"] != null)
                {

                    int AggregateFlagId = Convert.ToInt16(Session["CopyAggregateFlag"].ToString());
                    int[] FlagId = new int[lstcategory.Items.Count];

                    for (int i = 0; i < lstcategory.Items.Count; i++)
                    {
                        String FlagName = lstcategory.Items[i].Text;
                        //FlagId[i] = _sqlHelper.GetFlagId(FlagName);
                        FlagId[i] = Convert.ToInt16(lstcategory.Items[i].Value);
                    }

                    _user = (User)Session["UserDetail"];

                    if (_user.IsAdminUser)
                    {
                        if (radioFlagType.SelectedItem.Text == "Public")
                        {
                            FlagValue = true;
                        }
                        else
                        {
                            FlagValue = false;
                        }
                    }

                    _user = (User)Session["UserDetail"];

                    String UserId = _user.ExternalId;

                    IsAdded = _sqlHelper.AddAggregateFlag(AggregateFlagName, AggregateDescription, AggregateKeyword, FlagValue, FlagId, UserId);

                    if (IsAdded)
                    {
                        JArray _links = (JArray)Session["HomeLinks"];

                        String GetCustomLink = _inBloomApi.GetCustomLink(_links);
                        String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                        AggregateCls[] _aggregateCls = new AggregateCls[1];
                        AggregateFlagId = _sqlHelper.GetAggregateFlagById(AggregateFlagName);

                        IList<AggregateFlag> _aggregateFlag = _sqlHelper.GetAggregateFlag(AggregateFlagId);

                        _aggregateCls[0] = new AggregateCls();
                        _aggregateCls[0].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                        _aggregateCls[0].AggregateFlagDescription = _aggregateFlag[0].AggregateFlagDescription;
                        _aggregateCls[0].AggregateFlagName = _aggregateFlag[0].AggregateFlagName;
                        _aggregateCls[0].Keyword = _aggregateFlag[0].Keyword;

                        _aggregateCls[0].IsPublic = false;
                        _aggregateCls[0].IsFavorite = false;
                        _aggregateCls[0].UserId = _user.ExternalId;
                        _aggregateCls[0].CreatedBy = _user.FullName;
                        _aggregateCls[0].ModifiedBy = _user.FullName;
                        _aggregateCls[0].CreatedDate = DateTime.Now;
                        _aggregateCls[0].ModifiedDate = DateTime.Now;

                        int[] flagId = _sqlHelper.GetAllFlagIdOfAggregateFlagByAggregateFlagId(_aggregateFlag[0].AggregateFlagId);
                        FlagForAggregate[] _flagForAggregateList = new FlagForAggregate[flagId.Count()];
                        for (int j = 0; j < flagId.Count(); j++)
                        {
                            _flagForAggregateList[j] = new FlagForAggregate();

                            _flagForAggregateList[j].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                            _flagForAggregateList[j].FlagId = flagId[j];
                            _flagForAggregateList[j].CreatedBy = _aggregateFlag[0].CreatedBy;
                            _flagForAggregateList[j].CreatedDate = _aggregateFlag[0].CreatedDate;
                            _flagForAggregateList[j].ModifiedBy = _aggregateFlag[0].ModifiedBy;
                            _flagForAggregateList[j].ModifiedDate = _aggregateFlag[0].ModifiedDate;

                        }

                        _aggregateCls[0].FlagForAggregate = _flagForAggregateList;

                        if (_user.IsAdminUser && FlagValue == true)
                        {
                            // process to store the public flag into organization
                            _aggregateCls[0].IsPublic = true;
                            _inBloomApi.AddAggregateFlagIntoEducationOrganization(_user, EducationOrganizationId, _aggregateCls);
                        }
                        else
                        {
                            // process to add flag
                            _inBloomApi.AddAggregateFlagsIntoCustom(GetCustomLink, _user, _aggregateCls);
                        }

                        Session.Add("Success", "Aggregate flag copied successfully.");
                        Session["CopyAggregateFlag"] = null;
                        Response.Redirect("Search.aspx");
                    }
                    else
                    {
                        Session.Add("Success", "A flag with the name " + AggregateFlagName + " already exists.  Please enter a different name.");
                        Session["CopyAggregateFlag"] = AggregateFlagId;
                        Response.Redirect("CopyAggregateFlag.aspx");
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        public void AddAggregateFlagIntoEducationOrganization(User _user, String EducationOrganizationId, AggregateCls[] _aggregateFlagCls)
        {
            try
            {
                AdminCls _adminCls = GetFlagListForAdminUser(EducationOrganizationId, _user.ExternalId);
                Temp _flagListPrivateAdminCustom = null;
                if (_adminCls == null)
                {
                    // no data present in organization
                    Temp[] _temp = new Temp[1];
                    _temp[0] = new Temp();
                    _temp[0].AggregateFlagList = _aggregateFlagCls;
                    _temp[0].UserId = _user.ExternalId;
                    _temp[0].IsAdmin = _user.IsAdminUser;

                    _adminCls = new AdminCls();
                    _adminCls.AdminList = _temp;

                    PutCustomForAdmin(EducationOrganizationId, _adminCls);

                }
                else
                {
                    // data is present need to add new

                    Temp[] _temp = _adminCls.AdminList;
                    bool UserExist = false;
                    int Index = 0;
                    int UserIndex = 0;
                    for (int i = 0; i < _temp.Count(); i++)
                    {
                        if (_temp[i].UserId == _user.ExternalId)
                        {
                            // user record exist
                            UserExist = true;
                            Index = i;
                            break;
                        }
                    }

                    if (UserExist)
                    {

                        Temp _flagForEdit = _temp[Index];

                        AggregateCls[] _flagClsForEdit = _flagForEdit.AggregateFlagList;

                        if (_flagClsForEdit != null)
                        {

                            AggregateCls[] _flagClsNew = new AggregateCls[_flagClsForEdit.Count() + 1];

                            for (int i = 0; i < _flagClsForEdit.Count(); i++)
                            {
                                _flagClsNew[i] = new AggregateCls();
                                _flagClsNew[i] = _flagClsForEdit[i];
                            }

                            _flagClsNew[_flagClsForEdit.Count()] = new AggregateCls();
                            _flagClsNew[_flagClsForEdit.Count()] = _aggregateFlagCls[0];

                            _flagForEdit.AggregateFlagList = _flagClsNew;
                        }
                        else
                        {
                            _flagForEdit.AggregateFlagList = _aggregateFlagCls;
                        }
                        _temp[Index] = _flagForEdit;
                        _adminCls.AdminList = _temp;

                        PutCustomForAdmin(EducationOrganizationId, _adminCls);

                    }
                    else
                    {
                        // new admin user
                        Temp[] _tempForNew = new Temp[_temp.Count() + 1];
                        for (int i = 0; i < _temp.Count(); i++)
                        {
                            _tempForNew[i] = new Temp();
                            _tempForNew[i] = _temp[i];
                        }

                        Temp _tempAdmin = new Temp();
                        _tempAdmin.IsAdmin = _user.IsAdminUser;
                        _tempAdmin.UserId = _user.ExternalId;
                        _tempAdmin.AggregateFlagList = _aggregateFlagCls;

                        _tempForNew[_tempForNew.Count() - 1] = new Temp();
                        _tempForNew[_tempForNew.Count() - 1] = _tempAdmin;

                        _adminCls.AdminList = _tempForNew;

                        PutCustomForAdmin(EducationOrganizationId, _adminCls);
                    }
                }
            }
            catch (Exception Ex)
            {

            }
        }
        protected void gridViewFlag_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            #region Grid Operation Edit, Delete, Update, Favorite

            _slcApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());

            if (e.CommandName == "CmdDelete")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int FlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());

                    _user = (User) Session["UserDetail"];
                    IList<Flag> _flag = (IList<Flag>) _sqlHelper.GetFlag(FlagID);

                    if (_user.ExternalId == _flag[0].UserId)
                    {

                        _sqlHelper.DeleteFlag(FlagID);

                        JArray _links = (JArray) Session["HomeLinks"];

                        String UserCustomLink = _slcApi.GetCustomLink(_links);
                        String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                        String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                        String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                        bool IsAdminUser = (AdminUser == "True");

                        String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                        bool IsPublic = (IsPublicFag == "True");

                        FlagCls[] _flagCls = new FlagCls[1];
                        _flagCls[0] = new FlagCls();
                        _flagCls[0].FlagId = _flag[0].FlagId;
                        _flagCls[0].FlagName = _flag[0].FlagName;
                        _flagCls[0].FlagDescription = _flag[0].FlagDescription;
                        _flagCls[0].FlagKeyword = _flag[0].Keyword;
                        _flagCls[0].IsPublic = _flag[0].IsPublic;
                        _flagCls[0].IsFavorite = _flag[0].IsFavorite;
                        _flagCls[0].IsDeleted = true;
                        _flagCls[0].ConditionId = _flag[0].ConditionId;
                        _flagCls[0].DataElementId = _flag[0].DataElementId;
                        _flagCls[0].ValueSet1 = _flag[0].ValueSet1;
                        _flagCls[0].ValueSet2 = _flag[0].ValueSet2;
                        _flagCls[0].UserId = _flag[0].UserId;
                        _flagCls[0].CreatedBy = _flag[0].CreatedBy;
                        _flagCls[0].CreatedDate = _flag[0].CreatedDate;
                        _flagCls[0].ModifiedBy = _user.ExternalId;
                        _flagCls[0].ModifiedDate = DateTime.Now;

                        _slcApi.UpateFlag(FlagID, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _flagCls, _user);

                        LoadFlagGrid();
                    }
                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdFavorite")
            {
                try
                {
                    _user = (User) Session["UserDetail"];

                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int FlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());

                    _sqlHelper.FavoriteFlag(FlagID, _user.ExternalId);
                    _user = (User) Session["UserDetail"];

                    JArray _links = (JArray) Session["HomeLinks"];

                    String UserCustomLink = _slcApi.GetCustomLink(_links);
                    String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                    String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                    String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                    bool IsAdminUser = AdminUser == "True";

                    String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                    bool IsPublic = IsPublicFag == "True";

                    IList<Flag> _flag = _sqlHelper.GetFlag(FlagID);

                    FlagCls[] _flagCls = new FlagCls[1];
                    _flagCls[0] = new FlagCls();
                    _flagCls[0].FlagId = _flag[0].FlagId;
                    _flagCls[0].FlagName = _flag[0].FlagName;
                    _flagCls[0].FlagDescription = _flag[0].FlagDescription;
                    _flagCls[0].FlagKeyword = _flag[0].Keyword;
                    _flagCls[0].IsPublic = _flag[0].IsPublic;
                    _flagCls[0].IsFavorite = _flag[0].IsFavorite;
                    //_flagCls[0].IsDeleted =(bool) _flag[0].IsDeleted;
                    _flagCls[0].ConditionId = _flag[0].ConditionId;
                    _flagCls[0].DataElementId = _flag[0].DataElementId;
                    _flagCls[0].ValueSet1 = _flag[0].ValueSet1;
                    _flagCls[0].ValueSet2 = _flag[0].ValueSet2;
                    _flagCls[0].UserId = _flag[0].UserId;
                    _flagCls[0].CreatedBy = _flag[0].CreatedBy;
                    _flagCls[0].CreatedDate = _flag[0].CreatedDate;
                    _flagCls[0].ModifiedBy = _user.ExternalId;
                    _flagCls[0].ModifiedDate = DateTime.Now;

                    _slcApi.UpateFlag(FlagID, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _flagCls, _user);

                    UpdateMasterFlagList();

                    LoadFlagGrid();

                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdCopyFlag")
            {
                try
                {
                    int FlagID = Int32.Parse(e.CommandArgument.ToString());
                    Session.Add("CopyFlag", FlagID);
                    Response.Redirect("CopyFlag.aspx");
                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdEditFlag")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int FlagId = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());
                    String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                    String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                    bool IsAdminUser = (AdminUser == "True");

                    String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                    bool IsPublic = (IsPublicFag == "True");

                    String FlagType = gridViewFlag.Rows[RowIndex].Cells[7].Text;

                    Session.Add("CustomUserId", UserId);
                    Session.Add("CustomIsPublic", IsPublic);
                    Session.Add("CustomFlagType", FlagType);
                    Session.Add("CustomIsAdminUser", IsAdminUser);
                    Session.Add("EditFlag", FlagId);
                    Response.Redirect("EditFlag.aspx");
                }
                catch (Exception Ex)
                {
                }
            }
            else if (e.CommandName == "CmdCopyAggregateFlag")
            {
                try
                {
                    int AggregateFlagID = Int32.Parse(e.CommandArgument.ToString());
                    Session.Add("CopyAggregateFlag", AggregateFlagID);
                    Response.Redirect("CopyAggregateFlag.aspx");
                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdEditAggregateFlag")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int AggregateFlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());
                    String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                    String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                    bool IsAdminUser = (AdminUser == "True");

                    String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                    bool IsPublic = (IsPublicFag == "True");

                    String FlagType = gridViewFlag.Rows[RowIndex].Cells[7].Text;

                    Session.Add("CustomUserId", UserId);
                    Session.Add("CustomIsPublic", IsPublic);
                    Session.Add("CustomFlagType", FlagType);
                    Session.Add("CustomIsAdminUser", IsAdminUser);
                    Session.Add("EditAggregateFlag", AggregateFlagID);

                    Response.Redirect("EditAggregateFlag.aspx");
                }
                catch (Exception Ex)
                {
                }
            }
            else if (e.CommandName == "CmdFavoriteAggregateFlag")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int AggregateFlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());

                    _user = (User) Session["UserDetail"];
                    _sqlHelper.AggregatetFavoriteFlag(AggregateFlagID, _user.ExternalId);

                    JArray _links = (JArray) Session["HomeLinks"];

                    String UserCustomLink = _slcApi.GetCustomLink(_links);
                    String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                    String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                    String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                    bool IsAdminUser = (AdminUser == "True");

                    String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                    bool IsPublic = (IsPublicFag == "True");

                    AggregateCls[] _aggregateCls = new AggregateCls[1];

                    IList<AggregateFlag> _aggregateFlag = _sqlHelper.GetAggregateFlag(AggregateFlagID);

                    _aggregateCls[0] = new AggregateCls();
                    _aggregateCls[0].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                    _aggregateCls[0].AggregateFlagDescription = _aggregateFlag[0].AggregateFlagDescription;
                    _aggregateCls[0].AggregateFlagName = _aggregateFlag[0].AggregateFlagName;
                    _aggregateCls[0].Keyword = _aggregateFlag[0].Keyword;

                    _aggregateCls[0].IsPublic = _aggregateFlag[0].IsPublic;
                    _aggregateCls[0].IsFavorite = _aggregateFlag[0].IsFavorite;
                    _aggregateCls[0].UserId = _aggregateFlag[0].UserId;
                    _aggregateCls[0].CreatedBy = _aggregateFlag[0].CreatedBy;
                    _aggregateCls[0].ModifiedBy = _user.ExternalId;
                    _aggregateCls[0].CreatedDate = _aggregateFlag[0].CreatedDate;
                    _aggregateCls[0].ModifiedDate = DateTime.Now;

                    int[] flagId = _sqlHelper.GetAllFlagIdOfAggregateFlagByAggregateFlagId(AggregateFlagID);
                    FlagForAggregate[] _flagForAggregateList = new FlagForAggregate[flagId.Count()];
                    for (int j = 0; j < flagId.Count(); j++)
                    {
                        _flagForAggregateList[j] = new FlagForAggregate();

                        _flagForAggregateList[j].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                        _flagForAggregateList[j].FlagId = flagId[j];
                        _flagForAggregateList[j].CreatedBy = _aggregateFlag[0].CreatedBy;
                        _flagForAggregateList[j].CreatedDate = _aggregateFlag[0].CreatedDate;
                        _flagForAggregateList[j].ModifiedBy = _aggregateFlag[0].ModifiedBy;
                        _flagForAggregateList[j].ModifiedDate = _aggregateFlag[0].ModifiedDate;

                    }

                    _aggregateCls[0].FlagForAggregate = _flagForAggregateList;

                    _slcApi.UpateAggregateFlag(AggregateFlagID, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _aggregateCls, _user);

                    UpdateMasterFlagList();

                    LoadFlagGrid();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else if (e.CommandName == "CmdDeleteAggregateFlag")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int AggregateFlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());

                    _user = (User) Session["UserDetail"];

                    AggregateCls[] _aggregateCls = new AggregateCls[1];

                    IList<AggregateFlag> _aggregateFlag = _sqlHelper.GetAggregateFlag(AggregateFlagID);

                    if (_user.ExternalId == _aggregateFlag[0].UserId)
                    {
                        if (_sqlHelper.DeleteAggregateFlag(AggregateFlagID))
                        {
                            // deleted successfully

                            _user = (User) Session["UserDetail"];

                            JArray _links = (JArray) Session["HomeLinks"];

                            String UserCustomLink = _slcApi.GetCustomLink(_links);
                            String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                            String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                            String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                            bool IsAdminUser = (AdminUser == "True");

                            String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                            bool IsPublic = (IsPublicFag == "True");

                            _aggregateCls[0] = new AggregateCls();
                            _aggregateCls[0].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                            _aggregateCls[0].AggregateFlagDescription = _aggregateFlag[0].AggregateFlagDescription;
                            _aggregateCls[0].AggregateFlagName = _aggregateFlag[0].AggregateFlagName;
                            _aggregateCls[0].Keyword = _aggregateFlag[0].Keyword;
                            _aggregateCls[0].IsDeleted = true;
                            _aggregateCls[0].IsPublic = _aggregateFlag[0].IsPublic;
                            _aggregateCls[0].IsFavorite = _aggregateFlag[0].IsFavorite;
                            _aggregateCls[0].UserId = _aggregateFlag[0].UserId;
                            _aggregateCls[0].CreatedBy = _aggregateFlag[0].CreatedBy;
                            _aggregateCls[0].ModifiedBy = _user.ExternalId;
                            _aggregateCls[0].CreatedDate = _aggregateFlag[0].CreatedDate;
                            _aggregateCls[0].ModifiedDate = DateTime.Now;

                            int[] flagId = _sqlHelper.GetAllFlagIdOfAggregateFlagByAggregateFlagId(AggregateFlagID);
                            FlagForAggregate[] _flagForAggregateList = new FlagForAggregate[flagId.Count()];

                            for (int j = 0; j < flagId.Count(); j++)
                            {
                                _flagForAggregateList[j] = new FlagForAggregate();

                                _flagForAggregateList[j].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                                _flagForAggregateList[j].FlagId = flagId[j];
                                _flagForAggregateList[j].CreatedBy = _aggregateFlag[0].CreatedBy;
                                _flagForAggregateList[j].CreatedDate = _aggregateFlag[0].CreatedDate;
                                _flagForAggregateList[j].ModifiedBy = _aggregateFlag[0].ModifiedBy;
                                _flagForAggregateList[j].ModifiedDate = _aggregateFlag[0].ModifiedDate;
                            }

                            _aggregateCls[0].FlagForAggregate = _flagForAggregateList;

                            _slcApi.UpateAggregateFlag(AggregateFlagID, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _aggregateCls, _user);

                            LoadFlagGrid();
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdRunFlag")
            {
                //Session.Add("ResultPage", true);
                int RowIndex = Convert.ToInt16(e.CommandArgument);
                int FlagId = Convert.ToInt16(gridViewFlag.Rows[RowIndex].Cells[8].Text);
                String FlagType = gridViewFlag.Rows[RowIndex].Cells[7].Text;
                Session.Add("FlagId", FlagId);
                Session.Add("FlagType", FlagType);

                String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                bool IsAdminUser = (AdminUser == "True");

                String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                bool IsPublic = (IsPublicFag == "True");

                FlagType = gridViewFlag.Rows[RowIndex].Cells[7].Text;

                Session.Add("CustomUserId", UserId);
                Session.Add("CustomIsPublic", IsPublic);
                Session.Add("CustomFlagType", FlagType);
                Session.Add("CustomIsAdminUser", IsAdminUser);
                Session.Add("EditFlag", FlagId);

                Response.Redirect("Result.aspx");
            }

            #endregion
        }
        public void DeleteAggregateFlagsIntoCustom(String Link, User _user, AggregateCls[] _aggregateCls)
        {
            AggregateCls[] _forNew = _aggregateCls;
            FlagCls[] _forFlagCls = null;
            try
            {
                Temp _editedFlagByUser = new Temp();
                _editedFlagByUser.AggregateFlagList = _aggregateCls;

                JObject UserCustomResponse = JObject.Parse(RestApiHelper.CallApiWithParameter(Link, this.AccessToken));
                if (UserCustomResponse != null)
                {
                    Temp _list = FlagObjectDeserialize(UserCustomResponse.ToString());
                    if (_list != null)
                    {
                        Temp _temp = new Temp();
                        _forFlagCls = _list.FlagList;

                        if (_list.AggregateFlagList.Count() == 1)
                        {
                            _list.AggregateFlagList = null;
                        }
                        else
                        {
                            AggregateCls[] _flagClsNew = new AggregateCls[_list.AggregateFlagList.Count() - 1];
                            int Count = 0;
                            for (int i = 0; i < _list.AggregateFlagList.Count(); i++)
                            {
                                if (_list.AggregateFlagList[i].AggregateFlagId == _aggregateCls[0].AggregateFlagId)
                                    continue;
                                _flagClsNew[Count] = new AggregateCls();
                                _flagClsNew[Count] = _list.AggregateFlagList[i];
                                Count++;
                            }
                            _list.AggregateFlagList = _flagClsNew;

                        }
                        String Result = FlagObjectToJson(_list);

                        RestApiHelper.CallApiWithParameterForCustomPUT(Link, this.AccessToken, Result);
                    }
                }

            }
            catch (Exception Ex)
            {

            }
        }
        public void DeleteAggregateFlagIntoEducationOrganization(User _user, String EducationOrganizationId, AggregateCls[] _aggregateFlagCls)
        {
            try
            {
                AdminCls _adminCls = GetFlagListForAdminUser(EducationOrganizationId, _user.ExternalId);
                Temp _flagListPrivateAdminCustom = null;
                if (_adminCls == null)
                {

                }
                else
                {
                    // data is present need to add new

                    Temp[] _temp = _adminCls.AdminList;
                    bool UserExist = false;
                    int Index = 0;
                    int UserIndex = 0;
                    for (int i = 0; i < _temp.Count(); i++)
                    {
                        if (_temp[i].UserId == _user.ExternalId)
                        {
                            // user record exist
                            UserExist = true;
                            Index = i;
                            break;
                        }
                    }

                    if (UserExist)
                    {

                        Temp _flagForEdit = _temp[Index];

                        AggregateCls[] _flagClsForEdit = _flagForEdit.AggregateFlagList;

                        if (_flagForEdit.AggregateFlagList.Count() == 1)
                        {
                            _flagForEdit.AggregateFlagList = null;
                        }
                        else
                        {
                            AggregateCls[] _flagClsNew = new AggregateCls[_flagClsForEdit.Count() - 1];
                            int Count = 0;
                            for (int i = 0; i < _flagClsForEdit.Count(); i++)
                            {
                                if (_flagClsForEdit[i].AggregateFlagId == _aggregateFlagCls[0].AggregateFlagId)
                                    continue;
                                _flagClsNew[Count] = new AggregateCls();
                                _flagClsNew[Count] = _flagClsForEdit[i];
                                Count++;
                            }

                            _flagForEdit.AggregateFlagList = _flagClsNew;

                        }

                        _temp[Index] = _flagForEdit;
                        _adminCls.AdminList = _temp;

                        PutCustomForAdmin(EducationOrganizationId, _adminCls);

                    }
                    else
                    {

                    }
                }
            }
            catch (Exception Ex)
            {

            }
        }
        public void UpateAggregateFlag(int FlagId, String UserCustomLink, String EducationOrganizationId, bool IsAdmin, bool IsPublic, String FlagUserId, AggregateCls[] _aggregateFlagClsUpdated, User _user)
        {
            if (IsAdmin)
            {

                if (IsPublic)
                {
                    // get from orgnization
                    if (_aggregateFlagClsUpdated[0].IsPublic == false)
                    {
                        // process to delete flag from organization and add to custom of admin
                        //public to private
                        DeleteAggregateFlagIntoEducationOrganization(_user, EducationOrganizationId, _aggregateFlagClsUpdated);

                        AddAggregateFlagsIntoCustom(UserCustomLink, _user, _aggregateFlagClsUpdated);
                    }
                    else
                    {
                        // update at organization
                        UpdateAggregateFlagIntoEducationOrganization(_user, EducationOrganizationId, _aggregateFlagClsUpdated);
                    }
                }
                else
                    if ((!IsPublic) && _aggregateFlagClsUpdated[0].IsPublic)
                    {
                        // private to public

                        DeleteAggregateFlagsIntoCustom(UserCustomLink, _user, _aggregateFlagClsUpdated);
                        AddAggregateFlagIntoEducationOrganization(_user, EducationOrganizationId, _aggregateFlagClsUpdated);
                    }
                    else
                    {
                        // update at custom
                        UpdateAggregateFlagsIntoCustom(UserCustomLink, _user, _aggregateFlagClsUpdated);
                    }
            }
            else
                UpdateAggregateFlagsIntoCustom(UserCustomLink, _user, _aggregateFlagClsUpdated);
        }
        public void AddAggregateFlagsIntoCustom(String Link, User _user, AggregateCls[] _aggregateCls)
        {
            AggregateCls[] _forNew = _aggregateCls;
            FlagCls[] _forFlagCls = null;
            try
            {
                Temp _editedFlagByUser = new Temp();
                _editedFlagByUser.AggregateFlagList = _aggregateCls;

                JObject UserCustomResponse = JObject.Parse(RestApiHelper.CallApiWithParameter(Link, this.AccessToken));
                if (UserCustomResponse != null)
                {
                    Temp _list = FlagObjectDeserialize(UserCustomResponse.ToString());
                    if (_list != null)
                    {
                        Temp _temp = new Temp();
                        _forFlagCls = _list.FlagList;
                        AggregateCls[] _flagClsNew = new AggregateCls[_list.AggregateFlagList.Count() + 1];

                        for (int i = 0; i < _list.AggregateFlagList.Count(); i++)
                        {
                            _flagClsNew[i] = new AggregateCls();
                            _flagClsNew[i] = _list.AggregateFlagList[i];
                        }
                        _flagClsNew[_list.AggregateFlagList.Count()] = new AggregateCls();
                        _flagClsNew[_list.AggregateFlagList.Count()] = _aggregateCls[0];

                        _temp.AggregateFlagList = _flagClsNew;
                        _temp.FlagList = _list.FlagList;
                        _temp.UserId = _user.ExternalId;
                        _temp.IsAdmin = _user.IsAdminUser;

                        String Result = FlagObjectToJson(_temp);

                        RestApiHelper.CallApiWithParameterForCustomPUT(Link, this.AccessToken, Result);
                    }
                }

            }
            catch (Exception Ex)
            {
                // process to add first
                Temp _temp = new Temp();
                _temp.AggregateFlagList = _forNew;
                _temp.FlagList = _forFlagCls;
                _temp.UserId = _user.ExternalId;
                _temp.IsAdmin = _user.IsAdminUser;
                String Result = FlagObjectToJson(_temp);
                RestApiHelper.CallApiWithParameterForCustomPUT(Link, this.AccessToken, Result);
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            String AggregateFlagName = txtFlagName.Text.Trim();
            String AggregateDescription = txtDescription.Text.Trim();
            String AggregateKeyword = txtFlag.Text.Trim();

            bool FlagValue = false;
            bool IsAdded = false;

            if (Session["EditAggregateFlag"] != null)
            {
                int AggregateFlagId = Convert.ToInt16(Session["EditAggregateFlag"].ToString());
                int[] FlagId = new int[lstcategory.Items.Count];

                for (int i = 0; i < lstcategory.Items.Count; i++)
                {
                    FlagId[i] = Convert.ToInt16(lstcategory.Items[i].Value);
                }

                _user = (User)Session["UserDetail"];

                if (_user.IsAdminUser)
                {
                    FlagValue = radioFlagType.SelectedItem.Text == "Public";
                }

                _user = (User)Session["UserDetail"];

                String UserId = _user.ExternalId;

                IsAdded = _sqlHelper.EditAddAggregateFlag(AggregateFlagId, AggregateFlagName, AggregateDescription, AggregateKeyword, FlagValue, FlagId, UserId);

                if (IsAdded)
                {
                    JArray _links = (JArray)Session["HomeLinks"];

                    String GetCustomLink = _inBloomApi.GetCustomLink(_links);
                    String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                    String FlagUserId = Session["CustomUserId"].ToString();
                    bool IsAdminUser = (bool)Session["CustomIsAdminUser"];
                    bool IsPublic = (bool)Session["CustomIsPublic"];

                    AggregateCls[] _aggregateCls = new AggregateCls[1];

                    IList<AggregateFlag> _aggregateFlag = _sqlHelper.GetAggregateFlag(AggregateFlagId);

                    _aggregateCls[0] = new AggregateCls();
                    _aggregateCls[0].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                    _aggregateCls[0].AggregateFlagDescription = _aggregateFlag[0].AggregateFlagDescription;
                    _aggregateCls[0].AggregateFlagName = _aggregateFlag[0].AggregateFlagName;
                    _aggregateCls[0].Keyword = _aggregateFlag[0].Keyword;
                    _aggregateCls[0].IsPublic = _aggregateFlag[0].IsPublic;
                    _aggregateCls[0].IsFavorite = _aggregateFlag[0].IsFavorite;
                    _aggregateCls[0].UserId = _aggregateFlag[0].UserId;
                    _aggregateCls[0].CreatedBy = _aggregateFlag[0].CreatedBy;
                    _aggregateCls[0].ModifiedBy = _user.ExternalId;
                    _aggregateCls[0].CreatedDate = _aggregateFlag[0].CreatedDate;
                    _aggregateCls[0].ModifiedDate = DateTime.Now;

                    int[] flagId = _sqlHelper.GetAllFlagIdOfAggregateFlagByAggregateFlagId(_aggregateFlag[0].AggregateFlagId);
                    FlagForAggregate[] _flagForAggregateList = new FlagForAggregate[flagId.Count()];
                    for (int j = 0; j < flagId.Count(); j++)
                    {
                        _flagForAggregateList[j] = new FlagForAggregate();

                        _flagForAggregateList[j].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                        _flagForAggregateList[j].FlagId = flagId[j];
                        _flagForAggregateList[j].CreatedBy = _aggregateFlag[0].CreatedBy;
                        _flagForAggregateList[j].CreatedDate = _aggregateFlag[0].CreatedDate;
                        _flagForAggregateList[j].ModifiedBy = _aggregateFlag[0].ModifiedBy;
                        _flagForAggregateList[j].ModifiedDate = _aggregateFlag[0].ModifiedDate;

                    }

                    _aggregateCls[0].FlagForAggregate = _flagForAggregateList;

                    _inBloomApi.UpateAggregateFlag(AggregateFlagId, GetCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _aggregateCls, _user);

                    Session.Add("Success", "Aggregate flag was updated successfully.");
                    Session["EditAggregateFlag"] = null;
                    Response.Redirect("Search.aspx");
                }
                else
                {
                    Session["Success"] = "A Aggregate flag with the name " + txtFlagName.Text.ToString() + " already exists.  Please enter a different name.";
                    Session["EditAggregateFlag"] = AggregateFlagId;
                    Response.Redirect("EditAggregateFlag.aspx");
                }
            }
        }