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)
        {
            bool FlagValue = false;

                SqlHelper _sqlHelper = new SqlHelper();

                if (Session["UserDetail"] != null)
                {
                    _user = (User)Session["UserDetail"];

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

                    bool IsFlagAdded = false;
                    int FlagId = Convert.ToInt16(Session["EditFlag"]);
                    if (Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"] != null)
                    {
                        //int FlagId = Convert.ToInt16(Session["EditFlag"]);
                        IsFlagAdded = _sqlHelper.UpdateFlag(FlagId, txtFlagName.Text.Trim(), txtDescription.Text.Trim(), txtFlag.Text.Trim(), Convert.ToInt16(Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"]),
                        Convert.ToInt16(lstCondition.SelectedItem.Value), txtSetVal.Text.Trim(), txtSetVal2.Text.Trim(), _user.ExternalId, _user.FullName, FlagValue
                        );

                    }
                    if (IsFlagAdded)
                    {
                        //successfully update

                        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 (Session["EditFlag"] != null)
                        {
                            // flag is updated
                            Session["EditFlag"] = null;
                        }

                        Session.Add("Success", "Flag edited successfully.");
                        Session["FlagIdForEditPage"] = null;
                        Response.Redirect("Search.aspx");
                    }
                    else
                    {
                        Session["Success"] = "A flag with the name " + txtFlagName.Text.ToString() + " already exists.  Please enter a different name.";
                        Session["EditFlag"] = Session["FlagIdForEditPage"].ToString();
                        Response.Redirect("EditFlag.aspx");
                    }
                }
                else
                {
                    Response.Redirect("Search.aspx");
                }
        }
        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
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                bool flagValue = false;

                SqlHelper _sqlHelper = new SqlHelper();

                if (Session["UserDetail"] != null)
                {
                    _user = (User)Session["UserDetail"];

                    if (_user.IsAdminUser)
                        flagValue = radioFlagType.SelectedItem.Text == "Public";

                    bool IsFlagAdded = false;

                    if (Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"] != null)
                    {
                        IsFlagAdded = _sqlHelper.AddFlag(txtFlagName.Text.Trim(), txtDescription.Text.Trim(), txtFlag.Text.Trim(), Convert.ToInt16(Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"]),
                        Convert.ToInt16(lstCondition.SelectedItem.Value), txtSetVal.Text.Trim(), txtSetVal2.Text.Trim(), _user.ExternalId, _user.FullName, flagValue
                       );
                    }

                    if (IsFlagAdded)
                    {
                        JArray _links = (JArray)Session["HomeLinks"];
                        int FlagId = _sqlHelper.GetFlagId(txtFlagName.Text.Trim());

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

                        FlagCls[] _flagCls = new FlagCls[1];
                        _flagCls[0] = new FlagCls();
                        _flagCls[0].FlagId = FlagId;
                        _flagCls[0].FlagName = txtFlagName.Text.Trim();
                        _flagCls[0].FlagDescription = txtDescription.Text.Trim();
                        _flagCls[0].FlagKeyword = txtFlag.Text.Trim();
                        _flagCls[0].IsPublic = false;
                        _flagCls[0].IsFavorite = false;
                        _flagCls[0].IsDeleted = false;
                        _flagCls[0].ConditionId = Convert.ToInt16(lstCondition.SelectedItem.Value);
                        _flagCls[0].DataElementId = Convert.ToInt16(Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"]);
                        _flagCls[0].ValueSet1 = txtSetVal.Text.Trim();
                        _flagCls[0].ValueSet2 = txtSetVal2.Text.Trim();
                        _flagCls[0].UserId = _user.ExternalId;
                        _flagCls[0].CreatedBy = _user.ExternalId;
                        _flagCls[0].CreatedDate = DateTime.Now;
                        _flagCls[0].ModifiedBy = _user.ExternalId;
                        _flagCls[0].ModifiedDate = DateTime.Now;

                        if (_user.IsAdminUser && flagValue == true)
                        {
                            // process to store the public flag into organization
                            _flagCls[0].IsPublic = true;
                            _inBloomApi.AddFlagIntoEducationOrganization(_user, EducationOrganizationId, _flagCls);
                        }
                        else
                        {
                            // process to add flag
                            _inBloomApi.AddFlagsIntoCustom(GetCustomLink, _user, _flagCls);
                        }

                        //successfully added
                        Session["CopyFlag"] = null;
                        Session["Success"]="Flag copied Successfully";
                        Session["FlagIdForCopyPage"] = null;
                        Response.Redirect("Search.aspx");
                    }
                    else
                    {
                        Session["Success"] = "A flag with the name " + txtFlagName.Text.ToString() + " already exists.  Please enter a different name.";
                        Session["CopyFlag"] = Session["FlagIdForCopyPage"].ToString();
                        Response.Redirect("CopyFlag.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                 bool FlagValue = false;

                 _sqlHelper = new SqlHelper();

                if (Session["UserDetail"] != null)
                {
                    _user = (User)Session["UserDetail"];

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

                    String FlagKeyword = txtFlag.Text.Trim();
                    if (FlagKeyword.Equals("Enter one or more keywords for the flag (optional)"))
                    {
                        FlagKeyword = "";
                    }

                    bool IsFlagAdded = false;

                    if (Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"] != null)
                    {
                        IsFlagAdded = _sqlHelper.AddFlag(txtFlagName.Text.Trim(), txtDescription.Text.Trim(), FlagKeyword, Convert.ToInt16(Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"]),
                            Convert.ToInt16(lstCondition.SelectedItem.Value), txtSetVal.Text.Trim(), txtSetVal2.Text.Trim(), _user.ExternalId, _user.FullName, FlagValue
                            );

                    }

                    if (IsFlagAdded)
                    {
                        Session.Add("Success", "Flag added successfully.");

                        int FlagId = _sqlHelper.GetFlagIdByNameDescriptionAndKeyword(txtFlagName.Text.Trim(), txtDescription.Text.Trim(), FlagKeyword);

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

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

                        FlagCls[] _flagCls = new FlagCls[1];
                        _flagCls[0] = new FlagCls();
                        _flagCls[0].FlagId = FlagId;
                        _flagCls[0].FlagName = txtFlagName.Text.Trim();
                        _flagCls[0].FlagDescription = txtDescription.Text.Trim();
                        _flagCls[0].FlagKeyword = FlagKeyword;
                        _flagCls[0].IsPublic = false;
                        _flagCls[0].IsFavorite = false;
                        _flagCls[0].IsDeleted = false;
                        _flagCls[0].ConditionId = Convert.ToInt16(lstCondition.SelectedItem.Value);
                        _flagCls[0].DataElementId = Convert.ToInt16(Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"]);
                        _flagCls[0].ValueSet1 = txtSetVal.Text.Trim();
                        _flagCls[0].ValueSet2 = txtSetVal2.Text.Trim();
                        _flagCls[0].UserId = _user.ExternalId;
                        _flagCls[0].CreatedBy = _user.ExternalId;
                        _flagCls[0].CreatedDate = DateTime.Now;
                        _flagCls[0].ModifiedBy = _user.ExternalId;
                        _flagCls[0].ModifiedDate = DateTime.Now;

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

                        Response.Redirect("Search.aspx");

                    }
                    else
                    {
                        //flag is already exist or some exception
                        Session.Add("Success", "A flag with the name " + txtFlagName.Text.ToString()  + " already exists. Please enter a different name.");
                        // Session.Add("Success", "Flag is name already exist");
                        Response.Redirect("AddFlag.aspx");

                    }
                }
                else
                {
                    Response.Redirect("Search.aspx");
                }
            }
            catch (Exception Ex)
            {

            }
        }
        public void DeleteFlagsIntoCustom(String Link, User _user, FlagCls[] _flagCls)
        {
            FlagCls[] _forNew = _flagCls;
            AggregateCls[] _forAggregateCls = null;
            try
            {
                Temp _editedFlagByUser = new Temp();
                _editedFlagByUser.FlagList = _flagCls;

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

                        if (_list.FlagList.Count() == 1)
                        {
                            // only one need to delete
                            _temp.FlagList = null;
                        }
                        else
                        {
                            FlagCls[] _flagClsNew = new FlagCls[_list.FlagList.Count() - 1];
                            int Count = 0;
                            for (int i = 0; i < _list.FlagList.Count(); i++)
                            {
                                if (_list.FlagList[i].FlagId == _flagCls[0].FlagId)
                                    continue;
                                _flagClsNew[Count] = new FlagCls();
                                _flagClsNew[Count] = _list.FlagList[i];
                                Count++;
                            }

                            _temp.FlagList = _flagClsNew;
                            _temp.AggregateFlagList = _list.AggregateFlagList;
                            _temp.UserId = _user.ExternalId;
                            _temp.IsAdmin = _user.IsAdminUser;
                        }
                        String Result = FlagObjectToJson(_temp);

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

            }
            catch (Exception Ex)
            {

            }
        }
        public void DeleteFlagIntoEducationOrganization(User _user, String EducationOrganizationId, FlagCls[] _flagCls)
        {
            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];

                        FlagCls[] _flagClsForEdit = _flagForEdit.FlagList;
                        FlagCls[] _flagClsNew = null;

                        if (_flagClsForEdit.Count() == 1)
                        {
                            _flagClsNew = null;
                        }
                        else
                        {
                            int Count = 0;
                            _flagClsNew = new FlagCls[_flagClsForEdit.Count() - 1];

                            for (int i = 0; i < _flagClsForEdit.Count(); i++)
                            {
                                if (_flagClsForEdit[i].FlagId == _flagCls[0].FlagId)
                                    continue;
                                _flagClsNew[Count] = new FlagCls();
                                _flagClsNew[Count] = _flagClsForEdit[i];
                                Count++;
                            }

                        }

                        _flagForEdit.FlagList = _flagClsNew;

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

                        PutCustomForAdmin(EducationOrganizationId, _adminCls);

                    }
                    else
                    {

                    }
                }
            }
            catch (Exception Ex)
            {

            }
        }
        public void UpdateFlagsIntoCustom(String Link, User _user, FlagCls[] _flagCls)
        {
            FlagCls[] _forNew = _flagCls;
            AggregateCls[] _forAggregateCls = null;
            try
            {
                Temp _editedFlagByUser = new Temp();
                _editedFlagByUser.FlagList = _flagCls;

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

                        FlagCls[] _flagClsNew = new FlagCls[_list.FlagList.Count()];
                        for (int i = 0; i < _list.FlagList.Count(); i++)
                        {
                            _flagClsNew[i] = new FlagCls();
                            if (_list.FlagList[i].FlagId == _flagCls[0].FlagId)
                            {
                                _flagClsNew[i] = _flagCls[0];
                            }
                            else
                                _flagClsNew[i] = _list.FlagList[i];
                        }

                        _temp.FlagList = _flagClsNew;
                        _temp.AggregateFlagList = _list.AggregateFlagList;

                        String Result = FlagObjectToJson(_temp);

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

            }
            catch (Exception Ex)
            {

            }
        }
        public void UpateFlag(int FlagId, String UserCustomLink, String EducationOrganizationId, bool IsAdmin, bool IsPublic, String FlagUserId, FlagCls[] _flagClsUpdated, User _user)
        {
            if (IsAdmin)
            {

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

                        AddFlagsIntoCustom(UserCustomLink, _user, _flagClsUpdated);
                    }
                    else
                    {
                        // update at organization
                        UpdateFlagIntoEducationOrganization(_user, EducationOrganizationId, _flagClsUpdated);
                    }
                }
                else
                    if ((!IsPublic) && _flagClsUpdated[0].IsPublic)
                    {
                        // private to public
                        DeleteFlagsIntoCustom(UserCustomLink, _user, _flagClsUpdated);

                        AddFlagIntoEducationOrganization(_user, EducationOrganizationId, _flagClsUpdated);
                    }
                    else
                    {
                        // update at custom
                        UpdateFlagsIntoCustom(UserCustomLink, _user, _flagClsUpdated);
                    }
            }
            else
                UpdateFlagsIntoCustom(UserCustomLink, _user, _flagClsUpdated);
        }
        public void AddFlagIntoEducationOrganization(User _user, String EducationOrganizationId, FlagCls[] _flagCls)
        {
            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].FlagList = _flagCls;
                    _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];

                        FlagCls[] _flagClsForEdit = _flagForEdit.FlagList;

                        if (_flagClsForEdit != null)
                        {

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

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

                            _flagClsNew[_flagClsForEdit.Count()] = new FlagCls();
                            _flagClsNew[_flagClsForEdit.Count()] = _flagCls[0];

                            _flagForEdit.FlagList = _flagClsNew;
                        }
                        else
                        {
                            _flagForEdit.FlagList = _flagCls;
                        }
                        _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.FlagList = _flagCls;

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

                        _adminCls.AdminList = _tempForNew;

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

            }
        }